-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: Remove GMP dependency and switch to BC math for arithmetic operations.
- Loading branch information
1 parent
3698734
commit 0fab019
Showing
9 changed files
with
152 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TinyBlocks\Encoder\Internal; | ||
|
||
use TinyBlocks\Encoder\Base62; | ||
|
||
final readonly class Decimal | ||
{ | ||
private function __construct(private string $value) | ||
{ | ||
} | ||
|
||
public static function fromBase62(string $number, string $alphabet): Decimal | ||
{ | ||
$value = '0'; | ||
$length = strlen($number); | ||
|
||
for ($index = 0; $index < $length; $index++) { | ||
$digit = strpos($alphabet, $number[$index]); | ||
$value = bcmul($value, (string)Base62::BASE62_RADIX); | ||
$value = bcadd($value, (string)$digit); | ||
} | ||
|
||
return new Decimal(value: $value); | ||
} | ||
|
||
public function toHexadecimal(): string | ||
{ | ||
$value = $this->value; | ||
$hexadecimalValue = ''; | ||
|
||
while (bccomp($value, '0') > 0) { | ||
$remainder = bcmod($value, Hexadecimal::HEXADECIMAL_RADIX); | ||
$hexadecimalValue = sprintf('%s%s', Hexadecimal::HEXADECIMAL_ALPHABET[(int)$remainder], $hexadecimalValue); | ||
$value = bcdiv($value, Hexadecimal::HEXADECIMAL_RADIX); | ||
} | ||
|
||
return $hexadecimalValue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.