Std.Bytes
opaque type BytesAn immutable sequence of bytes.
fn empty() -> BytesThe sequence of no bytes.
from_list
Section titled “from_list”fn from_list(xs: List[Int]) -> Option[Bytes]The bytes in the order of xs. None if an element is outside 0 to 255.
to_list
Section titled “to_list”fn to_list(b: Bytes) -> List[Int]The bytes in order.
fn size(b: Bytes) -> IntThe number of bytes.
is_empty
Section titled “is_empty”fn is_empty(b: Bytes) -> BoolWhether 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) -> BytesThe bytes from from inclusive to to exclusive. Both are clamped to 0 to size(b); empty
if from is not less than to.
append
Section titled “append”fn append(a: Bytes, b: Bytes) -> BytesThe bytes of a, then those of b.
concat
Section titled “concat”fn concat(parts: List[Bytes]) -> BytesThe bytes of all of them in order.
encode_utf8
Section titled “encode_utf8”fn encode_utf8(s: String) -> BytesThe UTF-8 encoding of s. No BOM is added.
decode_utf8
Section titled “decode_utf8”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.
to_hex
Section titled “to_hex”fn to_hex(b: Bytes) -> StringTwo lowercase hexadecimal digits per byte, with no separator ("89504e47").
from_hex
Section titled “from_hex”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) -> BytesThe 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")).
to_base64
Section titled “to_base64”fn to_base64(b: Bytes) -> StringBase64 in the standard alphabet (A-Z, a-z, 0-9, +, /) of RFC 4648 section 4,
padded with = to a multiple of 4 characters.
from_base64
Section titled “from_base64”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).