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;Functions
Section titled “Functions”| Function | Result |
|---|---|
stringLen(s: String) Int | Length of an owned string |
fromChar(c: Char) String | One-character owned string |
pushChar(s: String, c: Char) String | A new string with c appended |
owned(v: StringView) String | Copies a view into an owned string |
trim(v: StringView) String | Copies the view without surrounding ASCII whitespace |
splitOnce(v: StringView, delim: Char) SplitPair | Splits at the first delimiter |
SplitPair contains left, right, and found fields.
TextBuf
Section titled “TextBuf”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().