Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Provide `--add-missing-ids` extraction option to update source code with auto-generated identifiers
- Add `Util\FormatHelper` that provides `getReader()` and `getWriter()` methods
- Introduce `Format\Format` final static class for format constants
- Port [@formatjs/icu-messageformat-parser](https://www.npmjs.com/package/@formatjs/icu-messageformat-parser) to FormatPHP (`FormatPHP\Icu\MessageFormat\Parser`)

### Changed

Expand Down
44 changes: 0 additions & 44 deletions src/Icu/MessageFormat/Parser/Util/AbstractCodePointMatcher.php

This file was deleted.

38 changes: 37 additions & 1 deletion src/Icu/MessageFormat/Parser/Util/CodePointHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
use function mb_ord;

/**
* A helper for working with code points
*
* @internal
*/
class CodePointHelper
Expand All @@ -49,32 +51,61 @@ public function __construct()
$this->isPotentialElementNameChar = new IsPotentialElementNameChar();
}

/**
* Checks whether a code point is an alphabet character
*
* @see IsAlpha::matches()
*/
public function isAlpha(int $codepoint): bool
{
return $this->isAlpha->matches($codepoint);
}

/**
* Checks whether a code point is an alphabet character or a forward slash ("/")
*
* @see IsAlpha::matches()
*/
public function isAlphaOrSlash(int $codepoint): bool
{
return $this->isAlpha->matchesWithSlash($codepoint);
return $codepoint === 0x002f || $this->isAlpha->matches($codepoint);
}

/**
* Checks whether a code point is in the Unicode Character Database
* White_Space and Pattern_White_Space groups
*
* @see IsWhiteSpace::matches()
*/
public function isWhiteSpace(int $codepoint): bool
{
return $this->isWhiteSpace->matches($codepoint);
}

/**
* Checks whether a code point is in the Unicode Character Database
* Pattern_Syntax group
*
* @see IsPatternSyntax::matches()
*/
public function isPatternSyntax(int $codepoint): bool
{
return $this->isPatternSyntax->matches($codepoint);
}

/**
* Checks whether the code point could be used in an HTML tag element name
*
* @see IsPotentialElementNameChar::matches()
*/
public function isPotentialElementNameChar(int $codepoint): bool
{
return $this->isPotentialElementNameChar->matches($codepoint);
}

/**
* Returns the code point for a character in a string array at a given offset
*
* @param string[] $stringArray
*/
public function charCodeAt(array $stringArray, int $offset): ?int
Expand All @@ -92,6 +123,9 @@ public function charCodeAt(array $stringArray, int $offset): ?int
return null;
}

/**
* Returns the string character for a given code point
*/
public function fromCharCode(int $code): ?string
{
$char = mb_chr($code, Parser::ENCODING);
Expand All @@ -104,6 +138,8 @@ public function fromCharCode(int $code): ?string
}

/**
* Returns a string of characters for the given code points
*
* @throws InvalidUtf8CodePointException
*/
public function fromCodePoint(int ...$codePoints): string
Expand Down
82 changes: 8 additions & 74 deletions src/Icu/MessageFormat/Parser/Util/IsAlpha.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,85 +23,19 @@
namespace FormatPHP\Icu\MessageFormat\Parser\Util;

/**
* Checks whether a code point is an alphabet character
*
* @internal
*/
class IsAlpha extends AbstractCodePointMatcher
class IsAlpha implements CodePointMatcherInterface
{
/**
* Latin alphabet code points
*
* This array provides fast lookup of values.
*/
protected const CODE_POINTS = [
0x0041,
0x0042,
0x0043,
0x0044,
0x0045,
0x0046,
0x0047,
0x0048,
0x0049,
0x004a,
0x004b,
0x004c,
0x004d,
0x004e,
0x004f,
0x0050,
0x0051,
0x0052,
0x0053,
0x0054,
0x0055,
0x0056,
0x0057,
0x0058,
0x0059,
0x005a,
0x0061,
0x0062,
0x0063,
0x0064,
0x0065,
0x0066,
0x0067,
0x0068,
0x0069,
0x006a,
0x006b,
0x006c,
0x006d,
0x006e,
0x006f,
0x0070,
0x0071,
0x0072,
0x0073,
0x0074,
0x0075,
0x0076,
0x0077,
0x0078,
0x0079,
0x007a,
];

/**
* Checks whether the code point is a forward slash ("/") or an uppercase
* or lowercase alphabet character
*/
public function matchesWithSlash(int $codepoint): bool
{
return $codepoint === 0x002f /* '/' */
|| $this->matches($codepoint);
}

/**
* @inheritdoc
* Returns true if the code point is a Latin uppercase letter A-Z or
* lowercase letter a-z
*/
protected function getCodePoints(): array
public function matches(int $codepoint): bool
{
return self::CODE_POINTS;
return ($codepoint >= 0x0041 && $codepoint <= 0x005a)
|| ($codepoint >= 0x0061 && $codepoint <= 0x007a);
}
}
Loading