mirror of
https://github.com/open-source-parsers/jsoncpp.git
synced 2024-12-14 02:35:09 +01:00
[1611376]by reserving the max string-size when escaped chars exist, we should save some runtime.
This commit is contained in:
parent
ce1f32981b
commit
5674738668
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user