Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
.position(|&byte| self.ascii_set.should_percent_encode(byte))
.unwrap_or(remaining.len()),
);
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
Loading