Normalizer.php 9.2 KB

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