From 56747386689484b92416f57f16fd0e30ad97210c Mon Sep 17 00:00:00 2001 From: Christopher Dunn Date: Fri, 23 Mar 2007 06:12:28 +0000 Subject: [PATCH] [1611376]by reserving the max string-size when escaped chars exist, we should save some runtime. --- src/lib_json/json_writer.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/lib_json/json_writer.cpp b/src/lib_json/json_writer.cpp index 6fa5993..063a66f 100644 --- a/src/lib_json/json_writer.cpp +++ b/src/lib_json/json_writer.cpp @@ -70,7 +70,10 @@ std::string valueToQuotedString( const char *value ) // We have to walk value and escape any special characters. // Appending to std::string is not efficient, but this should be rare. // (Note: forward slashes are *not* rare, but I am not escaping them.) - std::string result("\""); + unsigned maxsize = strlen(value)*2 + 3; // allescaped+quotes+NULL + std::string result; + result.reserve(maxsize); // to avoid lots of mallocs + result += "\""; for (const char* c=value; *c != 0; ++c){ switch(*c){ case '\"': @@ -102,7 +105,8 @@ std::string valueToQuotedString( const char *value ) result += *c; } } - return result + "\""; + result += "\""; + return result; } // Class Writer