String
pub type String = { ptr: ref u8, len: i64, cap: i64 }This is the classic string type.
An owned heap string: ptr, len, cap - each 8 bytes.
Fields
| Name | Type |
|---|---|
ptr | ref u8 |
len | i64 |
cap | i64 |
extern (C) fn strlen(s: ref u8) i64 = trust { ... } = "strlen"extern (C) fn strcmp(a: ref u8, b: ref u8) i64 = trust { ... } = "strcmp"pub type String = { ptr: ref u8, len: i64, cap: i64 }This is the classic string type.
An owned heap string: ptr, len, cap - each 8 bytes.
| Name | Type |
|---|---|
ptr | ref u8 |
len | i64 |
cap | i64 |
fn alloc_in[T: allocators.Allocator](ac: T, size: i64) ref voidfn alloc_string_in[T: allocators.Allocator](ac: T, len: i64) Stringfn alloc_substr_in[T: allocators.Allocator](ac: T, src: ref u8, start: i64, len: i64) ref u8fn alloc_in_general[T: allocators.GeneralAllocator](ac: T, size: i64) ref voidfn alloc_string_in_general[T: allocators.GeneralAllocator](ac: T, len: i64) Stringpub fn new() String = trust { ... }Create empty string
pub fn as_rchar[T](x: T) ref u8Extract `ref u8` from supported types.
pub fn rchar_cmp(a: ref u8, b: ref u8) i64Compare two raw strings.
pub fn compare[A, B](a: A, b: B) i64Compare a string or rchar with a string or rchar
pub fn from[T: allocators.Allocator](ac: T, cstr: ref u8) StringCreate from raw C string using the provided allocator.
pub fn string_free(s: mut String) void = trust { ... }Free string
pub fn len(s: String) i64Length of a String
pub fn len_raw(s: ref u8) i64Length of a raw u8 pointer
pub fn cstr(s: String) ref u8 = trust { ... }Raw pointer
pub fn rchar_starts_with(s: ref u8, prefix: ref u8) boolReturns true if s starts with prefix
pub fn rchar_ends_with(s: ref u8, suffix: ref u8) boolfn ensure_cap(ptr: ref void, needed: i64) void = trust { ... }Ensure capacity (private helper)
pub fn push_cstr(s: String, other: ref u8) void = trust { ... }Append a C string in place
pub fn push(s: String, other: String) void = trust { ... }Append another String in place
pub fn clone(s: String) String = trust { ... }Clone — returns a new heap-allocated String
pub fn reverse(s: String) void = trust { ... }Reverse in place
pub fn concat_raw(a: ref u8, b: ref u8) ref u8 = trust { ... }Concat raw u8 pointers — returns a new heap-allocated ref u8
pub fn concat(a: String, b: String) String = trust { ... }Concat — returns a new heap-allocated String
pub fn reserve(s: String, cap: i64) void = trust { ... }Reserve at least cap bytes of capacity
pub fn clear(s: String) void = trust { ... }Clear contents (set length to 0, keep allocation)
pub fn push_char(s: String, c: u8) void = trust { ... }Append a single byte character
pub fn get(s: String, i: i64) option[u8]Get byte at index i, returns none if out of bounds
pub fn byte_at(s: ref u8, i: i64) option[u8]Get byte at index i from raw u8 pointer, returns none if out of bounds
pub fn set(s: String, i: i64, c: u8) void = trust { ... }Set byte at index i (no-op if out of bounds)
pub fn slice(s: String, start: i64, end: i64) String = trust { ... }Return a new heap String containing bytes [start, end)
fn slice_general[T: allocators.GeneralAllocator](ac: T, s: String, start: i64, end: i64) Stringpub fn find(s: String, sub: String) option[i64]Find first occurrence of sub in s — returns some(index) or none
pub fn contains(s: String, sub: String) boolReturns true if sub appears anywhere in s
pub fn to_upper(s: String) void = trust { ... }Convert to uppercase in place
pub fn to_lower(s: String) void = trust { ... }Convert to lowercase in place
pub fn trim_left(s: String) void = trust { ... }Trim leading whitespace in place
pub fn trim_right(s: String) void = trust { ... }Trim trailing whitespace in place
pub fn trim(s: String) void = trust { ... }Trim leading and trailing whitespace in place
pub fn replace(s: String, from: String, to: String) String = trust { ... }Return a new String with all occurrences of `from` replaced by `to`
pub fn trim_prefix_raw[T: allocators.Allocator](ac: T, s: ref u8, prefix: ref u8) ref u8If s starts with prefix, return a new ref u8 without it, otherwise clone.
pub fn trim_suffix_raw[T: allocators.Allocator](ac: T, s: ref u8, suffix: ref u8) ref u8If s ends with suffix, return a new ref u8 without it, otherwise clone.
pub fn split[T: allocators.GeneralAllocator](ac: T, s: String, delim: String) result[ref void, ListAllocError]Split s by delim, returns an opaque list of Strings allocated with the provided general allocator.
pub fn equals(x: String, y: String) boolCheck if two strings are equal
pub fn trim_prefix(s: String, prefix: String) String = trust { ... }If s starts with prefix, return s without it, otherwise clone
pub fn trim_suffix(s: String, suffix: String) String = trust { ... }If s ends with suffix, return s without it, otherwise clone
pub fn starts_with(s: String, prefix: String) boolCheck if s starts with prefix
pub fn ends_with(s: String, suffix: String) boolCheck if s ends with suffix
pub type StringBuilder = { ptr: ref u8, len: i64, cap: i64 }The almighty String builder
| Name | Type |
|---|---|
ptr | ref u8 |
len | i64 |
cap | i64 |
pub fn (StringBuilder) new() StringBuilderCreate a new empty StringBuilder
pub fn (StringBuilder) append(sb: StringBuilder, s: String) void = trust { ... }Append a String to the builder
pub fn (StringBuilder) append_cstr(sb: StringBuilder, cstr: ref u8) void = trust { ... }Append a C string to the builder
pub fn (StringBuilder) build(sb: StringBuilder) String = trust { ... }Build — returns a new heap-allocated String with the accumulated content
pub fn (StringBuilder) free(sb: StringBuilder) void = trust { ... }Free the StringBuilder's internal buffer and header
pub fn (StringBuilder) append_char(sb: StringBuilder, c: u8) void = trust { ... }Append a single byte character
pub fn (StringBuilder) clear(sb: StringBuilder) void = trust { ... }Clear the builder (reset length to 0, keep allocation)
pub fn substr(src: ref u8, start: i64, len: i64) ref u8 = trust { ... }Return a new ref u8 containing the substring of `src` starting at `start` with length `len`.
pub fn copy(dst: ref u8, dst_off: i64, src: ref u8) void = trust { ... }Copy `len` bytes from `src` to `dst`, starting at offset `dst_off` in dst. The result is NOT null-terminated beyond what was already there.