Skip to content

Commit

Permalink
:octocat: i hate this less (might be slightly slower tho)
Browse files Browse the repository at this point in the history
  • Loading branch information
codemasher committed Sep 24, 2024
1 parent 9aecdcd commit 9964cf8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 74 deletions.
75 changes: 34 additions & 41 deletions src/Data/AlphaNum.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace chillerlan\QRCode\Data;

use chillerlan\QRCode\Common\{BitBuffer, Mode};
use function ceil, intdiv, str_split;
use function ceil, intdiv, preg_match, strpos;

/**
* Alphanumeric mode: 0 to 9, A to Z, space, $ % * + - . / :
Expand All @@ -25,30 +25,9 @@ final class AlphaNum extends QRDataModeAbstract{
/**
* ISO/IEC 18004:2000 Table 5
*
* @var int[]
* @var string
*/
private const CHAR_TO_ORD = [
// phpcs:ignore
'0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7,
// phpcs:ignore
'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,
];

/**
* @var string[]
*/
private const ORD_TO_CHAR = [
'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', ' ', '$', '%', '*',
'+', '-', '.', '/', ':',
];
private const CHAR_MAP = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:';

public const DATAMODE = Mode::ALPHANUM;

Expand All @@ -57,18 +36,7 @@ public function getLengthInBits():int{
}

public static function validateString(string $string):bool{

if($string === ''){
return false;
}

foreach(str_split($string) as $chr){
if(!isset(self::CHAR_TO_ORD[$chr])){
return false;
}
}

return true;
return (bool)preg_match('#^['.self::CHAR_MAP.']+$#', $string);
}

public function write(BitBuffer $bitBuffer, int $versionNumber):static{
Expand All @@ -82,14 +50,14 @@ public function write(BitBuffer $bitBuffer, int $versionNumber):static{
// encode 2 characters in 11 bits
for($i = 0; ($i + 1) < $len; $i += 2){
$bitBuffer->put(
(self::CHAR_TO_ORD[$this->data[$i]] * 45 + self::CHAR_TO_ORD[$this->data[($i + 1)]]),
($this->ord($this->data[$i]) * 45 + $this->ord($this->data[($i + 1)])),
11,
);
}

// encode a remaining character in 6 bits
if($i < $len){
$bitBuffer->put(self::CHAR_TO_ORD[$this->data[$i]], 6);
$bitBuffer->put($this->ord($this->data[$i]), 6);
}

return $this;
Expand All @@ -111,8 +79,8 @@ public static function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):s
}

$nextTwoCharsBits = $bitBuffer->read(11);
$result .= self::ORD_TO_CHAR[intdiv($nextTwoCharsBits, 45)];
$result .= self::ORD_TO_CHAR[($nextTwoCharsBits % 45)];
$result .= self::chr(intdiv($nextTwoCharsBits, 45));
$result .= self::chr($nextTwoCharsBits % 45);
$length -= 2;
}

Expand All @@ -122,10 +90,35 @@ public static function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):s
throw new QRCodeDataException('not enough bits available'); // @codeCoverageIgnore
}

$result .= self::ORD_TO_CHAR[$bitBuffer->read(6)];
$result .= self::chr($bitBuffer->read(6));
}

return $result;
}

/**
* @throws \chillerlan\QRCode\Data\QRCodeDataException
*/
private function ord(string $chr):int{
$ord = strpos(self::CHAR_MAP, $chr);

if($ord === false){
throw new QRCodeDataException('invalid character'); // @codeCoverageIgnore
}

return $ord;
}

/**
* @throws \chillerlan\QRCode\Data\QRCodeDataException
*/
private static function chr(int $ord):string{

if($ord < 0 || $ord > 44){
throw new QRCodeDataException('invalid character code'); // @codeCoverageIgnore
}

return self::CHAR_MAP[$ord];
}

}
41 changes: 8 additions & 33 deletions src/Data/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace chillerlan\QRCode\Data;

use chillerlan\QRCode\Common\{BitBuffer, Mode};
use function ceil, intdiv, str_split, substr, unpack;
use function ceil, intdiv, preg_match, substr, unpack;

/**
* Numeric mode: decimal digits 0 to 9
Expand All @@ -22,39 +22,14 @@
*/
final class Number extends QRDataModeAbstract{

/**
* @var int[]
*/
private const NUMBER_TO_ORD = [
'0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9,
];

/**
* @var string[]
*/
private const ORD_TO_NUMBER = [
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
];

public const DATAMODE = Mode::NUMBER;

public function getLengthInBits():int{
return (int)ceil($this->getCharCount() * (10 / 3));
}

public static function validateString(string $string):bool{

if($string === ''){
return false;
}

foreach(str_split($string) as $chr){
if(!isset(self::NUMBER_TO_ORD[$chr])){
return false;
}
}

return true;
return (bool)preg_match('/^\d+$/', $string);
}

public function write(BitBuffer $bitBuffer, int $versionNumber):static{
Expand Down Expand Up @@ -131,9 +106,9 @@ public static function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):s
throw new QRCodeDataException('error decoding numeric value');
}

$result .= self::ORD_TO_NUMBER[intdiv($threeDigitsBits, 100)];
$result .= self::ORD_TO_NUMBER[(intdiv($threeDigitsBits, 10) % 10)];
$result .= self::ORD_TO_NUMBER[($threeDigitsBits % 10)];
$result .= intdiv($threeDigitsBits, 100);
$result .= (($threeDigitsBits / 10) % 10);
$result .= ($threeDigitsBits % 10);

$length -= 3;
}
Expand All @@ -150,8 +125,8 @@ public static function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):s
throw new QRCodeDataException('error decoding numeric value');
}

$result .= self::ORD_TO_NUMBER[intdiv($twoDigitsBits, 10)];
$result .= self::ORD_TO_NUMBER[($twoDigitsBits % 10)];
$result .= intdiv($twoDigitsBits, 10);
$result .= ($twoDigitsBits % 10);
}
elseif($length === 1){
// One digit left over to read
Expand All @@ -165,7 +140,7 @@ public static function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):s
throw new QRCodeDataException('error decoding numeric value');
}

$result .= self::ORD_TO_NUMBER[$digitBits];
$result .= $digitBits;
}

return $result;
Expand Down

0 comments on commit 9964cf8

Please sign in to comment.