pub struct JsString { /* private fields */ }Expand description
A Latin1 or UTF-16–encoded, reference counted, immutable string.
This is pretty similar to a Rc<[u16]>, but without the
length metadata associated with the Rc fat pointer. Instead, the length of every string is
stored on the heap, along with its reference counter and its data.
The string can be latin1 (stored as a byte for space efficiency) or U16 encoding.
We define some commonly used string constants in an interner. For these strings, we don’t allocate memory on the heap to reduce the overhead of memory allocation and reference counting.
§Internal representation
The ptr field always points to a structure whose first field is a JsStringVTable.
This enables uniform vtable dispatch for all string operations without branching.
Because we ensure this invariant at every construction, we can directly point to this type to allow for better optimization (and simpler code).
Implementations§
Source§impl JsString
impl JsString
Sourcepub fn windows(&self, size: usize) -> Windows<'_>
pub fn windows(&self, size: usize) -> Windows<'_>
Create an iterator over overlapping subslices of length size.
Sourcepub fn to_std_string_escaped(&self) -> String
pub fn to_std_string_escaped(&self) -> String
Sourcepub fn to_std_string_lossy(&self) -> String
pub fn to_std_string_lossy(&self) -> String
Sourcepub fn to_std_string(&self) -> Result<String, FromUtf16Error>
pub fn to_std_string(&self) -> Result<String, FromUtf16Error>
Decodes a JsString into a String, returning an error if the string contains unpaired
surrogates.
§Errors
FromUtf16Error if it contains any invalid data.
Sourcepub fn to_std_string_with_surrogates(
&self,
) -> impl Iterator<Item = Result<String, u16>> + use<'_>
pub fn to_std_string_with_surrogates( &self, ) -> impl Iterator<Item = Result<String, u16>> + use<'_>
Decodes a JsString into an iterator of Result<String, u16>, returning surrogates as
errors.
Sourcepub fn map_valid_segments<F>(&self, f: F) -> Self
pub fn map_valid_segments<F>(&self, f: F) -> Self
Maps the valid segments of an UTF16 string and leaves the unpaired surrogates unchanged.
Sourcepub fn code_points(&self) -> CodePointsIter<'_>
pub fn code_points(&self) -> CodePointsIter<'_>
Gets an iterator of all the Unicode codepoints of a JsString.
Sourcepub fn variant(&self) -> JsStrVariant<'_>
pub fn variant(&self) -> JsStrVariant<'_>
Get the variant of this string.
Sourcepub fn index_of(
&self,
search_value: JsStr<'_>,
from_index: usize,
) -> Option<usize>
pub fn index_of( &self, search_value: JsStr<'_>, from_index: usize, ) -> Option<usize>
Sourcepub fn code_point_at(&self, position: usize) -> CodePoint
pub fn code_point_at(&self, position: usize) -> CodePoint
Abstract operation CodePointAt( string, position ).
The abstract operation CodePointAt takes arguments string (a String) and position (a
non-negative integer) and returns a Record with fields [[CodePoint]] (a code point),
[[CodeUnitCount]] (a positive integer), and [[IsUnpairedSurrogate]] (a Boolean). It
interprets string as a sequence of UTF-16 encoded code points, as described in 6.1.4, and reads
from it a single code point starting with the code unit at index position.
More information:
§Panics
If position is smaller than size of string.
Sourcepub fn trim_start(&self) -> JsString
pub fn trim_start(&self) -> JsString
Trim whitespace from the start of the JsString.
Sourcepub fn starts_with(&self, needle: JsStr<'_>) -> bool
pub fn starts_with(&self, needle: JsStr<'_>) -> bool
Returns true if needle is a prefix of the JsStr.
Sourcepub fn ends_with(&self, needle: JsStr<'_>) -> bool
pub fn ends_with(&self, needle: JsStr<'_>) -> bool
Returns true if needle is a suffix of the JsStr.
Sourcepub fn code_unit_at(&self, index: usize) -> Option<u16>
pub fn code_unit_at(&self, index: usize) -> Option<u16>
Get the u16 code unit at index. This does not parse any characters if there
are pairs, it is simply the index of the u16 elements.
Sourcepub fn get<I>(&self, index: I) -> Option<JsString>where
I: JsStringSliceIndex,
pub fn get<I>(&self, index: I) -> Option<JsString>where
I: JsStringSliceIndex,
Get the element at the given index, or None if the index is out of range.
Sourcepub fn get_expect<I>(&self, index: I) -> JsStringwhere
I: JsStringSliceIndex,
pub fn get_expect<I>(&self, index: I) -> JsStringwhere
I: JsStringSliceIndex,
Sourcepub fn display_escaped(&self) -> JsStrDisplayEscaped<'_>
pub fn display_escaped(&self) -> JsStrDisplayEscaped<'_>
Gets a displayable escaped string. This may be faster and has fewer
allocations than format!("{}", str.to_string_escaped()) when
displaying.
Sourcepub fn display_lossy(&self) -> JsStrDisplayLossy<'_>
pub fn display_lossy(&self) -> JsStrDisplayLossy<'_>
Gets a displayable lossy string. This may be faster and has fewer
allocations than format!("{}", str.to_string_lossy()) when displaying.
Sourcepub fn debug_info(&self) -> JsStringDebugInfo<'_>
pub fn debug_info(&self) -> JsStringDebugInfo<'_>
Get a debug displayable info and metadata for this string.
Sourcepub fn into_raw(self) -> NonNull<RawJsString>
pub fn into_raw(self) -> NonNull<RawJsString>
Consumes the JsString, returning the internal pointer.
To avoid a memory leak the pointer must be converted back to a JsString using
JsString::from_raw.
Sourcepub const unsafe fn from_raw(ptr: NonNull<RawJsString>) -> Self
pub const unsafe fn from_raw(ptr: NonNull<RawJsString>) -> Self
Constructs a JsString from the internal pointer.
The raw pointer must have been previously returned by a call to
JsString::into_raw.
§Safety
This function is unsafe because improper use may lead to memory unsafety,
even if the returned JsString is never accessed.
Source§impl JsString
Dealing with inner types.
impl JsString
Dealing with inner types.
Sourcepub const fn from_static(str: &'static StaticString) -> Self
pub const fn from_static(str: &'static StaticString) -> Self
Create a JsString from a StaticString instance. This is assumed that the
static string referenced is available for the duration of the JsString instance
returned.