Skip to content

Strings

In C3, multiple string types are available, each suited to different use cases.

String

distinct String = inline char[];


Strings are usually the typical type to use, they can be sliced , compared etc …
It is possible to access the length of a String instance through the .len operator.

ZString

distinct ZString = inline char*;


ZString is used when working with C code, which expects null-terminated C-style strings of type char*. it is a distinct type so converting to a ZString requires an explicit cast. This helps to remind the user to check there is appropriate \0 termination of the string data.

The ZString methods are outlined below.

WString

distinct WString = inline Char16*;


The WString type is similar to ZString but uses Char16*, typically for UTF-16 encoded strings. This type is useful for applications where 16-bit character encoding is required.

DString

distinct DString (OutStream) = void*;


DString is a dynamic string builder that supports various string operations at runtime, allowing for flexible manipulation without the need for manual memory allocation.

Member functions:

String Member Functions

fn Char16[]! String.to_new_utf16(s, Allocator allocator = allocator::heap())
fn Char16[]! String.to_temp_utf16(s);
fn WString! String.to_wstring(s, Allocator allocator)
fn String String.free(&s, Allocator allocator = allocator::heap())
fn String String.tcopy(s) => s.copy(allocator::temp()) @inline;
fn String String.copy(s, Allocator allocator = allocator::heap())
fn String String.strip_end(string, String needle);
fn String String.strip(string, String needle);
fn String String.trim(string, String to_trim);
fn bool String.contains(string, String needle);
fn bool String.starts_with(string, String needle);
fn bool String.ends_with(string, String needle);
fn usz! String.index_of_char(s, char needle);
fn usz! String.index_of(s, String needle)
fn usz! String.rindex_of(s, String needle)
fn String[] String.split(s, String needle, usz max = 0, Allocator allocator = allocator::heap());
fn String String.new_split(s, String needle, usz max = 0) => s.split(needle, max, allocator::heap()) @inline;
// temporary String split
fn String String.tsplit(s, String needle, usz max = 0) => s.split(needle, max, allocator::temp());
fn String String.tconcat(s1, String s2);
fn String String.tconcat(s1, String s2) => s1.concat(s2, allocator::temp());
fn WString! String.to_temp_wstring(s) => s.to_wstring(allocator::temp());
fn WString! String.to_new_wstring(s) => s.to_wstring(allocator::heap());
fn int128! String.to_int128(s, int base = 10) => s.to_integer(int128, base);
fn long! String.to_long(s, int base = 10) => s.to_integer(long, base);
fn int! String.to_int(s, int base = 10) => s.to_integer(int, base);
fn short! String.to_short(s, int base = 10) => s.to_integer(short, base);
fn ichar! String.to_ichar(s, int base = 10) => s.to_integer(ichar, base);
fn uint128! String.to_uint128(s, int base = 10) => s.to_integer(uint128, base);
fn ulong! String.to_ulong(s, int base = 10) => s.to_integer(ulong, base);
fn uint! String.to_uint(s, int base = 10) => s.to_integer(uint, base);
fn ushort! String.to_ushort(s, int base = 10) => s.to_integer(ushort, base);
fn char! String.to_uchar(s, int base = 10) => s.to_integer(char, base);
fn double! String.to_double(s) => s.to_real(double);
fn float! String.to_float(s) => s.to_real(float);
fn String String.new_ascii_to_upper(s, Allocator allocator = allocator::heap());
fn Char16[]! String.to_new_utf16(s, Allocator allocator = allocator::heap());
fn Char16[]! String.to_temp_utf16(s);
fn Char32[]! String.to_utf32(s, Allocator allocator);
fn Char32[]! String.to_new_utf32(s) => s.to_utf32(allocator::heap()) @inline;
fn Char32[]! String.to_temp_utf32(s) => s.to_utf32(allocator::temp()) @inline;
fn WString! String.to_wstring(s, Allocator allocator);
fn WString! String.to_new_wstring(s) => s.to_wstring(allocator::heap());
fn WString! String.to_temp_wstring(s) => s.to_wstring(allocator::temp());
fn StringIterator String.iterator(s);

ZString Member Functions

fn String ZString.str_view(str);
fn usz ZString.char_len(str);
fn usz ZString.len(str);
fn ZString String.zstr_copy(s, Allocator allocator = allocator::heap())
fn ZString String.zstr_tcopy(s) => s.zstr_copy(allocator::temp()) @inline;