Explorar o código

intToString with flexible length, computation of number of digits wrt arbitrary base implemented, Doxy also run on tcc-files

Alexander Freytag %!s(int64=11) %!d(string=hai) anos
pai
achega
0119edd1e3
Modificáronse 3 ficheiros con 50 adicións e 1 borrados
  1. 1 0
      Doxyfile
  2. 11 0
      core/basics/numerictools.cpp
  3. 38 1
      core/basics/numerictools.h

+ 1 - 0
Doxyfile

@@ -622,6 +622,7 @@ FILE_PATTERNS          = *.doxy \
 *.cxx \
 *.cpp \
 *.c++ \
+*.tcc \
 *.d \
 *.java \
 *.ii \

+ 11 - 0
core/basics/numerictools.cpp

@@ -5,9 +5,13 @@
  */
 #include "core/basics/numerictools.h"
 
+// STL includes
 #include <errno.h>
 #include <sstream>
+#include <iomanip>
 #include <clocale>
+
+// NICE-core includes
 #include <core/basics/Log.h>
 #include <core/basics/Timer.h>
 #include <core/basics/Exception.h>
@@ -82,6 +86,13 @@ std::string intToString(const int i) {
   return s.str();
 }
 
+std::string intToString(const int i, const uint & length) {
+  std::stringstream s;
+  s << std::setw( length ) << std::setfill('0') << i;
+  return s.str();
+}
+
+
 std::string doubleToString(const double d, const unsigned int digits,
                            const bool fixedPoint) {
   std::stringstream s;

+ 38 - 1
core/basics/numerictools.h

@@ -6,6 +6,7 @@
  * See file License for license information.
  */
 
+// STL includes
 #include <cmath>
 #include <math.h>
 #include <stdlib.h>
@@ -401,10 +402,21 @@ inline long stringToInt(std::string s) {
 }
 
 /**
- * Convert an integer into a string.
+ * @brief Convert an integer into a string
  */
 std::string intToString(const int i);
 
+/**
+ * @brief Convert an integer into a string of fixed length, add leading zeros if needed.
+ * @author Alxander Freytag
+ * @date 13-01-2014 (dd-mm-yyyy)
+ * 
+ * @param i - number to convert
+ * @param length - resulting length of string including leading zeros
+ * @return string
+ */
+std::string intToString(const int i, const uint & length);
+
 /**
  * Convert a double into a string.
  */
@@ -425,6 +437,31 @@ inline int roundInt(float d) {
   return int(roundf(d));
 }
 
+
+/**
+ * @brief Compute number of digits (including sign) of a given number w.r.t. a specified base (ONLY USEFUL FOR INT-LIKE VARIABLES)
+ * @author Alexander Freytag
+ * @date 13-01-2014 (dd-mm-yyyy)
+ *
+ * @param number 
+ * @param base  ( default is 10).
+ * @return int
+ **/
+template <class T>
+int getNumDigits(T number, T base = 10 )
+{
+    int digits ( 0 );
+    
+    // note: only possible, because we call it using call-by-value!
+    while ( number )
+    {
+        number /= base;
+        digits++;
+    }
+    
+    return digits;
+}
+
 } // namespace
 
 #endif // _NUMERICTOOLS_FBASICS_H