18#ifndef TRINITY_BASE_ENCODING_HPP
19#define TRINITY_BASE_ENCODING_HPP
32template <
typename Encoding>
38 static_assert(
BITS_PER_CHAR < 8,
"Encoding parameters are invalid");
41 static constexpr char PADDING = Encoding::PADDING;
59 static std::string
Encode(std::vector<uint8>
const& data)
61 auto it = data.begin(), end = data.end();
84 thisC = (*it & ((1 << bitsLeft) - 1)) << (
BITS_PER_CHAR - bitsLeft);
87 thisC |= (*it >> bitsLeft);
89 s.append(1, Encoding::Encode(thisC));
106 auto it = data.begin(), end = data.end();
108 return std::vector<uint8>();
110 std::vector<uint8> v;
113 uint8 currentByte = 0;
115 while ((it != end) && (*it !=
PADDING))
117 uint8 cur = Encoding::Decode(*(it++));
124 currentByte |= (cur << bitsLeft);
129 currentByte |= (cur >> bitsLeft);
130 v.push_back(currentByte);
131 currentByte = (cur & ((1 << bitsLeft) - 1));
132 bitsLeft = 8 - bitsLeft;
133 currentByte <<= bitsLeft;
141 while ((it != end) && (*it ==
PADDING) && (bitsLeft != 8))
std::optional< T > Optional
Optional helper class to wrap optional values within.
constexpr std::size_t size()
static constexpr std::size_t PAD_TO
static constexpr std::size_t BITS_PER_CHAR
static std::string Encode(std::vector< uint8 > const &data)
static Optional< std::vector< uint8 > > Decode(std::string_view data)
static constexpr char PADDING
static constexpr std::size_t EncodedSize(std::size_t size)
static constexpr uint8 DECODE_ERROR
static constexpr std::size_t DecodedSize(std::size_t size)