use super::profile::CharacterSet;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Shift {
Letters,
Figures,
}
pub(crate) const NUL: u8 = 0x00;
pub(crate) const LF: u8 = 0x02;
pub(crate) const SPACE: u8 = 0x04;
pub(crate) const CR: u8 = 0x08;
pub(crate) const FIGS: u8 = 0x1b;
pub(crate) const LTRS: u8 = 0x1f;
const LETTERS: [char; 32] = [
'\0', 'E', '\n', 'A', ' ', 'S', 'I', 'U', '\r', 'D', 'R', 'J', 'N', 'F', 'C', 'K', 'T', 'Z',
'L', 'W', 'H', 'Y', 'P', 'Q', 'O', 'B', 'G', '\0', 'M', 'X', 'V', '\0',
];
const FIGURES_COMMON: [char; 32] = [
'\0', '3', '\n', '-', ' ', '\0', '8', '7', '\r', '\0', '4', '\0', ',', '\0', ':', '(', '5',
'\0', ')', '2', '\0', '6', '0', '1', '9', '?', '\0', '\0', '.', '/', '\0', '\0',
];
const US_TTY_FIGURES: [(u8, char); 7] = [
(0x09, '$'),
(0x0b, '\''),
(0x0d, '!'),
(0x11, '"'),
(0x14, '#'),
(0x1a, '&'),
(0x1e, ';'),
];
const ITA2_FIGURES: [(u8, char); 3] = [(0x05, '\''), (0x11, '+'), (0x1e, '=')];
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Decoded {
Char(char),
ShiftTo(Shift),
Nothing,
}
pub(crate) fn decode(code: u8, shift: Shift, set: CharacterSet) -> Decoded {
let code = code & 0x1f;
match code {
LTRS => return Decoded::ShiftTo(Shift::Letters),
FIGS => return Decoded::ShiftTo(Shift::Figures),
NUL => return Decoded::Nothing,
_ => {}
}
let ch = match shift {
Shift::Letters => LETTERS[usize::from(code)],
Shift::Figures => figure(code, set),
};
if ch == '\0' {
Decoded::Nothing
} else {
Decoded::Char(ch)
}
}
fn figure(code: u8, set: CharacterSet) -> char {
let common = FIGURES_COMMON[usize::from(code)];
if common != '\0' {
return common;
}
let table: &[(u8, char)] = match set {
CharacterSet::UsTty => &US_TTY_FIGURES,
CharacterSet::Ita2 => &ITA2_FIGURES,
};
table
.iter()
.find_map(|(value, ch)| (*value == code).then_some(*ch))
.unwrap_or('\0')
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct Encoded {
pub(crate) code: u8,
pub(crate) shift: Option<Shift>,
}
pub(crate) fn encode(ch: char, set: CharacterSet) -> Option<Encoded> {
let ch = ch.to_ascii_uppercase();
match ch {
' ' => {
return Some(Encoded {
code: SPACE,
shift: None,
});
}
'\r' => {
return Some(Encoded {
code: CR,
shift: None,
});
}
'\n' => {
return Some(Encoded {
code: LF,
shift: None,
});
}
c if c.is_control() => return None,
_ => {}
}
if let Some(index) = LETTERS.iter().position(|candidate| *candidate == ch) {
return Some(Encoded {
code: index as u8,
shift: Some(Shift::Letters),
});
}
(0u8..32)
.find(|code| *code != LF && *code != SPACE && *code != CR && figure(*code, set) == ch)
.map(|code| Encoded {
code,
shift: Some(Shift::Figures),
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_encodable_character_round_trips_in_both_sets() {
for set in [CharacterSet::UsTty, CharacterSet::Ita2] {
for ch in (' '..='~').chain(['\r', '\n']) {
let Some(encoded) = encode(ch, set) else {
continue;
};
let shift = encoded.shift.unwrap_or(Shift::Letters);
assert_eq!(
decode(encoded.code, shift, set),
Decoded::Char(ch.to_ascii_uppercase()),
"{ch:?} in {set:?}"
);
}
}
}
#[test]
fn digits_and_amateur_punctuation_are_available() {
for ch in "0123456789-/?.,".chars() {
assert!(encode(ch, CharacterSet::UsTty).is_some(), "{ch}");
assert!(encode(ch, CharacterSet::Ita2).is_some(), "{ch}");
}
assert!(encode('%', CharacterSet::UsTty).is_none());
}
#[test]
fn shift_codes_are_reported() {
assert_eq!(
decode(LTRS, Shift::Figures, CharacterSet::UsTty),
Decoded::ShiftTo(Shift::Letters)
);
assert_eq!(
decode(FIGS, Shift::Letters, CharacterSet::UsTty),
Decoded::ShiftTo(Shift::Figures)
);
}
}