From b95aa146ec3226f31b7b75bef1b5f750af25fb8a Mon Sep 17 00:00:00 2001 From: Simon Gene Gottlieb Date: Wed, 17 Jul 2024 13:06:39 +0200 Subject: [PATCH] fix: use C locale by default --- include/yaml-cpp/emitter.h | 2 ++ include/yaml-cpp/node/convert.h | 2 ++ include/yaml-cpp/traits.h | 1 + src/node_data.cpp | 1 + src/parser.cpp | 1 + test/node/node_test.cpp | 12 ++++++++++++ 6 files changed, 19 insertions(+) diff --git a/include/yaml-cpp/emitter.h b/include/yaml-cpp/emitter.h index 210b1ec97..2897fc0a2 100644 --- a/include/yaml-cpp/emitter.h +++ b/include/yaml-cpp/emitter.h @@ -141,6 +141,7 @@ inline Emitter& Emitter::WriteIntegralType(T value) { PrepareNode(EmitterNodeType::Scalar); std::stringstream stream; + stream.imbue(std::locale("C")); PrepareIntegralStream(stream); stream << value; m_stream << stream.str(); @@ -158,6 +159,7 @@ inline Emitter& Emitter::WriteStreamable(T value) { PrepareNode(EmitterNodeType::Scalar); std::stringstream stream; + stream.imbue(std::locale("C")); SetStreamablePrecision(stream); bool special = false; diff --git a/include/yaml-cpp/node/convert.h b/include/yaml-cpp/node/convert.h index d49702f82..c8b3c336f 100644 --- a/include/yaml-cpp/node/convert.h +++ b/include/yaml-cpp/node/convert.h @@ -171,6 +171,7 @@ ConvertStreamTo(std::stringstream& stream, T& rhs) { \ static Node encode(const type& rhs) { \ std::stringstream stream; \ + stream.imbue(std::locale("C")); \ stream.precision(std::numeric_limits::max_digits10); \ conversion::inner_encode(rhs, stream); \ return Node(stream.str()); \ @@ -182,6 +183,7 @@ ConvertStreamTo(std::stringstream& stream, T& rhs) { } \ const std::string& input = node.Scalar(); \ std::stringstream stream(input); \ + stream.imbue(std::locale("C")); \ stream.unsetf(std::ios::dec); \ if ((stream.peek() == '-') && std::is_unsigned::value) { \ return false; \ diff --git a/include/yaml-cpp/traits.h b/include/yaml-cpp/traits.h index ffe9999f1..7c4cdd900 100644 --- a/include/yaml-cpp/traits.h +++ b/include/yaml-cpp/traits.h @@ -121,6 +121,7 @@ template struct streamable_to_string { static std::string impl(const Key& key) { std::stringstream ss; + ss.imbue(std::locale("C")); ss << key; return ss.str(); } diff --git a/src/node_data.cpp b/src/node_data.cpp index 8f5422ae6..3321263f2 100644 --- a/src/node_data.cpp +++ b/src/node_data.cpp @@ -310,6 +310,7 @@ void node_data::convert_sequence_to_map(const shared_memory_holder& pMemory) { reset_map(); for (std::size_t i = 0; i < m_sequence.size(); i++) { std::stringstream stream; + stream.imbue(std::locale("C")); stream << i; node& key = pMemory->create_node(); diff --git a/src/parser.cpp b/src/parser.cpp index b8b78ebab..5feda358b 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -77,6 +77,7 @@ void Parser::HandleYamlDirective(const Token& token) { } std::stringstream str(token.params[0]); + str.imbue(std::locale("C")); str >> m_pDirectives->version.major; str.get(); str >> m_pDirectives->version.minor; diff --git a/test/node/node_test.cpp b/test/node/node_test.cpp index 5f41ef255..b4444554d 100644 --- a/test/node/node_test.cpp +++ b/test/node/node_test.cpp @@ -849,5 +849,17 @@ TEST_F(NodeEmitterTest, NestFlowMapListNode) { ExpectOutput("{position: [1.5, 2.25, 3.125]}", mapNode); } + +TEST_F(NodeEmitterTest, RobustAgainstLocale) { + std::locale::global(std::locale("")); + Node node; + node.push_back(1.5); + node.push_back(2.25); + node.push_back(3.125); + node.push_back(123456789); + + ExpectOutput("- 1.5\n- 2.25\n- 3.125\n- 123456789", node); +} + } }