Skip to content

Commit c98773f

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

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

form_urlencoded/src/lib.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,25 @@ 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 MAGIC: u128 = const {
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+
144+
#[inline]
145+
pub fn byte_serialized_unchanged(byte: u8) -> bool {
146+
if byte > b'z' {
147+
return false;
148+
}
149+
((MAGIC >> byte) & 1) == 1
133150
}
134151

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

0 commit comments

Comments
 (0)