Skip to content

Commit

Permalink
Fix quadric memcpy on toJson (#259)
Browse files Browse the repository at this point in the history
* fix quadratic speed

* add reserve
  • Loading branch information
ydot13 authored Sep 15, 2024
1 parent b32fbde commit e98b3b5
Showing 1 changed file with 14 additions and 21 deletions.
35 changes: 14 additions & 21 deletions src/serialize_filters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,28 +152,21 @@ InternalValue Serialize::Filter(const InternalValue& value, RenderContext& conte
DocumentWrapper jsonDoc;
const auto jsonValue = jsonDoc.CreateValue(value);
const auto jsonString = jsonValue.AsString(static_cast<uint8_t>(indent));
const auto result = std::accumulate(jsonString.begin(), jsonString.end(), ""s, [](const auto &str, const auto &c)
{
switch (c)
{
case '<':
return str + "\\u003c";
break;
case '>':
return str +"\\u003e";
break;
case '&':
return str +"\\u0026";
break;
case '\'':
return str +"\\u0027";
break;
default:
return str + c;
break;
std::string result = ""s;
result.reserve(jsonString.size());
for (char c : jsonString) {
if (c == '<') {
result.append("\\u003c");
} else if (c == '>') {
result.append("\\u003e");
} else if (c == '&') {
result.append("\\u0026");
} else if (c == '\'') {
result.append("\\u0027");
} else {
result.push_back(c);
}
});

}
return result;
}

Expand Down

0 comments on commit e98b3b5

Please sign in to comment.