Struct Wtf8Buf
pub struct Wtf8Buf { pub(in ::wtf8) bytes: Vec<u8>, pub(in ::wtf8) is_known_utf8: bool }
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: boolDo we know that
bytesholds a valid UTF-8 encoding? We can easily know this if we're constructed from aStringor&str.It is possible for
bytesto 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
impl Wtf8Buf
fn new() -> Wtf8BufCreates a new, empty WTF-8 string.
fn with_capacity(capacity: usize) -> Wtf8BufCreates a new, empty WTF-8 string with pre-allocated capacity for
capacitybytes.unsafe fn from_bytes_unchecked(value: Vec<u8>) -> Wtf8BufCreates a WTF-8 string from a WTF-8 byte vec.
Safety
valuemust contain well-formed WTF-8.const fn from_string(string: String) -> Wtf8BufCreates a WTF-8 string from a UTF-8
String.This takes ownership of the
Stringand does not copy.Since WTF-8 is a superset of UTF-8, this always succeeds.
fn from_str(s: &str) -> Wtf8BufCreates a WTF-8 string from a UTF-8
&strslice.This copies the content of the slice.
Since WTF-8 is a superset of UTF-8, this always succeeds.
fn clear(&mut self)fn from_wide(v: &[u16]) -> Wtf8BufCreates 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.unsafe fn push_code_point_unchecked(&mut self, code_point: CodePoint)Appends the given
charto the end of this string. This does not include the WTF-8 concatenation check oris_known_utf8check. Copied from String::push.Safety
selfmust contain well-formed WTF-8, and appendingcode_pointmust preserve that invariant. In particular,code_pointmust not be a trailing surrogate ifselfends with a leading surrogate.If
self.is_known_utf8is true,code_pointmust not be a surrogate.fn as_slice(&self) -> &Wtf8fn as_mut_slice(&mut self) -> &mut Wtf8fn as_known_utf8(&self) -> Option<&str>Converts the string to UTF-8 without validation, if it was created from valid UTF-8.
fn reserve(&mut self, additional: usize)Reserves capacity for at least
additionalmore bytes to be inserted in the givenWtf8Buf. The collection may reserve more space to avoid frequent reallocations.Panics
Panics if the new capacity exceeds
isize::MAXbytes.fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>Tries to reserve capacity for at least
additionalmore bytes to be inserted in the givenWtf8Buf. TheWtf8Bufmay reserve more space to avoid frequent reallocations. After callingtry_reserve, capacity will be greater than or equal toself.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.
fn reserve_exact(&mut self, additional: usize)fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError>Tries to reserve the minimum capacity for exactly
additionalmore bytes to be inserted in the givenWtf8Buf. After callingtry_reserve_exact, capacity will be greater than or equal toself.len() + additionalif it returnsOk(()). Does nothing if the capacity is already sufficient.Note that the allocator may give the
Wtf8Bufmore space than it requests. Therefore, capacity can not be relied upon to be precisely minimal. Prefertry_reserveif future insertions are expected.Errors
If the capacity overflows, or the allocator reports a failure, then an error is returned.
fn shrink_to_fit(&mut self)fn shrink_to(&mut self, min_capacity: usize)fn leak<'a>(self) -> &'a mut Wtf8fn capacity(&self) -> usizeReturns the number of bytes that this string buffer can hold without reallocating.
fn push_str(&mut self, other: &str)Append a UTF-8 slice at the end of the string.
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.
fn push_char(&mut self, c: char)Append a Unicode scalar value at the end of the string.
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.
fn truncate(&mut self, new_len: usize)Shortens a string to the specified length.
If
new_lenis greater than the string's current length, this has no effect.Panics
Panics if
new_lendoes not lie on a code point boundary.fn into_bytes(self) -> Vec<u8>Consumes the WTF-8 string and tries to convert it to a vec of bytes.
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.
fn into_string_lossy(self) -> StringConsumes 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 “�”)fn into_box(self) -> Box<Wtf8>Converts this
Wtf8Bufinto a boxedWtf8.fn from_box(boxed: Box<Wtf8>) -> Wtf8BufConverts a
Box<Wtf8>into aWtf8Buf.unsafe fn extend_from_slice_unchecked(&mut self, other: &[u8])Provides plumbing to core
Vec::extend_from_slice. More well behaving alternative to allowing outer types full mutable access to the coreVec.Safety
selfandothermust contain well-formed WTF-8, and appendingothertoselfmust preserve that invariant. In particular,selfmust not end with a leading surrogate, orothermust not start with a trailing surrogate.
Trait Implementations
impl Clone for Wtf8Buf
fn clone(&self) -> Wtf8Buf
impl Debug for Wtf8Buf
fn fmt(&self, formatter: &mut Formatter<'_>) -> Result
impl Deref for Wtf8Buf
type Target = Wtf8;fn deref(&self) -> &Wtf8
impl DerefMut for Wtf8Buf
fn deref_mut(&mut self) -> &mut Wtf8
impl Display for Wtf8Buf
fn fmt(&self, formatter: &mut Formatter<'_>) -> Result
impl Eq for Wtf8Buf
fn assert_fields_are_eq(&self)
impl Extend<CodePoint> for Wtf8Buf
fn extend<T: IntoIterator<Item = CodePoint>>(&mut self, iter: T)fn extend_one(&mut self, code_point: CodePoint)fn extend_reserve(&mut self, additional: usize)
impl FromIterator<CodePoint> for Wtf8Buf
fn from_iter<T: IntoIterator<Item = CodePoint>>(iter: T) -> Wtf8Buf
impl Hash for Wtf8Buf
fn hash<H: Hasher>(&self, state: &mut H)
impl Ord for Wtf8Buf
fn cmp(&self, other: &Wtf8Buf) -> Ordering
impl PartialEq for Wtf8Buf
fn eq(&self, other: &Wtf8Buf) -> bool
impl PartialOrd for Wtf8Buf
fn partial_cmp(&self, other: &Wtf8Buf) -> Option<Ordering>