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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ shared/
Gemfile.lock
composer.lock
.phpunit.result.cache
test.php
32 changes: 30 additions & 2 deletions src/UnicodeString.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
*/
Expand Down Expand Up @@ -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 = '';

Expand All @@ -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;
}

Expand All @@ -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}";
}
}

Expand Down
24 changes: 23 additions & 1 deletion tests/UnicodeStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
use Opis\String\UnicodeString as wstr;
use Opis\String\Exception\{
UnicodeException,
InvalidStringException
InvalidStringException,
InvalidCodePointException
};

class UnicodeStringTest extends TestCase
Expand Down Expand Up @@ -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);
}
}
Loading