diff options
| author | 2019-06-12 17:47:05 -0400 | |
|---|---|---|
| committer | 2019-06-12 17:54:11 -0400 | |
| commit | 969cd6dc1d60acd98c89815dd53c11bf4dac2518 (patch) | |
| tree | 56f5632abd17d87ef128d8029b86438d928bcd97 /src/common | |
| parent | common/hex_util: Combine HexVectorToString() and HexArrayToString() (diff) | |
| download | yuzu-969cd6dc1d60acd98c89815dd53c11bf4dac2518.tar.gz yuzu-969cd6dc1d60acd98c89815dd53c11bf4dac2518.tar.xz yuzu-969cd6dc1d60acd98c89815dd53c11bf4dac2518.zip | |
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.
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/hex_util.h | 5 |
1 files changed, 5 insertions, 0 deletions
diff --git a/src/common/hex_util.h b/src/common/hex_util.h index a64c9b485..bb4736f96 100644 --- a/src/common/hex_util.h +++ b/src/common/hex_util.h | |||
| @@ -36,10 +36,15 @@ std::string HexToString(const ContiguousContainer& data, bool upper = true) { | |||
| 36 | static_assert(std::is_same_v<typename ContiguousContainer::value_type, u8>, | 36 | static_assert(std::is_same_v<typename ContiguousContainer::value_type, u8>, |
| 37 | "Underlying type within the contiguous container must be u8."); | 37 | "Underlying type within the contiguous container must be u8."); |
| 38 | 38 | ||
| 39 | constexpr std::size_t pad_width = 2; | ||
| 40 | |||
| 39 | std::string out; | 41 | std::string out; |
| 42 | out.reserve(std::size(data) * pad_width); | ||
| 43 | |||
| 40 | for (const u8 c : data) { | 44 | for (const u8 c : data) { |
| 41 | out += fmt::format(upper ? "{:02X}" : "{:02x}", c); | 45 | out += fmt::format(upper ? "{:02X}" : "{:02x}", c); |
| 42 | } | 46 | } |
| 47 | |||
| 43 | return out; | 48 | return out; |
| 44 | } | 49 | } |
| 45 | 50 | ||