Skip to content

Commit

Permalink
faster byte_serialized_unchanged
Browse files Browse the repository at this point in the history
  • Loading branch information
Orion Gonzalez committed Dec 31, 2024
1 parent 84cf467 commit 9d3ad4f
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions form_urlencoded/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,24 @@ pub struct ByteSerialize<'a> {
bytes: &'a [u8],
}

fn byte_serialized_unchanged(byte: u8) -> bool {
matches!(byte, b'*' | b'-' | b'.' | b'0' ..= b'9' | b'A' ..= b'Z' | b'_' | b'a' ..= b'z')
/// This is a precomputed table of which chars match and which don't.
const MAGIC: u128 = const {
let mut magic = 0_u128;
let mut c = 0;
while c < 128 {
magic |= (matches!(c, b'*' | b'-' | b'.' | b'0' ..= b'9' | b'A' ..= b'Z' | b'_' | b'a' ..= b'z')
as u128)
<< c;
c += 1;
}
magic
};

pub fn byte_serialized_unchanged(byte: u8) -> bool {
if byte > b'z' {
return false;
}
((MAGIC >> byte) & 1) == 1
}

impl<'a> Iterator for ByteSerialize<'a> {
Expand Down

0 comments on commit 9d3ad4f

Please sign in to comment.