diff options
author | Mario Emmenlauer <mario@emmenlauer.de> | 2021-06-09 16:38:56 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-09 16:38:56 +0200 |
commit | 43faa2d22b1d200921fe339fb20af167944d257f (patch) | |
tree | 3f202684aa4af6424a81d152640641c4f68efc88 /lib | |
parent | 93a316c51d491d9ea51506bba1ab90bca5766b6e (diff) | |
parent | ad76a18db73b038f94e4e6f891abea6ee7b1876b (diff) | |
download | thrift-43faa2d22b1d200921fe339fb20af167944d257f.tar.gz |
Merge pull request #2401 from aaronmjones/THRIFT-3840
THRIFT-3840: C++ TJSONProtocol still using locale dependent formatting
Diffstat (limited to 'lib')
-rw-r--r-- | lib/cpp/src/thrift/TToString.h | 5 | ||||
-rw-r--r-- | lib/cpp/test/ToStringTest.cpp | 21 |
2 files changed, 26 insertions, 0 deletions
diff --git a/lib/cpp/src/thrift/TToString.h b/lib/cpp/src/thrift/TToString.h index 25780f9d2..79743fdcd 100644 --- a/lib/cpp/src/thrift/TToString.h +++ b/lib/cpp/src/thrift/TToString.h @@ -22,6 +22,7 @@ #include <cmath> #include <limits> +#include <locale> #include <map> #include <set> #include <sstream> @@ -34,6 +35,7 @@ namespace thrift { template <typename T> std::string to_string(const T& t) { std::ostringstream o; + o.imbue(std::locale("C")); o << t; return o.str(); } @@ -42,6 +44,7 @@ std::string to_string(const T& t) { // is enabled. inline std::string to_string(const float& t) { std::ostringstream o; + o.imbue(std::locale("C")); o.precision(static_cast<std::streamsize>(std::ceil(static_cast<double>(std::numeric_limits<float>::digits * std::log10(2.0f) + 1)))); o << t; return o.str(); @@ -49,6 +52,7 @@ inline std::string to_string(const float& t) { inline std::string to_string(const double& t) { std::ostringstream o; + o.imbue(std::locale("C")); o.precision(static_cast<std::streamsize>(std::ceil(static_cast<double>(std::numeric_limits<double>::digits * std::log10(2.0f) + 1)))); o << t; return o.str(); @@ -56,6 +60,7 @@ inline std::string to_string(const double& t) { inline std::string to_string(const long double& t) { std::ostringstream o; + o.imbue(std::locale("C")); o.precision(static_cast<std::streamsize>(std::ceil(static_cast<double>(std::numeric_limits<long double>::digits * std::log10(2.0f) + 1)))); o << t; return o.str(); diff --git a/lib/cpp/test/ToStringTest.cpp b/lib/cpp/test/ToStringTest.cpp index 5a05ed70a..722e5904c 100644 --- a/lib/cpp/test/ToStringTest.cpp +++ b/lib/cpp/test/ToStringTest.cpp @@ -19,6 +19,7 @@ #include <vector> #include <map> +#include <locale> #include <boost/test/unit_test.hpp> @@ -40,6 +41,26 @@ BOOST_AUTO_TEST_CASE(base_types_to_string) { BOOST_CHECK_EQUAL(to_string("abc"), "abc"); } +BOOST_AUTO_TEST_CASE(locale_en_US_int_to_string) { +#if _WIN32 + std::locale::global(std::locale("en-US.UTF-8")); +#else + std::locale::global(std::locale("en_US.UTF-8")); +#endif + BOOST_CHECK_EQUAL(to_string(1000000), "1000000"); +} + +BOOST_AUTO_TEST_CASE(locale_de_DE_floating_point_to_string) { +#if _WIN32 + std::locale::global(std::locale("de-DE.UTF-8")); +#else + std::locale::global(std::locale("de_DE.UTF-8")); +#endif + BOOST_CHECK_EQUAL(to_string(1.5), "1.5"); + BOOST_CHECK_EQUAL(to_string(1.5f), "1.5"); + BOOST_CHECK_EQUAL(to_string(1.5L), "1.5"); +} + BOOST_AUTO_TEST_CASE(empty_vector_to_string) { std::vector<int> l; BOOST_CHECK_EQUAL(to_string(l), "[]"); |