From dddcecb0340f58fca9d45703a8d1efb6f0e946cc Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Tue, 12 Mar 2019 23:37:46 +0900 Subject: [PATCH 1/3] fix: use snprintf instead of stringstream to avoid the effect of locale --- toml/serializer.hpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/toml/serializer.hpp b/toml/serializer.hpp index cb150bb..c4fb689 100644 --- a/toml/serializer.hpp +++ b/toml/serializer.hpp @@ -5,6 +5,7 @@ #include "value.hpp" #include "lexer.hpp" #include +#include namespace toml { @@ -30,13 +31,12 @@ struct serializer } std::string operator()(const toml::floating f) const { - std::string token = [=] { - // every float value needs decimal point (or exponent). - std::ostringstream oss; - oss << std::setprecision(float_prec_) << std::showpoint << f; - return oss.str(); - }(); + const auto fmt = "%.*g"; + const auto bsz = std::snprintf(nullptr, 0, fmt, int(this->float_prec_), f); + std::vector buf(bsz + 1, '\0'); // +1 for null character(\0) + std::snprintf(buf.data(), buf.size(), fmt, int(this->float_prec_), f); + std::string token(buf.begin(), buf.end()); if(token.back() == '.') // 1. => 1.0 { token += '0'; From 46b35870c55356824a11078b483c40f0dea20d1d Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Wed, 13 Mar 2019 01:17:27 +0900 Subject: [PATCH 2/3] style: remove needless type casting --- toml/serializer.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/toml/serializer.hpp b/toml/serializer.hpp index c4fb689..c25f8b2 100644 --- a/toml/serializer.hpp +++ b/toml/serializer.hpp @@ -32,9 +32,9 @@ struct serializer std::string operator()(const toml::floating f) const { const auto fmt = "%.*g"; - const auto bsz = std::snprintf(nullptr, 0, fmt, int(this->float_prec_), f); + const auto bsz = std::snprintf(nullptr, 0, fmt, this->float_prec_, f); std::vector buf(bsz + 1, '\0'); // +1 for null character(\0) - std::snprintf(buf.data(), buf.size(), fmt, int(this->float_prec_), f); + std::snprintf(buf.data(), buf.size(), fmt, this->float_prec_, f); std::string token(buf.begin(), buf.end()); if(token.back() == '.') // 1. => 1.0 From 74ceceef73675bb870c712b040cd71c84efb9aff Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Wed, 13 Mar 2019 14:03:04 +0900 Subject: [PATCH 3/3] fix: suppress warning about sign-unsign comparison The solution is not ideal, but it's okay at the line --- toml/parser.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/toml/parser.hpp b/toml/parser.hpp index 4bd4b60..26b13b1 100644 --- a/toml/parser.hpp +++ b/toml/parser.hpp @@ -1058,7 +1058,7 @@ bool is_valid_forward_table_definition(const value& fwd, { // table keys always contains all the nodes from the root. const auto& tks = tabkeys.unwrap().first; - if(std::distance(key_first, key_last) == tks.size() && + if(std::size_t(std::distance(key_first, key_last)) == tks.size() && std::equal(tks.begin(), tks.end(), key_first)) { // the keys are equivalent. it is not allowed. @@ -1079,7 +1079,7 @@ bool is_valid_forward_table_definition(const value& fwd, // a dotted key starts from the node representing a table in which the // dotted key belongs to. const auto& dks = dotkeys.unwrap().first; - if(std::distance(key_curr, key_last) == dks.size() && + if(std::size_t(std::distance(key_curr, key_last)) == dks.size() && std::equal(dks.begin(), dks.end(), key_curr)) { // the keys are equivalent. it is not allowed.