Files
obitools4/autodoc/docmd/pkg/obikmer/varint.md
T
Eric Coissac 8c7017a99d ⬆️ version bump to v4.5
- Update obioptions.Version from "Release 4.4.29" to "/v/ Release v5"
- Update version.txt from 4.29 → .30
(automated by Makefile)
2026-04-13 13:34:53 +02:00

34 lines
1.5 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Variable-Length Integer Encoding/Decoding Utility
This Go package (`obikmer`) provides efficient serialization of `uint64` integers using **protobuf-style variable-length encoding (varint)**.
## Core Features
-`EncodeVarint(io.Writer, uint64) (n int, err error)`
Writes a `uint64` as a compact varint to any `io.Writer`. Uses **7 bits per byte**, with the MSB as a continuation flag. Max 10 bytes for `uint64`.
-`DecodeVarint(io.Reader) (val uint64, err error)`
Reads and decodes a varint from any `io.Reader`. Handles multi-byte sequences safely; returns error on malformed input or overflow (>70 bits).
-`VarintLen(uint64) int`
Computes the exact byte length required to encode a value *without* performing I/O — useful for buffer preallocation or size estimation.
## Encoding Scheme
- Each byte holds 7 bits of data; bit8 (MSB) = `1` if more bytes follow, else `0`.
- Example:
- `0x7F``1 byte`:`0111_1111`
- `0x80``2 bytes`:`1000_0000 0000_0001`
## Use Cases
- Network protocols & binary file formats requiring compact integer representation
- Serialization frameworks (e.g., custom protobuf-like codecs)
- Embedded systems or bandwidth-constrained environments where space efficiency matters
## Design Notes
- No external dependencies; uses only `io` from the standard library.
- Thread-safe *per call* (no shared state), but `io.Reader`/`Writer` concurrency must be handled externally.
- Compatible with standard protobuf varint format (e.g., interoperable with `encoding/binary` or gRPC).