#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
#[non_exhaustive]
pub enum ShortNameCharset {
#[default]
Verbatim,
Cp437,
Cp850,
Cp852,
Cp865,
Cp866,
Custom(&'static [char; 128]),
}
impl ShortNameCharset {
#[must_use]
pub const fn table(self) -> Option<&'static [char; 128]> {
match self {
ShortNameCharset::Verbatim => None,
ShortNameCharset::Cp437 => Some(&CP437),
ShortNameCharset::Cp850 => Some(&CP850),
ShortNameCharset::Cp852 => Some(&CP852),
ShortNameCharset::Cp865 => Some(&CP865),
ShortNameCharset::Cp866 => Some(&CP866),
ShortNameCharset::Custom(table) => Some(table),
}
}
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
ShortNameCharset::Verbatim => "verbatim",
ShortNameCharset::Cp437 => "cp437",
ShortNameCharset::Cp850 => "cp850",
ShortNameCharset::Cp852 => "cp852",
ShortNameCharset::Cp865 => "cp865",
ShortNameCharset::Cp866 => "cp866",
ShortNameCharset::Custom(_) => "custom",
}
}
#[must_use]
pub fn decode(self, name: &[u8]) -> Vec<u8> {
let Some(table) = self.table() else {
return name.to_vec();
};
let mut out = Vec::with_capacity(name.len());
let mut buf = [0u8; 4];
for &byte in name {
if byte < 0x80 {
out.push(byte);
} else {
out.extend_from_slice(table[byte as usize - 0x80].encode_utf8(&mut buf).as_bytes());
}
}
out
}
}
#[rustfmt::skip]
const CP437: [char; 128] = [
'Ç', 'ü', 'é', 'â', 'ä', 'à', 'å', 'ç', 'ê', 'ë', 'è', 'ï', 'î', 'ì', 'Ä', 'Å',
'É', 'æ', 'Æ', 'ô', 'ö', 'ò', 'û', 'ù', 'ÿ', 'Ö', 'Ü', '¢', '£', '¥', '₧', 'ƒ',
'á', 'í', 'ó', 'ú', 'ñ', 'Ñ', 'ª', 'º', '¿', '⌐', '¬', '½', '¼', '¡', '«', '»',
'░', '▒', '▓', '│', '┤', '╡', '╢', '╖', '╕', '╣', '║', '╗', '╝', '╜', '╛', '┐',
'└', '┴', '┬', '├', '─', '┼', '╞', '╟', '╚', '╔', '╩', '╦', '╠', '═', '╬', '╧',
'╨', '╤', '╥', '╙', '╘', '╒', '╓', '╫', '╪', '┘', '┌', '█', '▄', '▌', '▐', '▀',
'α', 'ß', 'Γ', 'π', 'Σ', 'σ', 'µ', 'τ', 'Φ', 'Θ', 'Ω', 'δ', '∞', 'φ', 'ε', '∩',
'≡', '±', '≥', '≤', '⌠', '⌡', '÷', '≈', '°', '∙', '·', '√', 'ⁿ', '²', '■', '\u{A0}',
];
#[rustfmt::skip]
const CP850: [char; 128] = [
'Ç', 'ü', 'é', 'â', 'ä', 'à', 'å', 'ç', 'ê', 'ë', 'è', 'ï', 'î', 'ì', 'Ä', 'Å',
'É', 'æ', 'Æ', 'ô', 'ö', 'ò', 'û', 'ù', 'ÿ', 'Ö', 'Ü', 'ø', '£', 'Ø', '×', 'ƒ',
'á', 'í', 'ó', 'ú', 'ñ', 'Ñ', 'ª', 'º', '¿', '®', '¬', '½', '¼', '¡', '«', '»',
'░', '▒', '▓', '│', '┤', 'Á', 'Â', 'À', '©', '╣', '║', '╗', '╝', '¢', '¥', '┐',
'└', '┴', '┬', '├', '─', '┼', 'ã', 'Ã', '╚', '╔', '╩', '╦', '╠', '═', '╬', '¤',
'ð', 'Ð', 'Ê', 'Ë', 'È', 'ı', 'Í', 'Î', 'Ï', '┘', '┌', '█', '▄', '¦', 'Ì', '▀',
'Ó', 'ß', 'Ô', 'Ò', 'õ', 'Õ', 'µ', 'þ', 'Þ', 'Ú', 'Û', 'Ù', 'ý', 'Ý', '¯', '´',
'\u{AD}', '±', '‗', '¾', '¶', '§', '÷', '¸', '°', '¨', '·', '¹', '³', '²', '■', '\u{A0}',
];
#[rustfmt::skip]
const CP852: [char; 128] = [
'Ç', 'ü', 'é', 'â', 'ä', 'ů', 'ć', 'ç', 'ł', 'ë', 'Ő', 'ő', 'î', 'Ź', 'Ä', 'Ć',
'É', 'Ĺ', 'ĺ', 'ô', 'ö', 'Ľ', 'ľ', 'Ś', 'ś', 'Ö', 'Ü', 'Ť', 'ť', 'Ł', '×', 'č',
'á', 'í', 'ó', 'ú', 'Ą', 'ą', 'Ž', 'ž', 'Ę', 'ę', '¬', 'ź', 'Č', 'ş', '«', '»',
'░', '▒', '▓', '│', '┤', 'Á', 'Â', 'Ě', 'Ş', '╣', '║', '╗', '╝', 'Ż', 'ż', '┐',
'└', '┴', '┬', '├', '─', '┼', 'Ă', 'ă', '╚', '╔', '╩', '╦', '╠', '═', '╬', '¤',
'đ', 'Đ', 'Ď', 'Ë', 'ď', 'Ň', 'Í', 'Î', 'ě', '┘', '┌', '█', '▄', 'Ţ', 'Ů', '▀',
'Ó', 'ß', 'Ô', 'Ń', 'ń', 'ň', 'Š', 'š', 'Ŕ', 'Ú', 'ŕ', 'Ű', 'ý', 'Ý', 'ţ', '´',
'\u{AD}', '˝', '˛', 'ˇ', '˘', '§', '÷', '¸', '°', '¨', '˙', 'ű', 'Ř', 'ř', '■', '\u{A0}',
];
const CP865: [char; 128] = {
let mut table = CP437;
table[0x9B - 0x80] = 'ø';
table[0x9D - 0x80] = 'Ø';
table[0xAF - 0x80] = '¤';
table
};
const CP866: [char; 128] = {
let mut table = ['\0'; 128];
let mut i = 0;
while i < 32 {
table[i] = match char::from_u32(0x0410 + i as u32) {
Some(c) => c,
None => '\u{FFFD}',
};
i += 1;
}
while i < 48 {
table[i] = match char::from_u32(0x0430 + (i - 32) as u32) {
Some(c) => c,
None => '\u{FFFD}',
};
i += 1;
}
while i < 96 {
table[i] = CP437[i];
i += 1;
}
while i < 112 {
table[i] = match char::from_u32(0x0440 + (i - 96) as u32) {
Some(c) => c,
None => '\u{FFFD}',
};
i += 1;
}
let tail = [
'Ё', 'ё', 'Є', 'є', 'Ї', 'ї', 'Ў', 'ў', '°', '∙', '·', '√', '№', '¤', '■', '\u{A0}',
];
let mut j = 0;
while j < 16 {
table[112 + j] = tail[j];
j += 1;
}
table
};
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn verbatim_interprets_nothing() {
let charset = ShortNameCharset::default();
assert_eq!(charset, ShortNameCharset::Verbatim);
assert!(charset.table().is_none());
for name in [&b"README TXT"[..], b"CAF\x82 TXT", b"\xFF\xFE\x00"] {
assert_eq!(charset.decode(name), name);
}
}
#[test]
fn every_table_is_a_page_and_ascii_is_untouched() {
for charset in [
ShortNameCharset::Cp437,
ShortNameCharset::Cp850,
ShortNameCharset::Cp852,
ShortNameCharset::Cp865,
ShortNameCharset::Cp866,
] {
let table = charset.table().expect("a named page has a table");
assert!(
table.iter().all(|&c| c != '\0'),
"{}: a table position was left unfilled",
charset.as_str()
);
assert_eq!(charset.decode(b"README TXT"), b"README TXT");
}
}
#[test]
fn code_page_437_is_where_its_landmarks_are() {
let t = ShortNameCharset::Cp437.table().unwrap();
assert_eq!(t[0x80 - 0x80], 'Ç', "the first accented character");
assert_eq!(t[0x9F - 0x80], 'ƒ', "the last before the second run");
assert_eq!(t[0xA0 - 0x80], 'á');
assert_eq!(t[0xB0 - 0x80], '░', "the box-drawing block begins");
assert_eq!(t[0xDF - 0x80], '▀', "and ends");
assert_eq!(t[0xE0 - 0x80], 'α', "the Greek run begins");
assert_eq!(t[0xE1 - 0x80], 'ß', "which is not all Greek");
assert_eq!(t[0xFF - 0x80], '\u{A0}', "a no-break space, not a space");
assert_eq!(t[0x9E - 0x80], '\u{20A7}');
}
#[test]
fn code_page_865_differs_from_437_in_exactly_three_positions() {
let a = ShortNameCharset::Cp437.table().unwrap();
let b = ShortNameCharset::Cp865.table().unwrap();
let differ: Vec<usize> = (0..128).filter(|&i| a[i] != b[i]).collect();
assert_eq!(differ, vec![0x9B - 0x80, 0x9D - 0x80, 0xAF - 0x80]);
assert_eq!(b[0x9B - 0x80], 'ø');
assert_eq!(b[0x9D - 0x80], 'Ø');
assert_eq!(b[0xAF - 0x80], '¤');
}
#[test]
fn code_page_866_holds_the_cyrillic_alphabet_in_order() {
let t = ShortNameCharset::Cp866.table().unwrap();
let upper: String = (0x80..0xB0).map(|b| t[b - 0x80]).collect();
assert_eq!(upper, "АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмноп");
let lower: String = (0xE0..0xF0).map(|b| t[b - 0x80]).collect();
assert_eq!(lower, "рстуфхцчшщъыьэюя");
assert_eq!(t[0xB0 - 0x80], '░', "the box block is 437's");
assert_eq!(t[0xDF - 0x80], '▀');
assert_eq!(t[0xF0 - 0x80], 'Ё', "the tail follows no run");
assert_eq!(t[0xFC - 0x80], '№');
}
#[test]
fn the_latin_pages_agree_where_they_agree_and_differ_where_they_differ() {
let (a, b, c) = (
ShortNameCharset::Cp437.table().unwrap(),
ShortNameCharset::Cp850.table().unwrap(),
ShortNameCharset::Cp852.table().unwrap(),
);
for i in 0..5 {
assert_eq!(a[i], b[i]);
assert_eq!(a[i], c[i]);
}
assert_eq!(a[0x85 - 0x80], 'à');
assert_eq!(b[0x85 - 0x80], 'à');
assert_eq!(c[0x85 - 0x80], 'ů');
assert_eq!(b[0xE1 - 0x80], 'ß');
assert_eq!(b[0xD0 - 0x80], 'ð');
assert_eq!(a[0xD0 - 0x80], '╨');
}
#[test]
fn a_name_decodes_to_the_utf8_of_its_page() {
let name = b"CAF\x82 TXT";
assert_eq!(
ShortNameCharset::Cp437.decode(name),
"CAFé TXT".as_bytes()
);
assert_eq!(
ShortNameCharset::Cp850.decode(name),
"CAFé TXT".as_bytes()
);
assert_eq!(
ShortNameCharset::Cp866.decode(name),
"CAFВ TXT".as_bytes()
);
assert_eq!(ShortNameCharset::Cp437.decode(b"\x9E"), "₧".as_bytes());
assert_eq!(ShortNameCharset::Cp437.decode(b"\x9E").len(), 3);
}
#[test]
fn a_custom_page_is_reached_without_a_release() {
const LATIN1: [char; 128] = {
let mut table = ['\0'; 128];
let mut i = 0;
while i < 128 {
table[i] = (0x80 + i as u32) as u8 as char;
i += 1;
}
table
};
let charset = ShortNameCharset::Custom(&LATIN1);
assert_eq!(charset.as_str(), "custom");
assert_eq!(charset.decode(&[b'A', 0xE9]), "Aé".as_bytes());
let copy = charset;
assert_eq!(copy, charset);
}
}