string/core

sourcestring/core.sx

strlen

private
extern (C) fn strlen(s: ref u8) i64 = trust { ... } = "strlen"

strcmp

private
extern (C) fn strcmp(a: ref u8, b: ref u8) i64 = trust { ... } = "strcmp"

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

NameType
ptrref u8
leni64
capi64

alloc_in

private
fn alloc_in[T: allocators.Allocator](ac: T, size: i64) ref void

alloc_string_in

private
fn alloc_string_in[T: allocators.Allocator](ac: T, len: i64) String

alloc_substr_in

private
fn alloc_substr_in[T: allocators.Allocator](ac: T, src: ref u8, start: i64, len: i64) ref u8

alloc_in_general

private
fn alloc_in_general[T: allocators.GeneralAllocator](ac: T, size: i64) ref void

alloc_string_in_general

private
fn alloc_string_in_general[T: allocators.GeneralAllocator](ac: T, len: i64) String

new

pub fn new() String = trust { ... }

Create empty string

as_rchar

pub fn as_rchar[T](x: T) ref u8

Extract `ref u8` from supported types.

rchar_cmp

pub fn rchar_cmp(a: ref u8, b: ref u8) i64

Compare two raw strings.

compare

pub fn compare[A, B](a: A, b: B) i64

Compare a string or rchar with a string or rchar

from

pub fn from[T: allocators.Allocator](ac: T, cstr: ref u8) String

Create from raw C string using the provided allocator.

string_free

pub fn string_free(s: mut String) void = trust { ... }

Free string

len

pub fn len(s: String) i64

Length of a String

len_raw

pub fn len_raw(s: ref u8) i64

Length of a raw u8 pointer

cstr

pub fn cstr(s: String) ref u8 = trust { ... }

Raw pointer

rchar_starts_with

pub fn rchar_starts_with(s: ref u8, prefix: ref u8) bool

Returns true if s starts with prefix

rchar_ends_with

pub fn rchar_ends_with(s: ref u8, suffix: ref u8) bool

ensure_cap

private
fn ensure_cap(ptr: ref void, needed: i64) void = trust { ... }

Ensure capacity (private helper)

push_cstr

pub fn push_cstr(s: String, other: ref u8) void = trust { ... }

Append a C string in place

push

pub fn push(s: String, other: String) void = trust { ... }

Append another String in place

clone

pub fn clone(s: String) String = trust { ... }

Clone — returns a new heap-allocated String

reverse

pub fn reverse(s: String) void = trust { ... }

Reverse in place

concat_raw

pub fn concat_raw(a: ref u8, b: ref u8) ref u8 = trust { ... }

Concat raw u8 pointers — returns a new heap-allocated ref u8

concat

pub fn concat(a: String, b: String) String = trust { ... }

Concat — returns a new heap-allocated String

reserve

pub fn reserve(s: String, cap: i64) void = trust { ... }

Reserve at least cap bytes of capacity

clear

pub fn clear(s: String) void = trust { ... }

Clear contents (set length to 0, keep allocation)

push_char

pub fn push_char(s: String, c: u8) void = trust { ... }

Append a single byte character

get

pub fn get(s: String, i: i64) option[u8]

Get byte at index i, returns none if out of bounds

byte_at

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

set

pub fn set(s: String, i: i64, c: u8) void = trust { ... }

Set byte at index i (no-op if out of bounds)

slice

pub fn slice(s: String, start: i64, end: i64) String = trust { ... }

Return a new heap String containing bytes [start, end)

slice_general

private
fn slice_general[T: allocators.GeneralAllocator](ac: T, s: String, start: i64, end: i64) String

find

pub fn find(s: String, sub: String) option[i64]

Find first occurrence of sub in s — returns some(index) or none

contains

pub fn contains(s: String, sub: String) bool

Returns true if sub appears anywhere in s

to_upper

pub fn to_upper(s: String) void = trust { ... }

Convert to uppercase in place

to_lower

pub fn to_lower(s: String) void = trust { ... }

Convert to lowercase in place

trim_left

pub fn trim_left(s: String) void = trust { ... }

Trim leading whitespace in place

trim_right

pub fn trim_right(s: String) void = trust { ... }

Trim trailing whitespace in place

trim

pub fn trim(s: String) void = trust { ... }

Trim leading and trailing whitespace in place

replace

pub fn replace(s: String, from: String, to: String) String = trust { ... }

Return a new String with all occurrences of `from` replaced by `to`

trim_prefix_raw

pub fn trim_prefix_raw[T: allocators.Allocator](ac: T, s: ref u8, prefix: ref u8) ref u8

If s starts with prefix, return a new ref u8 without it, otherwise clone.

trim_suffix_raw

pub fn trim_suffix_raw[T: allocators.Allocator](ac: T, s: ref u8, suffix: ref u8) ref u8

If s ends with suffix, return a new ref u8 without it, otherwise clone.

split

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.

equals

pub fn equals(x: String, y: String) bool

Check if two strings are equal

trim_prefix

pub fn trim_prefix(s: String, prefix: String) String = trust { ... }

If s starts with prefix, return s without it, otherwise clone

trim_suffix

pub fn trim_suffix(s: String, suffix: String) String = trust { ... }

If s ends with suffix, return s without it, otherwise clone

starts_with

pub fn starts_with(s: String, prefix: String) bool

Check if s starts with prefix

ends_with

pub fn ends_with(s: String, suffix: String) bool

Check if s ends with suffix

StringBuilder

pub type StringBuilder = { ptr: ref u8, len: i64, cap: i64 }

The almighty String builder

Fields

NameType
ptrref u8
leni64
capi64

new

pub fn (StringBuilder) new() StringBuilder

Create a new empty StringBuilder

append

pub fn (StringBuilder) append(sb: StringBuilder, s: String) void = trust { ... }

Append a String to the builder

append_cstr

pub fn (StringBuilder) append_cstr(sb: StringBuilder, cstr: ref u8) void = trust { ... }

Append a C string to the builder

build

pub fn (StringBuilder) build(sb: StringBuilder) String = trust { ... }

Build — returns a new heap-allocated String with the accumulated content

free

pub fn (StringBuilder) free(sb: StringBuilder) void = trust { ... }

Free the StringBuilder's internal buffer and header

append_char

pub fn (StringBuilder) append_char(sb: StringBuilder, c: u8) void = trust { ... }

Append a single byte character

clear

pub fn (StringBuilder) clear(sb: StringBuilder) void = trust { ... }

Clear the builder (reset length to 0, keep allocation)

substr

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`.

copy

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.