This commit is contained in:
1 parent
f7c532a648
commit
640bd67556
5801 files changed
+484532
-55857
No files matched your search
+36
-10
@@ -124,7 +124,7 @@ abstract class AbstractUnicodeString extends AbstractString
|
||||
}
|
||||
|
||||
if (null === $transliterator) {
|
||||
throw new InvalidArgumentException(sprintf('Unknown transliteration rule "%s".', $rule));
|
||||
throw new InvalidArgumentException(\sprintf('Unknown transliteration rule "%s".', $rule));
|
||||
}
|
||||
|
||||
self::$transliterators['any-latin/bgn'] = $transliterator;
|
||||
@@ -135,15 +135,21 @@ abstract class AbstractUnicodeString extends AbstractString
|
||||
} elseif (!\function_exists('iconv')) {
|
||||
$s = preg_replace('/[^\x00-\x7F]/u', '?', $s);
|
||||
} else {
|
||||
$s = @preg_replace_callback('/[^\x00-\x7F]/u', static function ($c) {
|
||||
$c = (string) iconv('UTF-8', 'ASCII//TRANSLIT', $c[0]);
|
||||
$previousLocale = setlocale(\LC_CTYPE, 0);
|
||||
try {
|
||||
setlocale(\LC_CTYPE, 'C');
|
||||
$s = @preg_replace_callback('/[^\x00-\x7F]/u', static function ($c) {
|
||||
$c = (string) iconv('UTF-8', 'ASCII//TRANSLIT', $c[0]);
|
||||
|
||||
if ('' === $c && '' === iconv('UTF-8', 'ASCII//TRANSLIT', '²')) {
|
||||
throw new \LogicException(sprintf('"%s" requires a translit-able iconv implementation, try installing "gnu-libiconv" if you\'re using Alpine Linux.', static::class));
|
||||
}
|
||||
if ('' === $c && '' === iconv('UTF-8', 'ASCII//TRANSLIT', '²')) {
|
||||
throw new \LogicException(\sprintf('"%s" requires a translit-able iconv implementation, try installing "gnu-libiconv" if you\'re using Alpine Linux.', static::class));
|
||||
}
|
||||
|
||||
return 1 < \strlen($c) ? ltrim($c, '\'`"^~') : ('' !== $c ? $c : '?');
|
||||
}, $s);
|
||||
return 1 < \strlen($c) ? ltrim($c, '\'`"^~') : ('' !== $c ? $c : '?');
|
||||
}, $s);
|
||||
} finally {
|
||||
setlocale(\LC_CTYPE, $previousLocale);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -258,7 +264,7 @@ abstract class AbstractUnicodeString extends AbstractString
|
||||
|
||||
public function normalize(int $form = self::NFC): static
|
||||
{
|
||||
if (!\in_array($form, [self::NFC, self::NFD, self::NFKC, self::NFKD])) {
|
||||
if (!\in_array($form, [self::NFC, self::NFD, self::NFKC, self::NFKD], true)) {
|
||||
throw new InvalidArgumentException('Unsupported normalization form.');
|
||||
}
|
||||
|
||||
@@ -354,7 +360,7 @@ abstract class AbstractUnicodeString extends AbstractString
|
||||
public function reverse(): static
|
||||
{
|
||||
$str = clone $this;
|
||||
$str->string = implode('', array_reverse(preg_split('/(\X)/u', $str->string, -1, \PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY)));
|
||||
$str->string = implode('', array_reverse(grapheme_str_split($str->string)));
|
||||
|
||||
return $str;
|
||||
}
|
||||
@@ -569,6 +575,8 @@ abstract class AbstractUnicodeString extends AbstractString
|
||||
private function wcswidth(string $string): int
|
||||
{
|
||||
$width = 0;
|
||||
$lastChar = null;
|
||||
$lastWidth = null;
|
||||
|
||||
foreach (preg_split('//u', $string, -1, \PREG_SPLIT_NO_EMPTY) as $c) {
|
||||
$codePoint = mb_ord($c, 'UTF-8');
|
||||
@@ -591,6 +599,20 @@ abstract class AbstractUnicodeString extends AbstractString
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (0xFE0F === $codePoint) {
|
||||
if (\PCRE_VERSION_MAJOR < 10 || \PCRE_VERSION_MAJOR === 10 && \PCRE_VERSION_MINOR < 40) {
|
||||
$regex = '/\p{So}/u';
|
||||
} else {
|
||||
$regex = '/\p{Emoji}/u';
|
||||
}
|
||||
if (null !== $lastChar && 1 === $lastWidth && preg_match($regex, $lastChar)) {
|
||||
++$width;
|
||||
$lastWidth = 2;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
self::$tableZero ??= require __DIR__.'/Resources/data/wcswidth_table_zero.php';
|
||||
|
||||
if ($codePoint >= self::$tableZero[0][0] && $codePoint <= self::$tableZero[$ubound = \count(self::$tableZero) - 1][1]) {
|
||||
@@ -621,6 +643,8 @@ abstract class AbstractUnicodeString extends AbstractString
|
||||
$ubound = $mid - 1;
|
||||
} else {
|
||||
$width += 2;
|
||||
$lastChar = $c;
|
||||
$lastWidth = 2;
|
||||
|
||||
continue 2;
|
||||
}
|
||||
@@ -628,6 +652,8 @@ abstract class AbstractUnicodeString extends AbstractString
|
||||
}
|
||||
|
||||
++$width;
|
||||
$lastChar = $c;
|
||||
$lastWidth = 1;
|
||||
}
|
||||
|
||||
return $width;
|
||||
|
||||
Reference in new issue
Block a user