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: 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

impl Wtf8Buf

fn new() -> Wtf8Buf

Creates a new, empty WTF-8 string.

fn with_capacity(capacity: usize) -> Wtf8Buf

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

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

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

Safety

value must contain well-formed WTF-8.

const 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.

fn from_str(s: &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.

fn clear(&mut self)
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.

unsafe fn push_code_point_unchecked(&mut self, code_point: CodePoint)

Appends the given char to the end of this string. This does not include the WTF-8 concatenation check or is_known_utf8 check. Copied from String::push.

Safety

self must contain well-formed WTF-8, and appending code_point must preserve that invariant. In particular, code_point must not be a trailing surrogate if self ends with a leading surrogate.

If self.is_known_utf8 is true, code_point must not be a surrogate.

fn as_slice(&self) -> &Wtf8
fn as_mut_slice(&mut self) -> &mut Wtf8
fn 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 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.

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.

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

fn shrink_to_fit(&mut self)
fn shrink_to(&mut self, min_capacity: usize)
fn leak<'a>(self) -> &'a mut Wtf8
fn capacity(&self) -> usize

Returns 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_len is greater than the string's current length, this has no effect.

Panics

Panics if new_len does 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) -> 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 “�”)

fn into_box(self) -> Box<Wtf8>

Converts this Wtf8Buf into a boxed Wtf8.

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

Converts a Box<Wtf8> into a Wtf8Buf.

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 core Vec.

Safety

self and other must contain well-formed WTF-8, and appending other to self must preserve that invariant. In particular, self must not end with a leading surrogate, or other must 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>

impl StructuralPartialEq for Wtf8Buf