gleam/bit_array
BitArrays are a sequence of binary data of any length.
Values
pub fn append(
to first: BitArray,
suffix second: BitArray,
) -> BitArray
Creates a new bit array by joining two bit arrays.
Examples
assert append(to: from_string("butter"), suffix: from_string("fly"))
== from_string("butterfly")
pub fn base16_decode(input: String) -> Result(BitArray, Nil)
Decodes a base 16 encoded string into a BitArray.
pub fn base16_encode(input: BitArray) -> String
Encodes a BitArray into a base 16 encoded string.
If the bit array does not contain a whole number of bytes then it is padded with zero bits prior to being encoded.
pub fn base64_decode(encoded: String) -> Result(BitArray, Nil)
Decodes a base 64 encoded string into a BitArray.
pub fn base64_encode(input: BitArray, padding: Bool) -> String
Encodes a BitArray into a base 64 encoded string.
If the bit array does not contain a whole number of bytes then it is padded with zero bits prior to being encoded.
pub fn base64_url_decode(
encoded: String,
) -> Result(BitArray, Nil)
Decodes a base 64 encoded string with URL and filename safe alphabet into a
BitArray.
pub fn base64_url_encode(
input: BitArray,
padding: Bool,
) -> String
Encodes a BitArray into a base 64 encoded string with URL and filename
safe alphabet.
If the bit array does not contain a whole number of bytes then it is padded with zero bits prior to being encoded.
pub fn bit_size(x: BitArray) -> Int
Returns an integer which is the number of bits in the bit array.
pub fn byte_size(x: BitArray) -> Int
Returns an integer which is the number of bytes in the bit array.
pub fn compare(a: BitArray, with b: BitArray) -> order.Order
Compare two bit arrays as sequences of bytes.
Examples
assert compare(<<1>>, <<2>>) == Lt
assert compare(<<"AB":utf8>>, <<"AA":utf8>>) == Gt
assert compare(<<1, 2:size(2)>>, with: <<1, 2:size(2)>>) == Eq
pub fn concat(bit_arrays: List(BitArray)) -> BitArray
Creates a new bit array by joining multiple binaries.
Examples
assert concat([from_string("butter"), from_string("fly")])
== from_string("butterfly")
pub fn from_string(x: String) -> BitArray
Converts a UTF-8 String type into a BitArray.
pub fn inspect(input: BitArray) -> String
Converts a bit array to a string containing the decimal value of each byte.
Use this over string.inspect when you have a bit array you want printed
in the array syntax even if it is valid UTF-8.
Examples
assert inspect(<<0, 20, 0x20, 255>>) == "<<0, 20, 32, 255>>"
assert inspect(<<100, 5:3>>) == "<<100, 5:size(3)>>"
pub fn pad_to_bytes(x: BitArray) -> BitArray
Pads a bit array with zeros so that it is a whole number of bytes.
pub fn slice(
from string: BitArray,
at position: Int,
take length: Int,
) -> Result(BitArray, Nil)
Extracts a sub-section of a bit array.
The slice will start at given position and continue up to specified length. A negative length can be used to extract bytes at the end of a bit array.
This function runs in constant time.
pub fn starts_with(bits: BitArray, prefix: BitArray) -> Bool
Checks whether the first BitArray starts with the second one.
Examples
assert starts_with(<<1, 2, 3, 4>>, <<1, 2>>)