Skip to content

Commit 749f80e

Browse files
author
Orion Gonzalez
committed
faster byte_serialized_unchanged
1 parent 84cf467 commit 749f80e

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

form_urlencoded/src/lib.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,26 @@ pub struct ByteSerialize<'a> {
128128
bytes: &'a [u8],
129129
}
130130

131-
fn byte_serialized_unchanged(byte: u8) -> bool {
132-
matches!(byte, b'*' | b'-' | b'.' | b'0' ..= b'9' | b'A' ..= b'Z' | b'_' | b'a' ..= b'z')
131+
/// This is a precomputed table of which chars match and which don't.
132+
const fn glyphless_mask() -> u128 {
133+
let mut magic = 0_u128;
134+
let mut c = 0;
135+
while c < 128 {
136+
magic |= (matches!(c, b'*' | b'-' | b'.' | b'0' ..= b'9' | b'A' ..= b'Z' | b'_' | b'a' ..= b'z')
137+
as u128)
138+
<< c;
139+
c += 1;
140+
}
141+
magic
142+
}
143+
const GLYPHLESS_MASK: u128 = glyphless_mask();
144+
145+
#[inline]
146+
pub fn byte_serialized_unchanged(byte: u8) -> bool {
147+
if byte > b'z' {
148+
return false;
149+
}
150+
((GLYPHLESS_MASK >> byte) & 1) == 1
133151
}
134152

135153
impl<'a> Iterator for ByteSerialize<'a> {

0 commit comments

Comments
 (0)