diff --git a/solanaceae/util/span.hpp b/solanaceae/util/span.hpp index faacd8c..0a7e954 100644 --- a/solanaceae/util/span.hpp +++ b/solanaceae/util/span.hpp @@ -39,6 +39,20 @@ struct Span final { return ptr[i]; } + constexpr bool operator==(const Span& other) const { + if (empty() || size != other.size) { + return false; + } + + for (uint64_t i = 0; i < size; i++) { + if ((*this)[i] != other[i]) { + return false; + } + } + + return true; + } + #if 0 constexpr T& operator[](uint64_t i) { if (i > size) { diff --git a/solanaceae/util/utils.cpp b/solanaceae/util/utils.cpp index d39991b..1b387b9 100644 --- a/solanaceae/util/utils.cpp +++ b/solanaceae/util/utils.cpp @@ -44,12 +44,16 @@ std::vector hex2bin(const std::string_view str) { return bin; } -std::string bin2hex(const std::vector& data) { +std::string bin2hex(const ByteSpan bin) { std::string str; - for (size_t i = 0; i < data.size(); i++) { - str.push_back(detail::nib_to_hex(data[i] >> 4)); - str.push_back(detail::nib_to_hex(data[i] & 0x0f)); + for (size_t i = 0; i < bin.size; i++) { + str.push_back(detail::nib_to_hex(bin[i] >> 4)); + str.push_back(detail::nib_to_hex(bin[i] & 0x0f)); } return str; } +std::string bin2hex(const std::vector& bin) { + return bin2hex(ByteSpan{bin}); +} + diff --git a/solanaceae/util/utils.hpp b/solanaceae/util/utils.hpp index 638427c..0ababdc 100644 --- a/solanaceae/util/utils.hpp +++ b/solanaceae/util/utils.hpp @@ -4,7 +4,10 @@ #include #include +#include "./span.hpp" + [[nodiscard]] std::vector hex2bin(const std::string& str); [[nodiscard]] std::vector hex2bin(const std::string_view str); +[[nodiscard]] std::string bin2hex(const ByteSpan bin); [[nodiscard]] std::string bin2hex(const std::vector& bin);