init::string

init/src/string.mo

String functions

defString.beq#
pub def String.beq (a b : String) : Bool
defString.concat#
pub def String.concat (a b : String) : String
defString.length#
pub def String.length (s : String) : I64
defString.count_newlines#
pub def String.count_newlines (s : String) (len : I64) : I64

Number 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.

defString.trailing_chars#
pub def String.trailing_chars (s : String) (len : I64) : I64

Number 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.

defString.to_lowercase#
pub def String.to_lowercase (s : String) : String
defString.slice#
pub def String.slice (s : String) (start : I64) (len : I64) : String
defString.drop#
pub def String.drop (n : I64) (s : String) : String
defString.starts_with#
pub def String.starts_with (prefix : String) (s : String) : Bool
defString.get#
pub def String.get (s : String) (i : I64) : Option U8
defString.get_char#
pub def String.get_char (s : String) (i : I64) : Option Char

NOT 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.

defString.to_list#
pub def String.to_list (s : String) : List U8
defString.from_list#
pub def String.from_list (bytes : List U8) : String
defString.hash#
pub def String.hash (s : String) : U64
instance_#
pub instance BEq String {
    def beq (a b : String) : Bool := String.beq a b
}
defString.lt#
pub def String.lt (a b : String) : Bool
defString.gt#
pub def String.gt (a b : String) : Bool
instance_#
pub 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)
}
instance_#
pub instance ToString String {
    def to_string (s : String) : String := s
}
instance_#
pub instance Add String {
    def add (a b : String) : String := String.concat a b
}
instance_#
pub instance Append String {
    def append (a b : String) : String := String.concat a b
}
instance_#
pub instance Hashable String {
    def hash (s : String) : U64 := String.hash s
}
defString.is_empty#
pub def String.is_empty (s : String) : Bool
defList.reverse#
pub def List.reverse (self : List A) : List A
defList.singleton#
pub def List.singleton (a : A) : List A
defString.ends_with#
pub def String.ends_with (s : String) (suffix : String) : Bool
defString.repeat#
pub def String.repeat (s : String) (n : I64) : String
defString.contains#
pub def String.contains (s : String) (sub : String) : Bool
defString.index_of#
pub def String.index_of (s : String) (sub : String) : Option I64

The 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.

defString.byte_index_of#
pub def String.byte_index_of (s : String) (sub : String) : Option I64

The 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.

defString.count#
pub def String.count (s : String) (sub : String) : I64

Non-overlapping occurrences of sub (count "aaa" "aa" is 1). An empty sub counts 0, matching the usual split convention.

defString.split#
pub def String.split (s : String) (sep : String) : List String

Split on a non-empty separator. Leading/trailing separators produce empty pieces ("a," is ["a", ""]); an empty separator returns [s].

defString.trim#
pub def String.trim (s : String) : String
defString.find_last#
pub def String.find_last (haystack : String) (needle : String) : I64
defString.concat_all#
pub def String.concat_all (ss : List String) : String

Concatenate 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.

defString.concat_list#
pub def String.concat_list (ss : List String) : String

Concatenate 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.