std/bit_array
Functions for BitArray, the type of lists of bits.
A bit array may have any number of bits, not only whole bytes. Functions
that count bytes say so. The base16 and base64 functions work on
whole bytes: they first fill the last byte with zero bits.
from_string
The UTF-8 bytes of a string.
from_string = (string : String) -> BitArray { <<string:utf8>> } to_string
The string, when the bits are whole bytes of correct UTF-8, or
Error(Nil).
bit_array.to_string(<<104, 105>>) // Ok("hi")
bit_array.to_string(<<255>>) // Error(Nil) to_string = (bits : BitArray) -> Result(String, Nil) { is_utf8
True when the bits are whole bytes of correct UTF-8.
is_utf8 = (bits : BitArray) -> Bool { bit_size
The number of bits.
bit_size = (bits : BitArray) -> Int { core.bit_size(bits) } byte_size
The number of bytes, counting a last byte that is not whole as one.
bit_array.byte_size(<<1, 2:u4>>) // 2 byte_size = (bits : BitArray) -> Int { { core.bit_size(bits) + 7 } / 8 } append
The bits of to, then the bits of suffix.
append = (to : BitArray, suffix : BitArray) -> BitArray { concat
The bit arrays one after the other, made with one copy. Their sizes may be any number of bits.
bit_array.concat([<<1>>, <<2, 3>>]) // <<1, 2, 3>> concat = (bit_arrays : List(BitArray)) -> BitArray { slice
take bytes from byte position, counting from 0. A negative take
takes the bytes before position. Error(Nil) when they are not all in
the bit array.
bit_array.slice(<<1, 2, 3, 4>>, 1, 2) // Ok(<<2, 3>>)
bit_array.slice(<<1, 2, 3, 4>>, 3, -2) // Ok(<<2, 3>>) slice = (from : BitArray, at : Int, take : Int) -> Result(BitArray, Nil) { starts_with
True when the bit array starts with the bits of prefix.
starts_with = (bits : BitArray, prefix : BitArray) -> Bool { pad_to_bytes
The bit array with zero bits after it, up to a whole number of bytes.
pad_to_bytes = (bits : BitArray) -> BitArray { compare
Compares two bit arrays byte by byte, from the start. When one is the
start of the other, the shorter one is Less. When the whole bytes are
the same and a part of a byte is left in both, these parts are compared
as unsigned numbers, and then by their number of bits.
bit_array.compare(<<1, 2>>, <<1, 3>>) // Less
bit_array.compare(<<4:u8>>, <<4:u5>>) // Greater compare = (a : BitArray, with : BitArray) -> Order { inspect
The text form of the bit array (§11.8).
bit_array.inspect(<<1, 2:u2>>) // "<<1, 2:u2>>" inspect = (bits : BitArray) -> String { core.inspect(bits) } base16_encode
The bytes in base 16, with two upper-case hex digits for each byte.
bit_array.base16_encode(<<255, 1>>) // "FF01" base16_encode = (bits : BitArray) -> String { base16_decode
Reads base 16 text: two hex digits, upper or lower case, for each byte.
Any other text gives Error(Nil).
bit_array.base16_decode("ff01") // Ok(<<255, 1>>) base16_decode = (input : String) -> Result(BitArray, Nil) { base64_encode
The bytes in base 64 (RFC 4648), with + and /. With padding, =
is put at the end to make the length a multiple of 4.
bit_array.base64_encode(<<"hi":utf8>>, True) // "aGk=" base64_encode = (input : BitArray, padding : Bool) -> String { base64_decode
Reads base 64 text (RFC 4648), with + and /. The = padding at the
end may be there or not. Any other text gives Error(Nil).
bit_array.base64_decode("aGk=") // Ok(<<104, 105>>)
bit_array.base64_decode("aGk") // Ok(<<104, 105>>) base64_decode = (encoded : String) -> Result(BitArray, Nil) { base64_url_encode
Like base64_encode, with - and _ in the place of + and /, so
that the text may be used in a URL.
base64_url_encode = (input : BitArray, padding : Bool) -> String { base64_url_decode
Like base64_decode, with - and _ in the place of + and /.
base64_url_decode = (encoded : String) -> Result(BitArray, Nil) {