Bytes
2 types1 value30 functionsA 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.
Types
@Bytes
A sequence of bytes. Opaque: build and read one with this module's functions.
Result
Shorthand for [Ok(a) | Err(e)], the variant this module's fallible
functions return.
Values
empty
No bytes.
Functions
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. Both are
held to 0 โฆ length(b) first, and an end at or before start is empty.
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. start is held to 0 โฆ length(haystack) first, so an empty
needle is found at the held start.
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. Costs nothing.
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. Otherwise InvalidUtf8, with the offset where the first ill-formed
sequence starts.
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. 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
Base64 with the standard alphabet, padded with = (RFC 4648, section 4).
fromBase64
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
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. OutOfBounds unless every one of its bytes lies inside.
u16Be
The unsigned 16-bit number whose bytes start at offset, most
significant first. OutOfBounds unless every one of its bytes lies inside.
u32Le
The unsigned 32-bit number whose bytes start at offset, least
significant first. OutOfBounds unless every one of its bytes lies inside.
u32Be
The unsigned 32-bit number whose bytes start at offset, most
significant first. OutOfBounds unless every one of its bytes lies inside.
u64Le
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
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
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.