Skip to main content

Wtf8

Struct Wtf8 

Source
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

Source

pub fn new(text: impl Into<String>) -> Wtf8

Wrap a Rust string, which is valid UTF-8 by construction.

Source

pub fn from_rc_str(text: &str) -> Wtf8

Build from a &str without copying it.

Wtf8::new takes impl Into<String>, which copies. A property atom is a &str that already lives in the compiled code and outlives every use of the key, so sharing the bytes is free — and this is on the path of every property access.

Source

pub fn from_parts(bytes: Vec<u8>, valid: bool) -> Wtf8

Build from bytes that are already WTF-8, with valid saying whether they contain an unpaired surrogate.

Used by the rope’s flattening path, which has just concatenated children whose validity it knows.

Source

pub fn is_valid(&self) -> bool

Whether the bytes are valid UTF-8, i.e. the string has no unpaired surrogate.

Source

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.

Source

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.

Source

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.

Source

pub fn as_bytes(&self) -> &[u8] ⓘ

The WTF-8 bytes. Two Wtf8s are equal exactly when these are.

Source

pub fn to_units(&self) -> Vec<u16>

The UTF-16 code units, which is what indexing and charCodeAt count.

Source

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.

Source

pub fn code_unit_at(&self, index: usize) -> Option<u16>

The code unit at index, or None past the end.

Source

pub fn is_empty(&self) -> bool

Whether the string is empty.

Source

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.

Source

pub fn slice_units(&self, start: usize, end: usize) -> Wtf8

The code units start..end, re-encoded canonically.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Clone for Wtf8

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Wtf8

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Wtf8

Source§

impl From<Wtf8> for PropertyKey

Source§

fn from(text: Wtf8) -> PropertyKey

Converts to this type from the input type.
Source§

impl PartialEq for Wtf8

Source§

fn eq(&self, other: &Self) -> bool

Byte equality, which the canonical encoding makes equal to code-unit equality.

1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more

Auto Trait Implementations§

§

impl !Send for Wtf8

§

impl !Sync for Wtf8

§

impl Forget for Wtf8

§

impl Freeze for Wtf8
where *const RcInner<[u8]> is !null: Freeze, PhantomData<RcInner<[u8]>>: Freeze,

§

impl Move for Wtf8

§

impl RefUnwindSafe for Wtf8
where Rc<[u8]>: RefUnwindSafe,

§

impl Unpin for Wtf8
where Rc<[u8]>: Unpin,

§

impl UnsafeUnpin for Wtf8
where *const RcInner<[u8]> is !null: UnsafeUnpin, PhantomData<RcInner<[u8]>>: UnsafeUnpin,

§

impl UnwindSafe for Wtf8
where Rc<[u8]>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.