pub struct Wtf8 { /* private fields */ }Expand description
A JavaScript string’s bytes, in WTF-8.
A JS string is a sequence of UTF-16 code units, and that sequence may contain an
unpaired surrogate: String.fromCharCode(0xD800) produces exactly one, and
"\uD800".length is 1. Rust’s String cannot represent that, so the bytes here are
WTF-8 — ordinary UTF-8, except that a code unit with no partner is written as the
three-byte sequence UTF-8 would use for U+D800..U+DFFF.
§The encoding is canonical, and that is load-bearing
A surrogate pair is written as the single four-byte sequence of the astral code point
it spells, never as two three-byte sequences. Without that rule the bytes of "\uD800" + "\uDC00" would differ from the bytes of "\u{10000}" while the two strings are
observably identical — same length, same charCodeAt at every index — and every
equality, property key and switch arm in the engine would disagree with the language.
The canonical form makes byte equality exactly code-unit equality, which is what lets a property key be compared without decoding it.
Implementations§
Source§impl Wtf8
impl Wtf8
Sourcepub fn new(text: impl Into<String>) -> Wtf8
pub fn new(text: impl Into<String>) -> Wtf8
Wrap a Rust string, which is valid UTF-8 by construction.
Sourcepub fn from_units(units: &[u16]) -> Wtf8
pub fn from_units(units: &[u16]) -> Wtf8
Build from UTF-16 code units, which is the form a JS string really has.
An unpaired surrogate survives; a pair is folded into its astral code point.
Sourcepub fn as_str(&self) -> Option<&str>
pub fn as_str(&self) -> Option<&str>
The text, when it is valid UTF-8.
None only for a string containing an unpaired surrogate — there is no &str for
one. Callers that only need to display or compare use Wtf8::to_string_lossy.
Sourcepub fn to_string_lossy(&self) -> Cow<'_, str>
pub fn to_string_lossy(&self) -> Cow<'_, str>
The text with unpaired surrogates replaced by U+FFFD.
Borrowed, and therefore free, for the ordinary string that has no surrogate.
Sourcepub fn to_units(&self) -> Vec<u16>
pub fn to_units(&self) -> Vec<u16>
The UTF-16 code units, which is what indexing and charCodeAt count.
Sourcepub fn utf16_len(&self) -> usize
pub fn utf16_len(&self) -> usize
Length in UTF-16 code units, which is what String.prototype.length reports.
Counted from the encoding rather than by decoding, so it does not allocate: a four-byte sequence is a surrogate pair and therefore two units, everything else is one.
Sourcepub fn code_unit_at(&self, index: usize) -> Option<u16>
pub fn code_unit_at(&self, index: usize) -> Option<u16>
The code unit at index, or None past the end.
Sourcepub fn unit_at(&self, index: usize) -> Option<Wtf8>
pub fn unit_at(&self, index: usize) -> Option<Wtf8>
One code unit as a string, which is what s[i] yields.
A single unit, not a code point: "\u{1F600}"[0] is the lone high surrogate, because
indexing a string in JS works on code units.
Sourcepub fn slice_units(&self, start: usize, end: usize) -> Wtf8
pub fn slice_units(&self, start: usize, end: usize) -> Wtf8
The code units start..end, re-encoded canonically.
Sourcepub fn code_points(&self) -> Vec<Wtf8>
pub fn code_points(&self) -> Vec<Wtf8>
One element per code point, which is how a string is iterated.
Iteration and indexing disagree on purpose: [...str] splits an astral character
into one element while str[0] splits it into half a surrogate pair.
Sourcepub fn concat(&self, other: &Wtf8) -> Wtf8
pub fn concat(&self, other: &Wtf8) -> Wtf8
Concatenate, keeping the encoding canonical.
Byte concatenation is already canonical except where a high surrogate at the end of
self meets a low surrogate at the start of other: those two must fold into one
astral sequence, or the result would be a different string from the one the language
says + produces.
Sourcepub fn code_point_values(&self) -> Vec<u32>
pub fn code_point_values(&self) -> Vec<u32>
The code points the regular expression engine matches against.
This is Wtf8::code_points flattened to u32, with a lone surrogate kept as its
own value rather than becoming U+FFFD: \p{Cs} has to match it, and no char can
hold it. A valid pair is one code point, so an offset into this list is still a code
point boundary.