From 889595a742dd5b36cd2d2aaba75bf741f1db83c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Mon, 6 Jul 2020 00:18:09 +0200 Subject: [PATCH] pkstrings: convert HEXTOSTR to const fn MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- pkstrings/src/lib.rs | 185 +++++++++++++++++++++---------------------- 1 file changed, 90 insertions(+), 95 deletions(-) diff --git a/pkstrings/src/lib.rs b/pkstrings/src/lib.rs index cc59b8b..4cc196f 100644 --- a/pkstrings/src/lib.rs +++ b/pkstrings/src/lib.rs @@ -108,100 +108,96 @@ lazy_static! { ('@', 0x50), ].iter().copied().collect() }; - static ref HEXTOSTR: Vec> = { - 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> = [None; 256].to_vec(); - for (k, v) in tmp.iter() { - vec[*k] = Some(*v); - } - vec - }; +const fn hextostr(hex: u8) -> Option { + 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);