fix: remove manual hex parsing in ResultParser

This commit is contained in:
Henry Schimke
2024-01-04 12:16:15 -06:00
parent 70d82863ef
commit 718b737877
2 changed files with 21 additions and 13 deletions

View File

@@ -226,17 +226,23 @@ pub fn unescapeBackslash(escaped: &str) -> String {
unescaped unescaped
} }
pub fn parseHexDigit(c: char) -> i32 { #[inline(always)]
if c.is_ascii_digit() { pub fn parseHexDigit(c: char) -> Option<u32> {
return (c as u8 - b'0') as i32; c.to_digit(16)
} // let Some(v) = c.to_digit(16) else {
if ('a'..='f').contains(&c) { // return -1
return 10 + (c as u8 - b'a') as i32; // };
} // v as i32
if ('A'..='F').contains(&c) { // if c.is_ascii_digit() {
return 10 + (c as u8 - b'A') as i32; // return (c as u8 - b'0') as i32;
} // }
-1 // if ('a'..='f').contains(&c) {
// return 10 + (c as u8 - b'a') as i32;
// }
// if ('A'..='F').contains(&c) {
// return 10 + (c as u8 - b'A') as i32;
// }
// -1
} }
#[inline(always)] #[inline(always)]

View File

@@ -337,8 +337,10 @@ fn decodeQuotedPrintable(value: &str, charset: &str) -> String {
let nextChar = value.chars().nth(i + 1).unwrap(); let nextChar = value.chars().nth(i + 1).unwrap();
if nextChar != '\r' && nextChar != '\n' { if nextChar != '\r' && nextChar != '\n' {
let nextNextChar = value.chars().nth(i + 2).unwrap(); let nextNextChar = value.chars().nth(i + 2).unwrap();
let firstDigit = ResultParser::parseHexDigit(nextChar); let firstDigit =
let secondDigit = ResultParser::parseHexDigit(nextNextChar); ResultParser::parseHexDigit(nextChar).map_or_else(|| -1, |d| d as i32);
let secondDigit =
ResultParser::parseHexDigit(nextNextChar).map_or_else(|| -1, |d| d as i32);
if firstDigit >= 0 && secondDigit >= 0 { if firstDigit >= 0 && secondDigit >= 0 {
fragmentBuffer.push(((firstDigit << 4) + secondDigit) as u8); fragmentBuffer.push(((firstDigit << 4) + secondDigit) as u8);
} // else ignore it, assume it was incorrectly encoded } // else ignore it, assume it was incorrectly encoded