Skip to content

Commit b65a455

Browse files
authored
Merge pull request servo#814 from klensy/percent_encode_byte-fast
percent_encoding: faster percent_encode_byte
2 parents 6ef3876 + 12d6abf commit b65a455

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

percent_encoding/src/lib.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,9 @@ pub const NON_ALPHANUMERIC: &AsciiSet = &CONTROLS
180180
/// assert_eq!("foo bar".bytes().map(percent_encode_byte).collect::<String>(),
181181
/// "%66%6F%6F%20%62%61%72");
182182
/// ```
183+
#[inline]
183184
pub fn percent_encode_byte(byte: u8) -> &'static str {
184-
let index = usize::from(byte) * 3;
185-
&"\
185+
static ENC_TABLE: &[u8; 768] = b"\
186186
%00%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F\
187187
%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F\
188188
%20%21%22%23%24%25%26%27%28%29%2A%2B%2C%2D%2E%2F\
@@ -199,7 +199,12 @@ pub fn percent_encode_byte(byte: u8) -> &'static str {
199199
%D0%D1%D2%D3%D4%D5%D6%D7%D8%D9%DA%DB%DC%DD%DE%DF\
200200
%E0%E1%E2%E3%E4%E5%E6%E7%E8%E9%EA%EB%EC%ED%EE%EF\
201201
%F0%F1%F2%F3%F4%F5%F6%F7%F8%F9%FA%FB%FC%FD%FE%FF\
202-
"[index..index + 3]
202+
";
203+
204+
let index = usize::from(byte) * 3;
205+
// SAFETY: ENC_TABLE is ascii-only, so any subset if it should be
206+
// ascii-only too, which is valid utf8.
207+
unsafe { str::from_utf8_unchecked(&ENC_TABLE[index..index + 3]) }
203208
}
204209

205210
/// Percent-encode the given bytes with the given set.

0 commit comments

Comments
 (0)