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
impl Wtf8Buf
Sourcepub fn with_capacity(capacity: usize) -> Wtf8Buf
pub fn with_capacity(capacity: usize) -> Wtf8Buf
Creates a new, empty WTF-8 string with pre-allocated capacity for capacity bytes.
Sourcepub const unsafe fn from_bytes_unchecked(value: Vec<u8>) -> Wtf8Buf
pub const unsafe fn from_bytes_unchecked(value: Vec<u8>) -> Wtf8Buf
Sourcepub fn from_bytes(value: Vec<u8>) -> Result<Self, Vec<u8>>
pub fn from_bytes(value: Vec<u8>) -> Result<Self, Vec<u8>>
Create a WTF-8 string from a WTF-8 byte vec.
Sourcepub fn from_string(string: String) -> Wtf8Buf
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.
pub fn join<I, S>(sep: impl AsRef<Wtf8>, iter: I) -> Wtf8Buf
pub fn clear(&mut self)
Sourcepub fn from_wide(v: &[u16]) -> Wtf8Buf
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.
pub fn as_slice(&self) -> &Wtf8
pub fn as_mut_slice(&mut self) -> &mut Wtf8
Sourcepub fn reserve(&mut self, additional: usize)
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.
Sourcepub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>
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.
pub fn reserve_exact(&mut self, additional: usize)
Sourcepub fn try_reserve_exact(
&mut self,
additional: usize,
) -> Result<(), TryReserveError>
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.
pub fn shrink_to_fit(&mut self)
pub fn shrink_to(&mut self, min_capacity: usize)
pub fn leak<'a>(self) -> &'a mut Wtf8
Sourcepub const fn capacity(&self) -> usize
pub const fn capacity(&self) -> usize
Returns the number of bytes that this string buffer can hold without reallocating.
pub fn pop(&mut self) -> Option<CodePoint>
Sourcepub fn truncate(&mut self, new_len: usize)
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.
Sourcepub fn insert(&mut self, idx: usize, c: CodePoint)
pub fn insert(&mut self, idx: usize, c: CodePoint)
Inserts a codepoint into this Wtf8Buf at a byte position.
Sourcepub fn insert_wtf8(&mut self, idx: usize, w: &Wtf8)
pub fn insert_wtf8(&mut self, idx: usize, w: &Wtf8)
Inserts a WTF-8 slice into this Wtf8Buf at a byte position.
Sourcepub fn into_bytes(self) -> Vec<u8> ⓘ
pub fn into_bytes(self) -> Vec<u8> ⓘ
Consumes the WTF-8 string and tries to convert it to a vec of bytes.
Sourcepub fn into_string(self) -> Result<String, Wtf8Buf>
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.
Sourcepub fn into_string_lossy(self) -> String
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 “�”)
Methods from Deref<Target = Wtf8>§
pub fn is_empty(&self) -> bool
Sourcepub fn ascii_byte_at(&self, position: usize) -> u8
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.
Sourcepub fn code_points(&self) -> Wtf8CodePoints<'_> ⓘ
pub fn code_points(&self) -> Wtf8CodePoints<'_> ⓘ
Returns an iterator for the string’s code points.
Sourcepub fn code_point_indices(&self) -> Wtf8CodePointIndices<'_> ⓘ
pub fn code_point_indices(&self) -> Wtf8CodePointIndices<'_> ⓘ
Returns an iterator for the string’s code points and their indices.
Sourcepub fn as_str(&self) -> Result<&str, Utf8Error>
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.
Sourcepub fn to_wtf8_buf(&self) -> Wtf8Buf
pub fn to_wtf8_buf(&self) -> Wtf8Buf
Creates an owned Wtf8Buf from a borrowed Wtf8.
Sourcepub fn to_string_lossy(&self) -> Cow<'_, str>
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).
Sourcepub fn encode_wide(&self) -> EncodeWide<'_> ⓘ
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.
pub fn chunks(&self) -> Wtf8Chunks<'_> ⓘ
pub fn map_utf8<'a, I>( &'a self, f: impl Fn(&'a str) -> I, ) -> impl Iterator<Item = CodePoint>
pub fn is_code_point_boundary(&self, index: usize) -> bool
pub fn make_ascii_lowercase(&mut self)
pub fn make_ascii_uppercase(&mut self)
pub fn to_ascii_lowercase(&self) -> Wtf8Buf
pub fn to_ascii_uppercase(&self) -> Wtf8Buf
pub fn to_lowercase(&self) -> Wtf8Buf
pub fn to_uppercase(&self) -> Wtf8Buf
pub fn is_ascii(&self) -> bool
pub fn is_utf8(&self) -> bool
pub fn eq_ignore_ascii_case(&self, other: &Self) -> bool
pub fn split(&self, pat: &Wtf8) -> impl Iterator<Item = &Self>
pub fn splitn(&self, n: usize, pat: &Wtf8) -> impl Iterator<Item = &Self>
pub fn rsplit(&self, pat: &Wtf8) -> impl Iterator<Item = &Self>
pub fn rsplitn(&self, n: usize, pat: &Wtf8) -> impl Iterator<Item = &Self>
pub fn trim(&self) -> &Self
pub fn trim_start(&self) -> &Self
pub fn trim_end(&self) -> &Self
pub fn trim_start_matches(&self, f: impl Fn(CodePoint) -> bool) -> &Self
pub fn trim_end_matches(&self, f: impl Fn(CodePoint) -> bool) -> &Self
pub fn trim_matches(&self, f: impl Fn(CodePoint) -> bool) -> &Self
pub fn find(&self, pat: &Wtf8) -> Option<usize>
pub fn rfind(&self, pat: &Wtf8) -> Option<usize>
pub fn find_iter(&self, pat: &Wtf8) -> impl Iterator<Item = usize>
pub fn rfind_iter(&self, pat: &Wtf8) -> impl Iterator<Item = usize>
pub fn contains(&self, pat: &Wtf8) -> bool
pub fn contains_code_point(&self, pat: CodePoint) -> bool
pub fn get(&self, range: impl RangeBounds<usize>) -> Option<&Self>
pub fn ends_with(&self, w: impl AsRef<Wtf8>) -> bool
pub fn starts_with(&self, w: impl AsRef<Wtf8>) -> bool
pub fn strip_prefix(&self, w: impl AsRef<Wtf8>) -> Option<&Self>
pub fn strip_suffix(&self, w: impl AsRef<Wtf8>) -> Option<&Self>
pub fn replace(&self, from: &Wtf8, to: &Wtf8) -> Wtf8Buf
pub fn replacen(&self, from: &Wtf8, to: &Wtf8, n: usize) -> Wtf8Buf
Trait Implementations§
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.
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§impl Display for Wtf8Buf
Formats the string with unpaired surrogates substituted with the replacement
character, U+FFFD.
impl Display for Wtf8Buf
Formats the string with unpaired surrogates substituted with the replacement character, U+FFFD.
Source§impl Extend<CodePoint> for Wtf8Buf
Append code points from an iterator to the string.
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)
fn extend<T: IntoIterator<Item = CodePoint>>(&mut self, iter: T)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<W: AsRef<Wtf8>> Extend<W> for Wtf8Buf
impl<W: AsRef<Wtf8>> Extend<W> for Wtf8Buf
Source§fn extend<T: IntoIterator<Item = W>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = W>>(&mut self, iter: T)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl Extend<char> for Wtf8Buf
impl Extend<char> for Wtf8Buf
Source§fn extend<T: IntoIterator<Item = char>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = char>>(&mut self, iter: T)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl From<AsciiString> for Wtf8Buf
impl From<AsciiString> for Wtf8Buf
Source§fn from(s: AsciiString) -> Self
fn from(s: AsciiString) -> Self
Source§impl FromIterator<CodePoint> for Wtf8Buf
Creates a new WTF-8 string from an iterator of code points.
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§impl<W: AsRef<Wtf8>> FromIterator<W> for Wtf8Buf
impl<W: AsRef<Wtf8>> FromIterator<W> for Wtf8Buf
Source§fn from_iter<T: IntoIterator<Item = W>>(iter: T) -> Self
fn from_iter<T: IntoIterator<Item = W>>(iter: T) -> Self
Source§impl Ord for Wtf8Buf
impl Ord for Wtf8Buf
Source§impl PartialOrd for Wtf8Buf
impl PartialOrd for Wtf8Buf
Source§impl Write for Wtf8Buf
impl Write for Wtf8Buf
impl Eq for Wtf8Buf
impl StructuralPartialEq for Wtf8Buf
Auto Trait Implementations§
impl Freeze for Wtf8Buf
impl RefUnwindSafe for Wtf8Buf
impl Send for Wtf8Buf
impl Sync for Wtf8Buf
impl Unpin for Wtf8Buf
impl UnsafeUnpin for Wtf8Buf
impl UnwindSafe for Wtf8Buf
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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