Grapheme.php
1 year ago
LICENSE
1 year ago
README.md
1 year ago
bootstrap.php
1 year ago
bootstrap80.php
1 year ago
composer.json
1 year ago
Grapheme.php
258 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | |
| 12 | namespace Symfony\Polyfill\Intl\Grapheme; |
| 13 | |
| 14 | \define('SYMFONY_GRAPHEME_CLUSTER_RX', ((float) \PCRE_VERSION < 10 ? (float) \PCRE_VERSION >= 8.32 : (float) \PCRE_VERSION >= 10.39) ? '\X' : Grapheme::GRAPHEME_CLUSTER_RX); |
| 15 | |
| 16 | /** |
| 17 | * Partial intl implementation in pure PHP. |
| 18 | * |
| 19 | * Implemented: |
| 20 | * - grapheme_extract - Extract a sequence of grapheme clusters from a text buffer, which must be encoded in UTF-8 |
| 21 | * - grapheme_stripos - Find position (in grapheme units) of first occurrence of a case-insensitive string |
| 22 | * - grapheme_stristr - Returns part of haystack string from the first occurrence of case-insensitive needle to the end of haystack |
| 23 | * - grapheme_strlen - Get string length in grapheme units |
| 24 | * - grapheme_strpos - Find position (in grapheme units) of first occurrence of a string |
| 25 | * - grapheme_strripos - Find position (in grapheme units) of last occurrence of a case-insensitive string |
| 26 | * - grapheme_strrpos - Find position (in grapheme units) of last occurrence of a string |
| 27 | * - grapheme_strstr - Returns part of haystack string from the first occurrence of needle to the end of haystack |
| 28 | * - grapheme_substr - Return part of a string |
| 29 | * |
| 30 | * @author Nicolas Grekas <p@tchwork.com> |
| 31 | * |
| 32 | * @internal |
| 33 | */ |
| 34 | final class Grapheme |
| 35 | { |
| 36 | // (CRLF|([ZWNJ-ZWJ]|T+|L*(LV?V+|LV|LVT)T*|L+|[^Control])[Extend]*|[Control]) |
| 37 | // This regular expression is a work around for http://bugs.exim.org/1279 |
| 38 | public const GRAPHEME_CLUSTER_RX = '(?:\r\n|(?:[ -~\x{200C}\x{200D}]|[ᆨ-ᇹ]+|[ᄀ-� |
| 39 | �]*(?:[가개갸걔거게겨계고과괘괴교구궈궤귀규그긔기까깨꺄꺠꺼께껴꼐꼬꽈꽤꾀꾜꾸꿔꿰뀌뀨끄끠끼나내냐냬너네� |
| 40 | �� |
| 41 | �� |
| 42 | �놔놰뇌뇨누눠눼뉘뉴느늬니다대댜댸더데뎌뎨도돠돼되됴두둬뒈뒤듀드듸디따때땨떄떠떼뗘뗴또똬뙈뙤뚀뚜뚸뛔뛰뜌뜨띄띠라래랴럐러레려례로롸뢔뢰료루뤄뤠뤼류르릐리마매먀먜머메며몌모뫄뫠뫼묘무뭐뭬뮈뮤므믜미바배뱌뱨버베벼볘보봐봬뵈뵤부붜붸뷔뷰브븨비빠빼뺘뺴뻐뻬뼈뼤뽀뽜뽸뾔뾰뿌뿨쀄쀠쀼쁘쁴삐사새샤섀서세� |
| 43 | �� |
| 44 | �소솨쇄쇠쇼수숴쉐쉬슈스싀시싸쌔쌰썌써쎄쎠쎼쏘쏴쐐쐬쑈쑤쒀쒜쒸쓔쓰씌씨아애야얘어에여예오와왜외요우워웨위유으의이자재쟈쟤저제져졔조좌좨죄죠주줘줴쥐쥬즈즤지짜째쨔쨰쩌쩨쪄쪠쪼쫘쫴쬐쬬쭈쭤쮀쮜쮸쯔쯰찌차채챠챼처체쳐쳬초촤쵀최쵸추춰췌취츄츠츼치카캐캬컈커케켜켸코콰쾌쾨쿄쿠쿼퀘퀴큐크킈키타태탸턔터� |
| 45 | �� |
| 46 | �톄토톼퇘퇴툐투퉈퉤튀튜트틔티파패퍄퍠퍼페펴폐포퐈퐤푀표푸풔풰퓌퓨프픠피하해햐햬허헤혀혜호화홰회효후훠훼휘휴흐희히]?[� |
| 47 | �-ᆢ]+|[가-힣])[ᆨ-ᇹ]*|[ᄀ-� |
| 48 | �]+|[^\p{Cc}\p{Cf}\p{Zl}\p{Zp}])[\p{Mn}\p{Me}\x{09BE}\x{09D7}\x{0B3E}\x{0B57}\x{0BBE}\x{0BD7}\x{0CC2}\x{0CD5}\x{0CD6}\x{0D3E}\x{0D57}\x{0DCF}\x{0DDF}\x{200C}\x{200D}\x{1D165}\x{1D16E}-\x{1D172}]*|[\p{Cc}\p{Cf}\p{Zl}\p{Zp}])'; |
| 49 | |
| 50 | private const CASE_FOLD = [ |
| 51 | ['µ', 'ſ', "\xCD\x85", 'ς', "\xCF\x90", "\xCF\x91", "\xCF\x95", "\xCF\x96", "\xCF\xB0", "\xCF\xB1", "\xCF\xB5", "\xE1\xBA\x9B", "\xE1\xBE\xBE"], |
| 52 | ['μ', 's', 'ι', 'σ', 'β', 'θ', 'φ', 'π', 'κ', 'ρ', 'ε', "\xE1\xB9\xA1", 'ι'], |
| 53 | ]; |
| 54 | |
| 55 | public static function grapheme_extract($s, $size, $type = \GRAPHEME_EXTR_COUNT, $start = 0, &$next = 0) |
| 56 | { |
| 57 | if (0 > $start) { |
| 58 | $start = \strlen($s) + $start; |
| 59 | } |
| 60 | |
| 61 | if (!\is_scalar($s)) { |
| 62 | $hasError = false; |
| 63 | set_error_handler(function () use (&$hasError) { $hasError = true; }); |
| 64 | $next = substr($s, $start); |
| 65 | restore_error_handler(); |
| 66 | if ($hasError) { |
| 67 | substr($s, $start); |
| 68 | $s = ''; |
| 69 | } else { |
| 70 | $s = $next; |
| 71 | } |
| 72 | } else { |
| 73 | $s = substr($s, $start); |
| 74 | } |
| 75 | $size = (int) $size; |
| 76 | $type = (int) $type; |
| 77 | $start = (int) $start; |
| 78 | |
| 79 | if (\GRAPHEME_EXTR_COUNT !== $type && \GRAPHEME_EXTR_MAXBYTES !== $type && \GRAPHEME_EXTR_MAXCHARS !== $type) { |
| 80 | if (80000 > \PHP_VERSION_ID) { |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | throw new \ValueError('grapheme_extract(): Argument #3 ($type) must be one of GRAPHEME_EXTR_COUNT, GRAPHEME_EXTR_MAXBYTES, or GRAPHEME_EXTR_MAXCHARS'); |
| 85 | } |
| 86 | |
| 87 | if (!isset($s[0]) || 0 > $size || 0 > $start) { |
| 88 | return false; |
| 89 | } |
| 90 | if (0 === $size) { |
| 91 | return ''; |
| 92 | } |
| 93 | |
| 94 | $next = $start; |
| 95 | |
| 96 | $s = preg_split('/('.SYMFONY_GRAPHEME_CLUSTER_RX.')/u', "\r\n".$s, $size + 1, \PREG_SPLIT_NO_EMPTY | \PREG_SPLIT_DELIM_CAPTURE); |
| 97 | |
| 98 | if (!isset($s[1])) { |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | $i = 1; |
| 103 | $ret = ''; |
| 104 | |
| 105 | do { |
| 106 | if (\GRAPHEME_EXTR_COUNT === $type) { |
| 107 | --$size; |
| 108 | } elseif (\GRAPHEME_EXTR_MAXBYTES === $type) { |
| 109 | $size -= \strlen($s[$i]); |
| 110 | } else { |
| 111 | $size -= iconv_strlen($s[$i], 'UTF-8//IGNORE'); |
| 112 | } |
| 113 | |
| 114 | if ($size >= 0) { |
| 115 | $ret .= $s[$i]; |
| 116 | } |
| 117 | } while (isset($s[++$i]) && $size > 0); |
| 118 | |
| 119 | $next += \strlen($ret); |
| 120 | |
| 121 | return $ret; |
| 122 | } |
| 123 | |
| 124 | public static function grapheme_strlen($s) |
| 125 | { |
| 126 | preg_replace('/'.SYMFONY_GRAPHEME_CLUSTER_RX.'/u', '', $s, -1, $len); |
| 127 | |
| 128 | return 0 === $len && '' !== $s ? null : $len; |
| 129 | } |
| 130 | |
| 131 | public static function grapheme_substr($s, $start, $len = null) |
| 132 | { |
| 133 | if (null === $len) { |
| 134 | $len = 2147483647; |
| 135 | } |
| 136 | |
| 137 | preg_match_all('/'.SYMFONY_GRAPHEME_CLUSTER_RX.'/u', $s, $s); |
| 138 | |
| 139 | $slen = \count($s[0]); |
| 140 | $start = (int) $start; |
| 141 | |
| 142 | if (0 > $start) { |
| 143 | $start += $slen; |
| 144 | } |
| 145 | if (0 > $start) { |
| 146 | if (\PHP_VERSION_ID < 80000) { |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | $start = 0; |
| 151 | } |
| 152 | if ($start >= $slen) { |
| 153 | return \PHP_VERSION_ID >= 80000 ? '' : false; |
| 154 | } |
| 155 | |
| 156 | $rem = $slen - $start; |
| 157 | |
| 158 | if (0 > $len) { |
| 159 | $len += $rem; |
| 160 | } |
| 161 | if (0 === $len) { |
| 162 | return ''; |
| 163 | } |
| 164 | if (0 > $len) { |
| 165 | return \PHP_VERSION_ID >= 80000 ? '' : false; |
| 166 | } |
| 167 | if ($len > $rem) { |
| 168 | $len = $rem; |
| 169 | } |
| 170 | |
| 171 | return implode('', \array_slice($s[0], $start, $len)); |
| 172 | } |
| 173 | |
| 174 | public static function grapheme_strpos($s, $needle, $offset = 0) |
| 175 | { |
| 176 | return self::grapheme_position($s, $needle, $offset, 0); |
| 177 | } |
| 178 | |
| 179 | public static function grapheme_stripos($s, $needle, $offset = 0) |
| 180 | { |
| 181 | return self::grapheme_position($s, $needle, $offset, 1); |
| 182 | } |
| 183 | |
| 184 | public static function grapheme_strrpos($s, $needle, $offset = 0) |
| 185 | { |
| 186 | return self::grapheme_position($s, $needle, $offset, 2); |
| 187 | } |
| 188 | |
| 189 | public static function grapheme_strripos($s, $needle, $offset = 0) |
| 190 | { |
| 191 | return self::grapheme_position($s, $needle, $offset, 3); |
| 192 | } |
| 193 | |
| 194 | public static function grapheme_stristr($s, $needle, $beforeNeedle = false) |
| 195 | { |
| 196 | return mb_stristr($s, $needle, $beforeNeedle, 'UTF-8'); |
| 197 | } |
| 198 | |
| 199 | public static function grapheme_strstr($s, $needle, $beforeNeedle = false) |
| 200 | { |
| 201 | return mb_strstr($s, $needle, $beforeNeedle, 'UTF-8'); |
| 202 | } |
| 203 | |
| 204 | private static function grapheme_position($s, $needle, $offset, $mode) |
| 205 | { |
| 206 | $needle = (string) $needle; |
| 207 | if (80000 > \PHP_VERSION_ID && !preg_match('/./us', $needle)) { |
| 208 | return false; |
| 209 | } |
| 210 | $s = (string) $s; |
| 211 | if (!preg_match('/./us', $s)) { |
| 212 | return false; |
| 213 | } |
| 214 | if ($offset > 0) { |
| 215 | $s = self::grapheme_substr($s, $offset); |
| 216 | } elseif ($offset < 0) { |
| 217 | if (2 > $mode) { |
| 218 | $offset += self::grapheme_strlen($s); |
| 219 | $s = self::grapheme_substr($s, $offset); |
| 220 | if (0 > $offset) { |
| 221 | $offset = 0; |
| 222 | } |
| 223 | } elseif (0 > $offset += self::grapheme_strlen($needle)) { |
| 224 | $s = self::grapheme_substr($s, 0, $offset); |
| 225 | $offset = 0; |
| 226 | } else { |
| 227 | $offset = 0; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | // As UTF-8 is self-synchronizing, and we have ensured the strings are valid UTF-8, |
| 232 | // we can use normal binary string functions here. For case-insensitive searches, |
| 233 | // case fold the strings first. |
| 234 | $caseInsensitive = $mode & 1; |
| 235 | $reverse = $mode & 2; |
| 236 | if ($caseInsensitive) { |
| 237 | // Use the same case folding mode as mbstring does for mb_stripos(). |
| 238 | // Stick to SIMPLE case folding to avoid changing the length of the string, which |
| 239 | // might result in offsets being shifted. |
| 240 | $mode = \defined('MB_CASE_FOLD_SIMPLE') ? \MB_CASE_FOLD_SIMPLE : \MB_CASE_LOWER; |
| 241 | $s = mb_convert_case($s, $mode, 'UTF-8'); |
| 242 | $needle = mb_convert_case($needle, $mode, 'UTF-8'); |
| 243 | |
| 244 | if (!\defined('MB_CASE_FOLD_SIMPLE')) { |
| 245 | $s = str_replace(self::CASE_FOLD[0], self::CASE_FOLD[1], $s); |
| 246 | $needle = str_replace(self::CASE_FOLD[0], self::CASE_FOLD[1], $needle); |
| 247 | } |
| 248 | } |
| 249 | if ($reverse) { |
| 250 | $needlePos = strrpos($s, $needle); |
| 251 | } else { |
| 252 | $needlePos = strpos($s, $needle); |
| 253 | } |
| 254 | |
| 255 | return false !== $needlePos ? self::grapheme_strlen(substr($s, 0, $needlePos)) + $offset : false; |
| 256 | } |
| 257 | } |
| 258 |