12[[nodiscard]]
static constexpr char encode2(uint8_t x)
14 return (x < 10) ? char(x +
'0') : char(x - 10 +
'A');
16[[nodiscard]]
static auto encode(uint8_t x)
18 return tmpStrCat(encode2(x >> 4), encode2(x & 15));
20std::string encode(std::span<const uint8_t> input,
bool newlines)
24 auto len = input.size();
26 if (newlines && !ret.empty()) ret +=
'\n';
27 int t = int(std::min<size_t>(16, len));
29 ret += encode(input[in++]);
30 if (i != (
t - 1)) ret +=
' ';
37[[nodiscard]]
static constexpr int decode(
char x)
39 if ((
'0' <= x) && (x <=
'9')) {
41 }
else if ((
'A' <= x) && (x <=
'F')) {
43 }
else if ((
'a' <= x) && (x <=
'f')) {
49std::pair<MemBuffer<uint8_t>,
size_t> decode(std::string_view input)
51 auto inSize = input.size();
52 auto outSize = inSize / 2;
58 for (
char c : input) {
60 if (d == -1)
continue;
64 tmp = narrow<uint8_t>(d);
66 ret[out++] = narrow<uint8_t>((tmp << 4) | d);
71 assert(outSize >= out);
73 return {std::move(ret), out};
79 auto outSize = output.size();
82 for (
char c : input) {
84 if (d == -1)
continue;
88 tmp = narrow<uint8_t>(d);
90 if (out == outSize) [[unlikely]]
return false;
91 output[out++] = narrow<uint8_t>((tmp << 4) | d);
96 return out == outSize;
This class manages the lifetime of a block of memory.
void resize(size_t size)
Grow or shrink the memory block.
bool decode_inplace(std::string_view input, std::span< uint8_t > output)
TemporaryString tmpStrCat(Ts &&... ts)
constexpr auto xrange(T e)