Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UTF-8 validation fast paths in Wtf8Buf #137154

Merged
merged 2 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions library/std/src/sys/os_str/wtf8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
26 changes: 25 additions & 1 deletion library/std/src/sys_common/wtf8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -364,7 +388,7 @@ impl Wtf8Buf {
_ => {
// If we'll be pushing a string containing a surrogate, we may
// no longer have UTF-8.
if other.next_surrogate(0).is_some() {
if self.is_known_utf8 && other.next_surrogate(0).is_some() {
self.is_known_utf8 = false;
}

Expand Down
Loading