@std/bytes.ms#
Import each required symbol with import Name from "@std/bytes.ms".
Bytes Native type#
An immutable sequence of bytes, such as "hello".to_bytes(). Use decode_utf8() to
read text; invalid UTF-8 returns null. Indices and slice bounds count bytes from
zero.
type Bytes {
}
fn move Bytes.into_string() String? { ... }
fn Bytes.contains(value Bytes) Bool { ... }
fn Bytes.split(separator Bytes) List[Bytes] { ... }
fn Bytes.slice(start Int, end Int) Result[Bytes, Error] { ... }
fn Bytes.view(start Int, end Int) Bytes { ... }
fn Bytes.index_of(value Bytes) Int? { ... }
fn Bytes.index_of_byte(value UInt8, start Int = 0) Int? { ... }
fn Bytes.last_index_of(value Bytes) Int? { ... }
fn Bytes.starts_with(prefix Bytes) Bool { ... }
fn Bytes.ends_with(suffix Bytes) Bool { ... }
fn Bytes.reversed() Bytes { ... }
fn Bytes.with_value(index Int, value UInt8) Bytes { ... }
fn Bytes.is_empty() Bool { ... }
fn Bytes.values() List[UInt8] { ... }
fn Bytes.concat(other Bytes) Bytes { ... }
fn Bytes.length() Int { ... }
fn Bytes.decode_utf8() String? { ... }
fn Bytes.decode_utf8_range(start Int, end Int) String? { ... }Bytes.as_bytes#
The bytes themselves, copied.
fn as_bytes() BytesBytes.clone#
Copies byte storage without changing the receiver.
fn clone() BytesBytes.concat#
Returns concatenated bytes without changing either operand.
fn concat(other Bytes) Bytes { ... }Bytes.contains#
Tests whether value occurs as a byte subsequence.
fn contains(value Bytes) Bool { ... }Bytes.decode_utf8#
Decodes valid UTF-8 as text, or returns null when the bytes are invalid.
fn decode_utf8() String? { ... }Bytes.decode_utf8_range#
Decodes [start, end) as decode_utf8 does, without copying the range into a new
Bytes first. Bounds outside 0 <= start <= end <= length() raise a fault.
fn decode_utf8_range(start Int, end Int) String? { ... }Bytes.ends_with#
Tests whether the receiver ends with these bytes.
fn ends_with(suffix Bytes) Bool { ... }Bytes.float32_be#
Reads an IEEE 754 binary32 big-endian float from four bytes at at; missing
bytes return an Error of kind ERANGE.
fn float32_be(at Int) Result[Float32, Error]Bytes.float32_le#
Reads an IEEE 754 binary32 little-endian float from four bytes at at; missing
bytes return an Error of kind ERANGE.
fn float32_le(at Int) Result[Float32, Error]Bytes.float64_be#
Reads an IEEE 754 binary64 big-endian float from eight bytes at at; missing
bytes return an Error of kind ERANGE.
fn float64_be(at Int) Result[Float, Error]Bytes.float64_le#
Reads an IEEE 754 binary64 little-endian float from eight bytes at at; missing
bytes return an Error of kind ERANGE.
fn float64_le(at Int) Result[Float, Error]Bytes.get#
Returns an unsigned byte; negative indexes are relative to the end. An out-of-range index raises a fault.
fn get(index Int) UInt8Bytes.get_or#
Returns an unsigned byte, or default when the index is out of range; negative
indexes are relative to the end.
fn get_or(index Int, default UInt8) UInt8Bytes.index_of#
Returns the first matching byte offset or null; empty value matches at zero.
fn index_of(value Bytes) Int? { ... }Bytes.index_of_byte#
Returns the first offset at or after start holding the byte value, or null.
A start past length() gives null; a negative start counts as zero.
fn index_of_byte(value UInt8, start Int = 0) Int? { ... }Bytes.int16_be#
Reads a signed 16-bit big-endian integer from two bytes at at; missing bytes
return an Error of kind ERANGE.
fn int16_be(at Int) Result[Int16, Error]Bytes.int16_le#
Reads a signed 16-bit little-endian integer from two bytes at at; missing bytes
return an Error of kind ERANGE.
fn int16_le(at Int) Result[Int16, Error]Bytes.int32_be#
Reads a signed 32-bit big-endian integer from four bytes at at; missing bytes
return an Error of kind ERANGE.
fn int32_be(at Int) Result[Int32, Error]Bytes.int32_le#
Reads a signed 32-bit little-endian integer from four bytes at at; missing
bytes return an Error of kind ERANGE.
fn int32_le(at Int) Result[Int32, Error]Bytes.int64_be#
Reads a signed 64-bit big-endian integer from eight bytes at at; missing bytes
return an Error of kind ERANGE.
fn int64_be(at Int) Result[Int, Error]Bytes.int64_le#
Reads a signed 64-bit little-endian integer from eight bytes at at; missing
bytes return an Error of kind ERANGE.
fn int64_le(at Int) Result[Int, Error]Bytes.int8#
Reads one signed byte at at; a missing byte returns an Error of kind
ERANGE. Offsets are absolute, so a negative at is out of range.
fn int8(at Int) Result[Int8, Error]Bytes.into_string#
Decodes the bytes as UTF-8 text, or returns null when they are not valid. The bytes
move into the string without a copy, so the receiver is consumed; decode_utf8
borrows and copies instead.
move fn into_string() String? { ... }Bytes.is_empty#
Tests whether the receiver contains no bytes.
fn is_empty() Bool { ... }Bytes.last_index_of#
Returns the last matching byte offset or null; empty value matches at
length().
fn last_index_of(value Bytes) Int? { ... }Bytes.length#
Returns the number of bytes, not the number of Unicode characters.
fn length() Int { ... }Bytes.reversed#
Returns a reversed copy without changing the receiver.
fn reversed() Bytes { ... }Bytes.slice#
Selects [start, end) in bytes; negative bounds are relative to the end.
Bounds outside 0 <= start <= end <= length() return an Error of kind ERANGE.
The result is a new copy; view borrows instead.
fn slice(start Int, end Int) Result[Bytes, Error] { ... }Bytes.split#
Splits at every occurrence of separator and returns the parts as a new list.
Empty parts are kept, so empty data with a nonempty separator yields one empty Bytes.
An empty separator yields one Bytes per byte, and no parts for empty data.
fn split(separator Bytes) List[Bytes] { ... }Bytes.starts_with#
Tests whether the receiver begins with these bytes.
fn starts_with(prefix Bytes) Bool { ... }Bytes.uint16_be#
Reads an unsigned 16-bit big-endian integer from two bytes at at; missing bytes
return an Error of kind ERANGE.
fn uint16_be(at Int) Result[UInt16, Error]Bytes.uint16_le#
Reads an unsigned 16-bit little-endian integer from two bytes at at; missing
bytes return an Error of kind ERANGE.
fn uint16_le(at Int) Result[UInt16, Error]Bytes.uint32_be#
Reads an unsigned 32-bit big-endian integer from four bytes at at; missing
bytes return an Error of kind ERANGE.
fn uint32_be(at Int) Result[UInt32, Error]Bytes.uint32_le#
Reads an unsigned 32-bit little-endian integer from four bytes at at; missing
bytes return an Error of kind ERANGE.
fn uint32_le(at Int) Result[UInt32, Error]Bytes.uint64_be#
Reads an unsigned 64-bit big-endian integer from eight bytes at at, including
the full UInt64 range. Negative offsets or missing bytes return an Error of
kind ERANGE.
fn uint64_be(at Int) Result[UInt64, Error]Bytes.uint64_le#
Reads an unsigned 64-bit little-endian integer from eight bytes at at,
including the full UInt64 range. Negative offsets or missing bytes return an
Error of kind ERANGE.
fn uint64_le(at Int) Result[UInt64, Error]Bytes.uint8#
Reads one unsigned byte at at; a missing byte returns an Error of kind
ERANGE.
fn uint8(at Int) Result[UInt8, Error]Bytes.uvarint#
Reads an unsigned base-128 integer, least-significant group first, starting at byte offset at.
Reads at most ten bytes, and the tenth must be 0 or 1. Accepts nonminimal encodings and ignores trailing bytes.
An offset outside 0 <= at <= length() returns ERANGE; input that ends before the encoding does returns EBADMSG.
fn uvarint(at Int) Result[VarintRead[UInt64], Error]Bytes.values#
Returns a fresh mutable list of unsigned byte values; changing it leaves the Bytes unchanged.
fn values() List[UInt8] { ... }Bytes.varint#
Reads a signed 64-bit zigzag integer: the encoded values 0, 1, 2, 3 decode to 0,
-1, 1, -2. Offsets, the ten-byte bound, nonminimal encodings and errors are the
same as for uvarint.
fn varint(at Int) Result[VarintRead[Int], Error]Bytes.view#
Borrows [start, end) of the receiver without copying; negative bounds are relative to the end.
Bounds outside 0 <= start <= end <= length() raise a fault. Like xs[i], the result borrows
the receiver, which cannot change while the view is used; .clone() keeps the bytes.
fn view(start Int, end Int) Bytes { ... }Bytes.with_value#
Returns a copy with one byte replaced; negative indexes are relative to the end. An out-of-range index faults.
fn with_value(index Int, value UInt8) Bytes { ... }BytesBuilder Native type#
Builds a byte sequence efficiently before freezing it into an immutable value.
type BytesBuilder {
}
fn var BytesBuilder.append(value UInt8) Void { ... }
fn var BytesBuilder.append_bytes(values Bytes) Void { ... }
fn move BytesBuilder.build() Bytes { ... }
fn var BytesBuilder.drain() Bytes { ... }
fn var BytesBuilder.drain_string() String? { ... }
fn BytesBuilder.length() Int { ... }
fn var BytesBuilder.clear() Void { ... }
fn var BytesBuilder.reserve(extra Int) Void { ... }
fn BytesBuilder.view(start Int, end Int) Bytes { ... }
fn var BytesBuilder.set(index Int, value UInt8) Void { ... }BytesBuilder.append#
Appends one byte to the end of the builder.
var fn append(value UInt8) Void { ... }BytesBuilder.append_bytes#
Appends all supplied bytes in order; the input is unchanged.
var fn append_bytes(values Bytes) Void { ... }BytesBuilder.append_float32_be#
Appends an IEEE 754 binary32 big-endian float.
var fn append_float32_be(value Float32) VoidBytesBuilder.append_float32_le#
Appends an IEEE 754 binary32 little-endian float.
var fn append_float32_le(value Float32) VoidBytesBuilder.append_float64_be#
Appends an IEEE 754 binary64 big-endian float.
var fn append_float64_be(value Float) VoidBytesBuilder.append_float64_le#
Appends an IEEE 754 binary64 little-endian float.
var fn append_float64_le(value Float) VoidBytesBuilder.append_int16_be#
Appends a signed 16-bit big-endian integer.
var fn append_int16_be(value Int16) VoidBytesBuilder.append_int16_le#
Appends a signed 16-bit little-endian integer.
var fn append_int16_le(value Int16) VoidBytesBuilder.append_int32_be#
Appends a signed 32-bit big-endian integer.
var fn append_int32_be(value Int32) VoidBytesBuilder.append_int32_le#
Appends a signed 32-bit little-endian integer.
var fn append_int32_le(value Int32) VoidBytesBuilder.append_int64_be#
Appends a signed 64-bit big-endian integer.
var fn append_int64_be(value Int) VoidBytesBuilder.append_int64_le#
Appends a signed 64-bit little-endian integer.
var fn append_int64_le(value Int) VoidBytesBuilder.append_int8#
Appends one signed byte.
var fn append_int8(value Int8) VoidBytesBuilder.append_uint16_be#
Appends an unsigned 16-bit big-endian integer.
var fn append_uint16_be(value UInt16) VoidBytesBuilder.append_uint16_le#
Appends an unsigned 16-bit little-endian integer.
var fn append_uint16_le(value UInt16) VoidBytesBuilder.append_uint32_be#
Appends an unsigned 32-bit big-endian integer.
var fn append_uint32_be(value UInt32) VoidBytesBuilder.append_uint32_le#
Appends an unsigned 32-bit little-endian integer.
var fn append_uint32_le(value UInt32) VoidBytesBuilder.append_uint64_be#
Appends an unsigned 64-bit big-endian integer, including the full UInt64 range.
var fn append_uint64_be(value UInt64) VoidBytesBuilder.append_uint64_le#
Appends an unsigned 64-bit little-endian integer, including the full UInt64 range.
var fn append_uint64_le(value UInt64) VoidBytesBuilder.append_uvarint#
Appends the shortest unsigned base-128 encoding, from one through ten bytes.
var fn append_uvarint(value UInt64) VoidBytesBuilder.append_varint#
Appends the shortest signed 64-bit zigzag encoding; even the most negative Int fits in ten bytes.
var fn append_varint(value Int) VoidBytesBuilder.build#
Consumes the builder and returns its bytes; the builder cannot be used afterwards.
move fn build() Bytes { ... }BytesBuilder.clear#
Removes every appended byte and keeps the allocated capacity for reuse.
var fn clear() Void { ... }BytesBuilder.drain#
Returns the bytes appended so far and leaves the builder empty, for a builder held in a field.
var fn drain() Bytes { ... }BytesBuilder.drain_string#
Returns the bytes appended so far as text without copying them, or null when they
are not valid UTF-8. The builder is empty afterwards either way, and keeps no buffer.
var fn drain_string() String? { ... }BytesBuilder.extend#
Appends every byte of values in order, like List.extend.
var fn extend(values List[UInt8]) VoidBytesBuilder.get#
Returns the appended byte at index; negative indexes are relative to the end. An
out-of-range index faults.
fn get(index Int) UInt8BytesBuilder.length#
Returns the number of bytes appended so far.
fn length() Int { ... }BytesBuilder.reserve#
Makes room for extra more bytes, so appends up to that count do not reallocate. The
bytes and length are unchanged; extra of zero or below does nothing.
var fn reserve(extra Int) Void { ... }BytesBuilder.set#
Overwrites the appended byte at index in place, for patching a length prefix or
header after the content is written. Negative indexes are relative to the end; an
out-of-range index faults.
var fn set(index Int, value UInt8) Void { ... }BytesBuilder.view#
Borrows [start, end) of the appended bytes without copying; negative bounds are relative to the end. Bounds outside 0 <= start <= end <= length() fault. The builder cannot change while the view is used.
fn view(start Int, end Int) Bytes { ... }VarintRead Type#
A decoded variable-length integer and the absolute offset of the next byte.
Example: first := data.uvarint(0)?; second := data.varint(first.next_offset)?.
type VarintRead[T] {
value T
next_offset Int
}VarintRead.next_offset#
The first byte after the encoding, relative to the receiver, including for a slice.
next_offset IntVarintRead.value#
The integer decoded as unsigned base-128 or signed zigzag by the read method.
value Tjoin Function#
Joins the parts end to end into one new Bytes; an empty list gives empty bytes.
It allocates once and copies each part once, unlike repeated concat.
fn join(parts List[Bytes]) Bytes { ... }