Skip to content

Commit e2ab773

Browse files
committed
Simplify control flow with while-let
1 parent 261ef65 commit e2ab773

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

library/std/src/sys_common/wtf8.rs

+14-22
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ impl Wtf8Buf {
219219
/// Since WTF-8 is a superset of UTF-8, this always succeeds.
220220
#[inline]
221221
pub fn from_str(s: &str) -> Wtf8Buf {
222-
Wtf8Buf { bytes: <[_]>::to_vec(s.as_bytes()), is_known_utf8: true }
222+
Wtf8Buf { bytes: s.as_bytes().to_vec(), is_known_utf8: true }
223223
}
224224

225225
pub fn clear(&mut self) {
@@ -467,24 +467,17 @@ impl Wtf8Buf {
467467
///
468468
/// Surrogates are replaced with `"\u{FFFD}"` (the replacement character “�”)
469469
pub fn into_string_lossy(mut self) -> String {
470-
// Fast path: If we already have UTF-8, we can return it immediately.
471-
if self.is_known_utf8 {
472-
return unsafe { String::from_utf8_unchecked(self.bytes) };
473-
}
474-
475-
let mut pos = 0;
476-
loop {
477-
match self.next_surrogate(pos) {
478-
Some((surrogate_pos, _)) => {
479-
pos = surrogate_pos + 3;
480-
// Surrogates and the replacement character are all 3 bytes,
481-
// so they can substituted in-place.
482-
self.bytes[surrogate_pos..pos]
483-
.copy_from_slice(UTF8_REPLACEMENT_CHARACTER.as_bytes());
484-
}
485-
None => return unsafe { String::from_utf8_unchecked(self.bytes) },
470+
if !self.is_known_utf8 {
471+
let mut pos = 0;
472+
while let Some((surrogate_pos, _)) = self.next_surrogate(pos) {
473+
pos = surrogate_pos + 3;
474+
// Surrogates and the replacement character are all 3 bytes, so
475+
// they can substituted in-place.
476+
self.bytes[surrogate_pos..pos]
477+
.copy_from_slice(UTF8_REPLACEMENT_CHARACTER.as_bytes());
486478
}
487479
}
480+
unsafe { String::from_utf8_unchecked(self.bytes) }
488481
}
489482

490483
/// Converts this `Wtf8Buf` into a boxed `Wtf8`.
@@ -703,9 +696,8 @@ impl Wtf8 {
703696
///
704697
/// This only copies the data if necessary (if it contains any surrogate).
705698
pub fn to_string_lossy(&self) -> Cow<'_, str> {
706-
let surrogate_pos = match self.next_surrogate(0) {
707-
None => return Cow::Borrowed(unsafe { str::from_utf8_unchecked(&self.bytes) }),
708-
Some((pos, _)) => pos,
699+
let Some((surrogate_pos, _)) = self.next_surrogate(0) else {
700+
return Cow::Borrowed(unsafe { str::from_utf8_unchecked(&self.bytes) });
709701
};
710702
let wtf8_bytes = &self.bytes;
711703
let mut utf8_bytes = Vec::with_capacity(self.len());
@@ -995,7 +987,7 @@ pub struct Wtf8CodePoints<'a> {
995987
bytes: slice::Iter<'a, u8>,
996988
}
997989

998-
impl<'a> Iterator for Wtf8CodePoints<'a> {
990+
impl Iterator for Wtf8CodePoints<'_> {
999991
type Item = CodePoint;
1000992

1001993
#[inline]
@@ -1021,7 +1013,7 @@ pub struct EncodeWide<'a> {
10211013

10221014
// Copied from libunicode/u_str.rs
10231015
#[stable(feature = "rust1", since = "1.0.0")]
1024-
impl<'a> Iterator for EncodeWide<'a> {
1016+
impl Iterator for EncodeWide<'_> {
10251017
type Item = u16;
10261018

10271019
#[inline]

0 commit comments

Comments
 (0)