Normalizer.php 9.2 KB

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