From ed971e180e4112b90ba7322eb3a89ff6e385c2e1 Mon Sep 17 00:00:00 2001 From: Christopher Dunn Date: Thu, 24 Jan 2008 20:09:48 +0000 Subject: [PATCH] Changed valueToString for floats so that zeroes after a decimal are truncated -- saves file size esp. for StyledStreamWriter which uses tabs instead of spaces. --- src/lib_json/json_writer.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/lib_json/json_writer.cpp b/src/lib_json/json_writer.cpp index 33b5cbc..4129984 100644 --- a/src/lib_json/json_writer.cpp +++ b/src/lib_json/json_writer.cpp @@ -55,6 +55,34 @@ std::string valueToString( double value ) #else sprintf(buffer, "%.16g", value); #endif + char* ch = buffer + strlen(buffer) - 1; + if (*ch != '0') return buffer; // nothing to truncate, so save time + while(ch > buffer && *ch == '0'){ + --ch; + } + char* last_nonzero = ch; + while(ch >= buffer){ + switch(*ch){ + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + --ch; + continue; + case '.': + // Truncate zeroes to save bytes in output, but keep one. + *(last_nonzero+2) = '\0'; + return buffer; + default: + return buffer; + } + } return buffer; }