univalue: make spaceStr thread-safe

Simply add spaces to the existing string instead of using a
temporary.

Fixes #4756.
This commit is contained in:
Wladimir J. van der Laan 2014-09-05 13:10:39 +02:00
parent 406f18cfc4
commit 41ef558aa9
No known key found for this signature in database
GPG Key ID: 74810B012346C9A6

View File

@ -70,15 +70,9 @@ string UniValue::write(unsigned int prettyIndent,
return s; return s;
} }
static string spaceStr; static void indentStr(unsigned int prettyIndent, unsigned int indentLevel, string& s)
static string indentStr(unsigned int prettyIndent, unsigned int indentLevel)
{ {
unsigned int spaces = prettyIndent * indentLevel; s.append(prettyIndent * indentLevel, ' ');
while (spaceStr.size() < spaces)
spaceStr += " ";
return spaceStr.substr(0, spaces);
} }
void UniValue::writeArray(unsigned int prettyIndent, unsigned int indentLevel, string& s) const void UniValue::writeArray(unsigned int prettyIndent, unsigned int indentLevel, string& s) const
@ -89,7 +83,7 @@ void UniValue::writeArray(unsigned int prettyIndent, unsigned int indentLevel, s
for (unsigned int i = 0; i < values.size(); i++) { for (unsigned int i = 0; i < values.size(); i++) {
if (prettyIndent) if (prettyIndent)
s += indentStr(prettyIndent, indentLevel); indentStr(prettyIndent, indentLevel, s);
s += values[i].write(prettyIndent, indentLevel + 1); s += values[i].write(prettyIndent, indentLevel + 1);
if (i != (values.size() - 1)) { if (i != (values.size() - 1)) {
s += ","; s += ",";
@ -101,7 +95,7 @@ void UniValue::writeArray(unsigned int prettyIndent, unsigned int indentLevel, s
} }
if (prettyIndent) if (prettyIndent)
s += indentStr(prettyIndent, indentLevel - 1); indentStr(prettyIndent, indentLevel - 1, s);
s += "]"; s += "]";
} }
@ -113,7 +107,7 @@ void UniValue::writeObject(unsigned int prettyIndent, unsigned int indentLevel,
for (unsigned int i = 0; i < keys.size(); i++) { for (unsigned int i = 0; i < keys.size(); i++) {
if (prettyIndent) if (prettyIndent)
s += indentStr(prettyIndent, indentLevel); indentStr(prettyIndent, indentLevel, s);
s += "\"" + json_escape(keys[i]) + "\":"; s += "\"" + json_escape(keys[i]) + "\":";
if (prettyIndent) if (prettyIndent)
s += " "; s += " ";
@ -125,7 +119,7 @@ void UniValue::writeObject(unsigned int prettyIndent, unsigned int indentLevel,
} }
if (prettyIndent) if (prettyIndent)
s += indentStr(prettyIndent, indentLevel - 1); indentStr(prettyIndent, indentLevel - 1, s);
s += "}"; s += "}";
} }