std.bytes

A growable byte buffer and string builder.

Importuse std.bytes

Types

struct Bytes#

Growable byte buffer / string builder from std.bytes.

An opaque handle: only std.bytes can produce one, and its contents are not reachable from moon source.

use std.bytes
use std.bytes.Bytes

fn main() {
  let out: Bytes = bytes.with_capacity(16)
  out.append_string("moon ")
  out.append_int(42)
  out.push(33)
  println(out.to_string())
  println(out.len())
  println(out.slice(0, 4).to_array())
  out.set(0, 109)
  out.clear()
}
_handle: NativeHandle

The native buffer.

Functions

fn new() -> Bytes#

An empty growable byte buffer (also the string builder).

fn with_capacity(int) -> Bytes#

An empty buffer with at least n bytes reserved.

fn of_string(string) -> Bytes#

A read-only buffer borrowing the string's bytes. O(1), no copy; the first mutation detaches.

fn of_array([int]) -> Bytes#

A buffer from an array of byte values (each 0..=255).

fn push(Bytes, int)#

Appends one byte (0..=255). Amortized O(1).

fn append(Bytes, Bytes)#

Appends another buffer's contents.

fn append_string(Bytes, string)#

Appends a string's bytes. Amortized O(1) per byte.

fn append_json_escaped(Bytes, string)#

Appends a string with JSON's escapes applied (quote, backslash, and every control byte; \u00xx for the ones without a short form), copying unescaped runs in bulk. One pass, no intermediate string, and no surrounding quotes — the caller writes those.

fn append_float(Bytes, float) -> int#

Appends a float in the same shortest round-trip spelling to_string gives it, with no intermediate string. Returns 0 when what it wrote has no . or exponent (an integral value), 1 when it has, and 2 for a non-finite value — which has no such spelling, so nothing is written and the caller decides what its format says instead.

fn append_int(Bytes, int)#

Appends an integer's decimal spelling to the builder — no intermediate string, and no digit loop in the caller.

fn scan_digits(string, int) -> int#

Index of the first byte at or after from that is not an ASCII digit (the string's length when it runs out). O(n) over the digits, allocates nothing.

fn scan_until_byte(string, int, int) -> int#

Index of the first occurrence of byte at or after from, or the string's length when there is none. Allocates nothing.

fn len(Bytes) -> int#
fn get(Bytes, int) -> int#

The byte at index i; errors when out of bounds.

fn set(Bytes, int, int)#

Stores a byte (0..=255) at index i; errors when out of bounds.

fn slice(Bytes, int, int) -> Bytes#

A shared view of the range [lo, hi); O(1), no copy. Mutating either side detaches first, so contents never alias observably.

fn to_string(Bytes) -> string#

The contents as a string, byte-faithful (the builder's finish). O(1): the backing is frozen and borrowed, not copied.

fn str_len(string) -> int#

Byte length of a string view. O(1), allocates nothing.

fn str_byte(string, int) -> int#

The byte at index i of a string view; errors when out of bounds. O(1), allocates nothing.

fn str_slice(string, int, int) -> string#

The byte range [lo, hi) as a string sharing the source backing. O(1), allocates nothing; errors when out of range.

fn str_range(Bytes, int, int) -> string#

The buffer's byte range [lo, hi) as a string sharing its backing — the ranged form of to_string. O(1), allocates nothing.

fn to_array(Bytes) -> [int]#

The contents as an array of byte values.

fn clear(Bytes)#

Resets length to zero, keeping capacity.