Std.BitString
Operations on raw binary data (Erlang bitstrings).
Functions
size
fun size : (bs: BitString) -> IntReturns the size of the bitstring in bytes.
from_list
fun from_list : (bytes: List Int) -> BitStringCreates a bitstring from a list of byte values (0-255).
to_list
fun to_list : (bs: BitString) -> List IntConverts a bitstring to a list of byte values.
from_string
fun from_string : (s: String) -> BitStringCreates 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 StringAttempts 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) -> StringReinterprets 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 IntReturns the byte at the given zero-based index, or Nothing if out of bounds.
slice
fun slice : (start: Int) -> (len: Int) -> (bs: BitString) -> BitStringExtracts a sub-bitstring starting at position for length bytes.
append
fun append : (a: BitString) -> (b: BitString) -> BitStringAppends two bitstrings. Also available via the <> operator.
concat
fun concat : (parts: List BitString) -> BitStringConcatenates 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) -> BitStringCreates a bitstring of the given size filled with zero bytes.
from_byte
fun from_byte : (byte: Int) -> BitStringCreates a single-byte bitstring from an integer (0-255).
encode_int
fun encode_int : (width: Int) -> (value: Int) -> BitStringEncodes an integer as a big-endian bitstring of the given byte width.
decode_int
fun decode_int : (bs: BitString) -> IntDecodes a big-endian integer from a bitstring.
encode_int_little
fun encode_int_little : (width: Int) -> (value: Int) -> BitStringEncodes an integer as a little-endian bitstring of the given byte width.
decode_int_little
fun decode_int_little : (bs: BitString) -> IntDecodes a little-endian integer from a bitstring.
is_empty
fun is_empty : BitString -> BoolReturns True if the bitstring is empty (zero bytes).