mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-08-20 04:13:07 +02:00
JSON: Improve json::decode() performance
- Improves "load" time by ~28% across all performance tests. - Improves the "add" performance test by ~20% since "load" takes most of the time there.
This commit is contained in:
parent
84c85b7596
commit
f4ba6015a8
1 changed files with 31 additions and 24 deletions
55
src/JSON.cpp
55
src/JSON.cpp
|
@ -405,38 +405,45 @@ std::string json::encode (const std::string& input)
|
|||
std::string json::decode (const std::string& input)
|
||||
{
|
||||
std::string output;
|
||||
for (unsigned int i = 0; i < input.length (); ++i)
|
||||
size_t pos = 0;
|
||||
|
||||
while (pos < input.length ())
|
||||
{
|
||||
if (input[i] == '\\')
|
||||
if (input[pos] == '\\')
|
||||
{
|
||||
++i;
|
||||
switch (input[i])
|
||||
++pos;
|
||||
switch (input[pos])
|
||||
{
|
||||
// Simple translations.
|
||||
case '"': output += '"'; break;
|
||||
case '\\': output += '\\'; break;
|
||||
case '/': output += '/'; break;
|
||||
case 'b': output += '\b'; break;
|
||||
case 'f': output += '\f'; break;
|
||||
case 'n': output += '\n'; break;
|
||||
case 'r': output += '\r'; break;
|
||||
case 't': output += '\t'; break;
|
||||
// Simple translations.
|
||||
case '"': output += '"'; break;
|
||||
case '\\': output += '\\'; break;
|
||||
case '/': output += '/'; break;
|
||||
case 'b': output += '\b'; break;
|
||||
case 'f': output += '\f'; break;
|
||||
case 'n': output += '\n'; break;
|
||||
case 'r': output += '\r'; break;
|
||||
case 't': output += '\t'; break;
|
||||
|
||||
// Compose a UTF8 unicode character.
|
||||
case 'u':
|
||||
output += utf8_character (utf8_codepoint (input.substr (++i)));
|
||||
i += 3;
|
||||
break;
|
||||
// Compose a UTF8 unicode character.
|
||||
case 'u':
|
||||
output += utf8_character (utf8_codepoint (input.substr (++pos)));
|
||||
pos += 3;
|
||||
break;
|
||||
|
||||
// If it is an unrecognized sequence, do nothing.
|
||||
default:
|
||||
output += '\\';
|
||||
output += input[i];
|
||||
break;
|
||||
// If it is an unrecognized sequence, do nothing.
|
||||
default:
|
||||
output += '\\';
|
||||
output += input[pos];
|
||||
break;
|
||||
}
|
||||
++pos;
|
||||
}
|
||||
else
|
||||
output += input[i];
|
||||
{
|
||||
size_t next_backslash = input.find ('\\', pos);
|
||||
output.append (input, pos, next_backslash - pos);
|
||||
pos = next_backslash;
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue