Skip to content

std.str - ZZ

String manipulation with byte-offset semantics matching Rust str::find exactly. Byte-identical results on VM and AOT backends. All functions take the string first, so they compose cleanly with |> pipelines.

import std.str

std.str is the canonical home for string operations. Bare ord, chr, and len are globals available without import; the std.str.* qualified forms enable method-call syntax ("hi".trim()) and selective imports (import std.str(trim)). Every function is pure (no side effects) and takes the string as its first parameter.

Strings are immutable UTF-8. Character-oriented operations (length, slicing via []) count Unicode scalars; byte-oriented operations (find, rfind, trim_span, bytes) operate on UTF-8 byte offsets. This distinction is intentional — mixing them without conversion is a common bug source.

Layer What it operates on Unit
Char layer length, indexing s[i], slicing s[i:j] Unicode scalar values
Byte layer find, rfind, find_in, rfind_in, count_in, trim_span, bytes, ord, chr UTF-8 bytes

Byte offsets are O(1) per call because ZZ strings store UTF-8 and cache byte length. Streaming over a string is O(n) total.

import std.str
s := " Hello World "
str.trim(s) // "Hello World"
str.to_upper(s) // " HELLO WORLD "
str.to_lower(s) // " hello world "
str.contains(s, "World") // true
str.split("a,b,c", ",") // ["a", "b", "c"]
str.replace("foo bar", "bar", "baz") // "foo baz"
str.starts_with("hello", "he") // true
str.ends_with("hello", "lo") // true
str.find("hello world", "o", 0) // 4
str.rfind("hello world", "o", 10) // 7
str.bytes("AB") // [65, 66]
ord("A") // 65
chr(233) // "é"

Every std.str function works as a method on a string value:

import std.str
" hello ".trim() // "hello"
"hello".to_upper() // "HELLO"
"hello world".to_lower() // "hello world"
"hello world".contains("world") // true
"hello world".split(" ") // ["hello", "world"]
"hello world".replace("world", "zz") // "hello zz"
"hello".starts_with("he") // true
"hello".ends_with("lo") // true
"abc123".find("123", 0) // 3

Method calls resolve through canonical_std_name fallback — s.trim() is sugar for std.str.trim(s).

  • Offsets are bytes; length is chars. str.byte_len and str.length can differ for non-ASCII. Always use byte offsets with find/rfind/*_at/*_in.
  • Empty sub semantics: find/rfind return the clamped from; find_in returns clamped start; rfind_in returns clamped end; count_in returns 0; starts_with_at/ends_with_at never match empty.
  • Negative inputs to find_in/rfind_in/trim_span clamp to 0.
  • ord requires exactly one scalar. ord("") and ord("ab") are runtime errors. chr rejects surrogates, negatives, and values > 0x10FFFF.
  • VM/AOT parity: Byte offsets and empty-substring semantics are byte-identical on both backends (matching Rust str::find).
  • find/rfind/contains are O(1) setup + O(n) worst-case scan (Rust str::find).
  • str.builder + str.finish is O(n); s = s + part in a loop is O(n²).
  • str.bytes makes one copy; prefer bytes.to_ints if you already have a bytes buffer.
  • All functions are readonly/pure — safe to call concurrently from multiple tasks.
  • Prefer raw backticks for patterns: `^[0-9a-f]{8}$` needs no {{ }} doubling. In "..." strings, quantifier braces must double ("{{8}}") like Rust format!.
  • Compile once, reuse: handles are cheap to query, compilation is the cost.
  • Handles can go stale across weird lifecycles — calls then error `...`: regexp handle is no longer valid instead of hanging.
  • For byte-offset search without regex, std.str find/rfind/*_in is faster.
  • fs.scan_counts(path, markers, bstart, bend, nested, whole) — fused read + NUL-sniff + str.classify → [lines, code, comments, blanks, binary].
  • fs.is_generated(path, markers) — minified/generated-content heuristic.

36 entries. Signatures come from the compiler’s own FuncSig table, so they match the runtime exactly; descriptions come from the module guide.

Function Signature Description
std.str.builder builder() -> [str] —
std.str.builder_len builder_len(b: [str]) -> int —
std.str.byte_len byte_len(s: str) -> int Number of UTF-8 bytes
std.str.bytes bytes(s: str) -> [int] UTF-8 bytes as plain ints (one copy)
std.str.chr chr(cp: int) -> str One-char string for scalar (validates range)
std.str.classify classify(text: str, markers: [str], bstart: str, bend: str, nested: bool, whole: bool) -> [int] Comment-aware line counts [lines, code, comments, blanks]
std.str.contains contains(s: str, sub: str) -> bool Substring test
std.str.count count(s: str, sub: str) -> int Non-overlapping occurrences; empty sub counts len+1
std.str.count_in count_in(s: str, sub: str, start: int, end: int) -> int Non-overlapping matches in [start, end)
std.str.ends_with ends_with(s: str, suffix: str) -> bool Suffix test
std.str.ends_with_at ends_with_at(s: str, sub: str, pos: int) -> bool Does sub match ending at byte pos?
std.str.find find(s: str, sub: str, from: int) -> int First match at/after byte from; -1 on miss
std.str.find_in find_in(s: str, sub: str, start: int, end: int) -> int First match in [start, end); -1 on miss
std.str.finish finish(b: [str]) -> str —
std.str.is_empty is_empty(s: str) -> bool True when zero scalars
std.str.join join(items: [str], sep: str) -> str —
std.str.join_parts join_parts(parts: [str], sep: str) -> str —
std.str.length length(s: str) -> int Number of Unicode scalars (char-oriented)
std.str.ord ord(ch: str) -> int Unicode scalar of a single-char string
std.str.pad_left pad_left(s: str, len: int, pad: str) -> str Left-pad to len scalars
std.str.pad_right pad_right(s: str, len: int, pad: str) -> str Right-pad to len scalars
std.str.push_part push_part(b: [str], s: str) -> [str] —
std.str.repeat repeat(s: str, n: int) -> str Repeat s n times
std.str.replace replace(s: str, old: str, new: str) -> str Replace all non-overlapping occurrences
std.str.reverse reverse(s: str) -> str Reverse scalar order
std.str.rfind rfind(s: str, sub: str, from: int) -> int Last match at/before byte from; -1 on miss
std.str.rfind_in rfind_in(s: str, sub: str, start: int, end: int) -> int Last match in [start, end); -1 on miss
std.str.split split(s: str, sep: str) -> [str] Split on separator (no regex)
std.str.starts_with starts_with(s: str, prefix: str) -> bool Prefix test
std.str.starts_with_at starts_with_at(s: str, sub: str, pos: int) -> bool Does sub match starting at byte pos?
std.str.to_lower to_lower(s: str) -> str Unicode lowercase
std.str.to_upper to_upper(s: str) -> str Unicode uppercase
std.str.trim trim(s: str) -> str Strip leading/trailing Unicode whitespace
std.str.trim_end trim_end(s: str) -> str —
std.str.trim_span trim_span(s: str, start: int, end: int) -> [int] Trimmed [lo, hi] byte offsets within [start,end)
std.str.trim_start trim_start(s: str) -> str —
  • std.regexp — Compiled regular expressions: matching, capture groups and replacement.

Full index: API reference · Standard library