🚿 fewer function calls for numeric and alphanum table checks

This commit is contained in:
codemasher
2020-04-01 00:51:46 +02:00
parent 4c395abfd4
commit 2f337fe43d
4 changed files with 15 additions and 14 deletions
+4 -5
View File
@@ -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];
}
}
+1 -1
View File
@@ -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));
}
+9 -7
View File
@@ -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,
];
/**
+1 -1
View File
@@ -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;
}
}