pub(crate) fn decode(input: &[u8]) -> Result<Vec<u8>, String> {
let mut out = Vec::new();
let mut pending: Option<u8> = None;
for (pos, &b) in input.iter().enumerate() {
if b == b'>' {
break;
}
if matches!(b, b'\x00' | b'\t' | b'\n' | b'\x0C' | b'\r' | b' ') {
continue;
}
let nibble = hex_nibble(b).ok_or_else(|| {
format!("ASCIIHexDecode: invalid character 0x{b:02X} at position {pos}")
})?;
match pending {
None => {
pending = Some(nibble);
}
Some(hi) => {
out.push((hi << 4) | nibble);
pending = None;
}
}
}
if let Some(hi) = pending {
out.push(hi << 4);
}
Ok(out)
}
#[inline]
fn hex_nibble(b: u8) -> Option<u8> {
match b {
b'0'..=b'9' => Some(b - b'0'),
b'A'..=b'F' => Some(b - b'A' + 10),
b'a'..=b'f' => Some(b - b'a' + 10),
_ => None,
}
}
pub(crate) fn encode(input: &[u8]) -> Vec<u8> {
let mut out = Vec::with_capacity(input.len() * 2 + 1);
for &b in input {
let hi = b >> 4;
let lo = b & 0x0F;
out.push(b"0123456789abcdef"[usize::from(hi)]);
out.push(b"0123456789abcdef"[usize::from(lo)]);
}
out.push(b'>');
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn decode_empty_input() {
assert_eq!(decode(b"").unwrap(), b"");
}
#[test]
fn decode_eod_only() {
assert_eq!(decode(b">").unwrap(), b"");
}
#[test]
fn decode_even_length() {
assert_eq!(decode(b"48656c6c6f").unwrap(), b"Hello");
}
#[test]
fn decode_odd_length_zero_pads() {
let result = decode(b"a").unwrap();
assert_eq!(result, &[0xA0]);
}
#[test]
fn decode_odd_length_full_example() {
let result = decode(b"4f6").unwrap();
assert_eq!(result, &[0x4F, 0x60]);
}
#[test]
fn decode_eod_ignores_trailing_data() {
let result = decode(b"48656c6c6f>garbage").unwrap();
assert_eq!(result, b"Hello");
}
#[test]
fn decode_whitespace_skipped() {
let result = decode(b"48 65\t6c\n6c\r6f").unwrap();
assert_eq!(result, b"Hello");
}
#[test]
fn decode_whitespace_null_skipped() {
let result = decode(b"48\x0065").unwrap();
assert_eq!(result, &[0x48, 0x65]);
}
#[test]
fn decode_whitespace_form_feed_skipped() {
let result = decode(b"48\x0C65").unwrap();
assert_eq!(result, &[0x48, 0x65]);
}
#[test]
fn decode_mixed_case() {
let result = decode(b"4F6C").unwrap();
assert_eq!(result, &[0x4F, 0x6C]);
let result2 = decode(b"4f6c").unwrap();
assert_eq!(result2, &[0x4F, 0x6C]);
assert_eq!(result, result2);
}
#[test]
fn decode_rejects_non_hex_char() {
let result = decode(b"4G");
assert!(result.is_err(), "expected error for non-hex character 'G'");
let err = result.unwrap_err();
assert!(
err.starts_with("ASCIIHexDecode:"),
"error should have ASCIIHexDecode: prefix, got: {err}"
);
}
#[test]
fn decode_rejects_invalid_byte() {
let result = decode(b"\xFF");
assert!(result.is_err());
let err = result.unwrap_err();
assert!(err.starts_with("ASCIIHexDecode:"));
}
#[test]
fn encode_empty() {
assert_eq!(encode(b""), b">");
}
#[test]
fn encode_single_byte() {
assert_eq!(encode(&[0xFF]), b"ff>");
assert_eq!(encode(&[0x00]), b"00>");
}
#[test]
fn encode_known_text() {
assert_eq!(encode(b"Hello"), b"48656c6c6f>");
}
#[test]
fn encode_lowercase_output() {
let enc = encode(&[0xAB, 0xCD, 0xEF]);
assert_eq!(enc, b"abcdef>");
}
fn round_trip(raw: &[u8]) {
let encoded = encode(raw);
let decoded = decode(&encoded).unwrap();
assert_eq!(
decoded,
raw,
"round-trip failed for input of length {}",
raw.len()
);
}
#[test]
fn round_trip_empty() {
round_trip(b"");
}
#[test]
fn round_trip_odd_length() {
round_trip(b"A");
round_trip(b"ABC");
round_trip(b"Hello");
}
#[test]
fn round_trip_even_length() {
round_trip(b"Hi");
round_trip(b"PDF!");
}
#[test]
fn round_trip_various_lengths() {
for len in [0, 1, 2, 3, 4, 5, 7, 8, 16, 100, 255] {
let data: Vec<u8> = (0u8..).take(len).collect();
round_trip(&data);
}
}
#[test]
fn round_trip_all_bytes() {
let data: Vec<u8> = (0u8..=255).collect();
round_trip(&data);
}
#[test]
fn decode_eod_with_odd_nibble_before_eod() {
let result = decode(b"4>junk").unwrap();
assert_eq!(result, &[0x40]);
}
}