Module: encoding.varint

Source: ./encoding/varint.reef


Overview

encoding/varint - Variable-length integer encoding

Implements Protocol Buffer style variable-length integer encoding (LEB128). Small values use fewer bytes, making this efficient for data that is typically small but occasionally large.

Encoding scheme (unsigned):

  • Each byte uses 7 bits for data, 1 bit as continuation flag
  • MSB = 1 means more bytes follow, MSB = 0 means last byte
  • Values 0-127 use 1 byte, 128-16383 use 2 bytes, etc.

Signed integers use ZigZag encoding: 0 -> 0, -1 -> 1, 1 -> 2, -2 -> 3, 2 -> 4, ...

Usage: import encoding.varint let buf: [int] = varint_alloc_buffer() let len: int = varint_encode(300, buf) // Returns bytes written let val: int = varint_decode(buf, 0) // Decode from offset 0


Functions

fn varint_alloc_buffer(): [int]

fn zigzag_encode(n: int): int

ZigZag encode: maps signed integers to unsigned 0 -> 0, -1 -> 1, 1 -> 2, -2 -> 3, 2 -> 4, ...

fn zigzag_decode(n: int): int

ZigZag decode: maps unsigned back to signed

fn varint_size(value: int): int

Calculate bytes needed to encode an unsigned value

fn varint_size_signed(value: int): int

Calculate bytes needed for signed value (using ZigZag)

fn varint_encode(value: int, buf: [int]): int

Encode unsigned varint to buffer, returns number of bytes written

fn varint_decode(buf: [int], offset: int): int

Decode unsigned varint from buffer at offset, returns value

fn varint_decode_size(buf: [int], offset: int): int

Calculate size of encoded varint at offset (without decoding value)

fn varint_encode_signed(value: int, buf: [int]): int

Encode signed integer using ZigZag encoding

fn varint_decode_signed(buf: [int], offset: int): int

Decode signed integer using ZigZag encoding

fn varint_encode_bytes(value: int, buf: [int], offset: int): int

Encode to buffer at offset, returns bytes written

fn varint_decode_bytes(buf: [int], offset: int, out_value: [int]): int

Decode from buffer at offset, puts value in out_value[0], returns bytes read


Generated by reefc doc