Expand description
The submilli string and its operations — the Rust port of the prelude’s
String methods.
Everything here works in UTF-16 code-unit space ([u16]), the
representation $string stores and the one JS string semantics are defined
over. Operations never decode to a Rust String: that would round-trip
through UTF-8 (lossy on lone surrogates, and an allocation on every call).
The $string ↔ Str marshalling and host-fn registration live in
install; this file is just the strings.
Structs§
- Admitted
Str - A native string whose allocation remains charged until its buffer is dropped. Only borrowed access is exposed so the reservation cannot be separated from the buffer.
- Range
Error - A string operation rejected its input the way JS would (a
RangeError).installraises it as a catchable guestRangeError. - Str
- A submilli string: its UTF-16 code units. Cheap to slice and concatenate, and
faithful to JS — indices,
length, and surrogate handling are all code-unit based.
Functions§
- at
at: likecharAt, but a negativeindexcounts from the end.Noneon OOB —Array#atandUint8Array#atboth answerundefinedthere, and""is indistinguishable from a legitimate empty read.- char_at
charAt: the single code unit atindex, or""out of range.- char_
code_ at charCodeAt: the UTF-16 code unit atindexasf64, orNaNout of range.- cmp
- Lexicographic compare over code units — the relational-operator primitive. Returns a sign: the first differing units’ difference, else the length difference (consumers test the sign only).
- concat
concat:selffollowed byother.- declare
- ends_
with endsWith: whethersearchends atendPosition(default end).- eq
- Structural equality over code units — the
===/==operator primitive. - from_
char_ code String.fromCharCode(...codes): one UTF-16 code unit per argument, through JavaScript’sToUint16, so-1is U+FFFF.- from_
code_ point String.fromCodePoint(...codePoints): astral code points encode as surrogate pairs; a negative, non-integer, or> U+10FFFFvalue throws.- includes
includes: whethersearchoccurs at or afterfromIndex.- index_
of indexOf: first match at or afterfromIndex, else-1.- install
- is_
well_ formed isWellFormed:truewhen the string contains no lone surrogate.- last_
index_ of lastIndexOf: last match at or beforefromIndex(default end), else-1.- normalize
normalize: Unicode normalization.formmust be"NFC"/"NFD"/"NFKC"/"NFKD"(default"NFC"); any other value throws. A lone surrogate is kept: it neither decomposes nor composes, so the text on each side of it normalizes on its own.- pad_end
padEnd: append repetitions ofpaduntil the string reachestarget_length. Copiesselfwhen already long enough orpadis empty.- pad_
start padStart: prepend repetitions ofpaduntil the string reachestarget_length. Copiesselfwhen already long enough orpadis empty.- repeat
String.prototype.repeat.ToInteger(count)truncates toward zero, leaving three bands — and the guard order below encodes them, soNaNmust fall through the negative check to the zero case:- slice
slice: negatives count from the end; empty whenstart >= endafter normalization.- starts_
with startsWith: whethersearchsits atposition.- substring
substring: negatives clamp to 0 (not from the end), and the arguments swap whenstart > end.- to_
lower_ case - to_
upper_ case - to_
well_ formed toWellFormed: a copy with every lone surrogate replaced by U+FFFD.- trim
- trim_
end - trim_
start