Skip to content

Commit 9554b31

Browse files
thaliaarchigitbot
authored and
gitbot
committed
Simplify control flow with while-let
1 parent 2472a00 commit 9554b31

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

std/src/sys_common/wtf8.rs

+14-22
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ impl Wtf8Buf {
208208
/// Since WTF-8 is a superset of UTF-8, this always succeeds.
209209
#[inline]
210210
pub fn from_str(s: &str) -> Wtf8Buf {
211-
Wtf8Buf { bytes: <[_]>::to_vec(s.as_bytes()), is_known_utf8: true }
211+
Wtf8Buf { bytes: s.as_bytes().to_vec(), is_known_utf8: true }
212212
}
213213

214214
pub fn clear(&mut self) {
@@ -444,24 +444,17 @@ impl Wtf8Buf {
444444
///
445445
/// Surrogates are replaced with `"\u{FFFD}"` (the replacement character “�”)
446446
pub fn into_string_lossy(mut self) -> String {
447-
// Fast path: If we already have UTF-8, we can return it immediately.
448-
if self.is_known_utf8 {
449-
return unsafe { String::from_utf8_unchecked(self.bytes) };
450-
}
451-
452-
let mut pos = 0;
453-
loop {
454-
match self.next_surrogate(pos) {
455-
Some((surrogate_pos, _)) => {
456-
pos = surrogate_pos + 3;
457-
// Surrogates and the replacement character are all 3 bytes,
458-
// so they can substituted in-place.
459-
self.bytes[surrogate_pos..pos]
460-
.copy_from_slice(UTF8_REPLACEMENT_CHARACTER.as_bytes());
461-
}
462-
None => return unsafe { String::from_utf8_unchecked(self.bytes) },
447+
if !self.is_known_utf8 {
448+
let mut pos = 0;
449+
while let Some((surrogate_pos, _)) = self.next_surrogate(pos) {
450+
pos = surrogate_pos + 3;
451+
// Surrogates and the replacement character are all 3 bytes, so
452+
// they can substituted in-place.
453+
self.bytes[surrogate_pos..pos]
454+
.copy_from_slice(UTF8_REPLACEMENT_CHARACTER.as_bytes());
463455
}
464456
}
457+
unsafe { String::from_utf8_unchecked(self.bytes) }
465458
}
466459

467460
/// Converts this `Wtf8Buf` into a boxed `Wtf8`.
@@ -680,9 +673,8 @@ impl Wtf8 {
680673
///
681674
/// This only copies the data if necessary (if it contains any surrogate).
682675
pub fn to_string_lossy(&self) -> Cow<'_, str> {
683-
let surrogate_pos = match self.next_surrogate(0) {
684-
None => return Cow::Borrowed(unsafe { str::from_utf8_unchecked(&self.bytes) }),
685-
Some((pos, _)) => pos,
676+
let Some((surrogate_pos, _)) = self.next_surrogate(0) else {
677+
return Cow::Borrowed(unsafe { str::from_utf8_unchecked(&self.bytes) });
686678
};
687679
let wtf8_bytes = &self.bytes;
688680
let mut utf8_bytes = Vec::with_capacity(self.len());
@@ -972,7 +964,7 @@ pub struct Wtf8CodePoints<'a> {
972964
bytes: slice::Iter<'a, u8>,
973965
}
974966

975-
impl<'a> Iterator for Wtf8CodePoints<'a> {
967+
impl Iterator for Wtf8CodePoints<'_> {
976968
type Item = CodePoint;
977969

978970
#[inline]
@@ -998,7 +990,7 @@ pub struct EncodeWide<'a> {
998990

999991
// Copied from libunicode/u_str.rs
1000992
#[stable(feature = "rust1", since = "1.0.0")]
1001-
impl<'a> Iterator for EncodeWide<'a> {
993+
impl Iterator for EncodeWide<'_> {
1002994
type Item = u16;
1003995

1004996
#[inline]

0 commit comments

Comments
 (0)