Skip to content

std/string

String owns its storage. StringView borrows a range from a string. The automatic prelude already provides StringView, len, at, slice, eq, view, startsWith, and indexOf.

import "std/string" as string;
var source = "hello";
var tail: StringView = slice(source, 1, 4);
println(string.owned(tail));

Import the module for the remaining helpers:

import "std/string" as string;
FunctionResult
stringLen(s: String) IntLength of an owned string
fromChar(c: Char) StringOne-character owned string
pushChar(s: String, c: Char) StringA new string with c appended
owned(v: StringView) StringCopies a view into an owned string
trim(v: StringView) StringCopies the view without surrounding ASCII whitespace
splitOnce(v: StringView, delim: Char) SplitPairSplits at the first delimiter

SplitPair contains left, right, and found fields.

TextBuf collects text and returns self from push and pushChar:

import "std/string" as string;
fun greeting(name: String) String {
var buffer = new string.TextBuf();
buffer.push("Hello, ").push(name).pushChar('!');
return buffer.build();
}

Methods: clear(), len(), isEmpty(), push(text), pushChar(c), build(), and view().