diff options
| author | 2019-06-15 16:47:13 -0400 | |
|---|---|---|
| committer | 2019-06-15 16:47:13 -0400 | |
| commit | c140b6ae2ca6bc318f47b74a6946ddb10d282dbe (patch) | |
| tree | 6407b0465b93a7cfc0a5733e0d8dd3bb26742a15 /src/common | |
| parent | Merge pull request #2582 from lioncash/reserved (diff) | |
| parent | common/hex_util: Reserve std::string memory ahead of time (diff) | |
| download | yuzu-c140b6ae2ca6bc318f47b74a6946ddb10d282dbe.tar.gz yuzu-c140b6ae2ca6bc318f47b74a6946ddb10d282dbe.tar.xz yuzu-c140b6ae2ca6bc318f47b74a6946ddb10d282dbe.zip | |
Merge pull request #2581 from lioncash/hex
common/hex_util: Combine HexVectorToString() and HexArrayToString()
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/hex_util.cpp | 7 | ||||
| -rw-r--r-- | src/common/hex_util.h | 16 |
2 files changed, 12 insertions, 11 deletions
diff --git a/src/common/hex_util.cpp b/src/common/hex_util.cpp index 5b63f9e81..c2f6cf0f6 100644 --- a/src/common/hex_util.cpp +++ b/src/common/hex_util.cpp | |||
| @@ -30,13 +30,6 @@ std::vector<u8> HexStringToVector(std::string_view str, bool little_endian) { | |||
| 30 | return out; | 30 | return out; |
| 31 | } | 31 | } |
| 32 | 32 | ||
| 33 | std::string HexVectorToString(const std::vector<u8>& vector, bool upper) { | ||
| 34 | std::string out; | ||
| 35 | for (u8 c : vector) | ||
| 36 | out += fmt::format(upper ? "{:02X}" : "{:02x}", c); | ||
| 37 | return out; | ||
| 38 | } | ||
| 39 | |||
| 40 | std::array<u8, 16> operator""_array16(const char* str, std::size_t len) { | 33 | std::array<u8, 16> operator""_array16(const char* str, std::size_t len) { |
| 41 | if (len != 32) { | 34 | if (len != 32) { |
| 42 | LOG_ERROR(Common, | 35 | LOG_ERROR(Common, |
diff --git a/src/common/hex_util.h b/src/common/hex_util.h index 68f003cb6..bb4736f96 100644 --- a/src/common/hex_util.h +++ b/src/common/hex_util.h | |||
| @@ -7,6 +7,7 @@ | |||
| 7 | #include <array> | 7 | #include <array> |
| 8 | #include <cstddef> | 8 | #include <cstddef> |
| 9 | #include <string> | 9 | #include <string> |
| 10 | #include <type_traits> | ||
| 10 | #include <vector> | 11 | #include <vector> |
| 11 | #include <fmt/format.h> | 12 | #include <fmt/format.h> |
| 12 | #include "common/common_types.h" | 13 | #include "common/common_types.h" |
| @@ -30,13 +31,20 @@ std::array<u8, Size> HexStringToArray(std::string_view str) { | |||
| 30 | return out; | 31 | return out; |
| 31 | } | 32 | } |
| 32 | 33 | ||
| 33 | std::string HexVectorToString(const std::vector<u8>& vector, bool upper = true); | 34 | template <typename ContiguousContainer> |
| 35 | std::string HexToString(const ContiguousContainer& data, bool upper = true) { | ||
| 36 | static_assert(std::is_same_v<typename ContiguousContainer::value_type, u8>, | ||
| 37 | "Underlying type within the contiguous container must be u8."); | ||
| 38 | |||
| 39 | constexpr std::size_t pad_width = 2; | ||
| 34 | 40 | ||
| 35 | template <std::size_t Size> | ||
| 36 | std::string HexArrayToString(std::array<u8, Size> array, bool upper = true) { | ||
| 37 | std::string out; | 41 | std::string out; |
| 38 | for (u8 c : array) | 42 | out.reserve(std::size(data) * pad_width); |
| 43 | |||
| 44 | for (const u8 c : data) { | ||
| 39 | out += fmt::format(upper ? "{:02X}" : "{:02x}", c); | 45 | out += fmt::format(upper ? "{:02X}" : "{:02x}", c); |
| 46 | } | ||
| 47 | |||
| 40 | return out; | 48 | return out; |
| 41 | } | 49 | } |
| 42 | 50 | ||