gleam/bit_array

BitArrays are a sequence of binary data of any length.

Functions

pub fn append(
  to first: BitArray,
  suffix second: BitArray,
) -> BitArray

Creates a new bit array by joining two bit arrays.

Examples

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

Compare two bit arrays as sequences of bytes.

Examples

compare(<<1>>, <<2>>)
// -> Lt

compare(<<"AB":utf8>>, <<"AA":utf8>>)
// -> Gt

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

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.

Examples

inspect(<<0, 20, 0x20, 255>>)
// -> "<<0, 20, 32, 255>>"

inspect(<<100, 5:3>>)
// -> "<<100, 5:size(3)>>"
pub fn is_utf8(bits: BitArray) -> Bool

Tests to see whether a bit array is valid UTF-8.

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

starts_with(<<1, 2, 3, 4>>, <<1, 2>>)
// -> True
pub fn to_string(bits: BitArray) -> Result(String, Nil)

Converts a bit array to a string.

Returns an error if the bit array is invalid UTF-8 data.

Search Document