refactor: use to_digit instead of custom HEX_MAP

This commit is contained in:
Lukas Lihotzki 2023-04-03 14:54:04 +02:00
parent 451256fb6c
commit 70761fb37a
2 changed files with 5 additions and 29 deletions

View file

@ -135,23 +135,3 @@ define_tokens_128! {
XOAUTH,
XOAUTH2,
}
/*
* Adapted from Daniel Lemire's source:
* https://github.com/lemire/Code-used-on-Daniel-Lemire-s-blog/blob/master/2019/04/17/hexparse.cpp
*
*/
pub(crate) static HEX_MAP: &[i8] = &[
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 10,
11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
];

View file

@ -583,19 +583,15 @@ impl<'x, 'y> Rfc5321Parser<'x, 'y> {
return Ok(value.into_string());
}
b'+' => {
let mut hex1 = 0;
let mut hex1 = None;
while let Some(&ch) = self.bytes.next() {
if ch.is_ascii_hexdigit() {
if hex1 != 0 {
let hex1 = HEX_MAP[hex1 as usize];
let hex2 = HEX_MAP[ch as usize];
if hex1 != -1 && hex2 != -1 {
value.push(((hex1 as u8) << 4) | hex2 as u8);
}
if let Some(digit) = char::from(ch).to_digit(16) {
if let Some(hex1) = hex1 {
value.push(((hex1 as u8) << 4) | digit as u8);
break;
} else {
hex1 = ch;
hex1 = Some(digit);
}
} else if ch == LF {
self.stop_char = b'\n';