pkstrings: convert HEXTOSTR to const fn

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2020-07-06 00:18:09 +02:00
parent ed9c806096
commit 889595a742
Signed by: pep
GPG key ID: DEDA74AEECA9D0F2

View file

@ -108,100 +108,96 @@ lazy_static! {
('@', 0x50),
].iter().copied().collect()
};
static ref HEXTOSTR: Vec<Option<char>> = {
let tmp = [
(0x80, 'A'),
(0x81, 'B'),
(0x82, 'C'),
(0x83, 'D'),
(0x84, 'E'),
(0x85, 'F'),
(0x86, 'G'),
(0x87, 'H'),
(0x88, 'I'),
(0x89, 'J'),
(0x8a, 'K'),
(0x8b, 'L'),
(0x8c, 'M'),
(0x8d, 'N'),
(0x8e, 'O'),
(0x8f, 'P'),
(0x90, 'Q'),
(0x91, 'R'),
(0x92, 'S'),
(0x93, 'T'),
(0x94, 'U'),
(0x95, 'V'),
(0x96, 'W'),
(0x97, 'X'),
(0x98, 'Y'),
(0x99, 'Z'),
(0x9a, '('),
(0x9b, ')'),
(0x9c, ':'),
(0x9d, ';'),
(0x9e, '['),
(0x9f, ']'),
(0xa0, 'a'),
(0xa1, 'b'),
(0xa2, 'c'),
(0xa3, 'd'),
(0xa4, 'e'),
(0xa5, 'f'),
(0xa6, 'g'),
(0xa7, 'h'),
(0xa8, 'i'),
(0xa9, 'j'),
(0xaa, 'k'),
(0xab, 'l'),
(0xac, 'm'),
(0xad, 'n'),
(0xae, 'o'),
(0xaf, 'p'),
(0xb0, 'q'),
(0xb1, 'r'),
(0xb2, 's'),
(0xb3, 't'),
(0xb4, 'u'),
(0xb5, 'v'),
(0xb6, 'w'),
(0xb7, 'x'),
(0xb8, 'y'),
(0xb9, 'z'),
(0xe0, '\''),
(0xe3, '-'),
(0xe6, '?'),
(0xe7, '!'),
(0xe8, '.'),
(0xec, '▷'),
(0xed, '▶'),
(0xee, '▼'),
(0xef, '♂'),
(0xf1, '×'),
(0xf2, '.'),
(0xf3, '/'),
(0xf4, ','),
(0xf5, '♀'),
(0xf6, '0'),
(0xf7, '1'),
(0xf8, '2'),
(0xf9, '3'),
(0xfa, '4'),
(0xfb, '5'),
(0xfc, '6'),
(0xfd, '7'),
(0xfe, '8'),
(0xff, '9'),
(0x7f, ' '),
(0x50, '@'),
];
}
let mut vec: Vec<Option<char>> = [None; 256].to_vec();
for (k, v) in tmp.iter() {
vec[*k] = Some(*v);
}
vec
};
const fn hextostr(hex: u8) -> Option<char> {
Some(match hex {
0x80 => 'A',
0x81 => 'B',
0x82 => 'C',
0x83 => 'D',
0x84 => 'E',
0x85 => 'F',
0x86 => 'G',
0x87 => 'H',
0x88 => 'I',
0x89 => 'J',
0x8a => 'K',
0x8b => 'L',
0x8c => 'M',
0x8d => 'N',
0x8e => 'O',
0x8f => 'P',
0x90 => 'Q',
0x91 => 'R',
0x92 => 'S',
0x93 => 'T',
0x94 => 'U',
0x95 => 'V',
0x96 => 'W',
0x97 => 'X',
0x98 => 'Y',
0x99 => 'Z',
0x9a => '(',
0x9b => ')',
0x9c => ':',
0x9d => ';',
0x9e => '[',
0x9f => ']',
0xa0 => 'a',
0xa1 => 'b',
0xa2 => 'c',
0xa3 => 'd',
0xa4 => 'e',
0xa5 => 'f',
0xa6 => 'g',
0xa7 => 'h',
0xa8 => 'i',
0xa9 => 'j',
0xaa => 'k',
0xab => 'l',
0xac => 'm',
0xad => 'n',
0xae => 'o',
0xaf => 'p',
0xb0 => 'q',
0xb1 => 'r',
0xb2 => 's',
0xb3 => 't',
0xb4 => 'u',
0xb5 => 'v',
0xb6 => 'w',
0xb7 => 'x',
0xb8 => 'y',
0xb9 => 'z',
0xe0 => '\'',
0xe3 => '-',
0xe6 => '?',
0xe7 => '!',
0xe8 => '.',
0xec => '▷',
0xed => '▶',
0xee => '▼',
0xef => '♂',
0xf1 => '×',
0xf2 => '.',
0xf3 => '/',
0xf4 => ',',
0xf5 => '♀',
0xf6 => '0',
0xf7 => '1',
0xf8 => '2',
0xf9 => '3',
0xfa => '4',
0xfb => '5',
0xfc => '6',
0xfd => '7',
0xfe => '8',
0xff => '9',
0x7f => ' ',
0x50 => '@',
_ => return None,
})
}
#[derive(Debug, Eq, PartialEq)]
@ -234,8 +230,7 @@ impl TryFrom<&[u8]> for PKString {
let placeholder = '_';
for ord in data {
let ord = *ord as usize;
if let Some(chr) = HEXTOSTR[ord] {
if let Some(chr) = hextostr(*ord) {
buf.push(chr.clone());
} else {
buf.push(placeholder);