Skip to content

Commit e98b3b5

Browse files
authored
Fix quadric memcpy on toJson (#259)
* fix quadratic speed * add reserve
1 parent b32fbde commit e98b3b5

File tree

1 file changed

+14
-21
lines changed

1 file changed

+14
-21
lines changed

src/serialize_filters.cpp

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -152,28 +152,21 @@ InternalValue Serialize::Filter(const InternalValue& value, RenderContext& conte
152152
DocumentWrapper jsonDoc;
153153
const auto jsonValue = jsonDoc.CreateValue(value);
154154
const auto jsonString = jsonValue.AsString(static_cast<uint8_t>(indent));
155-
const auto result = std::accumulate(jsonString.begin(), jsonString.end(), ""s, [](const auto &str, const auto &c)
156-
{
157-
switch (c)
158-
{
159-
case '<':
160-
return str + "\\u003c";
161-
break;
162-
case '>':
163-
return str +"\\u003e";
164-
break;
165-
case '&':
166-
return str +"\\u0026";
167-
break;
168-
case '\'':
169-
return str +"\\u0027";
170-
break;
171-
default:
172-
return str + c;
173-
break;
155+
std::string result = ""s;
156+
result.reserve(jsonString.size());
157+
for (char c : jsonString) {
158+
if (c == '<') {
159+
result.append("\\u003c");
160+
} else if (c == '>') {
161+
result.append("\\u003e");
162+
} else if (c == '&') {
163+
result.append("\\u0026");
164+
} else if (c == '\'') {
165+
result.append("\\u0027");
166+
} else {
167+
result.push_back(c);
174168
}
175-
});
176-
169+
}
177170
return result;
178171
}
179172

0 commit comments

Comments
 (0)