Skip to main content

Wtf8Buf

Struct Wtf8Buf 

Source
pub struct Wtf8Buf { /* private fields */ }
Expand description

An owned, growable string of well-formed WTF-8 data.

Similar to String, but can additionally contain surrogate code points if they’re not in a surrogate pair.

Implementations§

Source§

impl Wtf8Buf

Source

pub fn new() -> Wtf8Buf

Creates a new, empty WTF-8 string.

Source

pub fn with_capacity(capacity: usize) -> Wtf8Buf

Creates a new, empty WTF-8 string with pre-allocated capacity for capacity bytes.

Source

pub const unsafe fn from_bytes_unchecked(value: Vec<u8>) -> Wtf8Buf

Creates a WTF-8 string from a WTF-8 byte vec.

§Safety

value must contain valid WTF-8.

Source

pub fn from_bytes(value: Vec<u8>) -> Result<Self, Vec<u8>>

Create a WTF-8 string from a WTF-8 byte vec.

Source

pub fn from_string(string: String) -> Wtf8Buf

Creates a WTF-8 string from a UTF-8 String.

This takes ownership of the String and does not copy.

Since WTF-8 is a superset of UTF-8, this always succeeds.

Source

pub fn join<I, S>(sep: impl AsRef<Wtf8>, iter: I) -> Wtf8Buf
where I: IntoIterator<Item = S>, S: AsRef<Wtf8>,

Source

pub fn clear(&mut self)

Source

pub fn from_wide(v: &[u16]) -> Wtf8Buf

Creates a WTF-8 string from a potentially ill-formed UTF-16 slice of 16-bit code units.

This is lossless: calling .encode_wide() on the resulting string will always return the original code units.

Source

pub fn as_slice(&self) -> &Wtf8

Source

pub fn as_mut_slice(&mut self) -> &mut Wtf8

Source

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

Reserves capacity for at least additional more bytes to be inserted in the given Wtf8Buf. The collection may reserve more space to avoid frequent reallocations.

§Panics

Panics if the new capacity exceeds isize::MAX bytes.

Source

pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>

Tries to reserve capacity for at least additional more bytes to be inserted in the given Wtf8Buf. The Wtf8Buf may reserve more space to avoid frequent reallocations. After calling try_reserve, capacity will be greater than or equal to self.len() + additional. Does nothing if capacity is already sufficient. This method preserves the contents even if an error occurs.

§Errors

If the capacity overflows, or the allocator reports a failure, then an error is returned.

Source

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

Source

pub fn try_reserve_exact( &mut self, additional: usize, ) -> Result<(), TryReserveError>

Tries to reserve the minimum capacity for exactly additional more bytes to be inserted in the given Wtf8Buf. After calling try_reserve_exact, capacity will be greater than or equal to self.len() + additional if it returns Ok(()). Does nothing if the capacity is already sufficient.

Note that the allocator may give the Wtf8Buf more space than it requests. Therefore, capacity can not be relied upon to be precisely minimal. Prefer try_reserve if future insertions are expected.

§Errors

If the capacity overflows, or the allocator reports a failure, then an error is returned.

Source

pub fn shrink_to_fit(&mut self)

Source

pub fn shrink_to(&mut self, min_capacity: usize)

Source

pub fn leak<'a>(self) -> &'a mut Wtf8

Source

pub const fn capacity(&self) -> usize

Returns the number of bytes that this string buffer can hold without reallocating.

Source

pub fn push_str(&mut self, other: &str)

Append a UTF-8 slice at the end of the string.

Source

pub fn push_wtf8(&mut self, other: &Wtf8)

Append a WTF-8 slice at the end of the string.

Source

pub fn push_char(&mut self, c: char)

Append a Unicode scalar value at the end of the string.

Source

pub fn push(&mut self, code_point: CodePoint)

Append a code point at the end of the string.

Source

pub fn pop(&mut self) -> Option<CodePoint>

Source

pub fn truncate(&mut self, new_len: usize)

Shortens a string to the specified length.

§Panics

Panics if new_len > current length, or if new_len is not a code point boundary.

Source

pub fn insert(&mut self, idx: usize, c: CodePoint)

Inserts a codepoint into this Wtf8Buf at a byte position.

Source

pub fn insert_wtf8(&mut self, idx: usize, w: &Wtf8)

Inserts a WTF-8 slice into this Wtf8Buf at a byte position.

Source

pub fn into_bytes(self) -> Vec<u8>

Consumes the WTF-8 string and tries to convert it to a vec of bytes.

Source

pub fn into_string(self) -> Result<String, Wtf8Buf>

Consumes the WTF-8 string and tries to convert it to UTF-8.

This does not copy the data.

If the contents are not well-formed UTF-8 (that is, if the string contains surrogates), the original WTF-8 string is returned instead.

Source

pub fn into_string_lossy(self) -> String

Consumes the WTF-8 string and converts it lossily to UTF-8.

This does not copy the data (but may overwrite parts of it in place).

Surrogates are replaced with "\u{FFFD}" (the replacement character “�”)

Source

pub fn into_box(self) -> Box<Wtf8>

Converts this Wtf8Buf into a boxed Wtf8.

Source

pub fn from_box(boxed: Box<Wtf8>) -> Wtf8Buf

Converts a Box<Wtf8> into a Wtf8Buf.

Methods from Deref<Target = Wtf8>§

Source

pub fn len(&self) -> usize

Returns the length, in WTF-8 bytes.

Source

pub fn is_empty(&self) -> bool

Source

pub fn ascii_byte_at(&self, position: usize) -> u8

Returns the code point at position if it is in the ASCII range, or b'\xFF' otherwise.

§Panics

Panics if position is beyond the end of the string.

Source

pub fn code_points(&self) -> Wtf8CodePoints<'_>

Returns an iterator for the string’s code points.

Source

pub fn code_point_indices(&self) -> Wtf8CodePointIndices<'_>

Returns an iterator for the string’s code points and their indices.

Source

pub fn as_bytes(&self) -> &[u8]

Access raw bytes of WTF-8 data

Source

pub fn as_str(&self) -> Result<&str, Utf8Error>

Tries to convert the string to UTF-8 and return a &str slice.

Returns None if the string contains surrogates.

This does not copy the data.

Source

pub fn to_wtf8_buf(&self) -> Wtf8Buf

Creates an owned Wtf8Buf from a borrowed Wtf8.

Source

pub fn to_string_lossy(&self) -> Cow<'_, str>

Lossily converts the string to UTF-8. Returns a UTF-8 &str slice if the contents are well-formed in UTF-8.

Surrogates are replaced with "\u{FFFD}" (the replacement character “�”).

This only copies the data if necessary (if it contains any surrogate).

Source

pub fn encode_wide(&self) -> EncodeWide<'_>

Converts the WTF-8 string to potentially ill-formed UTF-16 and return an iterator of 16-bit code units.

This is lossless: calling Wtf8Buf::from_ill_formed_utf16 on the resulting code units would always return the original WTF-8 string.

Source

pub fn chunks(&self) -> Wtf8Chunks<'_>

Source

pub fn map_utf8<'a, I>( &'a self, f: impl Fn(&'a str) -> I, ) -> impl Iterator<Item = CodePoint>
where I: Iterator<Item = char>,

Source

pub fn is_code_point_boundary(&self, index: usize) -> bool

Source

pub fn into_box(&self) -> Box<Wtf8>

Boxes this Wtf8.

Source

pub fn make_ascii_lowercase(&mut self)

Source

pub fn make_ascii_uppercase(&mut self)

Source

pub fn to_ascii_lowercase(&self) -> Wtf8Buf

Source

pub fn to_ascii_uppercase(&self) -> Wtf8Buf

Source

pub fn to_lowercase(&self) -> Wtf8Buf

Source

pub fn to_uppercase(&self) -> Wtf8Buf

Source

pub fn is_ascii(&self) -> bool

Source

pub fn is_utf8(&self) -> bool

Source

pub fn eq_ignore_ascii_case(&self, other: &Self) -> bool

Source

pub fn split(&self, pat: &Wtf8) -> impl Iterator<Item = &Self>

Source

pub fn splitn(&self, n: usize, pat: &Wtf8) -> impl Iterator<Item = &Self>

Source

pub fn rsplit(&self, pat: &Wtf8) -> impl Iterator<Item = &Self>

Source

pub fn rsplitn(&self, n: usize, pat: &Wtf8) -> impl Iterator<Item = &Self>

Source

pub fn trim(&self) -> &Self

Source

pub fn trim_start(&self) -> &Self

Source

pub fn trim_end(&self) -> &Self

Source

pub fn trim_start_matches(&self, f: impl Fn(CodePoint) -> bool) -> &Self

Source

pub fn trim_end_matches(&self, f: impl Fn(CodePoint) -> bool) -> &Self

Source

pub fn trim_matches(&self, f: impl Fn(CodePoint) -> bool) -> &Self

Source

pub fn find(&self, pat: &Wtf8) -> Option<usize>

Source

pub fn rfind(&self, pat: &Wtf8) -> Option<usize>

Source

pub fn find_iter(&self, pat: &Wtf8) -> impl Iterator<Item = usize>

Source

pub fn rfind_iter(&self, pat: &Wtf8) -> impl Iterator<Item = usize>

Source

pub fn contains(&self, pat: &Wtf8) -> bool

Source

pub fn contains_code_point(&self, pat: CodePoint) -> bool

Source

pub fn get(&self, range: impl RangeBounds<usize>) -> Option<&Self>

Source

pub fn ends_with(&self, w: impl AsRef<Wtf8>) -> bool

Source

pub fn starts_with(&self, w: impl AsRef<Wtf8>) -> bool

Source

pub fn strip_prefix(&self, w: impl AsRef<Wtf8>) -> Option<&Self>

Source

pub fn strip_suffix(&self, w: impl AsRef<Wtf8>) -> Option<&Self>

Source

pub fn replace(&self, from: &Wtf8, to: &Wtf8) -> Wtf8Buf

Source

pub fn replacen(&self, from: &Wtf8, to: &Wtf8, n: usize) -> Wtf8Buf

Trait Implementations§

Source§

impl AsRef<Wtf8> for Wtf8Buf

Source§

fn as_ref(&self) -> &Wtf8

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Borrow<Wtf8> for Wtf8Buf

Source§

fn borrow(&self) -> &Wtf8

Immutably borrows from an owned value. Read more
Source§

impl Clone for Wtf8Buf

Source§

fn clone(&self) -> Wtf8Buf

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Wtf8Buf

Formats the string in double quotes, with characters escaped according to char::escape_debug and unpaired surrogates represented as \u{xxxx}, where each x is a hexadecimal digit.

For example, the code units [U+0061, U+D800, U+000A] are formatted as "a\u{D800}\n".

Source§

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

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

impl Default for Wtf8Buf

Source§

fn default() -> Wtf8Buf

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

impl Deref for Wtf8Buf

Source§

type Target = Wtf8

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Wtf8

Dereferences the value.
Source§

impl DerefMut for Wtf8Buf

Source§

fn deref_mut(&mut self) -> &mut Wtf8

Mutably dereferences the value.
Source§

impl Display for Wtf8Buf

Formats the string with unpaired surrogates substituted with the replacement character, U+FFFD.

Source§

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

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

impl Extend<CodePoint> for Wtf8Buf

Append code points from an iterator to the string.

This replaces surrogate code point pairs with supplementary code points, like concatenating ill-formed UTF-16 strings effectively would.

Source§

fn extend<T: IntoIterator<Item = CodePoint>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<W: AsRef<Wtf8>> Extend<W> for Wtf8Buf

Source§

fn extend<T: IntoIterator<Item = W>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl Extend<char> for Wtf8Buf

Source§

fn extend<T: IntoIterator<Item = char>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl From<&str> for Wtf8Buf

Source§

fn from(s: &str) -> Self

Converts to this type from the input type.
Source§

impl From<AsciiString> for Wtf8Buf

Source§

fn from(s: AsciiString) -> Self

Converts to this type from the input type.
Source§

impl From<Box<Wtf8>> for Wtf8Buf

Source§

fn from(w: Box<Wtf8>) -> Self

Converts to this type from the input type.
Source§

impl From<CodePoint> for Wtf8Buf

Source§

fn from(ch: CodePoint) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Wtf8Buf

Source§

fn from(s: String) -> Self

Converts to this type from the input type.
Source§

impl From<Wtf8Buf> for Box<Wtf8>

Source§

fn from(w: Wtf8Buf) -> Self

Converts to this type from the input type.
Source§

impl FromIterator<CodePoint> for Wtf8Buf

Creates a new WTF-8 string from an iterator of code points.

This replaces surrogate code point pairs with supplementary code points, like concatenating ill-formed UTF-16 strings effectively would.

Source§

fn from_iter<T: IntoIterator<Item = CodePoint>>(iter: T) -> Wtf8Buf

Creates a value from an iterator. Read more
Source§

impl<W: AsRef<Wtf8>> FromIterator<W> for Wtf8Buf

Source§

fn from_iter<T: IntoIterator<Item = W>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl Hash for Wtf8Buf

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Wtf8Buf

Source§

fn cmp(&self, other: &Wtf8Buf) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Wtf8Buf

Source§

fn eq(&self, other: &Wtf8Buf) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Wtf8Buf

Source§

fn partial_cmp(&self, other: &Wtf8Buf) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Write for Wtf8Buf

Source§

fn write_str(&mut self, s: &str) -> Result

Writes a string slice into this writer, returning whether the write succeeded. Read more
1.1.0 · Source§

fn write_char(&mut self, c: char) -> Result<(), Error>

Writes a char into this writer, returning whether the write succeeded. Read more
1.0.0 · Source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Glue for usage of the write! macro with implementors of this trait. Read more
Source§

impl Wtf8Concat for Wtf8Buf

Source§

fn fmt_wtf8(&self, buf: &mut Wtf8Buf)

Source§

impl Eq for Wtf8Buf

Source§

impl StructuralPartialEq for Wtf8Buf

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.