← All changes
|
vendor/symfony/polyfill-intl-normalizer/Normalizer.php
+69
-28
3.0.13
→
3.2.91
View file →
| @@ -33,8 +33,10 @@ | ||
| 33 | 33 | private static $C; |
| 34 | 34 | private static $D; |
| 35 | 35 | private static $KD; |
| 36 | 36 | private static $cC; |
| 37 | + private static $rawD; | |
| 38 | + private static $rawKD; | |
| 37 | 39 | private static $ulenMask = ["\xc0" => 2, "\xd0" => 2, "\xe0" => 3, "\xf0" => 4]; |
| 38 | 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"; |
| 39 | 41 | public static function isNormalized(string $s, int $form = self::FORM_C) |
| 40 | 42 | { |
| @@ -40,19 +42,57 @@ | ||
| 40 | 42 | { |
| 41 | 43 | if (!\in_array($form, [self::NFD, self::NFKD, self::NFC, self::NFKC])) { |
| 42 | 44 | return \false; |
| 43 | 45 | } |
| 44 | - if (!isset($s[\strspn($s, self::$ASCII)])) { | |
| 46 | + if (!isset($s[strspn($s, self::$ASCII)])) { | |
| 45 | 47 | return \true; |
| 46 | 48 | } |
| 47 | - if (self::NFC == $form && \preg_match('//u', $s) && !\preg_match('/[^\\x00-\\x{2FF}]/u', $s)) { | |
| 49 | + if (self::NFC == $form && preg_match('//u', $s) && !preg_match('/[^\x00-\x{2FF}]/u', $s)) { | |
| 48 | 50 | return \true; |
| 49 | 51 | } |
| 50 | 52 | return self::normalize($s, $form) === $s; |
| 51 | 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 | + } | |
| 52 | 92 | public static function normalize(string $s, int $form = self::FORM_C) |
| 53 | 93 | { |
| 54 | - if (!\preg_match('//u', $s)) { | |
| 94 | + if (!preg_match('//u', $s)) { | |
| 55 | 95 | return \false; |
| 56 | 96 | } |
| 57 | 97 | switch ($form) { |
| 58 | 98 | case self::NFC: |
| @@ -71,15 +111,16 @@ | ||
| 71 | 111 | $C = \false; |
| 72 | 112 | $K = \true; |
| 73 | 113 | break; |
| 74 | 114 | default: |
| 75 | - if (\defined('WindPressDeps\\Normalizer::NONE') && \Normalizer::NONE == $form) { | |
| 115 | + if (\defined('WindPressDeps\Normalizer::NONE') && \Normalizer::NONE == $form) { | |
| 76 | 116 | return $s; |
| 77 | 117 | } |
| 78 | 118 | if (80000 > \PHP_VERSION_ID) { |
| 79 | 119 | return \false; |
| 80 | 120 | } |
| 81 | - throw new \ValueError('normalizer_normalize(): Argument #2 ($form) must be a a valid normalization form'); | |
| 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'); | |
| 82 | 123 | } |
| 83 | 124 | if ('' === $s) { |
| 84 | 125 | return ''; |
| 85 | 126 | } |
| @@ -89,10 +130,10 @@ | ||
| 89 | 130 | if (null === self::$D) { |
| 90 | 131 | self::$D = self::getData('canonicalDecomposition'); |
| 91 | 132 | self::$cC = self::getData('combiningClass'); |
| 92 | 133 | } |
| 93 | - if (null !== ($mbEncoding = 2 & (int) \ini_get('mbstring.func_overload') ? \mb_internal_encoding() : null)) { | |
| 94 | - \mb_internal_encoding('8bit'); | |
| 134 | + if (null !== $mbEncoding = 2 & (int) \ini_get('mbstring.func_overload') ? mb_internal_encoding() : null) { | |
| 135 | + mb_internal_encoding('8bit'); | |
| 95 | 136 | } |
| 96 | 137 | $r = self::decompose($s, $K); |
| 97 | 138 | if ($C) { |
| 98 | 139 | if (null === self::$C) { |
| @@ -100,9 +141,9 @@ | ||
| 100 | 141 | } |
| 101 | 142 | $r = self::recompose($r); |
| 102 | 143 | } |
| 103 | 144 | if (null !== $mbEncoding) { |
| 104 | - \mb_internal_encoding($mbEncoding); | |
| 145 | + mb_internal_encoding($mbEncoding); | |
| 105 | 146 | } |
| 106 | 147 | return $r; |
| 107 | 148 | } |
| 108 | 149 | private static function recompose($s) |
| @@ -113,9 +154,9 @@ | ||
| 113 | 154 | $ulenMask = self::$ulenMask; |
| 114 | 155 | $result = $tail = ''; |
| 115 | 156 | $i = $s[0] < "\x80" ? 1 : $ulenMask[$s[0] & "\xf0"]; |
| 116 | 157 | $len = \strlen($s); |
| 117 | - $lastUchr = \substr($s, 0, $i); | |
| 158 | + $lastUchr = substr($s, 0, $i); | |
| 118 | 159 | $lastUcls = isset($combClass[$lastUchr]) ? 256 : 0; |
| 119 | 160 | while ($i < $len) { |
| 120 | 161 | if ($s[$i] < "\x80") { |
| 121 | 162 | // ASCII chars |
| @@ -122,10 +163,10 @@ | ||
| 122 | 163 | if ($tail) { |
| 123 | 164 | $lastUchr .= $tail; |
| 124 | 165 | $tail = ''; |
| 125 | 166 | } |
| 126 | - if ($j = \strspn($s, $ASCII, $i + 1)) { | |
| 127 | - $lastUchr .= \substr($s, $i, $j); | |
| 167 | + if ($j = strspn($s, $ASCII, $i + 1)) { | |
| 168 | + $lastUchr .= substr($s, $i, $j); | |
| 128 | 169 | $i += $j; |
| 129 | 170 | } |
| 130 | 171 | $result .= $lastUchr; |
| 131 | 172 | $lastUchr = $s[$i]; |
| @@ -133,9 +174,9 @@ | ||
| 133 | 174 | ++$i; |
| 134 | 175 | continue; |
| 135 | 176 | } |
| 136 | 177 | $ulen = $ulenMask[$s[$i] & "\xf0"]; |
| 137 | - $uchr = \substr($s, $i, $ulen); | |
| 178 | + $uchr = substr($s, $i, $ulen); | |
| 138 | 179 | if ($lastUchr < "ᄀ" || "ᄒ" < $lastUchr || $uchr < "ᅡ" || "ᅵ" < $uchr || $lastUcls) { |
| 139 | 180 | // Table lookup and combining chars composition |
| 140 | 181 | $ucls = $combClass[$uchr] ?? 0; |
| 141 | 182 | if (isset($compMap[$lastUchr . $uchr]) && (!$lastUcls || $lastUcls < $ucls)) { |
| @@ -154,12 +195,12 @@ | ||
| 154 | 195 | // Hangul chars |
| 155 | 196 | $L = \ord($lastUchr[2]) - 0x80; |
| 156 | 197 | $V = \ord($uchr[2]) - 0xa1; |
| 157 | 198 | $T = 0; |
| 158 | - $uchr = \substr($s, $i + $ulen, 3); | |
| 199 | + $uchr = substr($s, $i + $ulen, 3); | |
| 159 | 200 | if ("ᆧ" <= $uchr && $uchr <= "ᇂ") { |
| 160 | 201 | $T = \ord($uchr[2]) - 0xa7; |
| 161 | - 0 > $T && ($T += 0x40); | |
| 202 | + 0 > $T && $T += 0x40; | |
| 162 | 203 | $ulen += 3; |
| 163 | 204 | } |
| 164 | 205 | $L = 0xac00 + ($L * 21 + $V) * 28 + $T; |
| 165 | 206 | $lastUchr = \chr(0xe0 | $L >> 12) . \chr(0x80 | $L >> 6 & 0x3f) . \chr(0x80 | $L & 0x3f); |
| @@ -184,23 +225,23 @@ | ||
| 184 | 225 | while ($i < $len) { |
| 185 | 226 | if ($s[$i] < "\x80") { |
| 186 | 227 | // ASCII chars |
| 187 | 228 | if ($c) { |
| 188 | - \ksort($c); | |
| 189 | - $result .= \implode('', $c); | |
| 229 | + ksort($c); | |
| 230 | + $result .= implode('', $c); | |
| 190 | 231 | $c = []; |
| 191 | 232 | } |
| 192 | - $j = 1 + \strspn($s, $ASCII, $i + 1); | |
| 193 | - $result .= \substr($s, $i, $j); | |
| 233 | + $j = 1 + strspn($s, $ASCII, $i + 1); | |
| 234 | + $result .= substr($s, $i, $j); | |
| 194 | 235 | $i += $j; |
| 195 | 236 | continue; |
| 196 | 237 | } |
| 197 | 238 | $ulen = $ulenMask[$s[$i] & "\xf0"]; |
| 198 | - $uchr = \substr($s, $i, $ulen); | |
| 239 | + $uchr = substr($s, $i, $ulen); | |
| 199 | 240 | $i += $ulen; |
| 200 | 241 | if ($uchr < "가" || "힣" < $uchr) { |
| 201 | 242 | // Table lookup |
| 202 | - if ($uchr !== ($j = $compatMap[$uchr] ?? $decompMap[$uchr] ?? $uchr)) { | |
| 243 | + if ($uchr !== $j = $compatMap[$uchr] ?? $decompMap[$uchr] ?? $uchr) { | |
| 203 | 244 | $uchr = $j; |
| 204 | 245 | $j = \strlen($uchr); |
| 205 | 246 | $ulen = $uchr[0] < "\x80" ? 1 : $ulenMask[$uchr[0] & "\xf0"]; |
| 206 | 247 | if ($ulen != $j) { |
| @@ -207,9 +248,9 @@ | ||
| 207 | 248 | // Put trailing chars in $s |
| 208 | 249 | $j -= $ulen; |
| 209 | 250 | $i -= $j; |
| 210 | 251 | if (0 > $i) { |
| 211 | - $s = \str_repeat(' ', -$i) . $s; | |
| 252 | + $s = str_repeat(' ', -$i) . $s; | |
| 212 | 253 | $len -= $i; |
| 213 | 254 | $i = 0; |
| 214 | 255 | } |
| 215 | 256 | while ($j--) { |
| @@ -214,9 +255,9 @@ | ||
| 214 | 255 | } |
| 215 | 256 | while ($j--) { |
| 216 | 257 | $s[$i + $j] = $uchr[$ulen + $j]; |
| 217 | 258 | } |
| 218 | - $uchr = \substr($uchr, 0, $ulen); | |
| 259 | + $uchr = substr($uchr, 0, $ulen); | |
| 219 | 260 | } |
| 220 | 261 | } |
| 221 | 262 | if (isset($combClass[$uchr])) { |
| 222 | 263 | // Combining chars, for sorting |
| @@ -227,9 +268,9 @@ | ||
| 227 | 268 | continue; |
| 228 | 269 | } |
| 229 | 270 | } else { |
| 230 | 271 | // Hangul chars |
| 231 | - $uchr = \unpack('C*', $uchr); | |
| 272 | + $uchr = unpack('C*', $uchr); | |
| 232 | 273 | $j = ($uchr[1] - 224 << 12) + ($uchr[2] - 128 << 6) + $uchr[3] - 0xac80; |
| 233 | 274 | $uchr = "\xe1\x84" . \chr(0x80 + (int) ($j / 588)) . "\xe1\x85" . \chr(0xa1 + (int) ($j % 588 / 28)); |
| 234 | 275 | if ($j %= 28) { |
| 235 | 276 | $uchr .= $j < 25 ? "\xe1\x86" . \chr(0xa7 + $j) : "\xe1\x87" . \chr(0x67 + $j); |
| @@ -235,23 +276,23 @@ | ||
| 235 | 276 | $uchr .= $j < 25 ? "\xe1\x86" . \chr(0xa7 + $j) : "\xe1\x87" . \chr(0x67 + $j); |
| 236 | 277 | } |
| 237 | 278 | } |
| 238 | 279 | if ($c) { |
| 239 | - \ksort($c); | |
| 240 | - $result .= \implode('', $c); | |
| 280 | + ksort($c); | |
| 281 | + $result .= implode('', $c); | |
| 241 | 282 | $c = []; |
| 242 | 283 | } |
| 243 | 284 | $result .= $uchr; |
| 244 | 285 | } |
| 245 | 286 | if ($c) { |
| 246 | - \ksort($c); | |
| 247 | - $result .= \implode('', $c); | |
| 287 | + ksort($c); | |
| 288 | + $result .= implode('', $c); | |
| 248 | 289 | } |
| 249 | 290 | return $result; |
| 250 | 291 | } |
| 251 | 292 | private static function getData($file) |
| 252 | 293 | { |
| 253 | - if (\file_exists($file = __DIR__ . '/Resources/unidata/' . $file . '.php')) { | |
| 294 | + if (file_exists($file = __DIR__ . '/Resources/unidata/' . $file . '.php')) { | |
| 254 | 295 | return require $file; |
| 255 | 296 | } |
| 256 | 297 | return \false; |
| 257 | 298 | } |