pub fn hexits(bytes: &[u8]) -> String {
let mut out = String::with_capacity(bytes.len() * 2);
for b in bytes {
out.push(char::from_digit(u32::from(b >> 4), 16).expect("a nibble is a hexit"));
out.push(char::from_digit(u32::from(b & 0xf), 16).expect("a nibble is a hexit"));
}
out.to_ascii_uppercase()
}
pub fn literal(bytes: &[u8]) -> String {
format!("X'{}'", hexits(bytes))
}
pub fn from_hexits(text: &str) -> Option<Vec<u8>> {
let mut nibbles: Vec<u8> = Vec::with_capacity(text.len());
for c in text.chars() {
if c.is_ascii_whitespace() {
continue;
}
nibbles.push(c.to_digit(16)? as u8);
}
if !nibbles.len().is_multiple_of(2) {
return None;
}
Some(nibbles.chunks(2).map(|p| (p[0] << 4) | p[1]).collect())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_byte_string_writes_two_hexits_a_byte() {
assert_eq!(literal(&[0x00, 0xab, 0x00]), "X'00AB00'");
assert_eq!(literal(&[]), "X''");
}
#[test]
fn the_spelling_reads_back_as_what_was_written() {
for case in [&[][..], &[0][..], &[0xde, 0xad, 0xbe, 0xef][..]] {
assert_eq!(from_hexits(&hexits(case)).as_deref(), Some(case));
}
}
#[test]
fn either_case_of_hexit_reads_and_space_is_dropped() {
assert_eq!(from_hexits("00ab00"), Some(vec![0x00, 0xab, 0x00]));
assert_eq!(from_hexits("00 AB 00"), Some(vec![0x00, 0xab, 0x00]));
}
#[test]
fn half_a_byte_and_a_digit_that_is_not_a_hexit_are_not_byte_strings() {
assert_eq!(from_hexits("abc"), None);
assert_eq!(from_hexits("zz"), None);
}
}