Struct std::sys_common::wtf8::Wtf8Buf

source ·
pub struct Wtf8Buf {
    bytes: Vec<u8>,
    is_known_utf8: bool,
}
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.

Fields§

§bytes: Vec<u8>§is_known_utf8: bool

Do we know that bytes holds a valid UTF-8 encoding? We can easily know this if we’re constructed from a String or &str.

It is possible for bytes to have valid UTF-8 without this being set, such as when we’re concatenating &Wtf8’s and surrogates become paired, as we don’t bother to rescan the entire string.

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 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 from_str(str: &str) -> Wtf8Buf

Creates a WTF-8 string from a UTF-8 &str slice.

This copies the content of the slice.

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

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

fn push_code_point_unchecked(&mut self, code_point: CodePoint)

Copied from String::push This does not include the WTF-8 concatenation check or is_known_utf8 check.

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 overflows usize.

source

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

Tries to reserve capacity for at least additional more length units 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 length units 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 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.

This replaces newly paired surrogates at the boundary with a supplementary code point, like concatenating ill-formed UTF-16 strings effectively would.

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.

This replaces newly paired surrogates at the boundary with a supplementary code point, like concatenating ill-formed UTF-16 strings effectively would.

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 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 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_owned(&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

fn next_surrogate(&self, pos: usize) -> Option<(usize, u16)>

source

fn final_lead_surrogate(&self) -> Option<u16>

source

fn initial_trail_surrogate(&self) -> Option<u16>

source

pub fn clone_into(&self, buf: &mut Wtf8Buf)

source

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

Boxes this Wtf8.

source

pub fn into_arc(&self) -> Arc<Wtf8>

source

pub fn into_rc(&self) -> Rc<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 is_ascii(&self) -> bool

source

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

Trait Implementations§

source§

impl Clone for Wtf8Buf

source§

fn clone(&self) -> Wtf8Buf

Returns a copy 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

Format the string with double quotes, and surrogates as \u followed by four hexadecimal digits. Example: "a\u{D800}" for a string with code points [U+0061, U+D800]

source§

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

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

impl Deref for Wtf8Buf

§

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 Eq for Wtf8Buf

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, code_point: CodePoint)

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

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one #72631)
Reserves capacity in a collection for the given number of additional elements. Read more
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 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) -> Selfwhere Self: Sized,

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

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

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

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

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

impl PartialEq<Wtf8Buf> for Wtf8Buf

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd<Wtf8Buf> 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

This method 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

This method 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

This method 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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl StructuralEq for Wtf8Buf

source§

impl StructuralPartialEq for Wtf8Buf

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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 Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.