Skip to content

Commit

Permalink
simplify Iterator::next for PercentEncode
Browse files Browse the repository at this point in the history
This reduces duplication including of an unsafe block...

Signed-off-by: Marijn Schouten <[email protected]>
  • Loading branch information
hkBst committed Dec 29, 2024
1 parent 84cf467 commit 17e0264
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions percent_encoding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,15 @@ impl<'a> Iterator for PercentEncode<'a> {
self.bytes = remaining;
Some(percent_encode_byte(first_byte))
} else {
// The unsafe blocks here are appropriate because the bytes are
// confirmed as a subset of UTF-8 in should_percent_encode.
for (i, &byte) in remaining.iter().enumerate() {
if self.ascii_set.should_percent_encode(byte) {
// 1 for first_byte + i for previous iterations of this loop
let (unchanged_slice, remaining) = self.bytes.split_at(1 + i);
self.bytes = remaining;
return Some(unsafe { str::from_utf8_unchecked(unchanged_slice) });
}
}
let unchanged_slice = self.bytes;
self.bytes = &[][..];
let (unchanged_slice, remaining) = self.bytes.split_at(
// 1 for the first byte + rest in remaining
1 + remaining
.iter()

Check warning on line 159 in percent_encoding/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

percent_encoding/src/lib.rs#L159

Added line #L159 was not covered by tests
.position(|&byte| self.ascii_set.should_percent_encode(byte))
.unwrap_or(remaining.len()),

Check warning on line 161 in percent_encoding/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

percent_encoding/src/lib.rs#L161

Added line #L161 was not covered by tests
);
self.bytes = remaining;
// SAFETY: bytes are confirmed as a subset of UTF-8 in should_percent_encode.
Some(unsafe { str::from_utf8_unchecked(unchanged_slice) })
}
} else {
Expand Down

0 comments on commit 17e0264

Please sign in to comment.