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) // 12 34 56 78
@fixed.write_u16(buf, 0x1234, order=Little) // 34 12
let wire = buf.to_bytes()
@fixed.read_u32(wire[:]) // Some(305419896)
@fixed.read_u32(wire[:], at=10) // None, the view is shorter
// One number, in each protocol's own spelling.
@quic.encode(1337UL) // 45 39
@prefix.encode(1337, prefix=5) // 1f 9a 0a
@leb128.encode(1337UL) // b9 0a
@lenenc.encode(1337UL) // fc 39 05| 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.