init::string
init/src/string.mo
String functions
pub def String.beq (a b : String) : Boolpub def String.concat (a b : String) : Stringpub def String.length (s : String) : I64pub def String.count_newlines (s : String) (len : I64) : I64Number of '\n' bytes among the first len bytes of s.
Takes a LENGTH instead of a substring on purpose. The caller
(resolve_ascending, lang/parser/position.mo) asks about one segment
between two source spans and then moves on, so it would otherwise
have to String.slice a fresh string per span -- and the compiled
monad_string_slice (runtime/src/runtime.c) does a strlen plus a
malloc plus a memcpy per call, which would make that walk quadratic.
Scanning in place keeps it O(len) on both runtimes and allocates
nothing. len past the end of s is clamped, not an error.
pub def String.trailing_chars (s : String) (len : I64) : I64Number of CHARACTERS after the last '\n' among the first len
bytes of s -- or, if that range holds no newline, the character
count of the whole range. Together with String.count_newlines this
is enough to advance a line:column position across the range.
CHARACTERS, not bytes: a character is a byte that is not a UTF-8
continuation byte, matching is_utf8_continuation_byte
(lang/parser/position.mo). Counting bytes would put the column after
a multi-byte character in the wrong place -- see
test_resolve_offsets_column_counts_characters. Same length-not-
substring convention, and for the same reason, as
String.count_newlines above.
pub def String.to_lowercase (s : String) : Stringpub def String.slice (s : String) (start : I64) (len : I64) : Stringpub def String.drop (n : I64) (s : String) : Stringpub def String.starts_with (prefix : String) (s : String) : Boolpub def String.get (s : String) (i : I64) : Option U8pub def String.get_char (s : String) (i : I64) : Option CharNOT USABLE FOR SCANNING -- see AGENTS.md's Char item.
i is a CHARACTER index, not a byte offset, and the native decodes
the whole string into a Vec<char> on EVERY call, so any per-character
loop built on this is quadratic. lang/parser/combinators.mo's
utf8_char_width uses String.get (an O(1) byte read) for exactly
this reason. There is also nothing you can do with the Char you get
back: Char has no operations and no BEq instance anywhere.
pub def String.to_list (s : String) : List U8pub def String.from_list (bytes : List U8) : Stringpub def String.hash (s : String) : U64pub instance BEq String {
def beq (a b : String) : Bool := String.beq a b
}pub def String.lt (a b : String) : Boolpub def String.gt (a b : String) : Boolpub instance BOrd String {
def lt (a b : String) : Bool := String.lt a b
def gt (a b : String) : Bool := String.gt a b
def lte (a b : String) : Bool := Bool.not (String.gt a b)
def gte (a b : String) : Bool := Bool.not (String.lt a b)
}pub instance ToString String {
def to_string (s : String) : String := s
}pub instance Add String {
def add (a b : String) : String := String.concat a b
}pub instance Append String {
def append (a b : String) : String := String.concat a b
}pub instance Hashable String {
def hash (s : String) : U64 := String.hash s
}pub def String.is_empty (s : String) : Boolpub def List.reverse (self : List A) : List Apub def List.singleton (a : A) : List Apub def String.ends_with (s : String) (suffix : String) : Boolpub def String.repeat (s : String) (n : I64) : Stringpub def String.contains (s : String) (sub : String) : Boolpub def String.index_of (s : String) (sub : String) : Option I64The CODEPOINT index of the first occurrence of sub, or none. An
empty sub matches at 0. A String is addressed by byte at the
slice/drop level; this is the human-facing index, so "éllo" finds
"llo" at 1, not at 2.
pub def String.byte_index_of (s : String) (sub : String) : Option I64The BYTE index of the first occurrence of sub, or none. An empty
sub matches at 0. This is the offset slice/drop expect; for the
character index use String.index_of.
pub def String.count (s : String) (sub : String) : I64Non-overlapping occurrences of sub (count "aaa" "aa" is 1). An empty
sub counts 0, matching the usual split convention.
pub def String.split (s : String) (sep : String) : List StringSplit on a non-empty separator. Leading/trailing separators produce
empty pieces ("a," is ["a", ""]); an empty separator returns [s].
pub def String.trim (s : String) : Stringpub def String.find_last (haystack : String) (needle : String) : I64pub def String.concat_all (ss : List String) : StringConcatenate every string in a list, no separator.
(For a separator, use List.intercalate in std/list.mo.)
QUADRATIC in the total length -- see String.concat_list below, which is
the same function in one allocation and is what you want for anything
whose piece count grows with the input (lang/json.mo's and
lang/toml.mo's string bodies were switched to it for exactly that
reason: one piece per source character). Keep this one for the fixed,
small piece counts it was written for.
Moved here from lang/json.mo, which declared this String method
inside a compiler module; cli/src/args.mo's cli_concat_all and
lang/toml.mo's toml_concat_list_body were further copies.
pub def String.concat_list (ss : List String) : StringConcatenate every piece in ONE allocation.
String.concat copies both sides, so the fold String.concat_all
does recopies the accumulated tail at every step, costing time
quadratic in the total length. At the size the compiler's own LLVM
emitters work with -- a 3.8 MB module assembled from thousands of
pieces -- that is the difference between seconds and hours. This
measures the total once, allocates once, and copies each piece once.
A separate name rather than replacing concat_all's body because
concat_all is reachable from macro expansion, which runs in the
self-hosted meta-evaluator's deliberately small fixed native table
(lang/core_eval.mo); a codegen performance fix should not widen
that sandbox.