diff options
| author | ltdk <usr@ltdk.xyz> | 2025-08-13 18:54:48 -0400 |
|---|---|---|
| committer | ltdk <usr@ltdk.xyz> | 2025-08-20 20:31:33 -0400 |
| commit | 7c81a067ea4cfd289d30c3903ac60b113f481c87 (patch) | |
| tree | 468625ed2a63e3f813900b5c65a650f027f9b872 /library/alloc/src | |
| parent | 2914291e09cb13aab64207f9e11f2aaf74de3904 (diff) | |
| download | rust-7c81a067ea4cfd289d30c3903ac60b113f481c87.tar.gz rust-7c81a067ea4cfd289d30c3903ac60b113f481c87.zip | |
Diff-massaging commit
Diffstat (limited to 'library/alloc/src')
| -rw-r--r-- | library/alloc/src/wtf8/mod.rs | 75 |
1 files changed, 34 insertions, 41 deletions
diff --git a/library/alloc/src/wtf8/mod.rs b/library/alloc/src/wtf8/mod.rs index 95d317a5efb..047994adc44 100644 --- a/library/alloc/src/wtf8/mod.rs +++ b/library/alloc/src/wtf8/mod.rs @@ -451,53 +451,46 @@ impl Extend<CodePoint> for Wtf8Buf { } } -// helps diff -mod wtf8 { - use super::*; - - /// Creates an owned `Wtf8Buf` from a borrowed `Wtf8`. - pub(super) fn to_owned(slice: &Wtf8) -> Wtf8Buf { - Wtf8Buf { bytes: slice.as_bytes().to_vec(), is_known_utf8: false } - } +/// Creates an owned `Wtf8Buf` from a borrowed `Wtf8`. +pub(super) fn to_owned(slice: &Wtf8) -> Wtf8Buf { + Wtf8Buf { bytes: slice.as_bytes().to_vec(), is_known_utf8: false } +} - /// 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). - pub(super) fn to_string_lossy(slice: &Wtf8) -> Cow<'_, str> { - let Some((surrogate_pos, _)) = slice.next_surrogate(0) else { - return Cow::Borrowed(unsafe { str::from_utf8_unchecked(slice.as_bytes()) }); - }; - let wtf8_bytes = slice.as_bytes(); - let mut utf8_bytes = Vec::with_capacity(slice.len()); - utf8_bytes.extend_from_slice(&wtf8_bytes[..surrogate_pos]); - utf8_bytes.extend_from_slice("\u{FFFD}".as_bytes()); - let mut pos = surrogate_pos + 3; - loop { - match slice.next_surrogate(pos) { - Some((surrogate_pos, _)) => { - utf8_bytes.extend_from_slice(&wtf8_bytes[pos..surrogate_pos]); - utf8_bytes.extend_from_slice("\u{FFFD}".as_bytes()); - pos = surrogate_pos + 3; - } - None => { - utf8_bytes.extend_from_slice(&wtf8_bytes[pos..]); - return Cow::Owned(unsafe { String::from_utf8_unchecked(utf8_bytes) }); - } +/// 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). +pub(super) fn to_string_lossy(slice: &Wtf8) -> Cow<'_, str> { + let Some((surrogate_pos, _)) = slice.next_surrogate(0) else { + return Cow::Borrowed(unsafe { str::from_utf8_unchecked(slice.as_bytes()) }); + }; + let wtf8_bytes = slice.as_bytes(); + let mut utf8_bytes = Vec::with_capacity(slice.len()); + utf8_bytes.extend_from_slice(&wtf8_bytes[..surrogate_pos]); + utf8_bytes.extend_from_slice("\u{FFFD}".as_bytes()); + let mut pos = surrogate_pos + 3; + loop { + match slice.next_surrogate(pos) { + Some((surrogate_pos, _)) => { + utf8_bytes.extend_from_slice(&wtf8_bytes[pos..surrogate_pos]); + utf8_bytes.extend_from_slice("\u{FFFD}".as_bytes()); + pos = surrogate_pos + 3; + } + None => { + utf8_bytes.extend_from_slice(&wtf8_bytes[pos..]); + return Cow::Owned(unsafe { String::from_utf8_unchecked(utf8_bytes) }); } } } - - #[inline] - pub(super) fn clone_into(slice: &Wtf8, buf: &mut Wtf8Buf) { - buf.is_known_utf8 = false; - slice.as_bytes().clone_into(&mut buf.bytes); - } } -use self::wtf8::{to_owned, to_string_lossy, clone_into}; +#[inline] +pub(super) fn clone_into(slice: &Wtf8, buf: &mut Wtf8Buf) { + buf.is_known_utf8 = false; + slice.as_bytes().clone_into(&mut buf.bytes); +} #[cfg(not(test))] impl Wtf8 { |
