From 1c7bd8002397b5c45b48cc307c070a129f10f66f Mon Sep 17 00:00:00 2001
From: Rene Gassmoeller <R.Gassmoeller@mailbox.org>
Date: Mon, 30 Nov 2015 17:24:42 -0600
Subject: [PATCH] Add a Utilities::number_to_string function

---
 include/deal.II/base/utilities.h | 26 +++++++++++++++++++++++---
 source/base/utilities.cc         | 28 ++++++++++++++++++++++++++--
 2 files changed, 49 insertions(+), 5 deletions(-)

diff --git a/include/deal.II/base/utilities.h b/include/deal.II/base/utilities.h
index 1ac3b2ec70..8ba7baf461 100644
--- a/include/deal.II/base/utilities.h
+++ b/include/deal.II/base/utilities.h
@@ -50,17 +50,37 @@ namespace Utilities
 {
 
   /**
-   * Convert a number @p i to a string, with as many digits as given to fill
+   * Convert a number @p value to a string, with as many digits as given to fill
    * with leading zeros.
    *
    * If the second parameter is left at its default value, the number is not
-   * padded with leading zeros. The result is then the same as of the standard
+   * padded with leading zeros. The result is then the same as if the standard
    * C function <code>itoa()</code> had been called.
+   *
+   * When calling this function signed integers are implicitly converted to
+   * unsigned integers and long integers might experience an overflow.
+   *
+   * @note The use of this function is discouraged and users should use
+   * <code>Utilities::to_string()</code> instead. In its current implementation
+   * the function simply calls <code>to_string<unsigned int>()</code>.
    */
   std::string
-  int_to_string (const unsigned int i,
+  int_to_string (const unsigned int value,
                  const unsigned int digits = numbers::invalid_unsigned_int);
 
+  /**
+   * Convert a number @p value to a string, with as many digits as given to fill
+   * with leading zeros.
+   *
+   * If the second parameter is left at its default value, the number is not
+   * padded with leading zeros. The result is then the same as if the boost
+   * function <code>lexical_cast<std::string>()</code> had been called.
+   */
+  template <typename number>
+  std::string
+  to_string (const number value,
+             const unsigned int digits = numbers::invalid_unsigned_int);
+
   /**
    * Determine how many digits are needed to represent numbers at most as
    * large as the given number.
diff --git a/source/base/utilities.cc b/source/base/utilities.cc
index 9de4a062c6..1e07737018 100644
--- a/source/base/utilities.cc
+++ b/source/base/utilities.cc
@@ -80,6 +80,13 @@ namespace Utilities
 
   std::string
   int_to_string (const unsigned int value, const unsigned int digits)
+  {
+    return to_string(value,digits);
+  }
+
+  template <typename number>
+  std::string
+  to_string (const number value, const unsigned int digits)
   {
     std::string lc_string = boost::lexical_cast<std::string>(value);
 
@@ -87,8 +94,15 @@ namespace Utilities
       return lc_string;
     else if (lc_string.size() < digits)
       {
-        std::string padding(digits - lc_string.size(), '0');
-        lc_string.insert(0, padding);
+        // We have to add the padding zeroes in front of the number
+        const unsigned int padding_position = (lc_string[0] == '-')
+                                              ?
+                                              1
+                                              :
+                                              0;
+
+        const std::string padding(digits - lc_string.size(), '0');
+        lc_string.insert(padding_position, padding);
       }
     return lc_string;
   }
@@ -820,6 +834,16 @@ namespace Utilities
 
 #endif
 
+  template std::string to_string<int> (int, unsigned int);
+  template std::string to_string<long int> (long int, unsigned int);
+  template std::string to_string<long long int> (long long int, unsigned int);
+  template std::string to_string<unsigned int> (unsigned int, unsigned int);
+  template std::string to_string<unsigned long int> (unsigned long int, unsigned int);
+  template std::string to_string<unsigned long long int> (unsigned long long int, unsigned int);
+  template std::string to_string<float> (float, unsigned int);
+  template std::string to_string<double> (double, unsigned int);
+  template std::string to_string<long double> (long double, unsigned int);
+
 }
 
 DEAL_II_NAMESPACE_CLOSE
-- 
2.39.5