Debian Codegraph @

vendor/symfony/polyfill-intl-grapheme/Grapheme.php

raw
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
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.44) ? '\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 * - grapheme_str_split - Splits a string into an array of individual or chunks of graphemes
30 * - grapheme_levenshtein - Calculate the grapheme-unit Levenshtein distance between two strings
31 * - grapheme_strrev - Reverse a string by grapheme clusters
32 *
33 * @author Nicolas Grekas <[email protected]>
34 *
35 * @internal
36 */
37 final class Grapheme
38 {
39 // (CRLF|RI RI?|(([ZWNJ-ZWJ]|T+|L*(LV?V+|LV|LVT)T*|L+|[^Control])[Extend]*)(ZWJ([ZWNJ-ZWJ]|T+|L*(LV?V+|LV|LVT)T*|L+|[^Control])[Extend]*)*|[Control])
40 // This regular expression is a work around for http://bugs.exim.org/1279
41 public const GRAPHEME_CLUSTER_RX = '(?:\r\n|[\x{1F1E6}-\x{1F1FF}][\x{1F1E6}-\x{1F1FF}]?|(?:[ -~\x{200C}\x{200D}]|[ᆨ-ᇹ]+|[ᄀ-ᅟ]*(?:[가개갸걔거게겨계고과괘괴교구궈궤귀규그긔기까깨꺄꺠꺼께껴꼐꼬꽈꽤꾀꾜꾸꿔꿰뀌뀨끄끠끼나내냐냬너네녀녜노놔놰뇌뇨누눠눼뉘뉴느늬니다대댜댸더데뎌뎨도돠돼되됴두둬뒈뒤듀드듸디따때땨떄떠떼뗘뗴또똬뙈뙤뚀뚜뚸뛔뛰뜌뜨띄띠라래랴럐러레려례로롸뢔뢰료루뤄뤠뤼류르릐리마매먀먜머메며몌모뫄뫠뫼묘무뭐뭬뮈뮤므믜미바배뱌뱨버베벼볘보봐봬뵈뵤부붜붸뷔뷰브븨비빠빼뺘뺴뻐뻬뼈뼤뽀뽜뽸뾔뾰뿌뿨쀄쀠쀼쁘쁴삐사새샤섀서세셔셰소솨쇄쇠쇼수숴쉐쉬슈스싀시싸쌔쌰썌써쎄쎠쎼쏘쏴쐐쐬쑈쑤쒀쒜쒸쓔쓰씌씨아애야얘어에여예오와왜외요우워웨위유으의이자재쟈쟤저제져졔조좌좨죄죠주줘줴쥐쥬즈즤지짜째쨔쨰쩌쩨쪄쪠쪼쫘쫴쬐쬬쭈쭤쮀쮜쮸쯔쯰찌차채챠챼처체쳐쳬초촤쵀최쵸추춰췌취츄츠츼치카캐캬컈커케켜켸코콰쾌쾨쿄쿠쿼퀘퀴큐크킈키타태탸턔터테텨톄토톼퇘퇴툐투퉈퉤튀튜트틔티파패퍄퍠퍼페펴폐포퐈퐤푀표푸풔풰퓌퓨프픠피하해햐햬허헤혀혜호화홰회효후훠훼휘휴흐희히]?[ᅠ-ᆢ]+|[가-힣])[ᆨ-ᇹ]*|[ᄀ-ᅟ]+|[^\p{Cc}\p{Cf}\p{Zl}\p{Zp}])[\p{Mn}\p{Mc}\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{1D165}\x{1D16E}-\x{1D172}\x{1F3FB}-\x{1F3FF}\x{FE0E}-\x{FE0F}\x{E0020}-\x{E007F}]*(?:\x{200D}(?:[\x{1F1E6}-\x{1F1FF}]|[ -~\x{200C}\x{200D}]|[ᆨ-ᇹ]+|[ᄀ-ᅟ]*(?:[가-힣]?[ᅠ-ᆢ]+|[가-힣])[ᆨ-ᇹ]*|[ᄀ-ᅟ]+|[^\p{Cc}\p{Cf}\p{Zl}\p{Zp}])[\p{Mn}\p{Mc}\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{1D165}\x{1D16E}-\x{1D172}\x{1F3FB}-\x{1F3FF}\x{FE0E}-\x{FE0F}\x{E0020}-\x{E007F}]*)*|[\p{Cc}\p{Cf}\p{Zl}\p{Zp}])';
42
43 private const CASE_FOLD = [
44 ['µ', 'ſ', "\xCD\x85", 'ς', "\xCF\x90", "\xCF\x91", "\xCF\x95", "\xCF\x96", "\xCF\xB0", "\xCF\xB1", "\xCF\xB5", "\xE1\xBA\x9B", "\xE1\xBE\xBE"],
45 ['μ', 's', 'ι', 'σ', 'β', 'θ', 'φ', 'π', 'κ', 'ρ', 'ε', "\xE1\xB9\xA1", 'ι'],
46 ];
47
48 public static function grapheme_extract($s, $size, $type = \GRAPHEME_EXTR_COUNT, $start = 0, &$next = 0)
49 {
50 if (0 > $start) {
51 $start = \strlen($s) + $start;
52 }
53
54 if (!\is_scalar($s)) {
55 $hasError = false;
56 set_error_handler(static function () use (&$hasError) { $hasError = true; });
57 $next = substr($s, $start);
58 restore_error_handler();
59 if ($hasError) {
60 substr($s, $start);
61 $s = '';
62 } else {
63 $s = $next;
64 }
65 } else {
66 $s = substr($s, $start);
67 }
68 $size = (int) $size;
69 $type = (int) $type;
70 $start = (int) $start;
71
72 if (\GRAPHEME_EXTR_COUNT !== $type && \GRAPHEME_EXTR_MAXBYTES !== $type && \GRAPHEME_EXTR_MAXCHARS !== $type) {
73 if (80000 > \PHP_VERSION_ID) {
74 return false;
75 }
76
77 throw new \ValueError('grapheme_extract(): Argument #3 ($type) must be one of GRAPHEME_EXTR_COUNT, GRAPHEME_EXTR_MAXBYTES, or GRAPHEME_EXTR_MAXCHARS');
78 }
79
80 if (!isset($s[0]) || 0 > $size || 0 > $start) {
81 return false;
82 }
83 if (0 === $size) {
84 return '';
85 }
86
87 $next = $start;
88
89 $s = preg_split('/('.SYMFONY_GRAPHEME_CLUSTER_RX.')/u', "\r\n".$s, $size + 1, \PREG_SPLIT_NO_EMPTY | \PREG_SPLIT_DELIM_CAPTURE);
90
91 if (!isset($s[1])) {
92 return false;
93 }
94
95 $i = 1;
96 $ret = '';
97
98 do {
99 if (\GRAPHEME_EXTR_COUNT === $type) {
100 --$size;
101 } elseif (\GRAPHEME_EXTR_MAXBYTES === $type) {
102 $size -= \strlen($s[$i]);
103 } else {
104 $size -= iconv_strlen($s[$i], 'UTF-8//IGNORE');
105 }
106
107 if ($size >= 0) {
108 $ret .= $s[$i];
109 }
110 } while (isset($s[++$i]) && $size > 0);
111
112 $next += \strlen($ret);
113
114 return $ret;
115 }
116
117 public static function grapheme_strlen($s)
118 {
119 preg_replace('/'.SYMFONY_GRAPHEME_CLUSTER_RX.'/u', '', $s, -1, $len);
120
121 return 0 === $len && '' !== $s ? null : $len;
122 }
123
124 public static function grapheme_substr($s, $start, $len = null)
125 {
126 if (null === $len) {
127 $len = 2147483647;
128 }
129
130 preg_match_all('/'.SYMFONY_GRAPHEME_CLUSTER_RX.'/u', $s, $s);
131
132 $slen = \count($s[0]);
133 $start = (int) $start;
134
135 if (0 > $start) {
136 $start += $slen;
137 }
138 if (0 > $start) {
139 if (\PHP_VERSION_ID < 80000) {
140 return false;
141 }
142
143 $start = 0;
144 }
145 if ($start >= $slen) {
146 return \PHP_VERSION_ID >= 80000 ? '' : false;
147 }
148
149 $rem = $slen - $start;
150
151 if (0 > $len) {
152 $len += $rem;
153 }
154 if (0 === $len) {
155 return '';
156 }
157 if (0 > $len) {
158 return \PHP_VERSION_ID >= 80000 ? '' : false;
159 }
160 if ($len > $rem) {
161 $len = $rem;
162 }
163
164 return implode('', \array_slice($s[0], $start, $len));
165 }
166
167 public static function grapheme_strpos($s, $needle, $offset = 0)
168 {
169 return self::grapheme_position($s, $needle, $offset, 0);
170 }
171
172 public static function grapheme_stripos($s, $needle, $offset = 0)
173 {
174 return self::grapheme_position($s, $needle, $offset, 1);
175 }
176
177 public static function grapheme_strrpos($s, $needle, $offset = 0)
178 {
179 return self::grapheme_position($s, $needle, $offset, 2);
180 }
181
182 public static function grapheme_strripos($s, $needle, $offset = 0)
183 {
184 return self::grapheme_position($s, $needle, $offset, 3);
185 }
186
187 public static function grapheme_stristr($s, $needle, $beforeNeedle = false)
188 {
189 return mb_stristr($s, $needle, $beforeNeedle, 'UTF-8');
190 }
191
192 public static function grapheme_strstr($s, $needle, $beforeNeedle = false)
193 {
194 return mb_strstr($s, $needle, $beforeNeedle, 'UTF-8');
195 }
196
197 public static function grapheme_str_split($s, $len = 1)
198 {
199 if (0 > $len || 1073741823 < $len) {
200 if (80000 > \PHP_VERSION_ID) {
201 return false;
202 }
203
204 throw new \ValueError('grapheme_str_split(): Argument #2 ($length) must be greater than 0 and less than or equal to 1073741823.');
205 }
206
207 if ('' === $s) {
208 return [];
209 }
210
211 if (!preg_match_all('/('.SYMFONY_GRAPHEME_CLUSTER_RX.')/u', $s, $matches)) {
212 return false;
213 }
214
215 if (1 === $len) {
216 return $matches[0];
217 }
218
219 $chunks = array_chunk($matches[0], $len);
220
221 foreach ($chunks as &$chunk) {
222 $chunk = implode('', $chunk);
223 }
224
225 return $chunks;
226 }
227
228 public static function grapheme_levenshtein($s1, $s2, $insertion_cost = 1, $replacement_cost = 1, $deletion_cost = 1)
229 {
230 if (!preg_match('//u', $s1) || !preg_match('//u', $s2)) {
231 return false;
232 }
233
234 if (0 > $insertion_cost || 0 > $replacement_cost || 0 > $deletion_cost) {
235 if (80000 > \PHP_VERSION_ID) {
236 return false;
237 }
238
239 throw new \ValueError('grapheme_levenshtein(): Argument #3 ($insertion_cost), #4 ($replacement_cost), and #5 ($deletion_cost) must be greater than or equal to 0');
240 }
241
242 preg_match_all('/'.SYMFONY_GRAPHEME_CLUSTER_RX.'/u', $s1, $s1);
243 preg_match_all('/'.SYMFONY_GRAPHEME_CLUSTER_RX.'/u', $s2, $s2);
244
245 $s1 = $s1[0];
246 $s2 = $s2[0];
247 $l1 = \count($s1);
248 $l2 = \count($s2);
249
250 if (0 === $l1) {
251 return $l2 * $insertion_cost;
252 }
253 if (0 === $l2) {
254 return $l1 * $deletion_cost;
255 }
256
257 $dp = array_fill(0, $l1 + 1, array_fill(0, $l2 + 1, 0));
258
259 for ($i = 1; $i <= $l1; ++$i) {
260 $dp[$i][0] = $dp[$i - 1][0] + $deletion_cost;
261 }
262 for ($j = 1; $j <= $l2; ++$j) {
263 $dp[0][$j] = $dp[0][$j - 1] + $insertion_cost;
264 }
265
266 for ($i = 1; $i <= $l1; ++$i) {
267 for ($j = 1; $j <= $l2; ++$j) {
268 $cost = ($s1[$i - 1] === $s2[$j - 1]) ? 0 : $replacement_cost;
269 $dp[$i][$j] = min($dp[$i - 1][$j] + $deletion_cost, $dp[$i][$j - 1] + $insertion_cost, $dp[$i - 1][$j - 1] + $cost);
270 }
271 }
272
273 return $dp[$l1][$l2];
274 }
275
276 private static function grapheme_position($s, $needle, $offset, $mode)
277 {
278 $needle = (string) $needle;
279 if (80000 > \PHP_VERSION_ID && !preg_match('/./us', $needle)) {
280 return false;
281 }
282 $s = (string) $s;
283 if (!preg_match('/./us', $s)) {
284 return false;
285 }
286 if ($offset > 0) {
287 $s = self::grapheme_substr($s, $offset);
288 } elseif ($offset < 0) {
289 if (2 > $mode) {
290 $offset += self::grapheme_strlen($s);
291 $s = self::grapheme_substr($s, $offset);
292 if (0 > $offset) {
293 $offset = 0;
294 }
295 } elseif (0 > $offset += self::grapheme_strlen($needle)) {
296 $s = self::grapheme_substr($s, 0, $offset);
297 $offset = 0;
298 } else {
299 $offset = 0;
300 }
301 }
302
303 // As UTF-8 is self-synchronizing, and we have ensured the strings are valid UTF-8,
304 // we can use normal binary string functions here. For case-insensitive searches,
305 // case fold the strings first.
306 $caseInsensitive = $mode & 1;
307 $reverse = $mode & 2;
308 if ($caseInsensitive) {
309 // Use the same case folding mode as mbstring does for mb_stripos().
310 // Stick to SIMPLE case folding to avoid changing the length of the string, which
311 // might result in offsets being shifted.
312 $mode = \defined('MB_CASE_FOLD_SIMPLE') ? \MB_CASE_FOLD_SIMPLE : \MB_CASE_LOWER;
313 $s = mb_convert_case($s, $mode, 'UTF-8');
314 $needle = mb_convert_case($needle, $mode, 'UTF-8');
315
316 if (!\defined('MB_CASE_FOLD_SIMPLE')) {
317 $s = str_replace(self::CASE_FOLD[0], self::CASE_FOLD[1], $s);
318 $needle = str_replace(self::CASE_FOLD[0], self::CASE_FOLD[1], $needle);
319 }
320 }
321 if ($reverse) {
322 $needlePos = strrpos($s, $needle);
323 } else {
324 $needlePos = strpos($s, $needle);
325 }
326
327 return false !== $needlePos ? self::grapheme_strlen(substr($s, 0, $needlePos)) + $offset : false;
328 }
329
330 public static function grapheme_strrev(string $string)
331 {
332 if (!preg_match('//u', $string)) {
333 return false;
334 }
335
336 $units = grapheme_str_split($string);
337
338 if (false === $units) {
339 return false;
340 }
341
342 return implode('', array_reverse($units));
343 }
344 }