Module string/string
Immutable UTF-8 string type with comprehensive operations.
Types
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
| Name | Type | Description |
|---|---|---|
_bytes | Option(ArrayList(u8)) |
Trait Implementations
impl(String, ...)
new : (String) fn() -> StringCreate a new empty string (zero allocation)
Returns: String
with_capacity : (String) fn(capacity : usize) -> StringCreate 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
| Name | Type | Notes |
|---|---|---|
capacity | usize |
Returns: String
from_bytes : (String) fn(bytes : ArrayList(u8)) -> Stringfrom : (String) fn(slice : str) -> StringCreate 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
| Name | Type | Notes |
|---|---|---|
slice | str |
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
| Name | Type | Notes |
|---|---|---|
cstr | *(u8) |
Returns: Result(String, StringError)
to_cstr : (String) fn(self : String) -> ArrayList(u8)len : (String) fn(self : String) -> usizeGet the length of the string in Unicode characters (runes) For "你好世界", this returns 4
Parameters
| Name | Type | Notes |
|---|---|---|
self | String |
Returns: usize
is_empty : (String) fn(self : String) -> boolas_bytes : (String) fn(self : String) -> ArrayList(u8)as_str : (String) fn(self : String) -> strGet a str view into the string's UTF-8 bytes Returns a fat pointer (pointer + length) without copying
Parameters
| Name | Type | Notes |
|---|---|---|
self | String |
Returns: str
_decode_rune_at : (String) fn(self : String, byte_index : usize) -> Option(rune)at : (String) fn(self : String, index : usize) -> Option(rune)concat : (String) fn(self : String, other : String) -> Stringpush_string : (String) fn(self : String, other : String) -> unitpush_str : (String) fn(self : String, s : str) -> unitAppend a str slice to this string in-place (mutates self).
Parameters
| Name | Type | Notes |
|---|---|---|
self | String | |
s | str |
Returns: unit
push_byte : (String) fn(self : String, b : u8) -> unitAppend a single byte to this string in-place (mutates self).
The caller must ensure the byte maintains valid UTF-8.
Parameters
| Name | Type | Notes |
|---|---|---|
self | String | |
b | u8 |
Returns: unit
reserve : (String) fn(self : String, additional : usize) -> unitReserve capacity for at least additional more bytes.
If the string is empty, creates a new buffer with the given capacity.
Parameters
| Name | Type | Notes |
|---|---|---|
self | String | |
additional | usize |
Returns: unit
clear : (String) fn(self : String) -> unitClear the string content but keep the allocated buffer for reuse.
Parameters
| Name | Type | Notes |
|---|---|---|
self | String |
Returns: unit
bytes_len : (String) fn(self : String) -> usizeGet the number of bytes in the string (not Unicode characters).
Use len() for Unicode character count.
Parameters
| Name | Type | Notes |
|---|---|---|
self | String |
Returns: usize
substring : (String) fn(self : String, start : usize, end : usize) -> StringExtract 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
| Name | Type | Notes |
|---|---|---|
self | String | |
start | usize | |
end | usize |
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
| Name | Type | Notes |
|---|---|---|
self | String | |
substr | String | |
from_index | usize | default: [object Object] |
Returns: Option(usize)
contains : (String) fn(self : String, substr : String, (from_index : usize) ?= 0) -> boolsplit : (String) fn(self : String, separator : String) -> 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
| Name | Type | Notes |
|---|---|---|
self | String | |
substr | String | |
from_index | usize | default: [object Object] |
Returns: Option(usize)
starts_with : (String) fn(self : String, prefix : String, (position : usize) ?= 0) -> boolends_with : (String) fn(self : String, suffix : String, (end_position : usize) ?= (usize.MAX)) -> boolreplace : (String) fn(self : String, search_value : String, new_value : String) -> Stringreplace_all : (String) fn(self : String, search_value : String, new_value : String) -> Stringto_uppercase : (String) fn(self : String) -> Stringto_lowercase : (String) fn(self : String) -> String_is_whitespace_byte : (String) fn(byte : u8) -> boolHelper to check if a byte is ASCII whitespace Space (0x20), Tab (0x09), Newline (0x0A), Carriage Return (0x0D), Form Feed (0x0C), Vertical Tab (0x0B)
Parameters
| Name | Type | Notes |
|---|---|---|
byte | u8 |
Returns: bool
trim : (String) fn(self : String) -> Stringtrim_start : (String) fn(self : String) -> Stringimpl(String, Add(String)(...))
Output : Stringimpl(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) -> StringCharsReturns a rune iterator over the string's Unicode characters
Parameters
| Name | Type | Notes |
|---|---|---|
self | String |
Returns: StringChars
bytes : (String) fn(self : String) -> StringBytesReturns a byte iterator over the string's raw UTF-8 bytes
Parameters
| Name | Type | Notes |
|---|---|---|
self | String |
Returns: StringBytes
iter : (String) fn(self : String) -> _StringPosIterByte-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
| Name | Type | Notes |
|---|---|---|
self | String |
Returns: _StringPosIter
into_iter : (String) fn(self : String) -> StringCharsConsume the string and return a rune iterator (default iteration)
Parameters
| Name | Type | Notes |
|---|---|---|
self | String |
Returns: StringChars
impl(String, ...)
lines : (String) fn(self : String) -> StringLinesReturns 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
| Name | Type | Notes |
|---|---|---|
self | String |
Returns: StringLines
repeat : (String) fn(self : String, n : usize) -> Stringjoin : (String) fn(self : String, items : ArrayList(String)) -> StringJoin 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
| Name | Type | Notes |
|---|---|---|
self | String | |
items | ArrayList(String) |
Returns: String
impl(String, ...)
_is_digit_byte : (String) fn(byte : u8) -> boolHelper: check if a byte is an ASCII digit '0'-'9'
Parameters
| Name | Type | Notes |
|---|---|---|
byte | u8 |
Returns: bool
parse_i32 : (String) fn(self : String) -> Option(i32)parse_i64 : (String) fn(self : String) -> Option(i64)parse_u32 : (String) fn(self : String) -> Option(u32)parse_u64 : (String) fn(self : String) -> Option(u64)impl(String, Index(usize)(...))
Output : u8index : (fn(ref(self) : Self, idx : usize) -> *(Self.Output))impl(String, Indexable(usize)(...))
Element : u8project : (fn(ref(self) : Self, pos : usize) -> ref(u8))Parameters
| Name | Type | Notes |
|---|---|---|
pos | usize |
Returns: ref(u8)
impl(String, Clone(...))
clone : (fn(ref(self) : Self) -> Self)Returns: Self
String operation error variants.
Variants
| Variant | Fields | Description |
|---|---|---|
InvalidUtf8 | The input bytes are not valid UTF-8. | |
IndexOutOfBounds | index: usize, length: usize | The index is out of bounds for the string's byte length. |
Rune iterator for String — yields decoded Unicode runes.
Used by chars() and into_iter().
Fields
| Name | Type | Description |
|---|---|---|
_string | String | |
_byte_index | usize |
Trait Implementations
impl(forall(I : Type), where(I <: Iterator), I)
into_iter : fn(self : I) -> IConsume the string and return a rune iterator (default iteration)
Parameters
| Name | Type | Notes |
|---|---|---|
self | I |
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)filter : fn(forall(comptime(A) : Type, comptime(F) : Type), self : I, f : F) -> IterFilter(I, F)take : fn(forall(comptime(A) : Type), self : I, n : usize) -> IterTake(I)skip : fn(forall(comptime(A) : Type), self : I, n : usize) -> IterSkip(I)enumerate : fn(forall(comptime(A) : Type), self : I) -> IterEnumerate(I)zip : fn(forall(comptime(A) : Type, comptime(J) : Type, comptime(B) : Type), self : I, other : J) -> IterZip(I, J)fold : fn(forall(comptime(A) : Type, comptime(Acc) : Type, comptime(F) : Type), self : I, init : Acc, f : F) -> AccParameters
| Name | Type | Notes |
|---|---|---|
self | I | |
init | Acc | |
f | F |
Returns: Acc
for_each : fn(forall(comptime(A) : Type, comptime(F) : Type), self : I, f : F) -> unitParameters
| Name | Type | Notes |
|---|---|---|
self | I | |
f | F |
Returns: unit
count : fn(forall(comptime(A) : Type), self : I) -> usizeParameters
| Name | Type | Notes |
|---|---|---|
self | I |
Returns: usize
any : fn(forall(comptime(A) : Type, comptime(F) : Type), self : I, pred : F) -> boolParameters
| Name | Type | Notes |
|---|---|---|
self | I | |
pred | F |
Returns: bool
all : fn(forall(comptime(A) : Type, comptime(F) : Type), self : I, pred : F) -> boolParameters
| Name | Type | Notes |
|---|---|---|
self | I | |
pred | F |
Returns: bool
impl(StringChars, Iterator(...))
Item : runenext : (fn(ref(self) : Self) -> Option(rune))Returns: Option(rune)
Byte iterator for String — yields raw UTF-8 bytes.
Used by bytes().
Fields
| Name | Type | Description |
|---|---|---|
_string | String | |
_index | usize |
Trait Implementations
impl(forall(I : Type), where(I <: Iterator), I)
into_iter : fn(self : I) -> IConsume the string and return a rune iterator (default iteration)
Parameters
| Name | Type | Notes |
|---|---|---|
self | I |
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)filter : fn(forall(comptime(A) : Type, comptime(F) : Type), self : I, f : F) -> IterFilter(I, F)take : fn(forall(comptime(A) : Type), self : I, n : usize) -> IterTake(I)skip : fn(forall(comptime(A) : Type), self : I, n : usize) -> IterSkip(I)enumerate : fn(forall(comptime(A) : Type), self : I) -> IterEnumerate(I)zip : fn(forall(comptime(A) : Type, comptime(J) : Type, comptime(B) : Type), self : I, other : J) -> IterZip(I, J)fold : fn(forall(comptime(A) : Type, comptime(Acc) : Type, comptime(F) : Type), self : I, init : Acc, f : F) -> AccParameters
| Name | Type | Notes |
|---|---|---|
self | I | |
init | Acc | |
f | F |
Returns: Acc
for_each : fn(forall(comptime(A) : Type, comptime(F) : Type), self : I, f : F) -> unitParameters
| Name | Type | Notes |
|---|---|---|
self | I | |
f | F |
Returns: unit
count : fn(forall(comptime(A) : Type), self : I) -> usizeParameters
| Name | Type | Notes |
|---|---|---|
self | I |
Returns: usize
any : fn(forall(comptime(A) : Type, comptime(F) : Type), self : I, pred : F) -> boolParameters
| Name | Type | Notes |
|---|---|---|
self | I | |
pred | F |
Returns: bool
all : fn(forall(comptime(A) : Type, comptime(F) : Type), self : I, pred : F) -> boolParameters
| Name | Type | Notes |
|---|---|---|
self | I | |
pred | F |
Returns: bool
impl(StringBytes, Iterator(...))
Item : u8next : (fn(ref(self) : Self) -> Option(u8))Returns: Option(u8)
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
| Name | Type | Description |
|---|---|---|
_string | String | |
_byte_index | usize |
Trait Implementations
impl(forall(I : Type), where(I <: Iterator), I)
into_iter : fn(self : I) -> IConsume the string and return a rune iterator (default iteration)
Parameters
| Name | Type | Notes |
|---|---|---|
self | I |
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)filter : fn(forall(comptime(A) : Type, comptime(F) : Type), self : I, f : F) -> IterFilter(I, F)take : fn(forall(comptime(A) : Type), self : I, n : usize) -> IterTake(I)skip : fn(forall(comptime(A) : Type), self : I, n : usize) -> IterSkip(I)enumerate : fn(forall(comptime(A) : Type), self : I) -> IterEnumerate(I)zip : fn(forall(comptime(A) : Type, comptime(J) : Type, comptime(B) : Type), self : I, other : J) -> IterZip(I, J)fold : fn(forall(comptime(A) : Type, comptime(Acc) : Type, comptime(F) : Type), self : I, init : Acc, f : F) -> AccParameters
| Name | Type | Notes |
|---|---|---|
self | I | |
init | Acc | |
f | F |
Returns: Acc
for_each : fn(forall(comptime(A) : Type, comptime(F) : Type), self : I, f : F) -> unitParameters
| Name | Type | Notes |
|---|---|---|
self | I | |
f | F |
Returns: unit
count : fn(forall(comptime(A) : Type), self : I) -> usizeParameters
| Name | Type | Notes |
|---|---|---|
self | I |
Returns: usize
any : fn(forall(comptime(A) : Type, comptime(F) : Type), self : I, pred : F) -> boolParameters
| Name | Type | Notes |
|---|---|---|
self | I | |
pred | F |
Returns: bool
all : fn(forall(comptime(A) : Type, comptime(F) : Type), self : I, pred : F) -> boolParameters
| Name | Type | Notes |
|---|---|---|
self | I | |
pred | F |
Returns: bool
Functions
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
| Name | Type | Notes |
|---|---|---|
flag | bool | |
msg | String |
Returns: unit