9namespace Crunch::serdes {
25 static constexpr std::size_t
encode(uint64_t value,
26 std::span<std::byte> output,
27 std::size_t offset)
noexcept {
28 std::size_t start_offset = offset;
29 while (value >= 0x80) {
30 output[offset++] =
static_cast<std::byte
>((value & 0x7F) | 0x80);
33 output[offset++] =
static_cast<std::byte
>(value);
34 return offset - start_offset;
45 static constexpr std::optional<std::pair<uint64_t, std::size_t>>
decode(
46 std::span<const std::byte> input, std::size_t offset)
noexcept {
48 std::size_t shift = 0;
49 std::size_t bytes_read = 0;
51 while (offset + bytes_read < input.size()) {
52 uint8_t
byte =
static_cast<uint8_t
>(input[offset + bytes_read]);
60 value |=
static_cast<uint64_t
>(
byte & 0x7F) << shift;
63 if ((
byte & 0x80) == 0) {
64 return std::make_pair(value, bytes_read);
77 static constexpr std::size_t
size(uint64_t value)
noexcept {
81 std::size_t bytes = 0;
102 return (value_bits + 6) / 7;
Utility for Varint encoding/decoding.
Definition: crunch_varint.hpp:16
static constexpr std::optional< std::pair< uint64_t, std::size_t > > decode(std::span< const std::byte > input, std::size_t offset) noexcept
Decodes a Varint from a buffer.
Definition: crunch_varint.hpp:45
static constexpr std::size_t size(uint64_t value) noexcept
Calculates the size requirement for a value encoded as Varint.
Definition: crunch_varint.hpp:77
static consteval std::size_t max_varint_size(std::size_t value_bits)
Calculates the maximum varint size for a given number of bits.
Definition: crunch_varint.hpp:101
static constexpr std::size_t max_size
The maximum size required for a 64-bit integer encoded as Varint. ceil(64 / 7) = 10.
Definition: crunch_varint.hpp:94
static constexpr std::size_t encode(uint64_t value, std::span< std::byte > output, std::size_t offset) noexcept
Encodes a value as a Varint.
Definition: crunch_varint.hpp:25