lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [lmi] master a0f10c6 2/5: Prefer std::snprintf() to C99 sn


From: Greg Chicares
Subject: [lmi-commits] [lmi] master a0f10c6 2/5: Prefer std::snprintf() to C99 snprintf()
Date: Tue, 2 May 2017 14:56:23 -0400 (EDT)

branch: master
commit a0f10c68e451f4c340431fd8f3210fc2a6cc52a6
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>

    Prefer std::snprintf() to C99 snprintf()
---
 numeric_io_cast.hpp   |  8 ++++----
 numeric_io_traits.hpp |  4 ++--
 snprintf_test.cpp     | 28 ++++++++++++++--------------
 value_cast_test.cpp   |  4 ++--
 4 files changed, 22 insertions(+), 22 deletions(-)

diff --git a/numeric_io_cast.hpp b/numeric_io_cast.hpp
index 1c6b384..f650b63 100644
--- a/numeric_io_cast.hpp
+++ b/numeric_io_cast.hpp
@@ -30,7 +30,7 @@
 #include <cstring>                      // std::strcmp()
 #include <sstream>
 #include <stdexcept>
-#include <stdio.h>                      // snprintf() (C99, not C++98).
+#include <stdio.h>                      // std::snprintf()
 #include <string>
 #include <type_traits>
 #include <typeinfo>
@@ -214,7 +214,7 @@ struct numeric_converter<std::string, From>
         // The borland rtl has a similar problem.
         char buffer[1 + buffer_length];
         buffer[buffer_length] = '\0';
-        int actual_length = snprintf
+        int actual_length = std::snprintf
             (buffer
             ,buffer_length
             ,numeric_conversion_traits<From>::fmt()
@@ -227,7 +227,7 @@ struct numeric_converter<std::string, From>
             err
                 << "Attempt to convert '"
                 << from
-                << "' to string failed: snprintf returned "
+                << "' to string failed: std::snprintf returned "
                 << actual_length
                 << ", indicating an encoding error with format string '"
                 << numeric_conversion_traits<From>::fmt()
@@ -241,7 +241,7 @@ struct numeric_converter<std::string, From>
             err
                 << "Attempt to convert '"
                 << from
-                << "' to string failed: snprintf returned "
+                << "' to string failed: std::snprintf returned "
                 << actual_length
                 << ", but buffer length is only "
                 << buffer_length
diff --git a/numeric_io_traits.hpp b/numeric_io_traits.hpp
index 9fb5c98..19477ca 100644
--- a/numeric_io_traits.hpp
+++ b/numeric_io_traits.hpp
@@ -80,7 +80,7 @@ inline int floating_point_decimals(T t)
 /// Simplify a formatted floating-point number.
 ///
 /// Precondition: 's' is a floating-point number formatted as if by
-/// snprintf() with format "%#.*f" or "%#.*Lf".
+/// std::snprintf() with format "%#.*f" or "%#.*Lf".
 ///
 /// Returns: 's' without any insignificant characters (trailing zeros
 /// after the decimal point, and the decimal point itself if followed
@@ -134,7 +134,7 @@ struct numeric_conversion_traits
 };
 
 /// Return C99 7.19.6.1/8 default precision for integral types.
-/// Calling snprintf() with a precision of zero and a value of
+/// Calling std::snprintf() with a precision of zero and a value of
 /// zero would return no characters.
 
 struct Integral{};
diff --git a/snprintf_test.cpp b/snprintf_test.cpp
index 1dd4af1..75373dc 100644
--- a/snprintf_test.cpp
+++ b/snprintf_test.cpp
@@ -23,7 +23,7 @@
 
 #include "test_tools.hpp"
 
-#include <stdio.h>                      // snprintf() (C99, not C++98).
+#include <stdio.h>                      // std::snprintf()
 #include <string>
 
 int test_main(int, char*[])
@@ -35,40 +35,40 @@ int test_main(int, char*[])
     char buf[1000] = "zzzzzzzzz";
     int len;
 
-    len = snprintf(nullptr, 0, "%4d", 1234);
+    len = std::snprintf(nullptr, 0, "%4d", 1234);
     BOOST_TEST_EQUAL(4, len);
 
     // All tests in this group fail with the defective msvc rtl.
-    len = snprintf(buf, 0, "%4d", 1234);
+    len = std::snprintf(buf, 0, "%4d", 1234);
     BOOST_TEST_EQUAL(4, len);
 
     // All tests in this group fail with the defective msvc rtl.
-    len = snprintf(buf, 3, "%4d", 1234);
+    len = std::snprintf(buf, 3, "%4d", 1234);
     BOOST_TEST_EQUAL(4, len);
     // This test fails with borland C++ 5.5.1 .
     BOOST_TEST_EQUAL(std::string(buf, 9), std::string("12\0zzzzzz\0", 9));
 
-    len = snprintf(buf, 4, "%4d", 1234);
+    len = std::snprintf(buf, 4, "%4d", 1234);
     BOOST_TEST_EQUAL(4, len);
     // This test fails with the defective msvc rtl and also
     // with borland C++ 5.5.1 .
     BOOST_TEST_EQUAL(std::string(buf, 9), std::string("123\0zzzzz\0", 9));
 
-    len = snprintf(buf, 5, "%4d", 1234);
+    len = std::snprintf(buf, 5, "%4d", 1234);
     BOOST_TEST_EQUAL(4, len);
     BOOST_TEST_EQUAL(std::string(buf, 9), std::string("1234\0zzzz\0", 9));
 
     long double z = 2.718281828459045L;
-    len = snprintf(buf, 5, "%.5Lf", z);
+    len = std::snprintf(buf, 5, "%.5Lf", z);
     BOOST_TEST_EQUAL(7, len);
     // This should truncate to 2.71, not round to 2.72 .
     BOOST_TEST_EQUAL(std::string(buf, 9), std::string("2.71\0zzzz\0", 9));
-    len = snprintf(buf, 7, "%.5Lf", z);
+    len = std::snprintf(buf, 7, "%.5Lf", z);
     BOOST_TEST_EQUAL(7, len);
     BOOST_TEST_EQUAL(std::string(buf, 9), std::string("2.7182\0zz\0", 9));
-    len = snprintf(buf,       0, "%1.12Lf", z);
+    len = std::snprintf(buf,       0, "%1.12Lf", z);
     BOOST_TEST_EQUAL(14, len);
-    len = snprintf(buf, 1 + len, "%1.12Lf", z);
+    len = std::snprintf(buf, 1 + len, "%1.12Lf", z);
     BOOST_TEST_EQUAL(14, len);
     BOOST_TEST_EQUAL(std::string(buf, 15), std::string("2.718281828459\0", 
15));
 
@@ -76,16 +76,16 @@ int test_main(int, char*[])
     //   http://comments.gmane.org/gmane.comp.gnu.mingw.devel/2945
     //     [2008-05-11T11:46Z from François-Xavier Coudert]
     double g = 39.0;
-    len = snprintf(buf,       0, "%9.0e", g);
+    len = std::snprintf(buf,       0, "%9.0e", g);
     BOOST_TEST_EQUAL(9, len);
-    len = snprintf(buf, 1 + len, "%9.0e", g);
+    len = std::snprintf(buf, 1 + len, "%9.0e", g);
     BOOST_TEST_EQUAL(9, len);
     BOOST_TEST_EQUAL(std::string(buf, 9), std::string("    4e+01\0", 9));
 
     double d = 1e+161;
-    len = snprintf(buf,       0, "%#.*f", 16, d);
+    len = std::snprintf(buf,       0, "%#.*f", 16, d);
     BOOST_TEST_EQUAL(179, len);
-    len = snprintf(buf, 1 + len, "%#.*f", 16, d);
+    len = std::snprintf(buf, 1 + len, "%#.*f", 16, d);
     BOOST_TEST_EQUAL(179, len);
     std::string e
         ("1"
diff --git a/value_cast_test.cpp b/value_cast_test.cpp
index 5c29de1..2918ba4 100644
--- a/value_cast_test.cpp
+++ b/value_cast_test.cpp
@@ -346,9 +346,9 @@ int extra_tests0()
     {
     // Initialize 'nptr' to a string representation of
     //   FLT_RADIX^(DBL_MAX_EXP-1)
-    // produced by snprintf(), verifiable thus:
+    // produced by std::snprintf(), verifiable thus:
     //    double big = std::scalbn(1.0, DBL_MAX_EXP - 1.0);
-    //    snprintf(buffer, buffer_length, "%.*f", 0, big);
+    //    std::snprintf(buffer, buffer_length, "%.*f", 0, big);
     char const* nptr =
     //   12345678901234567890123456789012345678901234567890 <-- 50 digits/line
         "89884656743115795386465259539451236680898848947115"



reply via email to

[Prev in Thread] Current Thread [Next in Thread]