Skip to content

Std.Bytes

opaque type Bytes

An immutable sequence of bytes.

fn empty() -> Bytes

The sequence of no bytes.

fn from_list(xs: List[Int]) -> Option[Bytes]

The bytes in the order of xs. None if an element is outside 0 to 255.

fn to_list(b: Bytes) -> List[Int]

The bytes in order.

fn size(b: Bytes) -> Int

The number of bytes.

fn is_empty(b: Bytes) -> Bool

Whether there are no bytes.

fn get(b: Bytes, index: Int) -> Option[Int]

The byte at index (0-based). None if out of range or negative.

fn slice(b: Bytes, from: Int, to: Int) -> Bytes

The bytes from from inclusive to to exclusive. Both are clamped to 0 to size(b); empty if from is not less than to.

fn append(a: Bytes, b: Bytes) -> Bytes

The bytes of a, then those of b.

fn concat(parts: List[Bytes]) -> Bytes

The bytes of all of them in order.

fn encode_utf8(s: String) -> Bytes

The UTF-8 encoding of s. No BOM is added.

fn decode_utf8(b: Bytes) -> Option[String]

The string that b encodes in UTF-8. None if b is not valid UTF-8. A leading BOM is kept as U+FEFF.

fn to_hex(b: Bytes) -> String

Two lowercase hexadecimal digits per byte, with no separator ("89504e47").

fn from_hex(s: String) -> Option[Bytes]

The inverse of to_hex. Accepts uppercase and lowercase digits. None for an odd number of digits or any other character, including whitespace.

fn hex(s: String) -> Bytes

The bytes that from_hex reads from s. A defect if from_hex returns None. For constants written in the program (const PNG_SIGNATURE = Bytes.hex("89504e47")).

fn to_base64(b: Bytes) -> String

Base64 in the standard alphabet (A-Z, a-z, 0-9, +, /) of RFC 4648 section 4, padded with = to a multiple of 4 characters.

fn from_base64(s: String) -> Option[Bytes]

The inverse of to_base64. The padding is required. None for a length that is not a multiple of 4, misplaced padding, or any other character, including whitespace. Only what to_base64 writes is read, so the bits of the last character that no byte uses must be zero (RFC 4648 section 3.5).