Merge pull request #34 from ToruNiina/consider-locale

fix: use snprintf instead of stringstream
This commit is contained in:
Toru Niina
2019-03-13 09:56:32 +09:00
committed by GitHub

View File

@@ -5,6 +5,7 @@
#include "value.hpp" #include "value.hpp"
#include "lexer.hpp" #include "lexer.hpp"
#include <limits> #include <limits>
#include <cstdio>
namespace toml namespace toml
{ {
@@ -30,13 +31,12 @@ struct serializer
} }
std::string operator()(const toml::floating f) const std::string operator()(const toml::floating f) const
{ {
std::string token = [=] { const auto fmt = "%.*g";
// every float value needs decimal point (or exponent). const auto bsz = std::snprintf(nullptr, 0, fmt, this->float_prec_, f);
std::ostringstream oss; std::vector<char> buf(bsz + 1, '\0'); // +1 for null character(\0)
oss << std::setprecision(float_prec_) << std::showpoint << f; std::snprintf(buf.data(), buf.size(), fmt, this->float_prec_, f);
return oss.str();
}();
std::string token(buf.begin(), buf.end());
if(token.back() == '.') // 1. => 1.0 if(token.back() == '.') // 1. => 1.0
{ {
token += '0'; token += '0';