gleam/bit_string

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

Types

BitString

pub type BitString =
  BitString

Functions

append

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

Create a new bit string by joining two binaries.

Examples

> append(to: from_string("butter"), suffix: from_string("fly"))
from_string("butterfly")

byte_size

pub external fn byte_size(BitString) -> Int

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

from_string

pub external fn from_string(String) -> BitString

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

int_from_u32

pub external fn int_from_u32(BitString) -> Result(Int, Nil)

Convert unsigned 32 bits to an integer.

Returns an error if the bit string is not 32 bits in length.

int_to_u32

pub external fn int_to_u32(Int) -> Result(BitString, Nil)

Convert an integer to unsigned 32 bits.

Returns an error if integer is less than zero or equal to or larger than 2^32.

is_utf8

pub fn is_utf8(bits: BitString) -> Bool

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

part

pub external fn part(
  string: BitString,
  position: Int,
  length: Int,
) -> Result(BitString, Nil)

Extracts part of a bit string.

BitString part 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.

to_string

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

Convert a bit string to a string.

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