add span equality and span for bin2hex
This commit is contained in:
parent
659b410caa
commit
bee42d4688
@ -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) {
|
||||
|
@ -44,12 +44,16 @@ std::vector<uint8_t> hex2bin(const std::string_view str) {
|
||||
return bin;
|
||||
}
|
||||
|
||||
std::string bin2hex(const std::vector<uint8_t>& 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<uint8_t>& bin) {
|
||||
return bin2hex(ByteSpan{bin});
|
||||
}
|
||||
|
||||
|
@ -4,7 +4,10 @@
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
#include "./span.hpp"
|
||||
|
||||
[[nodiscard]] std::vector<uint8_t> hex2bin(const std::string& 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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user