std/text
Types
StringBuilder
pub type StringBuilder = {
// private fields omitted
}
Growable byte buffer for building a String without the O(n^2) cost of repeated String.concat. push keeps the buffer valid UTF-8; pushByte does not, so freeze validates and can fail.
StringBuilder.new
pub pure fn StringBuilder.new() -> StringBuilder
Creates an empty builder.
StringBuilder.tryNew
pub pure fn StringBuilder.tryNew() -> Result[StringBuilder, AllocError]
Creates an empty builder, or AllocError when the allocator refuses (new panics instead).
StringBuilder.push
pub pure fn StringBuilder.push(mut self, s: String)
Appends s; panics when memory runs out (tryPush reports it instead).
StringBuilder.tryPush
pub pure fn StringBuilder.tryPush(mut self, s: String) -> Result[Void, AllocError]
Appends s, or returns AllocError when memory runs out; the builder is unchanged then.
StringBuilder.pushByte
pub pure fn StringBuilder.pushByte(mut self, b: u8)
Appends a single byte; panics when memory runs out. Unlike push, this does not preserve UTF-8 validity, so a byte that breaks it makes freeze fail.
StringBuilder.tryPushByte
pub pure fn StringBuilder.tryPushByte(mut self, b: u8) -> Result[Void, AllocError]
Appends a single byte, or returns AllocError when memory runs out.
StringBuilder.len
pub pure noalloc fn StringBuilder.len(self) -> usize
Number of bytes appended so far.
StringBuilder.isEmpty
pub pure noalloc fn StringBuilder.isEmpty(self) -> Bool
Whether no bytes have been appended.
StringBuilder.freeze
pub pure fn StringBuilder.freeze(move self) -> String!
Consumes the builder and validates the bytes as UTF-8; Err when a pushByte call broke validity.
StringBuilder.tryFreeze
pub pure fn StringBuilder.tryFreeze(move self) -> String!
Same as freeze, but an allocator refusal is an error too (freeze panics instead).
Functions
indexOf
pub pure fn indexOf(s: String, c: Char) -> usize?
Byte offset of the first occurrence of c in s, or None.
lines
pub pure fn lines(s: String) -> Array[String]
Lines of s without their \n or \r\n endings, as BufReader.readLine reads them; a trailing newline adds no empty line and "" has no lines.
tryLines
pub pure fn tryLines(s: String) -> Result[Array[String], AllocError]
Same as lines, or AllocError when the allocator refuses (lines panics instead).
graphemes
pub pure fn graphemes(s: String) -> Array[String]
One string per Unicode scalar value of s.
tryGraphemes
pub pure fn tryGraphemes(s: String) -> Result[Array[String], AllocError]
Same as graphemes, or AllocError when the allocator refuses (graphemes panics instead).
parseFloat
pub pure fn parseFloat(s: String) -> Float?
Parses -? digits (. digits)? ([eE] [+-]? digits)? (the JSON number grammar); None for anything else, including a leading +, spaces, inf, nan, hex floats, underscores and a result that overflows to infinity.
parseInt
pub pure fn parseInt(s: String) -> Int?
Parses s (surrounding whitespace allowed, the same set trim removes) as a base-10 integer with an optional sign, or None.
parseUint
pub pure fn parseUint(s: String) -> u64?
Parses s (surrounding whitespace allowed, the same set trim removes) as a base-10 unsigned integer with an optional leading +, or None. The full u64 range, unlike parseInt.
parseI128
pub pure fn parseI128(s: String) -> i128?
Parses s (surrounding whitespace allowed, the same set trim removes) as a base-10 i128 with an optional sign, or None.
parseU128
pub pure fn parseU128(s: String) -> u128?
Parses s (surrounding whitespace allowed, the same set trim removes) as a base-10 unsigned integer with an optional leading +, or None. The full u128 range.
fromChar
pub pure fn fromChar(c: Char) -> String
String holding just c.
slice
pub pure fn slice(s: String, start: usize, end: usize) -> String?
Bytes start..end of s as a string; None unless both offsets are within s and on character boundaries.
trySlice
pub pure fn trySlice(s: String, start: usize, end: usize) -> Result[String?, AllocError]
Same as slice, or AllocError when the allocator refuses (slice panics instead).
replace
pub pure fn replace(s: String, old: String, new: String) -> String
s with every occurrence of old replaced by new; an empty old leaves s unchanged.
tryReplace
pub pure fn tryReplace(s: String, old: String, new: String) -> Result[String, AllocError]
Same as replace, or AllocError when the allocator refuses (replace panics instead).
find
pub pure fn find(s: String, sub: String) -> usize?
Byte offset of the first sub in s, or None. The empty sub is at 0. Offsets are byte offsets, the same as slice takes.
rfind
pub pure fn rfind(s: String, sub: String) -> usize?
Byte offset of the last sub in s, or None. The empty sub is at s.len().
equalFold
pub pure noalloc fn equalFold(a: String, b: String) -> Bool
Whether a and b are equal ignoring the case of ASCII letters. Bytes outside A-Za-z must match exactly, so this is not Unicode case folding. HTTP field names compare this way, per RFC 9110.
trimPrefix
pub pure fn trimPrefix(s: String, prefix: String) -> String
s without a leading prefix; s unchanged when it does not start with one.
tryTrimPrefix
pub pure fn tryTrimPrefix(s: String, prefix: String) -> Result[String, AllocError]
Same as trimPrefix, or AllocError when the allocator refuses (trimPrefix panics instead).
trimSuffix
pub pure fn trimSuffix(s: String, suffix: String) -> String
s without a trailing suffix; s unchanged when it does not end with one.
tryTrimSuffix
pub pure fn tryTrimSuffix(s: String, suffix: String) -> Result[String, AllocError]
Same as trimSuffix, or AllocError when the allocator refuses (trimSuffix panics instead).
parseUint
pub pure fn parseUint(s: String, base: usize) -> u64?
s as an unsigned integer in base (2 to 36, letters either case), or None when it is empty, has a stray character, or does not fit in a u64. Surrounding whitespace is allowed, the same set trim removes; a sign is not.