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)
pub fn base16_encode(input: BitArray) -> String
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.

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.

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

Only supported on Erlang target for now.

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 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 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