Expand description
Lexicographically ordered variable-length integer (varint) encoding.
This encoding ensures that the byte-wise (lexicographical) comparison of encoded values matches the numerical comparison of the original values.
This is achieved by:
- Dividing the value space into distinct length categories.
- Assigning each category a non-overlapping range of first-byte values, ordered by length.
- Encoding the remaining bits in Big-Endian order (which naturally matches numerical order).
Encoding rules (bit-level):
v < 0xc0(0..191): 1 byte- Pattern:
0b0xxxxxxxor0b10xxxxxx - First byte range:
0x00..=0xbf
- Pattern:
v < 0x2000(192..8191): 2 bytes- Encoded as
v | 0xc000(Big Endian) - Pattern:
0b110xxxxx xxxxxxxx - First byte range:
0xc0..=0xdf
- Encoded as
v < 0x1000_0000(8192..268435455): 4 bytes- Encoded as
v | 0xe000_0000(Big Endian) - Pattern:
0b1110xxxx xxxxxxxx ... - First byte range:
0xe0..=0xef
- Encoded as
v < 0x0f00_0000_0000_0000: 8 bytes- Encoded as
v | 0xf000_0000_0000_0000(Big Endian) - Pattern:
0b1111xxxx xxxxxxxx ... - First byte range:
0xf0..=0xfe
- Encoded as
- Else: 9 bytes
- Encoded as
0xfffollowed byv.to_be_bytes() - Pattern:
0b11111111 xxxxxxxx ... - First byte:
0xff
- Encoded as