velt/text
API referenceText processing: For a walkthrough, read the guide.
Exports
s with &, <, >, " and ' replaced by &, <, >, " and ': safe inside element content and in single- or double-quoted attribute values.RegExpErrorAn invalid pattern or flags (JS throws SyntaxError).RegExpA compiled regular expression: new RegExp("(\\d+)-(\\d+)", "g").RegExpMatchOne match: the matched text, where it is, and its capture groups.StringBuilderA string built by appending: toString() and take() return it without a copy.PatternA regular expression as a value to test strings with: RegExp implements it.Re-exported from velt/text/html
function escapeHtml(s: string): strings with &, <, >, " and ' replaced by &, <, >, " and ':
safe inside element content and in single- or double-quoted attribute values. Other
characters (including non-ASCII) are kept as they are.
Re-exported from velt/text/regex/errors
class RegExpError extends SyntaxErrorAn invalid pattern or flags (JS throws SyntaxError).
constructor(message: string)No doc comment yetRe-exported from velt/text/regex/regexp
class RegExp implements PatternA compiled regular expression: new RegExp("(\\d+)-(\\d+)", "g").
readonly source: stringThe pattern text as given.
readonly flags: stringThe flags as given.
readonly global: boolg: replace replaces every match.
readonly sticky: booly: test/exec only match starting exactly at the start position.
constructor(pattern: string, flags: string = "")Throws RegExpError for an invalid pattern or flags.
test(s: string, from?: usize): boolWhether s matches (anywhere at or after position from; exactly at from when sticky).
Without from, a g or y regex starts at lastIndex and updates it, like exec.
exec(s: string, from?: usize): RegExpMatch | nullThe first match at or after position from (exactly at from when sticky), or null.
Without from, a g or y regex starts at lastIndex and sets it past the match, or to 0
when there is none (JS exec); other regexes start at 0.
matchAll(s: string): RegExpMatch[]Every non-overlapping match, in order (JS s.matchAll(re), whatever the flags).
matches(s: string): string[]The text of every match (JS s.match(re) with the g flag); empty if none.
replace(s: string, replacement: string): stringJS s.replace(re, replacement): every match with the g flag, else the first.
replacement expands $&, $1…$99, $<name>, $`, $' and $$. A sticky regex
matches only at lastIndex (with g: from 0, each match where the previous one ended), and
lastIndex changes as in JS.
replaceAll(s: string, replacement: string): stringReplaces every match, whatever the flags (JS replaceAll).
replaceWith<E>(s: string, f: (m: RegExpMatch) => string throws E): string throws EReplaces matches (every one with g, else the first) with f(match); the matches are the
ones replace replaces.
[ [REPLACE_CALLING]<E>(s: string, f: (m: RegExpMatch, s: string) => string throws E): string throws EreplaceWith for s.replace(re, (match, ...groups) => …): f gets the subject too.
[ [MATCH](s: string): string[] | nullJS s.match(re): every match's text with g (null if none), else exec's groups.
[ [MATCH_ALL](s: string): RegExpMatch[]JS s.matchAll(re) (a g regex): every match from lastIndex on, which stays as is.
[ [SEARCH](s: string): i64JS s.search(re): where the first match from 0 starts, or -1; lastIndex stays.
[ [SPLIT](s: string, limit: number | null | undefined): string[]JS s.split(re, limit): no limit when null, [] for 0.
split(s: string, limit: usize = 0): string[]JS s.split(re, limit): captured groups are included in the result; limit 0 = none.
get lastIndex(): numberWhere exec(s) and test(s) of a g or y regex start, set past each match (0 after a
failed one); s.replace, s.match and s.matchAll read and set it as in JS. Kept by the
runtime, so the methods that update it don't need a mutable receiver.
set lastIndex(value: number)No doc comment yetget groupCount(): usizeNumber of capture groups (not counting the whole match).
static escape(s: string): strings with every regex metacharacter escaped, for building patterns from text.
clone(): RegExpAn independent copy (compiles the pattern again). It cannot throw: the pattern compiled
once already. So a deep copy for another task (spawn, a channel) can call it.
[Symbol.dispose]()Drop hook: releases the compiled program.
Re-exported from velt/text/regex/regexp_match
One match: the matched text, where it is, and its capture groups.
Parameter ofRegExp.replaceWithReturned byRegExp.execUsed byRegExp.[REPLACE_CALLING]
index: numberPosition of the match in the subject (code units).
end: numberPosition just past the match.
value: stringThe matched text (JS m[0]).
captures: (string | null)[]Groups 1.. (JS m[1], m[2], …); null for a group that did not take part.
names: string[]Name of each group in captures ("" when unnamed).
constructor(index: number, end: number, value: string, captures: (string | null)[], names: string[])No doc comment yet[ [INDEX](n: usize): stringm[n]: group n (0 = the whole match), "" if it did not take part or does not exist.
group(n: usize): string | nullGroup n (0 = the whole match); null if it did not take part or does not exist.
substitute(s: string, replacement: string): stringreplacement for this match of s, with $&, $1…$99, $<name>, $`, $' and
$$ expanded as JS's replace does.
named(name: string): string | nullThe named group (?<name>…); null if it did not take part or does not exist.
Re-exported from velt/text/string_builder
A string built by appending: toString() and take() return it without a copy.
Parameter ofvelt/encoding/jsonappendJsonStringappendJsonNumberappendJsonUintappendJsonValuewriteJson
constructor(capacity: usize = 0)An empty builder with room for capacity bytes before it grows (a hint).
append(s: string): voidAppends s.
appendCodePoint(code: f64): voidAppends the character with code point (or UTF-16 code unit) code, as String.fromCodePoint.
appendInt(n: i64): voidAppends the decimal digits of n (with - when negative).
appendNumber(x: f64): voidAppends x as String(x) writes it (1e+21, NaN, -0 as 0).
get length(): i64The length of the text so far, in UTF-16 code units (as toString().length).
toString(): stringThe text built so far; the builder keeps it. Shared, not copied: the next append copies the text once (it never changes the string returned).
take(): stringThe text built so far, handed over without a copy; the builder is empty again.
clear(): voidEmpties the builder.
Re-exported from velt/text/regex/pattern
A regular expression as a value to test strings with: RegExp implements it.
Used byRegExp
readonly source: stringThe pattern text as given.
readonly flags: stringThe flags as given.
test(s: string, from?: usize): boolWhether s matches (as RegExp's test).