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 const fn from_static_js_string(src: &'static StaticJsString) -> Self
pub const fn from_static_js_string(src: &'static StaticJsString) -> Self
Create a JsString
from a static js string.
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 concat(x: JsStr<'_>, y: JsStr<'_>) -> Self
pub fn concat(x: JsStr<'_>, y: JsStr<'_>) -> Self
Creates a new JsString
from the concatenation of x
and y
.
Sourcepub fn concat_array(strings: &[JsStr<'_>]) -> Self
pub fn concat_array(strings: &[JsStr<'_>]) -> Self
Creates a new JsString
from the concatenation of every element of
strings
.
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>
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) -> 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) -> 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::Value>where
I: JsSliceIndex<'a>,
pub fn get<'a, I>(&'a self, index: I) -> Option<I::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::Valuewhere
I: JsSliceIndex<'a>,
pub unsafe fn get_unchecked<'a, I>(&'a self, index: I) -> I::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::Valuewhere
I: JsSliceIndex<'a>,
pub fn get_expect<'a, I>(&'a self, index: I) -> I::Valuewhere
I: JsSliceIndex<'a>,
Sourcepub fn refcount(&self) -> Option<usize>
pub fn refcount(&self) -> Option<usize>
Gets the number of JsString
s which point to this allocation.
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.