Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/ShortUuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,28 @@ final class ShortUuid
/**
* @var array
*/
private $alphabet = [
'2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
];
private $alphabet;

/**
* @var int
* @var BigInteger
*/
private $alphabetLength = 57;
private $alphabetLength;

/**
* @param array|null $alphabet
*/
public function __construct(array $alphabet = null)
{
if (null !== $alphabet) {
$this->setAlphabet($alphabet);
if (null == $alphabet) {
$alphabet = [
'2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
];
}

$this->setAlphabet($alphabet);
}

/**
Expand Down Expand Up @@ -130,9 +132,7 @@ private function numToString(BigInteger $number) : string
{
$output = '';
while ($number->isPositive()) {
$previousNumber = clone $number;
$number = $number->dividedBy($this->alphabetLength, RoundingMode::DOWN);
$digit = $previousNumber->mod($this->alphabetLength);
[$number, $digit] = $number->quotientAndRemainder($this->alphabetLength);

@gharlan gharlan Jul 6, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.. but this has the bigger impact on the performance.


$output .= $this->alphabet[(int)$digit->toInt()];
}
Expand Down Expand Up @@ -163,7 +163,7 @@ private function stringToNum(string $string) : BigInteger
private function setAlphabet(array $alphabet)
{
$this->alphabet = $alphabet;
$this->alphabetLength = count($alphabet);
$this->alphabetLength = BigInteger::of(count($alphabet));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

brick/math converts this value in all operations into BigInteger objects. Now we are doing it here once, so it does not have to be converted again and again.

}

/**
Expand Down