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
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 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.
sourcepub fn from_str(str: &str) -> Wtf8Buf
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.
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.
sourcefn push_code_point_unchecked(&mut self, code_point: CodePoint)
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.
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 overflows usize
.
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 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.
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
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.
pub fn shrink_to_fit(&mut self)
pub fn shrink_to(&mut self, min_capacity: usize)
sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of bytes that this string buffer can hold without reallocating.
sourcepub fn push_wtf8(&mut self, other: &Wtf8)
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.
sourcepub fn push(&mut self, code_point: CodePoint)
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.
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 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 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_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.
fn next_surrogate(&self, pos: usize) -> Option<(usize, u16)>
fn final_lead_surrogate(&self) -> Option<u16>
fn initial_trail_surrogate(&self) -> Option<u16>
pub fn clone_into(&self, buf: &mut Wtf8Buf)
pub fn into_arc(&self) -> Arc<Wtf8>
pub fn into_rc(&self) -> Rc<Wtf8>
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 is_ascii(&self) -> bool
pub fn eq_ignore_ascii_case(&self, other: &Self) -> bool
Trait Implementations§
source§impl Debug for Wtf8Buf
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§impl Extend<CodePoint> for Wtf8Buf
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§impl FromIterator<CodePoint> for Wtf8Buf
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 Ord for Wtf8Buf
impl Ord for Wtf8Buf
source§impl PartialEq<Wtf8Buf> for Wtf8Buf
impl PartialEq<Wtf8Buf> for Wtf8Buf
source§impl PartialOrd<Wtf8Buf> for Wtf8Buf
impl PartialOrd<Wtf8Buf> for Wtf8Buf
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more