gleam/bit_string

Working with raw bit string data. The BitString type should be used instead of a String type when not utf8 encoded.

Functions

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

Creates a new bit string by joining two binaries.

Examples

> append(to: from_string("butter"), suffix: from_string("fly"))
from_string("butterfly")
pub fn byte_size(x: BitString) -> Int

Returns an integer which is the number of bytes in the bit string.

pub fn concat(bit_strings: List(BitString)) -> BitString

Creates a new bit string by joining multiple binaries.

Examples

> concat([from_string("butter"), from_string("fly")])
from_string("butterfly")
pub fn from_string(x: String) -> BitString

Converts a UTF-8 String type into a raw BitString type.

pub fn is_utf8(bits: BitString) -> Bool

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

pub fn slice(from string: BitString, at position: Int, take length: Int) -> Result(
  BitString,
  Nil,
)

Extracts a sub-section of a bit string.

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

This function runs in constant time.

pub fn to_string(bits: BitString) -> Result(String, Nil)

Converts a bit string to a string.

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