Skip to content

Commit

Permalink
Improve hexadecimal methods
Browse files Browse the repository at this point in the history
  • Loading branch information
antonkomarev committed Oct 14, 2023
1 parent cde0809 commit 19eeb38
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 12 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
},
"require": {
"php": "^7.4|^8.0",
"ext-mbstring": "*"
},
"ext-mbstring": "*"
},
"require-dev": {
"phpunit/phpunit": "^9.0|^10.0"
},
Expand Down
19 changes: 12 additions & 7 deletions src/CodePoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ private function __construct(
int $decimal
) {
$this->decimal = $decimal;

if ($decimal < 0x0000 || $decimal > 0x10FFFF) {
throw new \OutOfRangeException(
"Code point value `$decimal` is out of range",
);
}
}

public static function ofCharacter(
Expand All @@ -31,12 +37,6 @@ public static function ofCharacter(
public static function ofDecimal(
int $decimal
): self {
if ($decimal < 0) {
throw new \InvalidArgumentException(
"Code point decimal value `$decimal` cannot be negative",
);
}

return new self(
$decimal,
);
Expand All @@ -45,7 +45,12 @@ public static function ofDecimal(
public static function ofHexadecimal(
string $hexadecimal
): self {
// TODO: Assert format
if (preg_match('#^U\+[0-9A-Fa-f]{4,}$#', $hexadecimal) !== 1) {
throw new \InvalidArgumentException(
"Invalid hexadecimal format `$hexadecimal`",
);
}

return new self(
hexdec($hexadecimal),
);
Expand Down
7 changes: 4 additions & 3 deletions src/Glyph.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ public static function ofCharacters(
}

$codePointList = [];
$charactersCount = mb_strlen($characters);

for ($i = 0; $i < $charactersCount; $i++) {
$characterList = preg_split('//u', $characters, -1, PREG_SPLIT_NO_EMPTY);

for ($i = 0; $i < count($characterList); $i++) {
$codePointList[] = CodePoint::ofCharacter(
mb_substr($characters, $i, 1),
$characterList[$i],
);
}

Expand Down
38 changes: 38 additions & 0 deletions test/Unit/CodePointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,47 @@ public function testItCanInstantiateOfHexadecimal(
);
}

public function testItCannotInstantiateOfCharacterWithEmptyString(): void
{
$this->expectException(\InvalidArgumentException::class);

$character = '';

CodePoint::ofCharacter($character);
}

public function testItCannotInstantiateOfDecimalWithNegativeValue(): void
{
$this->expectException(\OutOfRangeException::class);

$decimal = -1;

CodePoint::ofDecimal($decimal);
}

public function testItCannotInstantiateOfHexadecimalWithTooLowValue(): void
{
$this->expectException(\OutOfRangeException::class);

$hexadecimal = 'U+FFFFFFFE'; // Min unicode hexadecimal -1

CodePoint::ofHexadecimal($hexadecimal);
}

public function testItCannotInstantiateOfHexadecimalWithTooBigValue(): void
{
$this->expectException(\OutOfRangeException::class);

$hexadecimal = 'U+110000'; // Max unicode hexadecimal +1

CodePoint::ofHexadecimal($hexadecimal);
}

public static function provideUnicodeMap(): array
{
return [
["\x00", 0, 'U+0000'],
['􏿿', 1114111, 'U+10FFFF'],
[' ', 32, 'U+0020'],
['A', 65, 'U+0041'],
['ÿ', 255, 'U+00FF'],
Expand Down
11 changes: 11 additions & 0 deletions test/Unit/GlyphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,20 @@ public function testItCanInstantiateOfCharacters(
);
}

public function testItCannotInstantiateOfCharactersWithEmptyString(): void
{
$this->expectException(\InvalidArgumentException::class);

$characters = '';

Glyph::ofCharacters($characters);
}

public static function provideUnicodeMap(): array
{
return [
["\x00"],
['􏿿'],
[' '],
['A'],
['ÿ'],
Expand Down

0 comments on commit 19eeb38

Please sign in to comment.