diff --git a/VERSION b/VERSION index bbd40c2..5fb9ef1 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2407.311824 +0.2407.311834 diff --git a/src/Serialisation/Base62.php b/src/Serialisation/Base62.php index c517523..92fd1e2 100644 --- a/src/Serialisation/Base62.php +++ b/src/Serialisation/Base62.php @@ -1,49 +1,52 @@ = 0; --$i) { - $index = (int)floor($input / (62 ** $i)); + for($i = floor(log10($integer) / log10(self::BASE)); $i >= 0; --$i) { + $index = (int)floor($integer / (self::BASE ** $i)); $output .= substr(self::CHARS, $index, 1); - $input -= $index * (62 ** $i); + $integer -= $index * (self::BASE ** $i); } return $output; } /** - * Decodes a Base62 string back to an integer. + * Converts a base62 integer to a base10 integer. * - * @param Stream|string $input Input Base62 string. - * @return int Integer. + * @param string $string The encoded integer. + * @return int|false Returns the decoded integer or false on failure. */ - public static function decode(Stream|string $input): int { - $input = (string)$input; + public static function decode(string $string): int|false { $output = 0; - $length = strlen($input) - 1; + $length = strlen($string) - 1; - for($i = 0; $i <= $length; ++$i) - $output += strpos(self::CHARS, $input[$i]) * (62 ** ($length - $i)); + for($i = 0; $i <= $length; ++$i) { + $pos = strpos(self::CHARS, $string[$i]); + if($pos === false) + return false; + + $output += $pos * (self::BASE ** ($length - $i)); + } return $output; }