18#ifndef TRINITY_BASE_ENCODING_HPP
19#define TRINITY_BASE_ENCODING_HPP
31template <
typename Encoding>
37 static_assert(
BITS_PER_CHAR < 8,
"Encoding parameters are invalid");
40 static constexpr char PADDING = Encoding::PADDING;
58 static std::string
Encode(std::vector<uint8>
const& data)
60 auto it = data.begin(), end = data.end();
83 thisC = (*it & ((1 << bitsLeft) - 1)) << (
BITS_PER_CHAR - bitsLeft);
86 thisC |= (*it >> bitsLeft);
88 s.append(1, Encoding::Encode(thisC));
105 auto it = data.begin(), end = data.end();
107 return std::vector<uint8>();
109 std::vector<uint8> v;
112 uint8 currentByte = 0;
114 while ((it != end) && (*it !=
PADDING))
116 uint8 cur = Encoding::Decode(*(it++));
123 currentByte |= (cur << bitsLeft);
128 currentByte |= (cur >> bitsLeft);
129 v.push_back(currentByte);
130 currentByte = (cur & ((1 << bitsLeft) - 1));
131 bitsLeft = 8 - bitsLeft;
132 currentByte <<= bitsLeft;
140 while ((it != end) && (*it ==
PADDING) && (bitsLeft != 8))
std::optional< T > Optional
Optional helper class to wrap optional values within.
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 const &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)