Normalizer.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Polyfill\Intl\Normalizer;
  11. /**
  12. * Normalizer is a PHP fallback implementation of the Normalizer class provided by the intl extension.
  13. *
  14. * It has been validated with Unicode 6.3 Normalization Conformance Test.
  15. * See http://www.unicode.org/reports/tr15/ for detailed info about Unicode normalizations.
  16. *
  17. * @author Nicolas Grekas <p@tchwork.com>
  18. *
  19. * @internal
  20. */
  21. class Normalizer
  22. {
  23. public const FORM_D = \Normalizer::FORM_D;
  24. public const FORM_KD = \Normalizer::FORM_KD;
  25. public const FORM_C = \Normalizer::FORM_C;
  26. public const FORM_KC = \Normalizer::FORM_KC;
  27. public const NFD = \Normalizer::NFD;
  28. public const NFKD = \Normalizer::NFKD;
  29. public const NFC = \Normalizer::NFC;
  30. public const NFKC = \Normalizer::NFKC;
  31. private static $C;
  32. private static $D;
  33. private static $KD;
  34. private static $cC;
  35. private static $ulenMask = ["\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4];
  36. private static $ASCII = "\x20\x65\x69\x61\x73\x6E\x74\x72\x6F\x6C\x75\x64\x5D\x5B\x63\x6D\x70\x27\x0A\x67\x7C\x68\x76\x2E\x66\x62\x2C\x3A\x3D\x2D\x71\x31\x30\x43\x32\x2A\x79\x78\x29\x28\x4C\x39\x41\x53\x2F\x50\x22\x45\x6A\x4D\x49\x6B\x33\x3E\x35\x54\x3C\x44\x34\x7D\x42\x7B\x38\x46\x77\x52\x36\x37\x55\x47\x4E\x3B\x4A\x7A\x56\x23\x48\x4F\x57\x5F\x26\x21\x4B\x3F\x58\x51\x25\x59\x5C\x09\x5A\x2B\x7E\x5E\x24\x40\x60\x7F\x00\x01\x02\x03\x04\x05\x06\x07\x08\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F";
  37. public static function isNormalized(string $s, int $form = self::FORM_C)
  38. {
  39. if (!\in_array($form, [self::NFD, self::NFKD, self::NFC, self::NFKC])) {
  40. return false;
  41. }
  42. if (!isset($s[strspn($s, self::$ASCII)])) {
  43. return true;
  44. }
  45. if (self::NFC == $form && preg_match('//u', $s) && !preg_match('/[^\x00-\x{2FF}]/u', $s)) {
  46. return true;
  47. }
  48. return self::normalize($s, $form) === $s;
  49. }
  50. public static function normalize(string $s, int $form = self::FORM_C)
  51. {
  52. if (!preg_match('//u', $s)) {
  53. return false;
  54. }
  55. switch ($form) {
  56. case self::NFC: $C = true; $K = false; break;
  57. case self::NFD: $C = false; $K = false; break;
  58. case self::NFKC: $C = true; $K = true; break;
  59. case self::NFKD: $C = false; $K = true; break;
  60. default:
  61. if (\defined('Normalizer::NONE') && \Normalizer::NONE == $form) {
  62. return $s;
  63. }
  64. if (80000 > \PHP_VERSION_ID) {
  65. return false;
  66. }
  67. throw new \ValueError('normalizer_normalize(): Argument #2 ($form) must be a a valid normalization form');
  68. }
  69. if ('' === $s) {
  70. return '';
  71. }
  72. if ($K && null === self::$KD) {
  73. self::$KD = self::getData('compatibilityDecomposition');
  74. }
  75. if (null === self::$D) {
  76. self::$D = self::getData('canonicalDecomposition');
  77. self::$cC = self::getData('combiningClass');
  78. }
  79. if (null !== $mbEncoding = (2 /* MB_OVERLOAD_STRING */ & (int) ini_get('mbstring.func_overload')) ? mb_internal_encoding() : null) {
  80. mb_internal_encoding('8bit');
  81. }
  82. $r = self::decompose($s, $K);
  83. if ($C) {
  84. if (null === self::$C) {
  85. self::$C = self::getData('canonicalComposition');
  86. }
  87. $r = self::recompose($r);
  88. }
  89. if (null !== $mbEncoding) {
  90. mb_internal_encoding($mbEncoding);
  91. }
  92. return $r;
  93. }
  94. private static function recompose($s)
  95. {
  96. $ASCII = self::$ASCII;
  97. $compMap = self::$C;
  98. $combClass = self::$cC;
  99. $ulenMask = self::$ulenMask;
  100. $result = $tail = '';
  101. $i = $s[0] < "\x80" ? 1 : $ulenMask[$s[0] & "\xF0"];
  102. $len = \strlen($s);
  103. $lastUchr = substr($s, 0, $i);
  104. $lastUcls = isset($combClass[$lastUchr]) ? 256 : 0;
  105. while ($i < $len) {
  106. if ($s[$i] < "\x80") {
  107. // ASCII chars
  108. if ($tail) {
  109. $lastUchr .= $tail;
  110. $tail = '';
  111. }
  112. if ($j = strspn($s, $ASCII, $i + 1)) {
  113. $lastUchr .= substr($s, $i, $j);
  114. $i += $j;
  115. }
  116. $result .= $lastUchr;
  117. $lastUchr = $s[$i];
  118. $lastUcls = 0;
  119. ++$i;
  120. continue;
  121. }
  122. $ulen = $ulenMask[$s[$i] & "\xF0"];
  123. $uchr = substr($s, $i, $ulen);
  124. if ($lastUchr < "\xE1\x84\x80" || "\xE1\x84\x92" < $lastUchr
  125. || $uchr < "\xE1\x85\xA1" || "\xE1\x85\xB5" < $uchr
  126. || $lastUcls) {
  127. // Table lookup and combining chars composition
  128. $ucls = $combClass[$uchr] ?? 0;
  129. if (isset($compMap[$lastUchr.$uchr]) && (!$lastUcls || $lastUcls < $ucls)) {
  130. $lastUchr = $compMap[$lastUchr.$uchr];
  131. } elseif ($lastUcls = $ucls) {
  132. $tail .= $uchr;
  133. } else {
  134. if ($tail) {
  135. $lastUchr .= $tail;
  136. $tail = '';
  137. }
  138. $result .= $lastUchr;
  139. $lastUchr = $uchr;
  140. }
  141. } else {
  142. // Hangul chars
  143. $L = \ord($lastUchr[2]) - 0x80;
  144. $V = \ord($uchr[2]) - 0xA1;
  145. $T = 0;
  146. $uchr = substr($s, $i + $ulen, 3);
  147. if ("\xE1\x86\xA7" <= $uchr && $uchr <= "\xE1\x87\x82") {
  148. $T = \ord($uchr[2]) - 0xA7;
  149. 0 > $T && $T += 0x40;
  150. $ulen += 3;
  151. }
  152. $L = 0xAC00 + ($L * 21 + $V) * 28 + $T;
  153. $lastUchr = \chr(0xE0 | $L >> 12).\chr(0x80 | $L >> 6 & 0x3F).\chr(0x80 | $L & 0x3F);
  154. }
  155. $i += $ulen;
  156. }
  157. return $result.$lastUchr.$tail;
  158. }
  159. private static function decompose($s, $c)
  160. {
  161. $result = '';
  162. $ASCII = self::$ASCII;
  163. $decompMap = self::$D;
  164. $combClass = self::$cC;
  165. $ulenMask = self::$ulenMask;
  166. if ($c) {
  167. $compatMap = self::$KD;
  168. }
  169. $c = [];
  170. $i = 0;
  171. $len = \strlen($s);
  172. while ($i < $len) {
  173. if ($s[$i] < "\x80") {
  174. // ASCII chars
  175. if ($c) {
  176. ksort($c);
  177. $result .= implode('', $c);
  178. $c = [];
  179. }
  180. $j = 1 + strspn($s, $ASCII, $i + 1);
  181. $result .= substr($s, $i, $j);
  182. $i += $j;
  183. continue;
  184. }
  185. $ulen = $ulenMask[$s[$i] & "\xF0"];
  186. $uchr = substr($s, $i, $ulen);
  187. $i += $ulen;
  188. if ($uchr < "\xEA\xB0\x80" || "\xED\x9E\xA3" < $uchr) {
  189. // Table lookup
  190. if ($uchr !== $j = $compatMap[$uchr] ?? ($decompMap[$uchr] ?? $uchr)) {
  191. $uchr = $j;
  192. $j = \strlen($uchr);
  193. $ulen = $uchr[0] < "\x80" ? 1 : $ulenMask[$uchr[0] & "\xF0"];
  194. if ($ulen != $j) {
  195. // Put trailing chars in $s
  196. $j -= $ulen;
  197. $i -= $j;
  198. if (0 > $i) {
  199. $s = str_repeat(' ', -$i).$s;
  200. $len -= $i;
  201. $i = 0;
  202. }
  203. while ($j--) {
  204. $s[$i + $j] = $uchr[$ulen + $j];
  205. }
  206. $uchr = substr($uchr, 0, $ulen);
  207. }
  208. }
  209. if (isset($combClass[$uchr])) {
  210. // Combining chars, for sorting
  211. if (!isset($c[$combClass[$uchr]])) {
  212. $c[$combClass[$uchr]] = '';
  213. }
  214. $c[$combClass[$uchr]] .= $uchr;
  215. continue;
  216. }
  217. } else {
  218. // Hangul chars
  219. $uchr = unpack('C*', $uchr);
  220. $j = (($uchr[1] - 224) << 12) + (($uchr[2] - 128) << 6) + $uchr[3] - 0xAC80;
  221. $uchr = "\xE1\x84".\chr(0x80 + (int) ($j / 588))
  222. ."\xE1\x85".\chr(0xA1 + (int) (($j % 588) / 28));
  223. if ($j %= 28) {
  224. $uchr .= $j < 25
  225. ? ("\xE1\x86".\chr(0xA7 + $j))
  226. : ("\xE1\x87".\chr(0x67 + $j));
  227. }
  228. }
  229. if ($c) {
  230. ksort($c);
  231. $result .= implode('', $c);
  232. $c = [];
  233. }
  234. $result .= $uchr;
  235. }
  236. if ($c) {
  237. ksort($c);
  238. $result .= implode('', $c);
  239. }
  240. return $result;
  241. }
  242. private static function getData($file)
  243. {
  244. if (file_exists($file = __DIR__.'/Resources/unidata/'.$file.'.php')) {
  245. return require $file;
  246. }
  247. return false;
  248. }
  249. }