type String = String
A primitive type representing a UTF-8 encoded string.
type String = String
A primitive type representing a UTF-8 encoded string.
An incremental string builder. Add strings with .add, then finalize with .build.
String.Builder.add("Hello").add(", ").add("world!").build
// = "Hello, world!"
type String.Parser<e> = recursive either { .empty!, .ready iterative@attempt choice { .char => Try<e, (Char) self>, .close => Try<e, !>, .minMax(String.Pattern, String.Pattern) => Try<e, either { .fail self@attempt, .match(String, String) self, }>, .minMaxEnd(String.Pattern, String.Pattern) => Try<e, either { .fail self@attempt, .match(String, String)!, }>, .remainder => Try<e, String>, }, }
A streaming string parser, parameterized by an error type e.
The error type comes from the underlying source: either {} (impossible) when parsing
a plain string, or the reader's error type when parsing from a Bytes.Reader.
Cases:
.empty! — input is cleanly exhausted..ready parser — input is available, or the underlying source has failed. In the latter
case, every parser operation returns .err.Parser operations:
.close — close the parser, returning any source error..remainder — consume the parser and return all remaining unparsed input..char — read the next character..minMax(prefix, suffix) — find the leftmost split where prefix matches the left part
and suffix matches the longest possible right part. Returns .match(prefix_str, suffix_str)
on success, or .fail if no match (parser position unchanged)..minMaxEnd(prefix, suffix) — like .minMax, but the suffix extends to the end of input.
Terminates the parser on success.Use .begin/.loop (from the recursive wrapper) to iterate over multiple matches.
String.Parse("Hello, world!").begin.case {
.empty! => ...
.ready r => r.minMax(.str "Hello", .str ", ").case {
.err e => ...
.ok .match(hello, comma) r => ...
.ok .fail r => ...
}
}
type String.Pattern = recursive either { .and List<self>, .concat List<self>, .empty!, .max Nat, .min Nat, .non Char.Class, .one Char.Class, .or List<self>, .repeat self, .repeat1 self, .str String, }
A pattern for matching within strings.
Atomic patterns:
.empty! — matches the empty string..str s — matches a literal string..one class — matches a single character of the given Char.Class..non class — matches a single character NOT in the given Char.Class..min n — matches at least n characters (any)..max n — matches at most n characters (any).Combinators:
.repeat p — matches zero or more repetitions of pattern p..repeat1 p — matches one or more repetitions of pattern p..concat ps — matches a sequence of patterns in order..and ps — matches only if all patterns match the same input..or ps — matches if any of the patterns match.Common idiom: .repeat.one.any! matches any number of any characters.
dec String.Builder : String.Builder
Creates a new string Builder.
Splits a string into a list of Unicode characters.
dec String.Close : <e>[String.Parser<e>] Try<e, !>
Closes a parser, returning any source error.
Concatenates a list of strings into a single string.
String.Concat(*("Hello", ", ", "world!")) // = "Hello, world!"
dec String.EndsWith : [String, String.Pattern] Bool
Returns .true! if a string ends with a pattern.
dec String.Find : [String, String.Pattern] Option<String>
Returns the first substring matched by a pattern, if one exists.
dec String.FindAndSplit : [String, String.Pattern] Option<(String, String, String)!>
Returns the first match together with the text before and after it.
Decodes bytes as UTF-8 into a string, replacing invalid sequences with the Unicode replacement character.
Joins a list of strings with a separator between each element.
String.Join(*("a", "b", "c"), ", ") // = "a, b, c"
dec String.Lines : <e>[String.Parser<e>] Stream<e, String>
Splits a parser's remaining input into lines separated by \n or \r\n.
dec String.Matches : [String, String.Pattern] Bool
Returns .true! if a whole string matches a pattern.
dec String.Parse : [String] String.Parser<either {}>
Creates a Parser from a string. The error type is either {} (impossible).
dec String.ParseReader : <e>[Bytes.Reader<e>] String.Parser<e>
Creates a string Parser from a Bytes.Reader.
The error type matches the reader's error type.
Wraps a string in quotes with escape sequences (e.g. \n, \t, \\).
dec String.ReadAll : <e>[Bytes.Reader<e>] Try<e, String>
Reads all bytes from a Reader and decodes them into a String,
replacing invalid sequences with the Unicode replacement character.
dec String.Remainder : <e>[String.Parser<e>] Try<e, String>
Consumes a parser and returns all remaining unparsed input.
Replaces non-overlapping occurrences of a string with another string.
String.Replace("red blue red", "red", "green")
// = "green blue green"
dec String.ReplacePattern : [String, String.Pattern, box [String] String] String
Replaces non-overlapping pattern matches using a replacement function.
String.ReplacePattern("a12b345", .repeat1.one.ascii.digit!, box [_] "#")
// = "a#b#"
dec String.SplitBy : <e>[String.Parser<e>] [String] Stream<e, String>
Splits a parser's remaining input on a string separator.
dec String.SplitByPattern : <e>[String.Parser<e>] [String.Pattern] Stream<e, String>
Splits a parser's remaining input on a string pattern.
dec String.StartsWith : [String, String.Pattern] Bool
Returns .true! if a string starts with a pattern.
Converts a string to lowercase.
Converts a string to uppercase.
Trims whitespace from both ends of a string.
dec String.TrimLeft : [String, String.Pattern] String
Trims the given pattern once from the start of a string.
dec String.TrimRight : [String, String.Pattern] String
Trims the given pattern once from the end of a string.