From 2f337fe43d4da5be30cd4ab6ec085dc7256b20a8 Mon Sep 17 00:00:00 2001 From: codemasher Date: Wed, 1 Apr 2020 00:51:46 +0200 Subject: [PATCH] :shower: fewer function calls for numeric and alphanum table checks --- src/Data/AlphaNum.php | 9 ++++----- src/Data/Number.php | 2 +- src/Data/QRDataInterface.php | 16 +++++++++------- src/QRCode.php | 2 +- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/Data/AlphaNum.php b/src/Data/AlphaNum.php index 9cfde177b..6c06305f8 100644 --- a/src/Data/AlphaNum.php +++ b/src/Data/AlphaNum.php @@ -14,7 +14,7 @@ namespace chillerlan\QRCode\Data; use chillerlan\QRCode\QRCode; -use function array_search, ord, sprintf; +use function ord, sprintf; /** * Alphanumeric mode: 0 to 9, A to Z, space, $ % * + - . / : @@ -44,13 +44,12 @@ final class AlphaNum extends QRDataAbstract{ * @throws \chillerlan\QRCode\Data\QRCodeDataException */ protected function getCharCode(string $chr):int{ - $i = array_search($chr, $this::CHAR_MAP_ALPHANUM); - if($i !== false){ - return $i; + if(!isset($this::CHAR_MAP_ALPHANUM[$chr])){ + throw new QRCodeDataException(sprintf('illegal char: "%s" [%d]', $chr, ord($chr))); } - throw new QRCodeDataException(sprintf('illegal char: "%s" [%d]', $chr, ord($chr))); + return $this::CHAR_MAP_ALPHANUM[$chr]; } } diff --git a/src/Data/Number.php b/src/Data/Number.php index ab03a70cd..fa1107050 100644 --- a/src/Data/Number.php +++ b/src/Data/Number.php @@ -59,7 +59,7 @@ final class Number extends QRDataAbstract{ for($i = 0; $i < $len; $i++){ $c = ord($string[$i]); - if(!in_array($string[$i], $this::CHAR_MAP_NUMBER, true)){ + if(!isset($this::CHAR_MAP_NUMBER[$string[$i]])){ throw new QRCodeDataException(sprintf('illegal char: "%s" [%d]', $string[$i], $c)); } diff --git a/src/Data/QRDataInterface.php b/src/Data/QRDataInterface.php index 9d0f40d13..8f99fbcdb 100644 --- a/src/Data/QRDataInterface.php +++ b/src/Data/QRDataInterface.php @@ -17,18 +17,20 @@ namespace chillerlan\QRCode\Data; */ interface QRDataInterface{ - const CHAR_MAP_NUMBER = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; + const CHAR_MAP_NUMBER = [ + '0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, + ]; /** * ISO/IEC 18004:2000 Table 5 */ const CHAR_MAP_ALPHANUM = [ - '0', '1', '2', '3', '4', '5', '6', '7', - '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', - 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', - 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', - 'W', 'X', 'Y', 'Z', ' ', '$', '%', '*', - '+', '-', '.', '/', ':', + '0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, + '8' => 8, '9' => 9, 'A' => 10, 'B' => 11, 'C' => 12, 'D' => 13, 'E' => 14, 'F' => 15, + 'G' => 16, 'H' => 17, 'I' => 18, 'J' => 19, 'K' => 20, 'L' => 21, 'M' => 22, 'N' => 23, + 'O' => 24, 'P' => 25, 'Q' => 26, 'R' => 27, 'S' => 28, 'T' => 29, 'U' => 30, 'V' => 31, + 'W' => 32, 'X' => 33, 'Y' => 34, 'Z' => 35, ' ' => 36, '$' => 37, '%' => 38, '*' => 39, + '+' => 40, '-' => 41, '.' => 42, '/' => 43, ':' => 44, ]; /** diff --git a/src/QRCode.php b/src/QRCode.php index c29346d40..b96557c14 100755 --- a/src/QRCode.php +++ b/src/QRCode.php @@ -246,7 +246,7 @@ class QRCode{ $len = strlen($string); for($i = 0; $i < $len; $i++){ - if(!in_array($string[$i], $charmap, true)){ + if(!isset($charmap[$string[$i]])){ return false; } }