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.
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>>
pub fn to_std_string_with_surrogates( &self, ) -> impl Iterator<Item = Result<String, u16>>
Decodes a JsString into an iterator of Result<String, u16>, returning surrogates as
errors.
Sourcepub fn map_valid_segments<F>(&self, f: F) -> JsString
pub fn map_valid_segments<F>(&self, f: F) -> JsString
Maps the valid segments of an UTF16 string and leaves the unpaired surrogates unchanged.
Sourcepub fn code_points(&self) -> impl Iterator<Item = CodePoint> + Clone
pub fn code_points(&self) -> impl Iterator<Item = CodePoint> + Clone
Gets an iterator of all the Unicode codepoints of a JsString.
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) -> JsStr<'_>
pub fn trim_start(&self) -> JsStr<'_>
Trim whitespace from the start of the JsString.
Sourcepub fn get<'a, I>(&'a self, index: I) -> Option<<I as JsSliceIndex<'a>>::Value>where
I: JsSliceIndex<'a>,
pub fn get<'a, I>(&'a self, index: I) -> Option<<I as JsSliceIndex<'a>>::Value>where
I: JsSliceIndex<'a>,
Get the element a the given index, None otherwise.
Sourcepub unsafe fn get_unchecked<'a, I>(
&'a self,
index: I,
) -> <I as JsSliceIndex<'a>>::Valuewhere
I: JsSliceIndex<'a>,
pub unsafe fn get_unchecked<'a, I>(
&'a self,
index: I,
) -> <I as JsSliceIndex<'a>>::Valuewhere
I: JsSliceIndex<'a>,
Returns an element or subslice depending on the type of index, without doing bounds check.
§Safety
Caller must ensure the index is not out of bounds
Sourcepub fn get_expect<'a, I>(&'a self, index: I) -> <I as JsSliceIndex<'a>>::Valuewhere
I: JsSliceIndex<'a>,
pub fn get_expect<'a, I>(&'a self, index: I) -> <I as JsSliceIndex<'a>>::Valuewhere
I: JsSliceIndex<'a>,
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 into_raw(self) -> NonNull<RawJsString>
pub fn into_raw(self) -> NonNull<RawJsString>
Consumes the JsString, returning a pointer to RawJsString.
To avoid a memory leak the pointer must be converted back to a JsString using
JsString::from_raw.
Sourcepub unsafe fn from_raw(ptr: NonNull<RawJsString>) -> JsString
pub unsafe fn from_raw(ptr: NonNull<RawJsString>) -> JsString
Constructs a JsString from a pointer to RawJsString.
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
impl JsString
Sourcepub const fn from_static_js_str(src: &'static JsStr<'static>) -> JsString
pub const fn from_static_js_str(src: &'static JsStr<'static>) -> JsString
Create a JsString from a static js string.
Sourcepub fn concat(x: JsStr<'_>, y: JsStr<'_>) -> JsString
pub fn concat(x: JsStr<'_>, y: JsStr<'_>) -> JsString
Creates a new JsString from the concatenation of x and y.
Sourcepub fn concat_array(strings: &[JsStr<'_>]) -> JsString
pub fn concat_array(strings: &[JsStr<'_>]) -> JsString
Creates a new JsString from the concatenation of every element of
strings.
Trait Implementations§
Source§impl From<JsString> for FunctionBinding
impl From<JsString> for FunctionBinding
Source§impl From<JsString> for PropertyKey
impl From<JsString> for PropertyKey
Source§impl<'a> IntoIterator for &'a JsString
impl<'a> IntoIterator for &'a JsString
Source§impl Ord for JsString
impl Ord for JsString
Source§impl PartialOrd for JsString
impl PartialOrd for JsString
Source§impl Trace for JsString
impl Trace for JsString
Source§unsafe fn trace_non_roots(&self)
unsafe fn trace_non_roots(&self)
Source§fn run_finalizer(&self)
fn run_finalizer(&self)
Finalize::finalize on this object and all
contained subobjects.impl Eq for JsString
impl JsData for JsString
Auto Trait Implementations§
impl Freeze for JsString
impl !RefUnwindSafe for JsString
impl !Send for JsString
impl !Sync for JsString
impl Unpin for JsString
impl !UnwindSafe for JsString
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> NativeObject for T
impl<T> NativeObject for T
Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
NativeObject to a &dyn Any.Source§fn as_mut_any(&mut self) -> &mut (dyn Any + 'static)
fn as_mut_any(&mut self) -> &mut (dyn Any + 'static)
NativeObject to a &mut dyn Any.Source§fn type_name_of_value(&self) -> &'static str
fn type_name_of_value(&self) -> &'static str
Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.