Skip to content

Commit

Permalink
Removed baseConvert()
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Wright <[email protected]>
  • Loading branch information
betterthanclay committed Nov 14, 2023
1 parent d75cd52 commit c64bf19
Show file tree
Hide file tree
Showing 6 changed files with 3 additions and 99 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* Removed baseConvert()
* Made PHP8.1 minimum version
* Refactored package file structure

Expand Down
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ echo Dictum::numericToAlpha(23345452);
echo Dictum::alphaToNumeric('aybfra')
// 23345452

echo Dictum::baseConvert(23345452, 10, 36);
// 4J4RS


echo Dictum::toBoolean('yes') ? 'true' : 'false';
// true
```
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"extra": {
"branch-alias": {
"dev-develop": "0.5.x-dev"
"dev-develop": "0.6.x-dev"
}
}
}
91 changes: 0 additions & 91 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use DecodeLabs\Dictum\Plugins\Number as NumberPlugin;
use DecodeLabs\Dictum\Plugins\Time as TimePlugin;

use DecodeLabs\Exceptional;

use DecodeLabs\Veneer;
use DecodeLabs\Veneer\LazyLoad;
Expand Down Expand Up @@ -520,96 +519,6 @@ public function alphaToNumeric(
return $text->alphaToNumeric();
}

/**
* Convert between any base from 2 to 62
*
* @phpstan-return ($input is null ? null : string)
*/
public function baseConvert(
string|Stringable|int|float|null $input,
int $fromBase,
int $toBase,
int $pad = 1
): ?string {
if ($input === null) {
return null;
}

if (
$fromBase < 2 ||
$fromBase > 62 ||
$toBase < 2 ||
$toBase > 62
) {
throw Exceptional::Overflow('Base must be between 2 and 62');
}

if (!is_string($input)) {
$input = sprintf('%0.0F', $input);
}


if (extension_loaded('gmp')) {
$output = gmp_strval(gmp_init($input, $fromBase), $toBase);

if ($pad > 1) {
$output = str_pad($output, $pad, '0', STR_PAD_LEFT);
}

return $output;
}


$digitChars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$inDigits = [];
$outChars = '';

$input = strtolower($input);
$length = strlen($input);


for ($i = 0; $i < $length; $i++) {
$digit = ord($input[$i]) - 48;

if ($digit > 9) {
$digit -= 39;
}

if ($digit > $fromBase) {
throw Exceptional::Overflow('Digit as exceeded base: ' . $fromBase);
}

$inDigits[] = $digit;
}


while (!empty($inDigits)) {
$work = 0;
$workDigits = [];

foreach ($inDigits as $digit) {
$work *= $fromBase;
$work += $digit;


if ($work < $toBase) {
if (!empty($workDigits)) {
$workDigits[] = 0;
}
} else {
$workDigits[] = (int)($work / $toBase);
$work %= $toBase;
}
}

$outChars = $digitChars[$work] . $outChars;
$inDigits = $workDigits;
}

return str_pad($outChars, $pad, '0', STR_PAD_LEFT);
}




/**
Expand Down
1 change: 1 addition & 0 deletions src/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -1317,6 +1317,7 @@ public function scanMatches(
break;
}

/** @phpstan-ignore-next-line */
if (false === ($result = mb_ereg_search_getregs())) {
throw Exceptional::InvalidArgument('Unable to complete mb regex with: ' . $pattern);
}
Expand Down
3 changes: 0 additions & 3 deletions stubs/DecodeLabs/Dictum.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,6 @@ public static function numericToAlpha(?int $number, ?string $encoding = NULL): ?
public static function alphaToNumeric(Ref0|string|int|float|null $text, ?string $encoding = NULL): ?int {
return static::$instance->alphaToNumeric(...func_get_args());
}
public static function baseConvert(Ref0|string|int|float|null $input, int $fromBase, int $toBase, int $pad = 1): ?string {
return static::$instance->baseConvert(...func_get_args());
}
public static function toBoolean(Ref0|string|int|float|null $text, ?string $encoding = NULL): bool {
return static::$instance->toBoolean(...func_get_args());
}
Expand Down

0 comments on commit c64bf19

Please sign in to comment.