servo / rust-url

URL parser for Rust
https://docs.rs/url/
Apache License 2.0
1.31k stars 325 forks source link

API To Determine if String Needs Encoding #784

Open tustvold opened 2 years ago

tustvold commented 2 years ago

Background

Sometimes you wish to take a string and verify that it has been correctly escaped. Particularly if % is in the escaped character list, you can't just percent-encode the string to be safe, as this will change the string.

This is exactly the case on https://github.com/apache/arrow-rs/pull/2356 where I wanted an API to check if a given byte is "legal".

Proposed Solution

AsciiSet::should_percent_encode would be perfect, however, it is private. I therefore hacked around this by doing

byte.is_ascii() || percent_encode(&[c], <AsciiSet>).next().unwrap().len() != 1

But it would be nicer if the method on AsciiSet were exposed.