Skip to content

Commit

Permalink
Added function imports
Browse files Browse the repository at this point in the history
  • Loading branch information
jrabausch committed Aug 5, 2022
1 parent d3350c9 commit c9a39ee
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/Transcoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@
use Semperton\Multibase\Exception\InvalidAlphabetException;
use Semperton\Multibase\Exception\InvalidCharsException;

use function mb_str_split;
use function array_diff;
use function array_diff_key;
use function array_unique;
use function array_reverse;
use function array_values;
use function array_flip;
use function count;
use function unpack;
use function pack;

class Transcoder implements TranscoderInterface
{
protected int $base;
Expand Down Expand Up @@ -46,24 +57,24 @@ public static function convert(array $values, int $fromBase, int $toBase): array

while ($count > 0) {

$divide = 0;
$newlen = 0;
$remainder = 0;
$length = 0;

for ($i = 0; $i < $count; ++$i) {

$divide = $divide * $fromBase + $values[$i];
$remainder = $remainder * $fromBase + $values[$i];

if ($divide >= $toBase) {
if ($remainder >= $toBase) {

$values[$newlen++] = (int)($divide / $toBase);
$divide = $divide % $toBase;
} else if ($newlen > 0) {
$values[$newlen++] = 0;
$values[$length++] = (int)($remainder / $toBase);
$remainder = $remainder % $toBase;
} else if ($length > 0) {
$values[$length++] = 0;
}
}

$count = $newlen;
$result[] = $divide;
$count = $length;
$result[] = $remainder;
}

return array_reverse($result);
Expand Down

0 comments on commit c9a39ee

Please sign in to comment.