pub struct EcmaString(/* private fields */);Expand description
An immutable ECMAScript string represented exactly as UTF-16 code units.
Unlike Rust’s str, this type preserves every u16 sequence, including
unpaired surrogate code units. Converting to UTF-8 is therefore explicit.
Implementations§
Source§impl EcmaString
impl EcmaString
Sourcepub fn from_utf8(value: &str) -> Self
pub fn from_utf8(value: &str) -> Self
Encodes a well-formed UTF-8 string as ECMAScript UTF-16 code units.
Sourcepub fn from_units(units: &[u16]) -> Self
pub fn from_units(units: &[u16]) -> Self
Copies exact UTF-16 code units, including unpaired surrogates.
Sourcepub fn slice_units(&self, range: Range<usize>) -> Self
pub fn slice_units(&self, range: Range<usize>) -> Self
Returns a copy of the requested code-unit range.
This deliberately permits ranges that split a surrogate pair, matching ECMAScript’s code-unit indexing semantics.
Sourcepub fn is_well_formed(&self) -> bool
pub fn is_well_formed(&self) -> bool
Returns whether every surrogate code unit is part of a valid pair.
Sourcepub fn code_points(&self) -> impl Iterator<Item = (usize, u32)> + '_
pub fn code_points(&self) -> impl Iterator<Item = (usize, u32)> + '_
Iterates decoded code points with their UTF-16 code-unit offsets.
Valid surrogate pairs yield one supplementary code point. Unpaired surrogates yield their raw code-unit values.
Sourcepub fn to_utf8_strict(&self) -> Result<String, IllFormedUtf16>
pub fn to_utf8_strict(&self) -> Result<String, IllFormedUtf16>
Converts to UTF-8, rejecting the first unpaired surrogate.
§Errors
Returns IllFormedUtf16 with the offset of the first unpaired
surrogate code unit.
Sourcepub fn to_utf8_lossy(&self) -> String
pub fn to_utf8_lossy(&self) -> String
Converts to UTF-8, replacing each unpaired surrogate with U+FFFD.
Trait Implementations§
Source§impl Clone for EcmaString
impl Clone for EcmaString
Source§fn clone(&self) -> EcmaString
fn clone(&self) -> EcmaString
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more