SagaSaga
Standard Library

Std.BitString

Operations on raw binary data (Erlang bitstrings).

Functions

size

fun size : (bs: BitString) -> Int

Returns the size of the bitstring in bytes.

from_list

fun from_list : (bytes: List Int) -> BitString

Creates a bitstring from a list of byte values (0-255).

to_list

fun to_list : (bs: BitString) -> List Int

Converts a bitstring to a list of byte values.

from_string

fun from_string : (s: String) -> BitString

Creates a bitstring from a UTF-8 encoded string. This is a zero-cost operation since Dylan strings are already UTF-8 binaries.

to_string

fun to_string : (bs: BitString) -> Result String String

Attempts to decode a bitstring as a UTF-8 string. Returns Err if the bitstring is not valid UTF-8.

to_string_unchecked

fun to_string_unchecked : (bs: BitString) -> String

Reinterprets a bitstring as a String with no validation. Zero-cost (Saga strings are already UTF-8 binaries), but UNSAFE: the caller must guarantee the bytes are valid UTF-8. Use when validity is established by construction (e.g. concatenating fragments that are themselves valid UTF-8) and the to_string validation pass is pure overhead. When in doubt, use to_string.

at

fun at : (index: Int) -> (bs: BitString) -> Maybe Int

Returns the byte at the given zero-based index, or Nothing if out of bounds.

slice

fun slice : (start: Int) -> (len: Int) -> (bs: BitString) -> BitString

Extracts a sub-bitstring starting at position for length bytes.

append

fun append : (a: BitString) -> (b: BitString) -> BitString

Appends two bitstrings. Also available via the <> operator.

concat

fun concat : (parts: List BitString) -> BitString

Concatenates a list of bitstrings into one, in a single pass. Backed by Erlang's iolist_to_binary, so the result is built once with the total size known up front. Prefer this over folding append/<> across a list: each append allocates and copies the whole accumulator, so an N-fragment fold is O(N^2), whereas concat is O(total bytes). This is the right way to flatten iodata-style fragment lists at a serialization boundary.

zeroes

fun zeroes : (n: Int) -> BitString

Creates a bitstring of the given size filled with zero bytes.

from_byte

fun from_byte : (byte: Int) -> BitString

Creates a single-byte bitstring from an integer (0-255).

encode_int

fun encode_int : (width: Int) -> (value: Int) -> BitString

Encodes an integer as a big-endian bitstring of the given byte width.

decode_int

fun decode_int : (bs: BitString) -> Int

Decodes a big-endian integer from a bitstring.

encode_int_little

fun encode_int_little : (width: Int) -> (value: Int) -> BitString

Encodes an integer as a little-endian bitstring of the given byte width.

decode_int_little

fun decode_int_little : (bs: BitString) -> Int

Decodes a little-endian integer from a bitstring.

is_empty

fun is_empty : BitString -> Bool

Returns True if the bitstring is empty (zero bytes).