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
}
pub fn parseHexDigit(c: char) -> i32 {
if c.is_ascii_digit() {
return (c as u8 - b'0') as i32;
}
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)]
pub fn parseHexDigit(c: char) -> Option<u32> {
c.to_digit(16)
// let Some(v) = c.to_digit(16) else {
// return -1
// };
// v as i32
// if c.is_ascii_digit() {
// return (c as u8 - b'0') as i32;
// }
// 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)]

View File

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