Normalizer.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. /**
  38. * @param string $s
  39. * @param int $form
  40. */
  41. public static function isNormalized($s, $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. /**
  55. * @param string $s
  56. * @param int $form
  57. */
  58. public static function normalize($s, $form = self::FORM_C)
  59. {
  60. if (!preg_match('//u', $s)) {
  61. return false;
  62. }
  63. switch ($form) {
  64. case self::NFC: $C = true; $K = false; break;
  65. case self::NFD: $C = false; $K = false; break;
  66. case self::NFKC: $C = true; $K = true; break;
  67. case self::NFKD: $C = false; $K = true; break;
  68. default:
  69. if (\defined('Normalizer::NONE') && \Normalizer::NONE == $form) {
  70. return $s;
  71. }
  72. if (80000 > \PHP_VERSION_ID) {
  73. return false;
  74. }
  75. throw new \ValueError('normalizer_normalize(): Argument #2 ($form) must be a a valid normalization form');
  76. }
  77. if ('' === $s) {
  78. return '';
  79. }
  80. if ($K && null === self::$KD) {
  81. self::$KD = self::getData('compatibilityDecomposition');
  82. }
  83. if (null === self::$D) {
  84. self::$D = self::getData('canonicalDecomposition');
  85. self::$cC = self::getData('combiningClass');
  86. }
  87. if (null !== $mbEncoding = (2 /* MB_OVERLOAD_STRING */ & (int) \ini_get('mbstring.func_overload')) ? mb_internal_encoding() : null) {
  88. mb_internal_encoding('8bit');
  89. }
  90. $r = self::decompose($s, $K);
  91. if ($C) {
  92. if (null === self::$C) {
  93. self::$C = self::getData('canonicalComposition');
  94. }
  95. $r = self::recompose($r);
  96. }
  97. if (null !== $mbEncoding) {
  98. mb_internal_encoding($mbEncoding);
  99. }
  100. return $r;
  101. }
  102. private static function recompose($s)
  103. {
  104. $ASCII = self::$ASCII;
  105. $compMap = self::$C;
  106. $combClass = self::$cC;
  107. $ulenMask = self::$ulenMask;
  108. $result = $tail = '';
  109. $i = $s[0] < "\x80" ? 1 : $ulenMask[$s[0] & "\xF0"];
  110. $len = \strlen($s);
  111. $lastUchr = substr($s, 0, $i);
  112. $lastUcls = isset($combClass[$lastUchr]) ? 256 : 0;
  113. while ($i < $len) {
  114. if ($s[$i] < "\x80") {
  115. // ASCII chars
  116. if ($tail) {
  117. $lastUchr .= $tail;
  118. $tail = '';
  119. }
  120. if ($j = strspn($s, $ASCII, $i + 1)) {
  121. $lastUchr .= substr($s, $i, $j);
  122. $i += $j;
  123. }
  124. $result .= $lastUchr;
  125. $lastUchr = $s[$i];
  126. $lastUcls = 0;
  127. ++$i;
  128. continue;
  129. }
  130. $ulen = $ulenMask[$s[$i] & "\xF0"];
  131. $uchr = substr($s, $i, $ulen);
  132. if ($lastUchr < "\xE1\x84\x80" || "\xE1\x84\x92" < $lastUchr
  133. || $uchr < "\xE1\x85\xA1" || "\xE1\x85\xB5" < $uchr
  134. || $lastUcls) {
  135. // Table lookup and combining chars composition
  136. $ucls = $combClass[$uchr] ?? 0;
  137. if (isset($compMap[$lastUchr.$uchr]) && (!$lastUcls || $lastUcls < $ucls)) {
  138. $lastUchr = $compMap[$lastUchr.$uchr];
  139. } elseif ($lastUcls = $ucls) {
  140. $tail .= $uchr;
  141. } else {
  142. if ($tail) {
  143. $lastUchr .= $tail;
  144. $tail = '';
  145. }
  146. $result .= $lastUchr;
  147. $lastUchr = $uchr;
  148. }
  149. } else {
  150. // Hangul chars
  151. $L = \ord($lastUchr[2]) - 0x80;
  152. $V = \ord($uchr[2]) - 0xA1;
  153. $T = 0;
  154. $uchr = substr($s, $i + $ulen, 3);
  155. if ("\xE1\x86\xA7" <= $uchr && $uchr <= "\xE1\x87\x82") {
  156. $T = \ord($uchr[2]) - 0xA7;
  157. 0 > $T && $T += 0x40;
  158. $ulen += 3;
  159. }
  160. $L = 0xAC00 + ($L * 21 + $V) * 28 + $T;
  161. $lastUchr = \chr(0xE0 | $L >> 12).\chr(0x80 | $L >> 6 & 0x3F).\chr(0x80 | $L & 0x3F);
  162. }
  163. $i += $ulen;
  164. }
  165. return $result.$lastUchr.$tail;
  166. }
  167. private static function decompose($s, $c)
  168. {
  169. $result = '';
  170. $ASCII = self::$ASCII;
  171. $decompMap = self::$D;
  172. $combClass = self::$cC;
  173. $ulenMask = self::$ulenMask;
  174. if ($c) {
  175. $compatMap = self::$KD;
  176. }
  177. $c = [];
  178. $i = 0;
  179. $len = \strlen($s);
  180. while ($i < $len) {
  181. if ($s[$i] < "\x80") {
  182. // ASCII chars
  183. if ($c) {
  184. ksort($c);
  185. $result .= implode('', $c);
  186. $c = [];
  187. }
  188. $j = 1 + strspn($s, $ASCII, $i + 1);
  189. $result .= substr($s, $i, $j);
  190. $i += $j;
  191. continue;
  192. }
  193. $ulen = $ulenMask[$s[$i] & "\xF0"];
  194. $uchr = substr($s, $i, $ulen);
  195. $i += $ulen;
  196. if ($uchr < "\xEA\xB0\x80" || "\xED\x9E\xA3" < $uchr) {
  197. // Table lookup
  198. if ($uchr !== $j = $compatMap[$uchr] ?? ($decompMap[$uchr] ?? $uchr)) {
  199. $uchr = $j;
  200. $j = \strlen($uchr);
  201. $ulen = $uchr[0] < "\x80" ? 1 : $ulenMask[$uchr[0] & "\xF0"];
  202. if ($ulen != $j) {
  203. // Put trailing chars in $s
  204. $j -= $ulen;
  205. $i -= $j;
  206. if (0 > $i) {
  207. $s = str_repeat(' ', -$i).$s;
  208. $len -= $i;
  209. $i = 0;
  210. }
  211. while ($j--) {
  212. $s[$i + $j] = $uchr[$ulen + $j];
  213. }
  214. $uchr = substr($uchr, 0, $ulen);
  215. }
  216. }
  217. if (isset($combClass[$uchr])) {
  218. // Combining chars, for sorting
  219. if (!isset($c[$combClass[$uchr]])) {
  220. $c[$combClass[$uchr]] = '';
  221. }
  222. $c[$combClass[$uchr]] .= $uchr;
  223. continue;
  224. }
  225. } else {
  226. // Hangul chars
  227. $uchr = unpack('C*', $uchr);
  228. $j = (($uchr[1] - 224) << 12) + (($uchr[2] - 128) << 6) + $uchr[3] - 0xAC80;
  229. $uchr = "\xE1\x84".\chr(0x80 + (int) ($j / 588))
  230. ."\xE1\x85".\chr(0xA1 + (int) (($j % 588) / 28));
  231. if ($j %= 28) {
  232. $uchr .= $j < 25
  233. ? ("\xE1\x86".\chr(0xA7 + $j))
  234. : ("\xE1\x87".\chr(0x67 + $j));
  235. }
  236. }
  237. if ($c) {
  238. ksort($c);
  239. $result .= implode('', $c);
  240. $c = [];
  241. }
  242. $result .= $uchr;
  243. }
  244. if ($c) {
  245. ksort($c);
  246. $result .= implode('', $c);
  247. }
  248. return $result;
  249. }
  250. private static function getData($file)
  251. {
  252. if (file_exists($file = __DIR__.'/Resources/unidata/'.$file.'.php')) {
  253. return require $file;
  254. }
  255. return false;
  256. }
  257. }