Skip to main content

Module string

Module string 

Source
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§

RangeError
A string operation rejected its input the way JS would (a RangeError). install raises it as a catchable guest RangeError.
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: like charAt, but a negative index counts from the end. None on OOB — Array#at and Uint8Array#at both answer undefined there, and "" is indistinguishable from a legitimate empty read.
char_at
charAt: the single code unit at index, or "" out of range.
char_code_at
charCodeAt: the UTF-16 code unit at index as f64, or NaN out 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).
declare
ends_with
endsWith: whether search ends at endPosition (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’s ToUint16, so -1 is U+FFFF.
from_code_point
String.fromCodePoint(...codePoints): astral code points encode as surrogate pairs; a negative, non-integer, or > U+10FFFF value throws.
includes
includes: whether search occurs at or after fromIndex.
index_of
indexOf: first match at or after fromIndex, else -1.
install
is_well_formed
isWellFormed: true when the string contains no lone surrogate.
last_index_of
lastIndexOf: last match at or before fromIndex (default end), else -1.
normalize
normalize: Unicode normalization. form must 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.
slice
slice: negatives count from the end; empty when start >= end after normalization.
starts_with
startsWith: whether search sits at position.
substring
substring: negatives clamp to 0 (not from the end), and the arguments swap when start > end.
to_well_formed
toWellFormed: a copy with every lone surrogate replaced by U+FFFD.

Type Aliases§

Result