Skip to content

Commit a453e68

Browse files
authored
Merge pull request #2 from tiny-blocks/release/1.1.0
Release/1.1.0
2 parents 832a929 + 88ad12e commit a453e68

File tree

3 files changed

+49
-22
lines changed

3 files changed

+49
-22
lines changed

infection.json.dist

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
},
1414
"mutators": {
1515
"@default": true,
16-
"FalseValue": false
16+
"Concat": false,
17+
"FalseValue": false,
18+
"IncrementInteger": false,
19+
"DecrementInteger": false,
20+
"ConcatOperandRemoval": false
1721
},
1822
"phpUnit": {
1923
"configDir": "",

src/Base62.php

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,44 @@ final class Base62
1010

1111
public static function encode(string $value): string
1212
{
13-
if (empty($value)) {
14-
return $value;
13+
$bytes = 0;
14+
$hexadecimal = bin2hex($value);
15+
16+
while (str_starts_with($hexadecimal, '00')) {
17+
$bytes++;
18+
$hexadecimal = substr($hexadecimal, 2);
1519
}
1620

17-
$hexadecimal = bin2hex($value);
18-
$decimal = gmp_init($hexadecimal, self::HEXADECIMAL_RADIX);
19-
$encoded = '';
21+
$base62 = str_repeat(self::BASE62_ALPHABET[0], $bytes);
2022

21-
while (gmp_cmp($decimal, '0') > 0) {
22-
$remainder = gmp_intval(gmp_mod($decimal, self::BASE62_RADIX));
23-
$decimal = gmp_div_q($decimal, self::BASE62_RADIX);
24-
$encoded = self::BASE62_ALPHABET[$remainder] . $encoded;
23+
if (empty($hexadecimal)) {
24+
return strtr($base62, self::BASE62_ALPHABET, self::BASE62_ALPHABET);
2525
}
2626

27-
return $encoded;
27+
$base62Conversion = gmp_strval(gmp_init($hexadecimal, self::HEXADECIMAL_RADIX), self::BASE62_RADIX);
28+
29+
return strtr($base62 . $base62Conversion, self::BASE62_ALPHABET, self::BASE62_ALPHABET);
2830
}
2931

3032
public static function decode(string $value): string
3133
{
34+
$bytes = 0;
35+
36+
while (!empty($value) && str_starts_with($value, self::BASE62_ALPHABET[0])) {
37+
$bytes++;
38+
$value = substr($value, 1);
39+
}
40+
3241
if (empty($value)) {
33-
return $value;
42+
return str_repeat("\x00", $bytes);
3443
}
3544

36-
$decimal = gmp_init('0');
45+
$hexadecimal = gmp_strval(gmp_init($value, self::BASE62_RADIX), self::HEXADECIMAL_RADIX);
3746

38-
for ($i = 0, $length = strlen($value); $i < $length; $i++) {
39-
$character = $value[$i];
40-
$position = strpos(self::BASE62_ALPHABET, $character);
41-
$decimal = gmp_mul($decimal, self::BASE62_RADIX);
42-
$decimal = gmp_add($decimal, $position);
47+
if (strlen($hexadecimal) % 2 !== 0) {
48+
$hexadecimal = '0' . $hexadecimal;
4349
}
4450

45-
$hexadecimal = gmp_strval($decimal, self::HEXADECIMAL_RADIX);
46-
$decoded = hex2bin($hexadecimal);
47-
48-
return $decoded !== false ? $decoded : '';
51+
return hex2bin(str_repeat('00', $bytes) . $hexadecimal);
4952
}
5053
}

tests/Base62Test.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ public function testDecode(string $value, string $expected)
2626
self::assertEquals($expected, $actual);
2727
}
2828

29+
/**
30+
* @dataProvider providerForTestEncodeAndDecodeWithLeadingZeroBytes
31+
*/
32+
public function testEncodeAndDecodeWithLeadingZeroBytes(string $value): void
33+
{
34+
$encoded = Base62::encode(value: $value);
35+
$actual = Base62::decode(value: $encoded);
36+
37+
self::assertEquals($value, $actual);
38+
}
39+
2940
public function providerForTestEncode(): array
3041
{
3142
return [
@@ -45,4 +56,13 @@ public function providerForTestDecode(): array
4556
['T8dgcjRGuYUueWht', 'Hello world!']
4657
];
4758
}
59+
60+
public function providerForTestEncodeAndDecodeWithLeadingZeroBytes(): array
61+
{
62+
return [
63+
['001jlt60MnKnB9ECKRt4gl'],
64+
[hex2bin('07d8e31da269bf28')],
65+
[hex2bin('0000010203040506')]
66+
];
67+
}
4868
}

0 commit comments

Comments
 (0)