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