Module string/string

string/string

Immutable UTF-8 string type with comprehensive operations.

Types

String newtype
String

Immutable UTF-8 encoded string. All operations return new strings; mutable operations require a pointer to self. Empty strings use zero allocation (represented as Option.None internally).

Fields

NameTypeDescription
_bytesOption(ArrayList(u8))

Trait Implementations

impl(String, ...)
new : (String) fn() -> String

Create a new empty string (zero allocation)

Returns: String

with_capacity : (String) fn(capacity : usize) -> String

Create a new empty string with pre-allocated capacity for the given number of bytes. The string starts empty but can hold capacity bytes without reallocating.

Parameters

NameTypeNotes
capacityusize

Returns: String

from_bytes : (String) fn(bytes : ArrayList(u8)) -> String

Create a string from raw bytes (assumes valid UTF-8) No validation is performed - the caller must ensure bytes are valid UTF-8

Parameters

NameTypeNotesDescription
bytesArrayList(u8)

Returns a byte iterator over the string's raw UTF-8 bytes

Returns: String

from : (String) fn(slice : str) -> String

Create a string from a slice of bytes str The slice is a fat pointer containing data pointer and length Example: String.from(slice) where slice is str

Parameters

NameTypeNotes
slicestr

Returns: String

from_cstr : (String) fn(cstr : *(u8)) -> Result(String, StringError)

Create a string from a C null-terminated string *(u8) Copies bytes until it finds a null byte (0) Example: String.from_cstr(c_string_ptr)

Note: We manually search for the null terminator by iterating

Parameters

NameTypeNotes
cstr*(u8)

Returns: Result(String, StringError)

to_cstr : (String) fn(self : String) -> ArrayList(u8)

Convert the string to a null-terminated C string. Returns an ArrayList(u8) with the string bytes followed by a \0.

Parameters

NameTypeNotes
selfString

Returns: ArrayList(u8)

len : (String) fn(self : String) -> usize

Get the length of the string in Unicode characters (runes) For "你好世界", this returns 4

Parameters

NameTypeNotes
selfString

Returns: usize

is_empty : (String) fn(self : String) -> bool

Check if the string is empty

Parameters

NameTypeNotes
selfString

Returns: bool

as_bytes : (String) fn(self : String) -> ArrayList(u8)

Get a reference to the internal byte array Returns an empty ArrayList if the string is empty

Parameters

NameTypeNotes
selfString

Returns: ArrayList(u8)

as_str : (String) fn(self : String) -> str

Get a str view into the string's UTF-8 bytes Returns a fat pointer (pointer + length) without copying

Parameters

NameTypeNotes
selfString

Returns: str

_decode_rune_at : (String) fn(self : String, byte_index : usize) -> Option(rune)

Decode a UTF-8 encoded rune starting at the given byte index Internal helper method

Parameters

NameTypeNotes
selfString
byte_indexusize

Returns: Option(rune)

at : (String) fn(self : String, index : usize) -> Option(rune)

Get the rune at the specified character index (0-based) Returns None if index is out of bounds For "你好世界", at(0) returns '你', at(1) returns '好', etc.

Parameters

NameTypeNotes
selfString
indexusize

Returns: Option(rune)

concat : (String) fn(self : String, other : String) -> String

Concatenate two strings (like JavaScript +)

Parameters

NameTypeNotes
selfString
otherString

Returns: String

push_string : (String) fn(self : String, other : String) -> unit

Append another String to this string in-place (mutates self).

Parameters

NameTypeNotes
selfString
otherString

Returns: unit

push_str : (String) fn(self : String, s : str) -> unit

Append a str slice to this string in-place (mutates self).

Parameters

NameTypeNotes
selfString
sstr

Returns: unit

push_byte : (String) fn(self : String, b : u8) -> unit

Append a single byte to this string in-place (mutates self). The caller must ensure the byte maintains valid UTF-8.

Parameters

NameTypeNotes
selfString
bu8

Returns: unit

reserve : (String) fn(self : String, additional : usize) -> unit

Reserve capacity for at least additional more bytes. If the string is empty, creates a new buffer with the given capacity.

Parameters

NameTypeNotes
selfString
additionalusize

Returns: unit

clear : (String) fn(self : String) -> unit

Clear the string content but keep the allocated buffer for reuse.

Parameters

NameTypeNotes
selfString

Returns: unit

bytes_len : (String) fn(self : String) -> usize

Get the number of bytes in the string (not Unicode characters). Use len() for Unicode character count.

Parameters

NameTypeNotes
selfString

Returns: usize

substring : (String) fn(self : String, start : usize, end : usize) -> String

Extract a substring from start to end character indices (like JavaScript substring) Returns a new string containing characters from start (inclusive) to end (exclusive) If end is greater than length, it will substring to the end of the string

Parameters

NameTypeNotes
selfString
startusize
endusize

Returns: String

index_of : (String) fn(self : String, substr : String, (from_index : usize) ?= 0) -> Option(usize)

Find the first occurrence of a substring (like JavaScript indexOf) Returns the character index of the first match, or None if not found from_index specifies the character index to start searching from

Parameters

NameTypeNotes
selfString
substrString
from_indexusizedefault: [object Object]

Returns: Option(usize)

contains : (String) fn(self : String, substr : String, (from_index : usize) ?= 0) -> bool

Check if the string contains a substring

Parameters

NameTypeNotes
selfString
substrString
from_indexusizedefault: [object Object]

Returns: bool

split : (String) fn(self : String, separator : String) -> ArrayList(String)

Split the string by a separator into an array of strings (like JavaScript split) Returns an ArrayList of String

Parameters

NameTypeNotes
selfString
separatorString

Returns: ArrayList(String)

last_index_of : (String) fn(self : String, substr : String, (from_index : usize) ?= (usize.MAX)) -> Option(usize)

Find the last occurrence of a substring (like JavaScript lastIndexOf) Returns the character index of the last match, or None if not found from_index specifies the character index to start searching backwards from

Parameters

NameTypeNotes
selfString
substrString
from_indexusizedefault: [object Object]

Returns: Option(usize)

starts_with : (String) fn(self : String, prefix : String, (position : usize) ?= 0) -> bool

Check if the string starts with the specified substring (like JavaScript startsWith) position: The character index to start checking from (default 0)

Parameters

NameTypeNotes
selfString
prefixString
positionusizedefault: [object Object]

Returns: bool

ends_with : (String) fn(self : String, suffix : String, (end_position : usize) ?= (usize.MAX)) -> bool

Check if the string ends with the specified substring (like JavaScript endsWith) end_position: The character length to consider (default is string length)

Parameters

NameTypeNotes
selfString
suffixString
end_positionusizedefault: [object Object]

Returns: bool

replace : (String) fn(self : String, search_value : String, new_value : String) -> String

Replace the first occurrence of search_value with new_value (like JavaScript replace)

Parameters

NameTypeNotes
selfString
search_valueString
new_valueString

Returns: String

replace_all : (String) fn(self : String, search_value : String, new_value : String) -> String

Replace all occurrences of search_value with new_value (like JavaScript replaceAll)

Parameters

NameTypeNotes
selfString
search_valueString
new_valueString

Returns: String

to_uppercase : (String) fn(self : String) -> String

Convert ASCII lowercase letters to uppercase (like JavaScript toUpperCase) Note: Only handles ASCII a-z, not full Unicode case mapping

Parameters

NameTypeNotes
selfString

Returns: String

to_lowercase : (String) fn(self : String) -> String

Convert ASCII uppercase letters to lowercase (like JavaScript toLowerCase) Note: Only handles ASCII A-Z, not full Unicode case mapping

Parameters

NameTypeNotes
selfString

Returns: String

_is_whitespace_byte : (String) fn(byte : u8) -> bool

Helper to check if a byte is ASCII whitespace Space (0x20), Tab (0x09), Newline (0x0A), Carriage Return (0x0D), Form Feed (0x0C), Vertical Tab (0x0B)

Parameters

NameTypeNotes
byteu8

Returns: bool

trim : (String) fn(self : String) -> String

Remove whitespace from both ends of the string (like JavaScript trim)

Parameters

NameTypeNotes
selfString

Returns: String

trim_start : (String) fn(self : String) -> String

Remove whitespace from the start of the string (like JavaScript trimStart)

Parameters

NameTypeNotes
selfString

Returns: String

trim_end : (String) fn(self : String) -> String

Remove whitespace from the end of the string (like JavaScript trimEnd)

Parameters

NameTypeNotes
selfString

Returns: String

impl(String, Add(String)(...))
Output : String
impl(String, Eq(String)(...))
impl(String, Hash( (hash) : (fn(ref(self) : Self) -> u64)({ h := u64(14695981039346656037); match( self._bytes, .None => (), .Some(al) => { i := usize(0); len := al.len(); while(i < len, i = (i + usize(1)), { match( al.get(i), .Some(b) => { h = (h ^ u64(b)); h = (h * u64(1099511628211)); }, .None => () ); }); } ); h }) ))
impl(String, ...)
chars : (String) fn(self : String) -> StringChars

Returns a rune iterator over the string's Unicode characters

Parameters

NameTypeNotes
selfString

Returns: StringChars

bytes : (String) fn(self : String) -> StringBytes

Returns a byte iterator over the string's raw UTF-8 bytes

Parameters

NameTypeNotes
selfString

Returns: StringBytes

iter : (String) fn(self : String) -> _StringPosIter

Byte-position iterator for for(s, ref(b) => body). Yields usize byte indices; the for-macro pairs each with s.project(pos) (Indexable) so the body sees ref(u8) bound to each byte. Iteration is byte-level, not rune-level — for codepoint iteration use chars() explicitly.

Parameters

NameTypeNotes
selfString

Returns: _StringPosIter

into_iter : (String) fn(self : String) -> StringChars

Consume the string and return a rune iterator (default iteration)

Parameters

NameTypeNotes
selfString

Returns: StringChars

impl(String, ...)
lines : (String) fn(self : String) -> StringLines

Returns a line iterator over the string. Each call to next() yields the next line (without the trailing \n).

Example

s := `hello\nworld`;
iter := s.lines();
assert(iter.next() == .Some(`hello`), "first line");
assert(iter.next() == .Some(`world`), "second line");
assert(iter.next() == .None, "done");

Parameters

NameTypeNotes
selfString

Returns: StringLines

repeat : (String) fn(self : String, n : usize) -> String

Returns a new string containing n copies of self. Returns an empty string when n == 0 or self is empty.

Example

assert(`ab`.repeat(usize(3)) == `ababab`, "repeat");
assert(`x`.repeat(usize(0)) == ``, "repeat 0");

Parameters

NameTypeNotes
selfString
nusize

Returns: String

join : (String) fn(self : String, items : ArrayList(String)) -> String

Join an ArrayList(String) with this string as separator. Returns an empty string when items is empty.

Example

items := ArrayList(String).new();
items.push(`a`);
items.push(`b`);
items.push(`c`);
result := `, `.join(items);
assert(result == `a, b, c`, "join");

Parameters

NameTypeNotes
selfString
itemsArrayList(String)

Returns: String

impl(String, ...)
_is_digit_byte : (String) fn(byte : u8) -> bool

Helper: check if a byte is an ASCII digit '0'-'9'

Parameters

NameTypeNotes
byteu8

Returns: bool

parse_i32 : (String) fn(self : String) -> Option(i32)

Parse the string as a signed 32-bit integer. Returns .Some(value) on success, .None on failure (empty, invalid chars, overflow).

Parameters

NameTypeNotes
selfString

Returns: Option(i32)

parse_i64 : (String) fn(self : String) -> Option(i64)

Parse the string as a signed 64-bit integer. Returns .Some(value) on success, .None on failure. Note: does not detect i64 overflow during accumulation.

Parameters

NameTypeNotes
selfString

Returns: Option(i64)

parse_u32 : (String) fn(self : String) -> Option(u32)

Parse the string as an unsigned 32-bit integer. Returns .Some(value) on success, .None on failure (empty, invalid chars, overflow, negative sign).

Parameters

NameTypeNotes
selfString

Returns: Option(u32)

parse_u64 : (String) fn(self : String) -> Option(u64)

Parse the string as an unsigned 64-bit integer. Returns .Some(value) on success, .None on failure. Note: does not detect u64 overflow during accumulation.

Parameters

NameTypeNotes
selfString

Returns: Option(u64)

parse_bool : (String) fn(self : String) -> Option(bool)

Parse the string as a boolean. Returns .Some(true) for "true", .Some(false) for "false", .None for anything else.

Parameters

NameTypeNotes
selfString

Returns: Option(bool)

impl(String, Index(usize)(...))
Output : u8
index : (fn(ref(self) : Self, idx : usize) -> *(Self.Output))

Parameters

NameTypeNotes
idxusize

Returns: *(Self.Output)

impl(String, Indexable(usize)(...))
Element : u8
project : (fn(ref(self) : Self, pos : usize) -> ref(u8))

Parameters

NameTypeNotes
posusize

Returns: ref(u8)

impl(String, Clone(...))
clone : (fn(ref(self) : Self) -> Self)

Returns: Self

StringError

String operation error variants.

Variants

VariantFieldsDescription
InvalidUtf8

The input bytes are not valid UTF-8.

IndexOutOfBoundsindex: usize, length: usize

The index is out of bounds for the string's byte length.

StringChars struct
StringChars

Rune iterator for String — yields decoded Unicode runes. Used by chars() and into_iter().

Fields

NameTypeDescription
_stringString
_byte_indexusize

Trait Implementations

impl(forall(I : Type), where(I <: Iterator), I)
into_iter : fn(self : I) -> I

Consume the string and return a rune iterator (default iteration)

Parameters

NameTypeNotes
selfI

Returns: I

impl(forall(I : Type), where(I <: Iterator), I)
map : fn(forall(comptime(A) : Type, comptime(B) : Type, comptime(F) : Type), self : I, f : F) -> IterMap(I, B, F)

Parameters

NameTypeNotes
selfI
fF

Returns: IterMap(I, B, F)

filter : fn(forall(comptime(A) : Type, comptime(F) : Type), self : I, f : F) -> IterFilter(I, F)

Parameters

NameTypeNotes
selfI
fF

Returns: IterFilter(I, F)

take : fn(forall(comptime(A) : Type), self : I, n : usize) -> IterTake(I)

Parameters

NameTypeNotes
selfI
nusize

Returns: IterTake(I)

skip : fn(forall(comptime(A) : Type), self : I, n : usize) -> IterSkip(I)

Parameters

NameTypeNotes
selfI
nusize

Returns: IterSkip(I)

enumerate : fn(forall(comptime(A) : Type), self : I) -> IterEnumerate(I)

Parameters

NameTypeNotes
selfI

Returns: IterEnumerate(I)

zip : fn(forall(comptime(A) : Type, comptime(J) : Type, comptime(B) : Type), self : I, other : J) -> IterZip(I, J)

Parameters

NameTypeNotes
selfI
otherJ

Returns: IterZip(I, J)

fold : fn(forall(comptime(A) : Type, comptime(Acc) : Type, comptime(F) : Type), self : I, init : Acc, f : F) -> Acc

Parameters

NameTypeNotes
selfI
initAcc
fF

Returns: Acc

for_each : fn(forall(comptime(A) : Type, comptime(F) : Type), self : I, f : F) -> unit

Parameters

NameTypeNotes
selfI
fF

Returns: unit

count : fn(forall(comptime(A) : Type), self : I) -> usize

Parameters

NameTypeNotes
selfI

Returns: usize

any : fn(forall(comptime(A) : Type, comptime(F) : Type), self : I, pred : F) -> bool

Parameters

NameTypeNotes
selfI
predF

Returns: bool

all : fn(forall(comptime(A) : Type, comptime(F) : Type), self : I, pred : F) -> bool

Parameters

NameTypeNotes
selfI
predF

Returns: bool

impl(StringChars, Iterator(...))
Item : rune
next : (fn(ref(self) : Self) -> Option(rune))

Returns: Option(rune)

StringBytes struct
StringBytes

Byte iterator for String — yields raw UTF-8 bytes. Used by bytes().

Fields

NameTypeDescription
_stringString
_indexusize

Trait Implementations

impl(forall(I : Type), where(I <: Iterator), I)
into_iter : fn(self : I) -> I

Consume the string and return a rune iterator (default iteration)

Parameters

NameTypeNotes
selfI

Returns: I

impl(forall(I : Type), where(I <: Iterator), I)
map : fn(forall(comptime(A) : Type, comptime(B) : Type, comptime(F) : Type), self : I, f : F) -> IterMap(I, B, F)

Parameters

NameTypeNotes
selfI
fF

Returns: IterMap(I, B, F)

filter : fn(forall(comptime(A) : Type, comptime(F) : Type), self : I, f : F) -> IterFilter(I, F)

Parameters

NameTypeNotes
selfI
fF

Returns: IterFilter(I, F)

take : fn(forall(comptime(A) : Type), self : I, n : usize) -> IterTake(I)

Parameters

NameTypeNotes
selfI
nusize

Returns: IterTake(I)

skip : fn(forall(comptime(A) : Type), self : I, n : usize) -> IterSkip(I)

Parameters

NameTypeNotes
selfI
nusize

Returns: IterSkip(I)

enumerate : fn(forall(comptime(A) : Type), self : I) -> IterEnumerate(I)

Parameters

NameTypeNotes
selfI

Returns: IterEnumerate(I)

zip : fn(forall(comptime(A) : Type, comptime(J) : Type, comptime(B) : Type), self : I, other : J) -> IterZip(I, J)

Parameters

NameTypeNotes
selfI
otherJ

Returns: IterZip(I, J)

fold : fn(forall(comptime(A) : Type, comptime(Acc) : Type, comptime(F) : Type), self : I, init : Acc, f : F) -> Acc

Parameters

NameTypeNotes
selfI
initAcc
fF

Returns: Acc

for_each : fn(forall(comptime(A) : Type, comptime(F) : Type), self : I, f : F) -> unit

Parameters

NameTypeNotes
selfI
fF

Returns: unit

count : fn(forall(comptime(A) : Type), self : I) -> usize

Parameters

NameTypeNotes
selfI

Returns: usize

any : fn(forall(comptime(A) : Type, comptime(F) : Type), self : I, pred : F) -> bool

Parameters

NameTypeNotes
selfI
predF

Returns: bool

all : fn(forall(comptime(A) : Type, comptime(F) : Type), self : I, pred : F) -> bool

Parameters

NameTypeNotes
selfI
predF

Returns: bool

impl(StringBytes, Iterator(...))
Item : u8
next : (fn(ref(self) : Self) -> Option(u8))

Returns: Option(u8)

StringLines struct
StringLines

Line iterator for String — yields one line at a time (split on \n). The trailing newline is not included in each yielded line. Used by lines().

Fields

NameTypeDescription
_stringString
_byte_indexusize

Trait Implementations

impl(forall(I : Type), where(I <: Iterator), I)
into_iter : fn(self : I) -> I

Consume the string and return a rune iterator (default iteration)

Parameters

NameTypeNotes
selfI

Returns: I

impl(forall(I : Type), where(I <: Iterator), I)
map : fn(forall(comptime(A) : Type, comptime(B) : Type, comptime(F) : Type), self : I, f : F) -> IterMap(I, B, F)

Parameters

NameTypeNotes
selfI
fF

Returns: IterMap(I, B, F)

filter : fn(forall(comptime(A) : Type, comptime(F) : Type), self : I, f : F) -> IterFilter(I, F)

Parameters

NameTypeNotes
selfI
fF

Returns: IterFilter(I, F)

take : fn(forall(comptime(A) : Type), self : I, n : usize) -> IterTake(I)

Parameters

NameTypeNotes
selfI
nusize

Returns: IterTake(I)

skip : fn(forall(comptime(A) : Type), self : I, n : usize) -> IterSkip(I)

Parameters

NameTypeNotes
selfI
nusize

Returns: IterSkip(I)

enumerate : fn(forall(comptime(A) : Type), self : I) -> IterEnumerate(I)

Parameters

NameTypeNotes
selfI

Returns: IterEnumerate(I)

zip : fn(forall(comptime(A) : Type, comptime(J) : Type, comptime(B) : Type), self : I, other : J) -> IterZip(I, J)

Parameters

NameTypeNotes
selfI
otherJ

Returns: IterZip(I, J)

fold : fn(forall(comptime(A) : Type, comptime(Acc) : Type, comptime(F) : Type), self : I, init : Acc, f : F) -> Acc

Parameters

NameTypeNotes
selfI
initAcc
fF

Returns: Acc

for_each : fn(forall(comptime(A) : Type, comptime(F) : Type), self : I, f : F) -> unit

Parameters

NameTypeNotes
selfI
fF

Returns: unit

count : fn(forall(comptime(A) : Type), self : I) -> usize

Parameters

NameTypeNotes
selfI

Returns: usize

any : fn(forall(comptime(A) : Type, comptime(F) : Type), self : I, pred : F) -> bool

Parameters

NameTypeNotes
selfI
predF

Returns: bool

all : fn(forall(comptime(A) : Type, comptime(F) : Type), self : I, pred : F) -> bool

Parameters

NameTypeNotes
selfI
predF

Returns: bool

impl(StringLines, Iterator(...))
Item : String
next : (fn(ref(self) : Self) -> Option(String))

Returns: Option(String)

Functions

assert_dyn function
fn(flag : bool, msg : String) -> unit

Like assert, but accepts a runtime String message (e.g. a template string with interpolation: `error: ${value}`).

Example: name := String.from("rule1"); assert_dyn(name.len() > usize(0), Invalid rule name: ${name});

Parameters

NameTypeNotes
flagbool
msgString

Returns: unit

panic_dyn function
fn(msg : String) -> unit

Like panic, but accepts a runtime String message (e.g. a template string with interpolation: `error: ${value}`).

Example: panic_dyn(Something went wrong: ${details});

Parameters

NameTypeNotes
msgString

Returns: unit