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.strPurpose
Section titled “Purpose”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.
Conceptual model
Section titled “Conceptual model”| 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.
Quickstart
Section titled “Quickstart”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") // truestr.split("a,b,c", ",") // ["a", "b", "c"]str.replace("foo bar", "bar", "baz") // "foo baz"str.starts_with("hello", "he") // truestr.ends_with("hello", "lo") // truestr.find("hello world", "o", 0) // 4str.rfind("hello world", "o", 10) // 7str.bytes("AB") // [65, 66]ord("A") // 65chr(233) // "é"Method syntax
Section titled “Method syntax”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) // 3Method calls resolve through canonical_std_name fallback — s.trim() is sugar for std.str.trim(s).
Edge cases & gotchas
Section titled “Edge cases & gotchas”- Offsets are bytes;
lengthis chars.str.byte_lenandstr.lengthcan differ for non-ASCII. Always use byte offsets withfind/rfind/*_at/*_in. - Empty
subsemantics:find/rfindreturn the clampedfrom;find_inreturns clampedstart;rfind_inreturns clampedend;count_inreturns0;starts_with_at/ends_with_atnever match empty. - Negative inputs to
find_in/rfind_in/trim_spanclamp to0. ordrequires exactly one scalar.ord("")andord("ab")are runtime errors.chrrejects surrogates, negatives, and values >0x10FFFF.- VM/AOT parity: Byte offsets and empty-substring semantics are byte-identical on both backends (matching Rust
str::find).
Performance notes
Section titled “Performance notes”find/rfind/containsare O(1) setup + O(n) worst-case scan (Ruststr::find).str.builder+str.finishis O(n);s = s + partin a loop is O(n²).str.bytesmakes one copy; preferbytes.to_intsif you already have abytesbuffer.- 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 Rustformat!. - 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 validinstead of hanging. - For byte-offset search without regex,
std.strfind/rfind/*_inis faster.
Power helpers
Section titled “Power helpers”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.
API reference
Section titled “API reference”36 entries. Signatures come from the compiler’s own FuncSig table, so they match the runtime exactly; descriptions come from the module guide.
See also
Section titled “See also”std.regexp— Compiled regular expressions: matching, capture groups and replacement.
Full index: API reference · Standard library