Skip to content

Commit

Permalink
Avoid potentially unnecessary string reallocs.
Browse files Browse the repository at this point in the history
In case dest is not created using a subset of the `input` `result.reserve(input.size())`
would result in increasing `result`'s size with likely reallocation.
  • Loading branch information
ttsugriy authored and anonrig committed Aug 28, 2023
1 parent cf504eb commit aeaec10
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/unicode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,10 @@ std::string percent_encode(const std::string_view input,
return std::string(input);
}

std::string result(input.substr(0, std::distance(input.begin(), pointer)));
std::string result;
result.reserve(input.length()); // in the worst case, percent encoding might
// produce 3 characters.
result.append(input.substr(0, std::distance(input.begin(), pointer)));

for (; pointer != input.end(); pointer++) {
if (character_sets::bit_at(character_set, *pointer)) {
Expand Down

0 comments on commit aeaec10

Please sign in to comment.