From 098b4b31dd9f864f25c9c8e117703aa13ffe4766 Mon Sep 17 00:00:00 2001 From: Lucas Michot Date: Thu, 24 Sep 2015 18:58:28 +0200 Subject: [PATCH] Factorize tables --- src/Base32.php | 83 +++----------------------------------------------- 1 file changed, 4 insertions(+), 79 deletions(-) diff --git a/src/Base32.php b/src/Base32.php index 5ddead1..6eb8de5 100644 --- a/src/Base32.php +++ b/src/Base32.php @@ -19,86 +19,11 @@ class Base32 { /** - * Table for encoding base32 + * Alphabet for encoding and decoding base32 * * @var array */ - private static $encode = array( - 0 => 'A', - 1 => 'B', - 2 => 'C', - 3 => 'D', - 4 => 'E', - 5 => 'F', - 6 => 'G', - 7 => 'H', - 8 => 'I', - 9 => 'J', - 10 => 'K', - 11 => 'L', - 12 => 'M', - 13 => 'N', - 14 => 'O', - 15 => 'P', - 16 => 'Q', - 17 => 'R', - 18 => 'S', - 19 => 'T', - 20 => 'U', - 21 => 'V', - 22 => 'W', - 23 => 'X', - 24 => 'Y', - 25 => 'Z', - 26 => 2, - 27 => 3, - 28 => 4, - 29 => 5, - 30 => 6, - 31 => 7, - 32 => '=', - ); - - /** - * Table for decoding base32 - * - * @var array - */ - private static $decode = array( - 'A' => 0, - 'B' => 1, - 'C' => 2, - 'D' => 3, - 'E' => 4, - 'F' => 5, - 'G' => 6, - 'H' => 7, - 'I' => 8, - 'J' => 9, - 'K' => 10, - 'L' => 11, - 'M' => 12, - 'N' => 13, - 'O' => 14, - 'P' => 15, - 'Q' => 16, - 'R' => 17, - 'S' => 18, - 'T' => 19, - 'U' => 20, - 'V' => 21, - 'W' => 22, - 'X' => 23, - 'Y' => 24, - 'Z' => 25, - 2 => 26, - 3 => 27, - 4 => 28, - 5 => 29, - 6 => 30, - 7 => 31, - '=' => 32, - ); + private static $alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567='; /** * Creates an array from a binary string into a given chunk size @@ -162,7 +87,7 @@ public static function encode($string) } // Base32 character - $base32String .= self::$encode[$char]; + $base32String .= substr(self::$alphabet, $char, 1); } return $base32String; @@ -194,7 +119,7 @@ public static function decode($base32String) $string = ''; foreach ($base32Array as $str) { - $char = self::$decode[$str]; + $char = strpos(self::$alphabet, $str); // Ignore the padding character if ($char !== 32) {