string/core

sourcestring/core.sx

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

StringView

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

A borrowed, non-owning view over a byte range.

A view does not own ptr and is not required to be null-terminated. The caller must ensure the underlying bytes outlive the view.

Fields

NameType
ptrref u8
leni64

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

as_cstr

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

Extract ref u8 from supported types.

view

pub fn view(ptr: ref u8, len: i64) StringView

Build a borrowed view over len bytes starting at ptr.

from_cstr_impl

private
fn from_cstr_impl(s: ref u8) StringView = trust { ... }

Build a view over an existing null-terminated C str.

from_cstr

pub fn from_cstr(s: ref u8) StringView

Build a view over an existing null-terminated C str.

as_view

pub fn as_view[T](x: T) StringView

Extract a borrowed view from a String, StringView, or raw C str.

len

pub fn len(v: StringView) i64

ptr

pub fn ptr(v: StringView) ref u8

is_empty

pub fn is_empty(v: StringView) bool

cmp

pub fn cmp(a: StringView, b: StringView) i64 = trust { ... }

Compare two views byte-for-byte using their lengths.

eql

pub fn eql(a: StringView, b: StringView) bool = trust { ... }

eql_cstr

pub fn eql_cstr(v: StringView, s: ref u8) bool = trust { ... }

Compare a view with a null-terminated string literal.

slice

pub fn slice(v: StringView, start: i64, end: i64) StringView = trust { ... }

Return a sub-view clamped to the bounds of v.

starts_with

pub fn starts_with(v: StringView, prefix: StringView) bool = trust { ... }

ends_with

pub fn ends_with(v: StringView, suffix: StringView) bool = trust { ... }

find

pub fn find(v: StringView, needle: StringView) option[i64] = trust { ... }

contains

pub fn contains(v: StringView, needle: StringView) bool = trust { ... }

trim_left

pub fn trim_left(v: StringView) StringView = trust { ... }

trim_right

pub fn trim_right(v: StringView) StringView = trust { ... }

trim

pub fn trim(v: StringView) StringView = trust { ... }

ZString

pub type ZString = { ptr: ref u8 }

An owned, NUL-terminated C str.

Construct with zstr_from; release with zstr_free using the same allocator.

Fields

NameType
ptrref u8

zstr_from

pub fn zstr_from[T: allocators.GeneralAllocator](ac: T, v: StringView) ZString

Allocate a NUL-terminated copy of a view with an explicit allocator.

zstr_from_cstr

pub fn zstr_from_cstr[T: allocators.GeneralAllocator](ac: T, s: ref u8) ZString

zstr_ptr

pub fn zstr_ptr(s: ZString) ref u8

zstr

pub fn zstr(s: ZString) StringView

zstr_free

pub fn zstr_free[T: allocators.GeneralAllocator](ac: T, s: ZString) void

to_string

pub fn to_string[T: allocators.Allocator](ac: T, v: StringView) String

Copy a view into a newly allocated owning String.

to_lower

pub fn to_lower[T: allocators.Allocator](ac: T, v: StringView) String

Copy a view into a new owned string with ASCII letters lowercased.

to_upper

pub fn to_upper[T: allocators.Allocator](ac: T, v: StringView) String

Copy a view into a new owned string with ASCII letters uppercased.

compare

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

Compare supported string-like values using their lengths.

is_alpha

pub fn is_alpha(c: u8) bool

Returns true when c is an ASCII letter (A-Z or a-z).