Varint
LEB128 base-128 integers, demoing the p/moul/x/daily/varint library.
Unsigned
Seven payload bits per byte, so the length steps at every power of 128.
| value | encoding | bytes |
|---|---|---|
0 |
00 |
1 |
1 |
01 |
1 |
127 |
7f |
1 |
128 |
80 01 |
2 |
300 |
ac 02 |
2 |
16383 |
ff 7f |
2 |
16384 |
80 80 01 |
3 |
4294967296 |
80 80 80 80 10 |
5 |
18446744073709551615 |
ff ff ff ff ff ff ff ff ff 01 |
10 |
Signed, through zigzag
Encoding a negative directly would sign-extend it to ten bytes. Zigzag interleaves the signs first, so -1 costs one byte and not ten.
| value | zigzag | encoding | bytes |
|---|---|---|---|
0 |
0 |
00 |
1 |
-1 |
1 |
01 |
1 |
1 |
2 |
02 |
1 |
-64 |
127 |
7f |
1 |
64 |
128 |
80 01 |
2 |
-9223372036854775808 |
18446744073709551615 |
ff ff ff ff ff ff ff ff ff 01 |
10 |
9223372036854775807 |
18446744073709551614 |
fe ff ff ff ff ff ff ff ff 01 |
10 |
Only the canonical encoding decodes
A padded varint means the same number has two spellings, which is a different hash for the same state. The decoder refuses them.
| input | result |
|---|---|
80 00, zero with one padding group |
varint: non-canonical encoding |
81 00, one with one padding group |
varint: non-canonical encoding |
80, continuation bit then nothing |
varint: truncated |
ff ... ff 02, ten groups, eleven bits too many |
varint: value overflows uint64 |
Try another value: append it to the path, for instance
/-300.