use crate::DecodeError;
#[allow(clippy::cast_possible_wrap)]
#[allow(clippy::cast_possible_truncation)]
pub const N_TRAIL_VALUES: i32 = 256 - (N_EXCLUDED_CODES as i32);
const_assert_eq!(assert0; N_TRAIL_VALUES, 243);
const N_EXCLUDED_CODES: usize = 13;
const EXCLUDED_CODE_BYTES: [u8; N_EXCLUDED_CODES] = [
0x00, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
0x1A, 0x1B, 0x20,
];
#[inline]
#[allow(clippy::cast_sign_loss)]
#[allow(clippy::cast_possible_truncation)]
pub fn trail_to_byte(b: u8) -> u8 {
assert!(b < (N_TRAIL_VALUES as u8));
let v = match b {
0x00..=0x05 => b + 1, 0x06..=0x0F => b + 1 + 9, 0x10..=0x13 => b + 1 + 9 + 2, _ => b + 1 + 9 + 2 + 1, };
trace!(
"TrailingByteSelection:trail_to_byte(0x{:x}) => 0x{:x}",
b,
v
);
assert!(!EXCLUDED_CODE_BYTES.contains(&v));
v
}
#[inline]
pub fn byte_to_trail(b: u8) -> Result<u8, DecodeError> {
let v = match b {
0x01..=0x06 => Ok(b - 1),
0x10..=0x19 => Ok((b - 1) - 9),
0x1C..=0x1F => Ok(((b - 1) - 9) - 2),
0x21..=0xFF => Ok((((b - 1) - 9) - 2) - 1),
_ => Err(DecodeError::TrailByteOutOfRange(b)),
};
match v {
Err(_) => trace!("TrailingByteSelection:byte_to_trail(0x{:x}) => Err", b),
Ok(x) => trace!(
"TrailingByteSelection:byte_to_trail(0x{:x}) => 0x{:x}",
b,
x
),
}
v
}