std.bytes
API summary (generated from the Beans source by npm run coverage): 6 package functions.
std.bytes adds a CRC-32 checksum and unsigned varint helpers on top of the
builtin Bytes buffer. It is written in Beans; read
it at
stdlib/std/bytes/bytes.b.
import std.bytesA varint is a compact way to store an integer, using fewer bytes for small values. The unsigned form here is LEB128.
Checksum
Section titled “Checksum”pub fn crc32(data: Bytes) -> u32crc32is the IEEE CRC-32 checksum, using polynomial0xedb88320.
Unsigned varints
Section titled “Unsigned varints”pub fn uvarint_size(value: u64) -> intpub fn encode_uvarint(value: u64) -> Bytespub fn append_uvarint(data: Bytes, value: u64)pub fn decode_uvarint(data: Bytes) -> Option<u64>pub fn decode_uvarint_at_or(data: Bytes, start: int, fallback: u64) -> u64uvarint_sizereports how many bytesvalueneeds as an unsigned varint.encode_uvarintreturns a new buffer holdingvalueas an unsigned varint.append_uvarintappendsvalueas a varint todatain place and returns nothing.decode_uvarintreads a varint from the start ofdata. It returnsnoneif the data is truncated or the value overflows 64 bits, and it reads at most 10 bytes.decode_uvarint_at_orreads a varint starting atstart. It returnsfallbackwhenstartis out of range, or when the value is truncated or overflows.
import std.ioimport std.bytes
fn main() { let buf: Bytes = bytes.encode_uvarint(300) io.println(buf.len()) // 2 let value: Option<u64> = bytes.decode_uvarint(buf) io.println(value) // some(300) io.println(bytes.crc32(Bytes.from("hello")))}See also
Section titled “See also”- Bytes, the buffer type, which also has its own
crc32andappend_uvarintmethods. - std.encoding.binary, a fuller set of fixed-width and varint reads and writes.