Normalizer.php 11 KB

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