Skip to content

Commit

Permalink
Merge pull request #1178 from iex-rs/tiny-bit-faster-hex
Browse files Browse the repository at this point in the history
Optimize Unicode decoding by 1% 🤡
  • Loading branch information
dtolnay authored Aug 19, 2024
2 parents 6130f9b + 9ffb43a commit 50c4328
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1055,15 +1055,17 @@ static HEX0: [i16; 256] = build_hex_table(0);
static HEX1: [i16; 256] = build_hex_table(4);

fn decode_four_hex_digits(a: u8, b: u8, c: u8, d: u8) -> Option<u16> {
let a = HEX1[a as usize];
let b = HEX0[b as usize];
let c = HEX1[c as usize];
let d = HEX0[d as usize];
let a = HEX1[a as usize] as i32;
let b = HEX0[b as usize] as i32;
let c = HEX1[c as usize] as i32;
let d = HEX0[d as usize] as i32;

let codepoint = ((a | b) << 8) | c | d;

// A single sign bit check.
if (a | b | c | d) < 0 {
return None;
if codepoint >= 0 {
Some(codepoint as u16)
} else {
None
}

Some((((a | b) << 8) | c | d) as u16)
}

0 comments on commit 50c4328

Please sign in to comment.