From: Jakub Skowron Date: Tue, 7 Feb 2017 18:39:54 +0000 (+0100) Subject: Fix picojson number representation X-Git-Tag: submit/tizen_3.0/20170208.081148~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F67%2F113467%2F1;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git Fix picojson number representation Previous implementation assumed default global locale. Failed when locale used other decimal point than '.' Change-Id: I1ffe2847c249cf666e720342e41ed068178cc459 Signed-off-by: Jakub Skowron --- diff --git a/src/common/picojson.h b/src/common/picojson.h index 6921b1f1..eca7efc9 100644 --- a/src/common/picojson.h +++ b/src/common/picojson.h @@ -36,6 +36,8 @@ #include #include #include +#include +#include #include #include #include @@ -264,10 +266,14 @@ namespace picojson { case null_type: return "null"; case boolean_type: return u_.boolean_ ? "true" : "false"; case number_type: { - char buf[256]; - double tmp; - SNPRINTF(buf, sizeof(buf), fabs(u_.number_) < (1ULL << 53) && modf(u_.number_, &tmp) == 0 ? "%.f" : "%.17g", u_.number_); - return buf; + std::stringstream num_str; + num_str.imbue( std::locale::classic() ); + double ignored; + if( std::modf(u_.number_, &ignored) != 0 || std::fabs(u_.number_) >= (1ULL << 53) ) { + num_str << std::scientific << std::setprecision(16); + } + num_str << u_.number_; + return num_str.str(); } case string_type: return *u_.string_; case array_type: return "array"; @@ -557,20 +563,23 @@ namespace picojson { } template inline bool _parse_number(double& out, input& in) { - std::string num_str; - while (1) { + std::stringstream num_str; + num_str.imbue( std::locale::classic() ); + + while (true) { int ch = in.getc(); if (('0' <= ch && ch <= '9') || ch == '+' || ch == '-' || ch == '.' || ch == 'e' || ch == 'E') { - num_str.push_back(ch); + num_str.put(ch); } else { in.ungetc(); break; } } - char* endp; - out = strtod(num_str.c_str(), &endp); - return endp == num_str.c_str() + num_str.size(); + + num_str >> out; + + return num_str && num_str.eof(); } template inline bool _parse(Context& ctx, input& in) {