Tiny speedup when listing tags

Noticed that the code was originally concatenating strings just to
then append the result to another string. Now it does not create
temporaries and also preallocates the string buffer.
This commit is contained in:
Martin Hořeňovský 2019-08-05 19:12:25 +02:00
parent 3701c2e2e6
commit cf55cfd76f
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A

View File

@ -84,9 +84,18 @@ namespace Catch {
}
std::string TagInfo::all() const {
std::string out;
for( auto const& spelling : spellings )
out += "[" + spelling + "]";
size_t size = 0;
for (auto const& spelling : spellings) {
// Add 2 for the brackes
size += spelling.size() + 2;
}
std::string out; out.reserve(size);
for (auto const& spelling : spellings) {
out += '[';
out += spelling;
out += ']';
}
return out;
}