From de5927f3f4c8eedfbd25040232e497c223c8756e Mon Sep 17 00:00:00 2001 From: Sorin Sarca Date: Tue, 24 Nov 2026 23:28:10 +0200 Subject: [PATCH] Added invalid option to getStringFromCodePoints --- .gitignore | 1 + src/UnicodeString.php | 32 ++++++++++++++++++++++++++++++-- tests/UnicodeStringTest.php | 24 +++++++++++++++++++++++- 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 4188f39..4b69fed 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ shared/ Gemfile.lock composer.lock .phpunit.result.cache +test.php diff --git a/src/UnicodeString.php b/src/UnicodeString.php index 21d2666..87dc6b1 100644 --- a/src/UnicodeString.php +++ b/src/UnicodeString.php @@ -39,6 +39,21 @@ class UnicodeString implements Countable, ArrayAccess, JsonSerializable const ASCII_CONV = 4; + /** + * Ignores the invalid code point + */ + const INVALID_IGNORE = 0; + + /** + * Replaces the invalid code point with U+FFFD + */ + const INVALID_SUBSTITUTE = 1; + + /** + * Throws InvalidCodePointException on invalid code point + */ + const INVALID_THROW = 2; + /** * @var int[] */ @@ -1418,12 +1433,13 @@ public static function getCharsFromString(string $str, int $mode = self::KEEP_CA /** * Converts all code points to chars and returns the string - * Invalid code points are ignored * @param array $codes * @param int $mode + * @param int $invalid By default, invalid code points are ignored, see self::INVALID_* * @return string + * @throws InvalidCodePointException */ - public static function getStringFromCodePoints(array $codes, int $mode = self::KEEP_CASE): string + public static function getStringFromCodePoints(array $codes, int $mode = self::KEEP_CASE, int $invalid = self::INVALID_IGNORE): string { $str = ''; @@ -1445,6 +1461,11 @@ public static function getStringFromCodePoints(array $codes, int $mode = self::K } if ($code >= 0xD800 && $code <= 0xDFFF) { + if ($invalid === self::INVALID_THROW) { + throw new InvalidCodePointException($code); + } else if ($invalid === self::INVALID_SUBSTITUTE) { + $str .= "\u{FFFD}"; + } continue; } @@ -1462,6 +1483,13 @@ public static function getStringFromCodePoints(array $codes, int $mode = self::K chr((($code >> 12) & 0x3F) + 0x80) . chr((($code >> 6) & 0x3F) + 0x80) . chr(($code & 0x3F) + 0x80); + continue; + } + + if ($invalid === self::INVALID_THROW) { + throw new InvalidCodePointException($code); + } elseif ($invalid === self::INVALID_SUBSTITUTE) { + $str .= "\u{FFFD}"; } } diff --git a/tests/UnicodeStringTest.php b/tests/UnicodeStringTest.php index 9785976..557bb60 100644 --- a/tests/UnicodeStringTest.php +++ b/tests/UnicodeStringTest.php @@ -23,7 +23,8 @@ use Opis\String\UnicodeString as wstr; use Opis\String\Exception\{ UnicodeException, - InvalidStringException + InvalidStringException, + InvalidCodePointException }; class UnicodeStringTest extends TestCase @@ -744,4 +745,25 @@ public function toAsciiDataProvider() ["ăĂâÂîÎșȘțȚ", "aAaAiIsStT"], ]; } + + public function testInvalidOptions() + { + $cp = [ + 0x61, // a + 0x74, // t + 0x74, // t + 0x61, // a + 0xD800, // surrogate code point + 0x63, // c + 0x6B, // k + ]; + $mode = wstr::KEEP_CASE; + + $this->assertEquals("attack", wstr::getStringFromCodePoints($cp)); + $this->assertEquals("attack", wstr::getStringFromCodePoints($cp, $mode, wstr::INVALID_IGNORE)); + $this->assertEquals("atta\u{FFFD}ck", wstr::getStringFromCodePoints($cp, $mode, wstr::INVALID_SUBSTITUTE)); + + $this->expectException(InvalidCodePointException::class); + wstr::getStringFromCodePoints($cp, $mode, wstr::INVALID_THROW); + } }