From cb93e2fae0500067dd5d49cb7927dfc95cb2518b 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 01:20:47 +0200 Subject: [PATCH] pkstrings: Use ranges to reduce match pattern sizes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Thanks ekleog! Signed-off-by: Maxime “pep” Buquet --- pkstrings/src/lib.rs | 130 ++----------------------------------------- 1 file changed, 6 insertions(+), 124 deletions(-) diff --git a/pkstrings/src/lib.rs b/pkstrings/src/lib.rs index 2cec5d3..eb1baef 100644 --- a/pkstrings/src/lib.rs +++ b/pkstrings/src/lib.rs @@ -19,64 +19,14 @@ use std::ops::Deref; const fn strtohex(chr: &char) -> Option { Some(match chr { - 'A' => 0x80, - 'B' => 0x81, - 'C' => 0x82, - 'D' => 0x83, - 'E' => 0x84, - 'F' => 0x85, - 'G' => 0x86, - 'H' => 0x87, - 'I' => 0x88, - 'J' => 0x89, - 'K' => 0x8a, - 'L' => 0x8b, - 'M' => 0x8c, - 'N' => 0x8d, - 'O' => 0x8e, - 'P' => 0x8f, - 'Q' => 0x90, - 'R' => 0x91, - 'S' => 0x92, - 'T' => 0x93, - 'U' => 0x94, - 'V' => 0x95, - 'W' => 0x96, - 'X' => 0x97, - 'Y' => 0x98, - 'Z' => 0x99, + cap @ 'A'..='Z' => 0x80 - ('A' as u8) + (*cap as u8), '(' => 0x9a, ')' => 0x9b, ':' => 0x9c, ';' => 0x9d, '[' => 0x9e, ']' => 0x9f, - 'a' => 0xa0, - 'b' => 0xa1, - 'c' => 0xa2, - 'd' => 0xa3, - 'e' => 0xa4, - 'f' => 0xa5, - 'g' => 0xa6, - 'h' => 0xa7, - 'i' => 0xa8, - 'j' => 0xa9, - 'k' => 0xaa, - 'l' => 0xab, - 'm' => 0xac, - 'n' => 0xad, - 'o' => 0xae, - 'p' => 0xaf, - 'q' => 0xb0, - 'r' => 0xb1, - 's' => 0xb2, - 't' => 0xb3, - 'u' => 0xb4, - 'v' => 0xb5, - 'w' => 0xb6, - 'x' => 0xb7, - 'y' => 0xb8, - 'z' => 0xb9, + low @ 'a'..='z' => 0xa0 - ('a' as u8) + (*low as u8), '\'' => 0xe0, '-' => 0xe3, '?' => 0xe6, @@ -98,16 +48,7 @@ const fn strtohex(chr: &char) -> Option { '/' => 0xf3, ',' => 0xf4, '♀' => 0xf5, - '0' => 0xf6, - '1' => 0xf7, - '2' => 0xf8, - '3' => 0xf9, - '4' => 0xfa, - '5' => 0xfb, - '6' => 0xfc, - '7' => 0xfd, - '8' => 0xfe, - '9' => 0xff, + num @ '0'..='9' => 0xf6 - ('0' as u8) + (*num as u8), ' ' => 0x7f, '@' => 0x50, _ => return None, @@ -116,64 +57,14 @@ const fn strtohex(chr: &char) -> Option { 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', + cap @ 0x80..=0x99 => (('A' as u8) + cap - 0x80) as char, 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', + low @ 0xa0..=0xb9 => (('a' as u8) + low - 0xa0) as char, 0xe0 => '\'', 0xe3 => '-', 0xe6 => '?', @@ -188,16 +79,7 @@ const fn hextostr(hex: u8) -> Option { 0xf3 => '/', 0xf4 => ',', 0xf5 => '♀', - 0xf6 => '0', - 0xf7 => '1', - 0xf8 => '2', - 0xf9 => '3', - 0xfa => '4', - 0xfb => '5', - 0xfc => '6', - 0xfd => '7', - 0xfe => '8', - 0xff => '9', + num @ 0xf6..=0xff => (('0' as u8) + num - 0xf6) as char, 0x7f => ' ', 0x50 => '@', _ => return None,