Fixed-width and variable-length integer encodings for MoonBit.
// The fixed widths, either way round, reading nothing it was not given.
let buf = Buffer()
@fixed.write_u32(buf, 0x12345678)
@fixed.write_u16(buf, 0x1234, order=Little)
let wire = buf.to_bytes()
assert_eq(wire, b"\x12\x34\x56\x78\x34\x12")
assert_eq(@fixed.read_u32(wire[:]), Some(305419896))
assert_eq(@fixed.read_u32(wire[:], at=10), None) // the view is shorter
// One number, in each protocol's own spelling.
assert_eq(@quic.encode(1337UL), b"\x45\x39")
assert_eq(@prefix.encode(1337, prefix=5), b"\x1f\x9a\x0a")
assert_eq(@leb128.encode(1337UL), b"\xb9\x0a")
assert_eq(@lenenc.encode(1337UL), b"\xfc\x39\x05")| Encoding | Written by | |
|---|---|---|
| fixed | Byte order for u8…u64, i8…i64, f32, f64, the 24-bit length protocols use, and a width carried in a variable | Everything |
| quic | Two bits name the width, sixty-two carry the value — RFC 9000 §16 | QUIC, HTTP/3 |
| prefix | A value in the low N bits, continuing when it does not fit — RFC 7541 §5.1 | HPACK, QPACK |
| leb128 | Seven bits an octet, least significant first, plus zigzag for signed values | protobuf, DWARF |
| lenenc | One octet below 251, otherwise a marker and two, three or eight little-endian octets | MySQL, MariaDB |
| fixed | quic | prefix(5) | leb128 | lenenc |
|---|---|---|---|---|
| 05 39 | 45 39 | 1f 9a 0a | b9 0a | fc 39 05 |
moon add moonbitstack/moonvarimport {
"moonbitstack/moonvar/quic",
}| src/examples/tour | The whole surface in one run: every encoding, both directions, and the octet count each costs |
| src/examples/frame | A length-prefixed frame — a varint length, then a payload — with the truncated case refused rather than half-read |
moon run src/examples/tour
moon run src/examples/frameInstall
Download zipFixed-width and variable-length integer encodings for MoonBit.