Skip to main content

CommonJsStringBuilder

Struct CommonJsStringBuilder 

Source
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>

Source

pub const fn new() -> Self

Creates a new CommonJsStringBuilder with capacity of zero.

Source

pub fn with_capacity(capacity: usize) -> Self

Similar to Vec::with_capacity.

Creates a new CommonJsStringBuilder with given capacity.

Source

pub fn reserve(&mut self, additional: usize)

Similar to Vec::reserve.

Reserves additional capacity for the inner vector.

Source

pub fn reserve_exact(&mut self, additional: usize)

Similar to Vec::reserve_exact.

Reserves the minimum capacity for the inner vector.

Source

pub fn push<T: Into<Segment<'ref_str>>>(&mut self, seg: T)

Appends string segments to the back of the inner vector.

Source

pub fn can_be_latin1(&self) -> bool

Checks if all string segments can be represented as Latin1 characters.

Source

pub fn len(&self) -> usize

Returns the number of string segment in inner vector.

Source

pub fn is_empty(&self) -> bool

Returns true if this CommonJsStringBuilder has a length of zero, and false otherwise.

Source

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 in Latin1.
  • Segment::Str(s): Encodes the string slice if it can be represented in Latin1.
  • Segment::Latin1(b): Encodes the byte if it’s within the ASCII range.
  • Segment::CodePoint(ch): Encodes the code point by converting it to a byte if it’s within the ASCII range.

Return None if any segment fails to encode.

Source

pub fn build_from_utf16(self) -> JsString

Builds Utf-16 encoded JsString from string segments.

Source

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.
Source

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>

Source§

type Output = CommonJsStringBuilder<'ref_str>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: T) -> Self::Output

Performs the + operation. Read more
Source§

impl<'ref_str, T: Into<Segment<'ref_str>>> AddAssign<T> for CommonJsStringBuilder<'ref_str>

Source§

fn add_assign(&mut self, rhs: T)

Performs the += operation. Read more
Source§

impl<'a> Clone for CommonJsStringBuilder<'a>

Source§

fn clone(&self) -> CommonJsStringBuilder<'a>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for CommonJsStringBuilder<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Default for CommonJsStringBuilder<'a>

Source§

fn default() -> CommonJsStringBuilder<'a>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.