Skip to content
Menu
Contents

Reading the tags

pure

Same arguments, same result; touches nothing outside.

pure if arg2 is

Pure when that argument is a pure function; otherwise as impure as it is.

impure

Reads or changes something outside the program.

Bytes

2 types1 value30 functions

A sequence of arbitrary bytes, @Bytes, with no promise that they are text. A String comes out only through toUtf8, which checks the bytes, or toUtf8Lossy, which repairs them. Offsets count bytes from 0. Nothing here traps: an offset out of range is clamped, answered with a default, or an Err.

Bytes.toHex(Bytes.fromList([222, 173, 190, 239]))
Returns
"deadbeef"

At a glance

  • @Bytes

    A sequence of bytes.

  • Result

    Shorthand for [Ok(a) | Err(e)], the variant this module's fallible functions return.

  • empty

    No bytes.

  • length

    The number of bytes.

  • byteAtOr

    The byte at offset, 0 โ€ฆ 255, or default when offset is negative or not below the length.

  • slice

    The bytes from offset start up to, not including, offset end.

  • concat

    The bytes of a, then those of b.

  • concatAll

    Every part's bytes, in order, copied once.

  • eq

    Whether both hold the same bytes.

  • indexOf

    The first offset at or after start where the bytes of needle occur in haystack.

  • fromList

    One byte per element: each Int's low eight bits, so 256 is 0 and -1 is 255.

  • toList

    Each byte as an Int, 0 โ€ฆ 255.

  • fromString

    The bytes of a string, as they are.

  • toUtf8

    The bytes as a String when they are strict UTF-8: no stray continuation byte, overlong form, surrogate, code point past U+10FFFF, or sequence cut short.

  • toUtf8Lossy

    The bytes as a String, with each maximal ill-formed subpart replaced by U+FFFD: a sequence that starts well but is cut short is one replacement, and every other byte that starts nothing well-formed is one each.

  • toHex

    Two lowercase hex digits per byte.

  • fromHex

    The bytes two hex digits stand for, in either case.

  • toBase64

    Base64 with the standard alphabet, padded with = (RFC 4648, section 4).

  • fromBase64

    The bytes standard, padded base64 stands for.

  • toBase64Url

    Base64 with the URL-safe alphabet, - and _ for + and /, and no padding (RFC 4648, section 5).

  • fromBase64Url

    fromBase64 for the URL-safe alphabet, where the padding may be left out.

  • u16Le

    The unsigned 16-bit number whose bytes start at offset, least significant first.

  • u16Be

    The unsigned 16-bit number whose bytes start at offset, most significant first.

  • u32Le

    The unsigned 32-bit number whose bytes start at offset, least significant first.

  • u32Be

    The unsigned 32-bit number whose bytes start at offset, most significant first.

  • u64Le

    The unsigned 64-bit number whose bytes start at offset, least significant first.

  • u64Be

    The unsigned 64-bit number whose bytes start at offset, most significant first.

  • fromU16Le

    The low 16 bits of an Int, least significant byte first.

  • fromU16Be

    The low 16 bits of an Int, most significant byte first.

  • fromU32Le

    The low 32 bits of an Int, least significant byte first.

  • fromU32Be

    The low 32 bits of an Int, most significant byte first.

  • fromU64Le

    The low 64 bits of an Int, least significant byte first.

  • fromU64Be

    The low 64 bits of an Int, most significant byte first.

Types

@Bytes

type @Bytes

A sequence of bytes. Opaque: build and read one with this module's functions.

Result

Result(a, e) = [Ok(a) | Err(e)]

Shorthand for [Ok(a) | Err(e)], the variant this module's fallible functions return.

Values

empty

@Bytespure

No bytes.

Functions

length

(@Bytes) -> Intpure

The number of bytes.

byteAtOr

(@Bytes, Int, Int) -> Intpure

The byte at offset, 0 โ€ฆ 255, or default when offset is negative or not below the length.

slice

(@Bytes, Int, Int) -> @Bytespure

The bytes from offset start up to, not including, offset end. Both are held to 0 โ€ฆ length(b) first, and an end at or before start is empty.

concat

(@Bytes, @Bytes) -> @Bytespure

The bytes of a, then those of b.

concatAll

(List(@Bytes)) -> @Bytespure

Every part's bytes, in order, copied once.

eq

(@Bytes, @Bytes) -> Boolpure

Whether both hold the same bytes.

indexOf

(@Bytes, @Bytes, Int) -> Result(Int, [NotFound | e])pure

The first offset at or after start where the bytes of needle occur in haystack. start is held to 0 โ€ฆ length(haystack) first, so an empty needle is found at the held start.

fromList

(List(Int)) -> @Bytespure

One byte per element: each Int's low eight bits, so 256 is 0 and -1 is 255.

toList

(@Bytes) -> List(Int)pure

Each byte as an Int, 0 โ€ฆ 255.

fromString

(String) -> @Bytespure

The bytes of a string, as they are. Costs nothing.

toUtf8

(@Bytes) -> Result(String, [InvalidUtf8({ pos: Int }) | e])pure

The bytes as a String when they are strict UTF-8: no stray continuation byte, overlong form, surrogate, code point past U+10FFFF, or sequence cut short. Otherwise InvalidUtf8, with the offset where the first ill-formed sequence starts.

toUtf8Lossy

(@Bytes) -> Stringpure

The bytes as a String, with each maximal ill-formed subpart replaced by U+FFFD: a sequence that starts well but is cut short is one replacement, and every other byte that starts nothing well-formed is one each.

toHex

(@Bytes) -> Stringpure

Two lowercase hex digits per byte.

fromHex

(String) -> Result(@Bytes, [InvalidHex({ pos: Int }) | e])pure

The bytes two hex digits stand for, in either case. A character that is not a hex digit is InvalidHex at its offset; with every character a digit, an odd count is InvalidHex at the length.

toBase64

(@Bytes) -> Stringpure

Base64 with the standard alphabet, padded with = (RFC 4648, section 4).

fromBase64

(String) -> Result(@Bytes, [InvalidBase64({ pos: Int }) | e])pure

The bytes standard, padded base64 stands for. Only the canonical encoding is accepted: a character outside the alphabet (whitespace included), missing or misplaced padding, or a last character with bits set past the last byte is InvalidBase64, at the offset of the first problem, or at the length when the input stops short.

toBase64Url

(@Bytes) -> Stringpure

Base64 with the URL-safe alphabet, - and _ for + and /, and no padding (RFC 4648, section 5).

fromBase64Url

(String) -> Result(@Bytes, [InvalidBase64({ pos: Int }) | e])pure

fromBase64 for the URL-safe alphabet, where the padding may be left out.

u16Le

(@Bytes, Int) -> Result(Int, [OutOfBounds | e])pure

The unsigned 16-bit number whose bytes start at offset, least significant first. OutOfBounds unless every one of its bytes lies inside.

u16Be

(@Bytes, Int) -> Result(Int, [OutOfBounds | e])pure

The unsigned 16-bit number whose bytes start at offset, most significant first. OutOfBounds unless every one of its bytes lies inside.

u32Le

(@Bytes, Int) -> Result(Int, [OutOfBounds | e])pure

The unsigned 32-bit number whose bytes start at offset, least significant first. OutOfBounds unless every one of its bytes lies inside.

u32Be

(@Bytes, Int) -> Result(Int, [OutOfBounds | e])pure

The unsigned 32-bit number whose bytes start at offset, most significant first. OutOfBounds unless every one of its bytes lies inside.

u64Le

(@Bytes, Int) -> Result(Int, [OutOfBounds | e])pure

The unsigned 64-bit number whose bytes start at offset, least significant first. OutOfBounds unless every one of its bytes lies inside. A number past the Int range reads as negative, the same bits as an Int.

u64Be

(@Bytes, Int) -> Result(Int, [OutOfBounds | e])pure

The unsigned 64-bit number whose bytes start at offset, most significant first. OutOfBounds unless every one of its bytes lies inside. A number past the Int range reads as negative, the same bits as an Int.

fromU16Le

(Int) -> @Bytespure

The low 16 bits of an Int, least significant byte first.

fromU16Be

(Int) -> @Bytespure

The low 16 bits of an Int, most significant byte first.

fromU32Le

(Int) -> @Bytespure

The low 32 bits of an Int, least significant byte first.

fromU32Be

(Int) -> @Bytespure

The low 32 bits of an Int, most significant byte first.

fromU64Le

(Int) -> @Bytespure

The low 64 bits of an Int, least significant byte first.

fromU64Be

(Int) -> @Bytespure

The low 64 bits of an Int, most significant byte first.

Reading the tags

pure

Same arguments, same result; touches nothing outside.

pure if arg2 is

Pure when that argument is a pure function; otherwise as impure as it is.

impure

Reads or changes something outside the program.