| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <fabien@symfony.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
namespace Symfony\Polyfill\Intl\Normalizer; |
| 12 |
|
| 13 |
/** |
| 14 |
* Normalizer is a PHP fallback implementation of the Normalizer class provided by the intl extension. |
| 15 |
* |
| 16 |
* It has been validated with Unicode 6.3 Normalization Conformance Test. |
| 17 |
* See http://www.unicode.org/reports/tr15/ for detailed info about Unicode normalizations. |
| 18 |
* |
| 19 |
* @author Nicolas Grekas <p@tchwork.com> |
| 20 |
* |
| 21 |
* @internal |
| 22 |
*/ |
| 23 |
class Normalizer |
| 24 |
{ |
| 25 |
public const FORM_D = \Normalizer::FORM_D; |
| 26 |
public const FORM_KD = \Normalizer::FORM_KD; |
| 27 |
public const FORM_C = \Normalizer::FORM_C; |
| 28 |
public const FORM_KC = \Normalizer::FORM_KC; |
| 29 |
public const NFD = \Normalizer::NFD; |
| 30 |
public const NFKD = \Normalizer::NFKD; |
| 31 |
public const NFC = \Normalizer::NFC; |
| 32 |
public const NFKC = \Normalizer::NFKC; |
| 33 |
private static $C; |
| 34 |
private static $D; |
| 35 |
private static $KD; |
| 36 |
private static $cC; |
| 37 |
private static $rawD; |
| 38 |
private static $rawKD; |
| 39 |
private static $ulenMask = ["\xc0" => 2, "\xd0" => 2, "\xe0" => 3, "\xf0" => 4]; |
| 40 |
private static $ASCII = " eiasntrolud][cmp'\ng|hv.fb,:=-q10C2*yx)(L9AS/P\"EjMIk3>5T<D4}B{8FwR67UGN;JzV#HOW_&!K?XQ%Y\\\tZ+~^\$@`\x00\x01\x02\x03\x04\x05\x06\x07\x08\v\f\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"; |
| 41 |
public static function isNormalized(string $s, int $form = self::FORM_C) |
| 42 |
{ |
| 43 |
if (!\in_array($form, [self::NFD, self::NFKD, self::NFC, self::NFKC])) { |
| 44 |
return \false; |
| 45 |
} |
| 46 |
if (!isset($s[strspn($s, self::$ASCII)])) { |
| 47 |
return \true; |
| 48 |
} |
| 49 |
if (self::NFC == $form && preg_match('//u', $s) && !preg_match('/[^\x00-\x{2FF}]/u', $s)) { |
| 50 |
return \true; |
| 51 |
} |
| 52 |
return self::normalize($s, $form) === $s; |
| 53 |
} |
| 54 |
public static function getRawDecomposition(string $s, int $form = self::FORM_C) |
| 55 |
{ |
| 56 |
if ('' === $s || !preg_match('//u', $s)) { |
| 57 |
return null; |
| 58 |
} |
| 59 |
$ulen = $s[0] < "\x80" ? 1 : self::$ulenMask[$s[0] & "\xf0"] ?? 0; |
| 60 |
if (!$ulen || \strlen($s) !== $ulen) { |
| 61 |
return null; |
| 62 |
} |
| 63 |
if (self::NFC !== $form && self::NFD !== $form && self::NFKC !== $form && self::NFKD !== $form) { |
| 64 |
return ''; |
| 65 |
} |
| 66 |
if ($s >= "가" && $s <= "힣") { |
| 67 |
$u = unpack('C*', $s); |
| 68 |
$j = ($u[1] - 224 << 12) + ($u[2] - 128 << 6) + $u[3] - 0xac80; |
| 69 |
if ($t = $j % 28) { |
| 70 |
$j -= $t; |
| 71 |
$lv = 0xac00 + $j; |
| 72 |
$r = \chr(0xe0 | $lv >> 12) . \chr(0x80 | $lv >> 6 & 0x3f) . \chr(0x80 | $lv & 0x3f); |
| 73 |
$r .= $t < 25 ? "\xe1\x86" . \chr(0xa7 + $t) : "\xe1\x87" . \chr(0x67 + $t); |
| 74 |
return $r; |
| 75 |
} |
| 76 |
return "\xe1\x84" . \chr(0x80 + (int) ($j / 588)) . "\xe1\x85" . \chr(0xa1 + (int) ($j % 588 / 28)); |
| 77 |
} |
| 78 |
if (null === self::$rawD) { |
| 79 |
self::$rawD = self::getData('rawCanonicalDecomposition'); |
| 80 |
} |
| 81 |
if (isset(self::$rawD[$s])) { |
| 82 |
return self::$rawD[$s]; |
| 83 |
} |
| 84 |
if (self::NFKC === $form || self::NFKD === $form) { |
| 85 |
if (null === self::$rawKD) { |
| 86 |
self::$rawKD = self::getData('rawCompatibilityDecomposition'); |
| 87 |
} |
| 88 |
return self::$rawKD[$s] ?? null; |
| 89 |
} |
| 90 |
return null; |
| 91 |
} |
| 92 |
public static function normalize(string $s, int $form = self::FORM_C) |
| 93 |
{ |
| 94 |
if (!preg_match('//u', $s)) { |
| 95 |
return \false; |
| 96 |
} |
| 97 |
switch ($form) { |
| 98 |
case self::NFC: |
| 99 |
$C = \true; |
| 100 |
$K = \false; |
| 101 |
break; |
| 102 |
case self::NFD: |
| 103 |
$C = \false; |
| 104 |
$K = \false; |
| 105 |
break; |
| 106 |
case self::NFKC: |
| 107 |
$C = \true; |
| 108 |
$K = \true; |
| 109 |
break; |
| 110 |
case self::NFKD: |
| 111 |
$C = \false; |
| 112 |
$K = \true; |
| 113 |
break; |
| 114 |
default: |
| 115 |
if (\defined('WindPressDeps\Normalizer::NONE') && \Normalizer::NONE == $form) { |
| 116 |
return $s; |
| 117 |
} |
| 118 |
if (80000 > \PHP_VERSION_ID) { |
| 119 |
return \false; |
| 120 |
} |
| 121 |
// the doubled article was fixed in PHP 8.6 |
| 122 |
throw new \ValueError('normalizer_normalize(): Argument #2 ($form) must be a ' . (80600 > \PHP_VERSION_ID ? 'a ' : '') . 'valid normalization form'); |
| 123 |
} |
| 124 |
if ('' === $s) { |
| 125 |
return ''; |
| 126 |
} |
| 127 |
if ($K && null === self::$KD) { |
| 128 |
self::$KD = self::getData('compatibilityDecomposition'); |
| 129 |
} |
| 130 |
if (null === self::$D) { |
| 131 |
self::$D = self::getData('canonicalDecomposition'); |
| 132 |
self::$cC = self::getData('combiningClass'); |
| 133 |
} |
| 134 |
if (null !== $mbEncoding = 2 & (int) \ini_get('mbstring.func_overload') ? mb_internal_encoding() : null) { |
| 135 |
mb_internal_encoding('8bit'); |
| 136 |
} |
| 137 |
$r = self::decompose($s, $K); |
| 138 |
if ($C) { |
| 139 |
if (null === self::$C) { |
| 140 |
self::$C = self::getData('canonicalComposition'); |
| 141 |
} |
| 142 |
$r = self::recompose($r); |
| 143 |
} |
| 144 |
if (null !== $mbEncoding) { |
| 145 |
mb_internal_encoding($mbEncoding); |
| 146 |
} |
| 147 |
return $r; |
| 148 |
} |
| 149 |
private static function recompose($s) |
| 150 |
{ |
| 151 |
$ASCII = self::$ASCII; |
| 152 |
$compMap = self::$C; |
| 153 |
$combClass = self::$cC; |
| 154 |
$ulenMask = self::$ulenMask; |
| 155 |
$result = $tail = ''; |
| 156 |
$i = $s[0] < "\x80" ? 1 : $ulenMask[$s[0] & "\xf0"]; |
| 157 |
$len = \strlen($s); |
| 158 |
$lastUchr = substr($s, 0, $i); |
| 159 |
$lastUcls = isset($combClass[$lastUchr]) ? 256 : 0; |
| 160 |
while ($i < $len) { |
| 161 |
if ($s[$i] < "\x80") { |
| 162 |
// ASCII chars |
| 163 |
if ($tail) { |
| 164 |
$lastUchr .= $tail; |
| 165 |
$tail = ''; |
| 166 |
} |
| 167 |
if ($j = strspn($s, $ASCII, $i + 1)) { |
| 168 |
$lastUchr .= substr($s, $i, $j); |
| 169 |
$i += $j; |
| 170 |
} |
| 171 |
$result .= $lastUchr; |
| 172 |
$lastUchr = $s[$i]; |
| 173 |
$lastUcls = 0; |
| 174 |
++$i; |
| 175 |
continue; |
| 176 |
} |
| 177 |
$ulen = $ulenMask[$s[$i] & "\xf0"]; |
| 178 |
$uchr = substr($s, $i, $ulen); |
| 179 |
if ($lastUchr < "ᄀ" || "ᄒ" < $lastUchr || $uchr < "� |
| 180 |
�" || "� |
| 181 |
�" < $uchr || $lastUcls) { |
| 182 |
// Table lookup and combining chars composition |
| 183 |
$ucls = $combClass[$uchr] ?? 0; |
| 184 |
if (isset($compMap[$lastUchr . $uchr]) && (!$lastUcls || $lastUcls < $ucls)) { |
| 185 |
$lastUchr = $compMap[$lastUchr . $uchr]; |
| 186 |
} elseif ($lastUcls = $ucls) { |
| 187 |
$tail .= $uchr; |
| 188 |
} else { |
| 189 |
if ($tail) { |
| 190 |
$lastUchr .= $tail; |
| 191 |
$tail = ''; |
| 192 |
} |
| 193 |
$result .= $lastUchr; |
| 194 |
$lastUchr = $uchr; |
| 195 |
} |
| 196 |
} else { |
| 197 |
// Hangul chars |
| 198 |
$L = \ord($lastUchr[2]) - 0x80; |
| 199 |
$V = \ord($uchr[2]) - 0xa1; |
| 200 |
$T = 0; |
| 201 |
$uchr = substr($s, $i + $ulen, 3); |
| 202 |
if ("ᆧ" <= $uchr && $uchr <= "ᇂ") { |
| 203 |
$T = \ord($uchr[2]) - 0xa7; |
| 204 |
0 > $T && $T += 0x40; |
| 205 |
$ulen += 3; |
| 206 |
} |
| 207 |
$L = 0xac00 + ($L * 21 + $V) * 28 + $T; |
| 208 |
$lastUchr = \chr(0xe0 | $L >> 12) . \chr(0x80 | $L >> 6 & 0x3f) . \chr(0x80 | $L & 0x3f); |
| 209 |
} |
| 210 |
$i += $ulen; |
| 211 |
} |
| 212 |
return $result . $lastUchr . $tail; |
| 213 |
} |
| 214 |
private static function decompose($s, $c) |
| 215 |
{ |
| 216 |
$result = ''; |
| 217 |
$ASCII = self::$ASCII; |
| 218 |
$decompMap = self::$D; |
| 219 |
$combClass = self::$cC; |
| 220 |
$ulenMask = self::$ulenMask; |
| 221 |
if ($c) { |
| 222 |
$compatMap = self::$KD; |
| 223 |
} |
| 224 |
$c = []; |
| 225 |
$i = 0; |
| 226 |
$len = \strlen($s); |
| 227 |
while ($i < $len) { |
| 228 |
if ($s[$i] < "\x80") { |
| 229 |
// ASCII chars |
| 230 |
if ($c) { |
| 231 |
ksort($c); |
| 232 |
$result .= implode('', $c); |
| 233 |
$c = []; |
| 234 |
} |
| 235 |
$j = 1 + strspn($s, $ASCII, $i + 1); |
| 236 |
$result .= substr($s, $i, $j); |
| 237 |
$i += $j; |
| 238 |
continue; |
| 239 |
} |
| 240 |
$ulen = $ulenMask[$s[$i] & "\xf0"]; |
| 241 |
$uchr = substr($s, $i, $ulen); |
| 242 |
$i += $ulen; |
| 243 |
if ($uchr < "가" || "힣" < $uchr) { |
| 244 |
// Table lookup |
| 245 |
if ($uchr !== $j = $compatMap[$uchr] ?? $decompMap[$uchr] ?? $uchr) { |
| 246 |
$uchr = $j; |
| 247 |
$j = \strlen($uchr); |
| 248 |
$ulen = $uchr[0] < "\x80" ? 1 : $ulenMask[$uchr[0] & "\xf0"]; |
| 249 |
if ($ulen != $j) { |
| 250 |
// Put trailing chars in $s |
| 251 |
$j -= $ulen; |
| 252 |
$i -= $j; |
| 253 |
if (0 > $i) { |
| 254 |
$s = str_repeat(' ', -$i) . $s; |
| 255 |
$len -= $i; |
| 256 |
$i = 0; |
| 257 |
} |
| 258 |
while ($j--) { |
| 259 |
$s[$i + $j] = $uchr[$ulen + $j]; |
| 260 |
} |
| 261 |
$uchr = substr($uchr, 0, $ulen); |
| 262 |
} |
| 263 |
} |
| 264 |
if (isset($combClass[$uchr])) { |
| 265 |
// Combining chars, for sorting |
| 266 |
if (!isset($c[$combClass[$uchr]])) { |
| 267 |
$c[$combClass[$uchr]] = ''; |
| 268 |
} |
| 269 |
$c[$combClass[$uchr]] .= $uchr; |
| 270 |
continue; |
| 271 |
} |
| 272 |
} else { |
| 273 |
// Hangul chars |
| 274 |
$uchr = unpack('C*', $uchr); |
| 275 |
$j = ($uchr[1] - 224 << 12) + ($uchr[2] - 128 << 6) + $uchr[3] - 0xac80; |
| 276 |
$uchr = "\xe1\x84" . \chr(0x80 + (int) ($j / 588)) . "\xe1\x85" . \chr(0xa1 + (int) ($j % 588 / 28)); |
| 277 |
if ($j %= 28) { |
| 278 |
$uchr .= $j < 25 ? "\xe1\x86" . \chr(0xa7 + $j) : "\xe1\x87" . \chr(0x67 + $j); |
| 279 |
} |
| 280 |
} |
| 281 |
if ($c) { |
| 282 |
ksort($c); |
| 283 |
$result .= implode('', $c); |
| 284 |
$c = []; |
| 285 |
} |
| 286 |
$result .= $uchr; |
| 287 |
} |
| 288 |
if ($c) { |
| 289 |
ksort($c); |
| 290 |
$result .= implode('', $c); |
| 291 |
} |
| 292 |
return $result; |
| 293 |
} |
| 294 |
private static function getData($file) |
| 295 |
{ |
| 296 |
if (file_exists($file = __DIR__ . '/Resources/unidata/' . $file . '.php')) { |
| 297 |
return require $file; |
| 298 |
} |
| 299 |
return \false; |
| 300 |
} |
| 301 |
} |
| 302 |
|