[1611376]by reserving the max string-size when escaped chars exist, we should save some runtime.

This commit is contained in:
Christopher Dunn 2007-03-23 06:12:28 +00:00
parent ce1f32981b
commit 5674738668

View File

@ -70,7 +70,10 @@ std::string valueToQuotedString( const char *value )
// We have to walk value and escape any special characters. // We have to walk value and escape any special characters.
// Appending to std::string is not efficient, but this should be rare. // Appending to std::string is not efficient, but this should be rare.
// (Note: forward slashes are *not* rare, but I am not escaping them.) // (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){ for (const char* c=value; *c != 0; ++c){
switch(*c){ switch(*c){
case '\"': case '\"':
@ -102,7 +105,8 @@ std::string valueToQuotedString( const char *value )
result += *c; result += *c;
} }
} }
return result + "\""; result += "\"";
return result;
} }
// Class Writer // Class Writer