pub const ALPHABET_N: &[u8; 85] =
b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?`_~()[]{}@%$#";
pub const RSET_ASCII: [u8; 13] = [
b' ', b'"', b'\'', b',', b';', b'\\', b'|', b'<', b'>', b'&', b'\t', b'\n', b'\r', ];
pub const RSET_LEN: usize = 13;
pub const NUM_PROFILES: usize = 8;
pub const PROFILES: [[u8; RSET_LEN]; NUM_PROFILES] = [
*b"~^?%@+`$#!*.-",
*b"~^+[]`?@!%#*(",
*b"^~$#?%!`@[]+_",
*b"~+?%@!^[]:`()",
*b"~%^`+?!$@(){}",
*b"^~?@!+%*$()_#",
*b"^~@%?$+!#[]=*",
*b"^$~@?!%`[]:}{",
];
const fn index_table(chars: &[u8]) -> [i8; 256] {
let mut table = [-1i8; 256];
let mut i = 0usize;
while i < chars.len() {
table[chars[i] as usize] = i as i8;
i += 1;
}
table
}
pub const ALPHABET_VALUE: [i8; 256] = index_table(ALPHABET_N);
pub const RSET_INDEX: [i8; 256] = index_table(&RSET_ASCII);
pub const RANK_PACKED: [u64; 256] = {
let mut table = [NOT_REPRESENTABLE; 256];
let mut b = 0usize;
while b < 256 {
if ALPHABET_VALUE[b] >= 0 || RSET_INDEX[b] >= 0 {
let mut packed = 0u64;
let mut p = 0usize;
while p < NUM_PROFILES {
let mut rank = RANK_ABSENT_LANE;
let mut r = 0usize;
while r < RSET_LEN {
if PROFILES[p][r] as usize == b {
rank = r as u64;
}
r += 1;
}
packed |= rank << (8 * p);
p += 1;
}
table[b] = packed;
}
b += 1;
}
table
};
pub const RANK_ABSENT_LANE: u64 = RSET_LEN as u64;
pub const NOT_REPRESENTABLE: u64 = u64::MAX;
pub const RANK_ABSENT_ALL: u64 = RANK_ABSENT_LANE * 0x0101_0101_0101_0101;
pub const DP_CLASS: [u8; 256] = {
let mut table = [DP_STOP; 256];
let mut slot = 0u8;
let mut b = 0usize;
while b < 256 {
if RSET_INDEX[b] >= 0 {
table[b] = DP_RSET_BASE + RSET_INDEX[b] as u8;
} else if ALPHABET_VALUE[b] >= 0 {
let mut is_donor = false;
let mut p = 0usize;
while p < NUM_PROFILES {
let mut r = 0usize;
while r < RSET_LEN {
if PROFILES[p][r] as usize == b {
is_donor = true;
}
r += 1;
}
p += 1;
}
if is_donor {
table[b] = DP_DONOR_BASE + slot;
slot += 1;
} else {
table[b] = DP_PLAIN;
}
}
b += 1;
}
table
};
pub const DP_PLAIN: u8 = 0;
pub const DP_RSET_BASE: u8 = 1;
pub const DP_DONOR_BASE: u8 = 1 + RSET_LEN as u8;
pub const DP_STOP: u8 = 63;
pub const IS_REPRESENTABLE: [u8; 256] = {
let mut table = [0u8; 256];
let mut b = 0usize;
while b < 256 {
if ALPHABET_VALUE[b] >= 0 || RSET_INDEX[b] >= 0 {
table[b] = 1;
}
b += 1;
}
table
};
#[cfg(not(feature = "simd"))]
pub const IDENTITY_ASCII: [u8; 128] = {
let mut table = [0u8; 128];
let mut b = 0usize;
while b < 128 {
table[b] = b as u8;
b += 1;
}
table
};
pub const DEC_INVALID: u16 = 0x8000;
pub const DEC_BASE: [u16; 256] = {
let mut table = [0u16; 256];
let mut c = 0usize;
while c < 256 {
table[c] = if ALPHABET_VALUE[c] < 0 {
DEC_INVALID | c as u16
} else {
c as u16
};
c += 1;
}
table
};
#[inline]
pub fn donors(profile: usize, mask: u16) -> ([(u8, u8); RSET_LEN], usize) {
let mut pairs = [(0u8, 0u8); RSET_LEN];
let mut rank = 0usize;
for (j, &rset) in RSET_ASCII.iter().enumerate() {
if mask & (1 << j) != 0 {
pairs[rank] = (rset, PROFILES[profile][rank]);
rank += 1;
}
}
(pairs, rank)
}
pub fn char_to_value(c: char) -> Option<u8> {
if !c.is_ascii() {
return None;
}
match ALPHABET_VALUE[c as usize] {
-1 => None,
v => Some(v as u8),
}
}
#[cfg(test)]
pub fn value_to_char(v: u8) -> char {
ALPHABET_N[v as usize] as char
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn section_12_1_structural() {
assert_eq!(ALPHABET_N.len(), 85);
for (i, &c) in ALPHABET_N.iter().enumerate() {
assert!(
!ALPHABET_N[..i].contains(&c),
"Alphabet-N repeats {:?}",
c as char
);
}
assert_eq!(RSET_ASCII.len(), RSET_LEN);
for (i, &r) in RSET_ASCII.iter().enumerate() {
assert!(!RSET_ASCII[..i].contains(&r), "the R-Set repeats {:?}", r as char);
assert!(!ALPHABET_N.contains(&r), "{:?} is in both sets", r as char);
}
}
#[test]
fn representable_bytes_lie_in_the_range_the_word_gate_tests() {
let representable: Vec<u8> = (0..=255u8).filter(|&b| IS_REPRESENTABLE[b as usize] != 0).collect();
assert_eq!(representable.len(), 98);
assert_eq!(*representable.iter().min().unwrap(), 0x09);
assert_eq!(*representable.iter().max().unwrap(), 0x7E);
for &b in &representable {
assert!((0x09..=0x7E).contains(&b), "{b:#04x} is outside the gate's range");
}
}
#[test]
fn representable_bytes_are_ascii() {
for b in 0..=255u8 {
if IS_REPRESENTABLE[b as usize] != 0 {
assert!(b < 128, "{b:#04x} is representable and not ASCII");
assert_eq!(
b & 0x7f,
b,
"{b:#04x} would be translated as a different byte"
);
}
}
#[cfg(not(feature = "simd"))]
assert_eq!(IDENTITY_ASCII.len(), 128);
}
#[test]
fn no_ignorable_whitespace_is_an_alphabet_n_character() {
for &ws in b" \t\n\r" {
assert!(
!ALPHABET_N.contains(&ws),
"{ws:#04x} is whitespace the decoder ignores and a character it would read"
);
}
assert_eq!(*ALPHABET_N.iter().min().unwrap(), 0x21);
}
}