Skip to main content

Module varint

Module varint 

Source
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:

  1. Dividing the value space into distinct length categories.
  2. Assigning each category a non-overlapping range of first-byte values, ordered by length.
  3. Encoding the remaining bits in Big-Endian order (which naturally matches numerical order).

Encoding rules (bit-level):

  • v < 0xc0 (0..191): 1 byte
    • Pattern: 0b0xxxxxxx or 0b10xxxxxx
    • First byte range: 0x00..=0xbf
  • v < 0x2000 (192..8191): 2 bytes
    • Encoded as v | 0xc000 (Big Endian)
    • Pattern: 0b110xxxxx xxxxxxxx
    • First byte range: 0xc0..=0xdf
  • v < 0x1000_0000 (8192..268435455): 4 bytes
    • Encoded as v | 0xe000_0000 (Big Endian)
    • Pattern: 0b1110xxxx xxxxxxxx ...
    • First byte range: 0xe0..=0xef
  • v < 0x0f00_0000_0000_0000: 8 bytes
    • Encoded as v | 0xf000_0000_0000_0000 (Big Endian)
    • Pattern: 0b1111xxxx xxxxxxxx ...
    • First byte range: 0xf0..=0xfe
  • Else: 9 bytes
    • Encoded as 0xff followed by v.to_be_bytes()
    • Pattern: 0b11111111 xxxxxxxx ...
    • First byte: 0xff

Traits§

Buffer

Functions§

decode_varint
encode_varint