add span equality and span for bin2hex

This commit is contained in:
Green Sky 2024-04-10 13:26:54 +02:00
parent 659b410caa
commit bee42d4688
No known key found for this signature in database
3 changed files with 25 additions and 4 deletions

View File

@ -39,6 +39,20 @@ struct Span final {
return ptr[i]; 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 #if 0
constexpr T& operator[](uint64_t i) { constexpr T& operator[](uint64_t i) {
if (i > size) { if (i > size) {

View File

@ -44,12 +44,16 @@ std::vector<uint8_t> hex2bin(const std::string_view str) {
return bin; return bin;
} }
std::string bin2hex(const std::vector<uint8_t>& data) { std::string bin2hex(const ByteSpan bin) {
std::string str; std::string str;
for (size_t i = 0; i < data.size(); i++) { for (size_t i = 0; i < bin.size; i++) {
str.push_back(detail::nib_to_hex(data[i] >> 4)); str.push_back(detail::nib_to_hex(bin[i] >> 4));
str.push_back(detail::nib_to_hex(data[i] & 0x0f)); str.push_back(detail::nib_to_hex(bin[i] & 0x0f));
} }
return str; return str;
} }
std::string bin2hex(const std::vector<uint8_t>& bin) {
return bin2hex(ByteSpan{bin});
}

View File

@ -4,7 +4,10 @@
#include <string_view> #include <string_view>
#include <vector> #include <vector>
#include "./span.hpp"
[[nodiscard]] std::vector<uint8_t> hex2bin(const std::string& str); [[nodiscard]] std::vector<uint8_t> hex2bin(const std::string& str);
[[nodiscard]] std::vector<uint8_t> hex2bin(const std::string_view str); [[nodiscard]] std::vector<uint8_t> hex2bin(const std::string_view str);
[[nodiscard]] std::string bin2hex(const ByteSpan bin);
[[nodiscard]] std::string bin2hex(const std::vector<uint8_t>& bin); [[nodiscard]] std::string bin2hex(const std::vector<uint8_t>& bin);