Skip to content

Commit 17e0264

Browse files
committed
simplify Iterator::next for PercentEncode
This reduces duplication including of an unsafe block... Signed-off-by: Marijn Schouten <[email protected]>
1 parent 84cf467 commit 17e0264

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

percent_encoding/src/lib.rs

+9-12
Original file line numberDiff line numberDiff line change
@@ -153,18 +153,15 @@ impl<'a> Iterator for PercentEncode<'a> {
153153
self.bytes = remaining;
154154
Some(percent_encode_byte(first_byte))
155155
} else {
156-
// The unsafe blocks here are appropriate because the bytes are
157-
// confirmed as a subset of UTF-8 in should_percent_encode.
158-
for (i, &byte) in remaining.iter().enumerate() {
159-
if self.ascii_set.should_percent_encode(byte) {
160-
// 1 for first_byte + i for previous iterations of this loop
161-
let (unchanged_slice, remaining) = self.bytes.split_at(1 + i);
162-
self.bytes = remaining;
163-
return Some(unsafe { str::from_utf8_unchecked(unchanged_slice) });
164-
}
165-
}
166-
let unchanged_slice = self.bytes;
167-
self.bytes = &[][..];
156+
let (unchanged_slice, remaining) = self.bytes.split_at(
157+
// 1 for the first byte + rest in remaining
158+
1 + remaining
159+
.iter()
160+
.position(|&byte| self.ascii_set.should_percent_encode(byte))
161+
.unwrap_or(remaining.len()),
162+
);
163+
self.bytes = remaining;
164+
// SAFETY: bytes are confirmed as a subset of UTF-8 in should_percent_encode.
168165
Some(unsafe { str::from_utf8_unchecked(unchanged_slice) })
169166
}
170167
} else {

0 commit comments

Comments
 (0)