diff options
author | Zach Hilman <DarkLordZach@users.noreply.github.com> | 2019-06-15 16:47:13 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-06-15 16:47:13 -0400 |
commit | c140b6ae2ca6bc318f47b74a6946ddb10d282dbe (patch) | |
tree | 6407b0465b93a7cfc0a5733e0d8dd3bb26742a15 /src/common | |
parent | 0360c40e90eda9ad0cf9db3a3ee638d68d16d5bd (diff) | |
parent | 969cd6dc1d60acd98c89815dd53c11bf4dac2518 (diff) |
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) { return out; } -std::string HexVectorToString(const std::vector<u8>& vector, bool upper) { - std::string out; - for (u8 c : vector) - out += fmt::format(upper ? "{:02X}" : "{:02x}", c); - return out; -} - std::array<u8, 16> operator""_array16(const char* str, std::size_t len) { if (len != 32) { 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 @@ #include <array> #include <cstddef> #include <string> +#include <type_traits> #include <vector> #include <fmt/format.h> #include "common/common_types.h" @@ -30,13 +31,20 @@ std::array<u8, Size> HexStringToArray(std::string_view str) { return out; } -std::string HexVectorToString(const std::vector<u8>& vector, bool upper = true); +template <typename ContiguousContainer> +std::string HexToString(const ContiguousContainer& data, bool upper = true) { + static_assert(std::is_same_v<typename ContiguousContainer::value_type, u8>, + "Underlying type within the contiguous container must be u8."); + + constexpr std::size_t pad_width = 2; -template <std::size_t Size> -std::string HexArrayToString(std::array<u8, Size> array, bool upper = true) { std::string out; - for (u8 c : array) + out.reserve(std::size(data) * pad_width); + + for (const u8 c : data) { out += fmt::format(upper ? "{:02X}" : "{:02x}", c); + } + return out; } |