Optimize hex parser

This commit is contained in:
2021-12-16 18:46:19 +01:00
parent fb167ef899
commit 8cc2245492

View File

@@ -162,9 +162,22 @@ fn parse_packet(input: Input) -> IResult<Input, (usize, Packet)> {
}
fn convert_hex(hex: &[u8]) -> Vec<u8> {
fn val(c: u8) -> u8 {
match c {
b'A'..=b'F' => c - b'A' + 10,
b'0'..=b'9' => c - b'0',
_ => panic!("Invalid hex digit {}", c),
}
}
let mut binary = Vec::with_capacity(hex.len() / 2);
binary.extend(
hex.chunks_exact(2)
.map(|chunk| u8::from_str_radix(std::str::from_utf8(chunk).unwrap(), 16).unwrap())
.collect()
.map(|chunk| (val(chunk[0]) << 4) | val(chunk[1])),
);
binary
}
pub fn part1(input: &mut dyn Read) -> String {