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
self*(String)
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
self*(String)
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
self*(String)
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
self*(String)
additionalusize

Returns: unit

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

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

Parameters

NameTypeNotes
self*(String)

Returns: unit

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

Create a deep copy of this string with its own buffer.

Parameters

NameTypeNotes
selfString

Returns: String

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(...))
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

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

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

Parameters

NameTypeNotes
selfString

Returns: StringChars

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(self: *(Self), idx: usize) -> *(Self.Output))

Parameters

NameTypeNotes
idxusize

Returns: *(Self.Output)

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(StringChars, Iterator(...))
Item : rune
next : (fn(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(StringBytes, Iterator(...))
Item : u8
next : (fn(self : *(Self)) -> Option(u8))

Returns: Option(u8)