forked from ShuriZma/suyu
1
0
Fork 0

common/hex_util: Reserve std::string memory ahead of time

Avoids potentially performing multiple reallocations (depending on the
size of the input data) by reserving the necessary amount of memory
ahead of time.

This is trivially doable, so there's no harm in it.
This commit is contained in:
Lioncash 2019-06-12 17:47:05 -04:00
parent a62088539e
commit 969cd6dc1d
No known key found for this signature in database
GPG Key ID: 4E3C3CC1031BA9C7
1 changed files with 5 additions and 0 deletions

View File

@ -36,10 +36,15 @@ std::string HexToString(const ContiguousContainer& data, bool upper = true) {
static_assert(std::is_same_v<typename ContiguousContainer::value_type, u8>, static_assert(std::is_same_v<typename ContiguousContainer::value_type, u8>,
"Underlying type within the contiguous container must be u8."); "Underlying type within the contiguous container must be u8.");
constexpr std::size_t pad_width = 2;
std::string out; std::string out;
out.reserve(std::size(data) * pad_width);
for (const u8 c : data) { for (const u8 c : data) {
out += fmt::format(upper ? "{:02X}" : "{:02x}", c); out += fmt::format(upper ? "{:02X}" : "{:02x}", c);
} }
return out; return out;
} }