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
clone : (String) fn(self : String) -> Stringbytes_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(...))
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
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, ...)
_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(self: *(Self), idx: usize) -> *(Self.Output))Parameters
| Name | Type | Notes |
|---|---|---|
idx | usize |
Returns: *(Self.Output)
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. |