1
Fork 0

Add fast path for displaying pre-validated Wtf8Buf

This commit is contained in:
Thalia Archibald 2025-02-05 19:37:27 -08:00
parent 4e1356b959
commit 0842f2c65c
2 changed files with 26 additions and 2 deletions

View file

@ -41,13 +41,13 @@ impl AsInner<Wtf8> for Buf {
impl fmt::Debug for Buf {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self.as_slice(), f)
fmt::Debug::fmt(&self.inner, f)
}
}
impl fmt::Display for Buf {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self.as_slice(), f)
fmt::Display::fmt(&self.inner, f)
}
}

View file

@ -169,6 +169,18 @@ impl fmt::Debug for Wtf8Buf {
}
}
/// Formats the string with unpaired surrogates substituted with the replacement
/// character, U+FFFD.
impl fmt::Display for Wtf8Buf {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(s) = self.as_known_utf8() {
fmt::Display::fmt(s, formatter)
} else {
fmt::Display::fmt(&**self, formatter)
}
}
}
impl Wtf8Buf {
/// Creates a new, empty WTF-8 string.
#[inline]
@ -262,6 +274,18 @@ impl Wtf8Buf {
unsafe { Wtf8::from_mut_bytes_unchecked(&mut self.bytes) }
}
/// Converts the string to UTF-8 without validation, if it was created from
/// valid UTF-8.
#[inline]
fn as_known_utf8(&self) -> Option<&str> {
if self.is_known_utf8 {
// SAFETY: The buffer is known to be valid UTF-8.
Some(unsafe { str::from_utf8_unchecked(self.as_bytes()) })
} else {
None
}
}
/// 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.