pub struct CommonJsStringBuilder<'a> { /* private fields */ }Expand description
Common JsString builder that accepts multiple variant of string or character.
Originally based on kiesel-js
Implementations§
Source§impl<'seg, 'ref_str: 'seg> CommonJsStringBuilder<'seg>
impl<'seg, 'ref_str: 'seg> CommonJsStringBuilder<'seg>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Similar to Vec::with_capacity.
Creates a new CommonJsStringBuilder with given capacity.
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Similar to Vec::reserve.
Reserves additional capacity for the inner vector.
Sourcepub fn reserve_exact(&mut self, additional: usize)
pub fn reserve_exact(&mut self, additional: usize)
Similar to Vec::reserve_exact.
Reserves the minimum capacity for the inner vector.
Sourcepub fn push<T: Into<Segment<'ref_str>>>(&mut self, seg: T)
pub fn push<T: Into<Segment<'ref_str>>>(&mut self, seg: T)
Appends string segments to the back of the inner vector.
Sourcepub fn can_be_latin1(&self) -> bool
pub fn can_be_latin1(&self) -> bool
Checks if all string segments can be represented as Latin1 characters.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if this CommonJsStringBuilder has a length of zero, and false otherwise.
Sourcepub fn build_from_latin1(&self) -> Option<JsString>
pub fn build_from_latin1(&self) -> Option<JsString>
Builds Latin1 encoded JsString from string segments.
This doesn’t consume the builder itself because it may fails to build and the caller may wants to keep the builder for further operations.
This processes the following types of segments:
Segment::String(s): Encodes the string if it can be represented inLatin1.Segment::Str(s): Encodes the string slice if it can be represented inLatin1.Segment::Latin1(b): Encodes the byte if it’s within theASCIIrange.Segment::CodePoint(ch): Encodes the code point by converting it to a byte if it’s within theASCIIrange.
Return None if any segment fails to encode.
Sourcepub fn build_from_utf16(self) -> JsString
pub fn build_from_utf16(self) -> JsString
Builds Utf-16 encoded JsString from string segments.
Sourcepub fn build(self) -> JsString
pub fn build(self) -> JsString
Builds JsString from CommonJsStringBuilder,
This function first checks if the instance is empty:
- If it is empty, it returns the default
JsString. - If it can be represented as Latin1 characters, it safely encodes it as
Latin1. - Otherwise, it falls back to encoding using
UTF-16.
Sourcepub unsafe fn build_as_latin1(self) -> JsString
pub unsafe fn build_as_latin1(self) -> JsString
Builds Latin1 encoded JsString from CommonJsStringBuilder, return None if segments can’t be encoded as Latin1
§Safety
Caller must ensure that the string segments can be Latin1 encoded.
If string segments can’t be Latin1 encoded, it may lead to encoding errors,
resulting in an incorrect or malformed JsString. This could cause undefined behavior
when the resulting string is used in further operations or when interfacing with other
parts of the system that expect valid Latin1 encoded string.
Trait Implementations§
Source§impl<'ref_str, T: Into<Segment<'ref_str>>> Add<T> for CommonJsStringBuilder<'ref_str>
impl<'ref_str, T: Into<Segment<'ref_str>>> Add<T> for CommonJsStringBuilder<'ref_str>
Source§impl<'ref_str, T: Into<Segment<'ref_str>>> AddAssign<T> for CommonJsStringBuilder<'ref_str>
impl<'ref_str, T: Into<Segment<'ref_str>>> AddAssign<T> for CommonJsStringBuilder<'ref_str>
Source§fn add_assign(&mut self, rhs: T)
fn add_assign(&mut self, rhs: T)
+= operation. Read moreSource§impl<'a> Clone for CommonJsStringBuilder<'a>
impl<'a> Clone for CommonJsStringBuilder<'a>
Source§fn clone(&self) -> CommonJsStringBuilder<'a>
fn clone(&self) -> CommonJsStringBuilder<'a>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more