| 148 |
lars |
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\Component\String;
|
|
|
13 |
|
|
|
14 |
use Symfony\Component\String\Exception\ExceptionInterface;
|
|
|
15 |
use Symfony\Component\String\Exception\InvalidArgumentException;
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Represents a string of Unicode grapheme clusters encoded as UTF-8.
|
|
|
19 |
*
|
|
|
20 |
* A letter followed by combining characters (accents typically) form what Unicode defines
|
|
|
21 |
* as a grapheme cluster: a character as humans mean it in written texts. This class knows
|
|
|
22 |
* about the concept and won't split a letter apart from its combining accents. It also
|
|
|
23 |
* ensures all string comparisons happen on their canonically-composed representation,
|
|
|
24 |
* ignoring e.g. the order in which accents are listed when a letter has many of them.
|
|
|
25 |
*
|
|
|
26 |
* @see https://unicode.org/reports/tr15/
|
|
|
27 |
*
|
|
|
28 |
* @author Nicolas Grekas <p@tchwork.com>
|
|
|
29 |
* @author Hugo Hamon <hugohamon@neuf.fr>
|
|
|
30 |
*
|
|
|
31 |
* @throws ExceptionInterface
|
|
|
32 |
*/
|
|
|
33 |
class UnicodeString extends AbstractUnicodeString
|
|
|
34 |
{
|
|
|
35 |
public function __construct(string $string = '')
|
|
|
36 |
{
|
|
|
37 |
$this->string = normalizer_is_normalized($string) ? $string : normalizer_normalize($string);
|
|
|
38 |
|
|
|
39 |
if (false === $this->string) {
|
|
|
40 |
throw new InvalidArgumentException('Invalid UTF-8 string.');
|
|
|
41 |
}
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
public function append(string ...$suffix): static
|
|
|
45 |
{
|
|
|
46 |
$str = clone $this;
|
|
|
47 |
$str->string = $this->string.(1 >= \count($suffix) ? ($suffix[0] ?? '') : implode('', $suffix));
|
|
|
48 |
normalizer_is_normalized($str->string) ?: $str->string = normalizer_normalize($str->string);
|
|
|
49 |
|
|
|
50 |
if (false === $str->string) {
|
|
|
51 |
throw new InvalidArgumentException('Invalid UTF-8 string.');
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
return $str;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
public function chunk(int $length = 1): array
|
|
|
58 |
{
|
|
|
59 |
if (1 > $length) {
|
|
|
60 |
throw new InvalidArgumentException('The chunk length must be greater than zero.');
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
if ('' === $this->string) {
|
|
|
64 |
return [];
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
$rx = '/(';
|
|
|
68 |
while (65535 < $length) {
|
|
|
69 |
$rx .= '\X{65535}';
|
|
|
70 |
$length -= 65535;
|
|
|
71 |
}
|
|
|
72 |
$rx .= '\X{'.$length.'})/u';
|
|
|
73 |
|
|
|
74 |
$str = clone $this;
|
|
|
75 |
$chunks = [];
|
|
|
76 |
|
|
|
77 |
foreach (preg_split($rx, $this->string, -1, \PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY) as $chunk) {
|
|
|
78 |
$str->string = $chunk;
|
|
|
79 |
$chunks[] = clone $str;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
return $chunks;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
public function endsWith(string|iterable|AbstractString $suffix): bool
|
|
|
86 |
{
|
|
|
87 |
if ($suffix instanceof AbstractString) {
|
|
|
88 |
$suffix = $suffix->string;
|
|
|
89 |
} elseif (!\is_string($suffix)) {
|
|
|
90 |
return parent::endsWith($suffix);
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
$form = null === $this->ignoreCase ? \Normalizer::NFD : \Normalizer::NFC;
|
|
|
94 |
normalizer_is_normalized($suffix, $form) ?: $suffix = normalizer_normalize($suffix, $form);
|
|
|
95 |
|
|
|
96 |
if ('' === $suffix || false === $suffix) {
|
|
|
97 |
return false;
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
if ($this->ignoreCase) {
|
|
|
101 |
return 0 === mb_stripos(grapheme_extract($this->string, \strlen($suffix), \GRAPHEME_EXTR_MAXBYTES, \strlen($this->string) - \strlen($suffix)), $suffix, 0, 'UTF-8');
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
return $suffix === grapheme_extract($this->string, \strlen($suffix), \GRAPHEME_EXTR_MAXBYTES, \strlen($this->string) - \strlen($suffix));
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
public function equalsTo(string|iterable|AbstractString $string): bool
|
|
|
108 |
{
|
|
|
109 |
if ($string instanceof AbstractString) {
|
|
|
110 |
$string = $string->string;
|
|
|
111 |
} elseif (!\is_string($string)) {
|
|
|
112 |
return parent::equalsTo($string);
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
$form = null === $this->ignoreCase ? \Normalizer::NFD : \Normalizer::NFC;
|
|
|
116 |
normalizer_is_normalized($string, $form) ?: $string = normalizer_normalize($string, $form);
|
|
|
117 |
|
|
|
118 |
if ('' !== $string && false !== $string && $this->ignoreCase) {
|
|
|
119 |
return \strlen($string) === \strlen($this->string) && 0 === mb_stripos($this->string, $string, 0, 'UTF-8');
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
return $string === $this->string;
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
public function indexOf(string|iterable|AbstractString $needle, int $offset = 0): ?int
|
|
|
126 |
{
|
|
|
127 |
if ($needle instanceof AbstractString) {
|
|
|
128 |
$needle = $needle->string;
|
|
|
129 |
} elseif (!\is_string($needle)) {
|
|
|
130 |
return parent::indexOf($needle, $offset);
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
$form = null === $this->ignoreCase ? \Normalizer::NFD : \Normalizer::NFC;
|
|
|
134 |
normalizer_is_normalized($needle, $form) ?: $needle = normalizer_normalize($needle, $form);
|
|
|
135 |
|
|
|
136 |
if ('' === $needle || false === $needle) {
|
|
|
137 |
return null;
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
try {
|
|
|
141 |
$i = $this->ignoreCase ? grapheme_stripos($this->string, $needle, $offset) : grapheme_strpos($this->string, $needle, $offset);
|
|
|
142 |
} catch (\ValueError) {
|
|
|
143 |
return null;
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
return false === $i ? null : $i;
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
public function indexOfLast(string|iterable|AbstractString $needle, int $offset = 0): ?int
|
|
|
150 |
{
|
|
|
151 |
if ($needle instanceof AbstractString) {
|
|
|
152 |
$needle = $needle->string;
|
|
|
153 |
} elseif (!\is_string($needle)) {
|
|
|
154 |
return parent::indexOfLast($needle, $offset);
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
$form = null === $this->ignoreCase ? \Normalizer::NFD : \Normalizer::NFC;
|
|
|
158 |
normalizer_is_normalized($needle, $form) ?: $needle = normalizer_normalize($needle, $form);
|
|
|
159 |
|
|
|
160 |
if ('' === $needle || false === $needle) {
|
|
|
161 |
return null;
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
$string = $this->string;
|
|
|
165 |
|
|
|
166 |
if (0 > $offset) {
|
|
|
167 |
// workaround https://bugs.php.net/74264
|
|
|
168 |
if (0 > $offset += grapheme_strlen($needle)) {
|
|
|
169 |
$string = grapheme_substr($string, 0, $offset);
|
|
|
170 |
}
|
|
|
171 |
$offset = 0;
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
$i = $this->ignoreCase ? grapheme_strripos($string, $needle, $offset) : grapheme_strrpos($string, $needle, $offset);
|
|
|
175 |
|
|
|
176 |
return false === $i ? null : $i;
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
public function join(array $strings, string $lastGlue = null): static
|
|
|
180 |
{
|
|
|
181 |
$str = parent::join($strings, $lastGlue);
|
|
|
182 |
normalizer_is_normalized($str->string) ?: $str->string = normalizer_normalize($str->string);
|
|
|
183 |
|
|
|
184 |
return $str;
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
public function length(): int
|
|
|
188 |
{
|
|
|
189 |
return grapheme_strlen($this->string);
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
public function normalize(int $form = self::NFC): static
|
|
|
193 |
{
|
|
|
194 |
$str = clone $this;
|
|
|
195 |
|
|
|
196 |
if (\in_array($form, [self::NFC, self::NFKC], true)) {
|
|
|
197 |
normalizer_is_normalized($str->string, $form) ?: $str->string = normalizer_normalize($str->string, $form);
|
|
|
198 |
} elseif (!\in_array($form, [self::NFD, self::NFKD], true)) {
|
|
|
199 |
throw new InvalidArgumentException('Unsupported normalization form.');
|
|
|
200 |
} elseif (!normalizer_is_normalized($str->string, $form)) {
|
|
|
201 |
$str->string = normalizer_normalize($str->string, $form);
|
|
|
202 |
$str->ignoreCase = null;
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
return $str;
|
|
|
206 |
}
|
|
|
207 |
|
|
|
208 |
public function prepend(string ...$prefix): static
|
|
|
209 |
{
|
|
|
210 |
$str = clone $this;
|
|
|
211 |
$str->string = (1 >= \count($prefix) ? ($prefix[0] ?? '') : implode('', $prefix)).$this->string;
|
|
|
212 |
normalizer_is_normalized($str->string) ?: $str->string = normalizer_normalize($str->string);
|
|
|
213 |
|
|
|
214 |
if (false === $str->string) {
|
|
|
215 |
throw new InvalidArgumentException('Invalid UTF-8 string.');
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
return $str;
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
public function replace(string $from, string $to): static
|
|
|
222 |
{
|
|
|
223 |
$str = clone $this;
|
|
|
224 |
normalizer_is_normalized($from) ?: $from = normalizer_normalize($from);
|
|
|
225 |
|
|
|
226 |
if ('' !== $from && false !== $from) {
|
|
|
227 |
$tail = $str->string;
|
|
|
228 |
$result = '';
|
|
|
229 |
$indexOf = $this->ignoreCase ? 'grapheme_stripos' : 'grapheme_strpos';
|
|
|
230 |
|
|
|
231 |
while ('' !== $tail && false !== $i = $indexOf($tail, $from)) {
|
|
|
232 |
$slice = grapheme_substr($tail, 0, $i);
|
|
|
233 |
$result .= $slice.$to;
|
|
|
234 |
$tail = substr($tail, \strlen($slice) + \strlen($from));
|
|
|
235 |
}
|
|
|
236 |
|
|
|
237 |
$str->string = $result.$tail;
|
|
|
238 |
normalizer_is_normalized($str->string) ?: $str->string = normalizer_normalize($str->string);
|
|
|
239 |
|
|
|
240 |
if (false === $str->string) {
|
|
|
241 |
throw new InvalidArgumentException('Invalid UTF-8 string.');
|
|
|
242 |
}
|
|
|
243 |
}
|
|
|
244 |
|
|
|
245 |
return $str;
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
public function replaceMatches(string $fromRegexp, string|callable $to): static
|
|
|
249 |
{
|
|
|
250 |
$str = parent::replaceMatches($fromRegexp, $to);
|
|
|
251 |
normalizer_is_normalized($str->string) ?: $str->string = normalizer_normalize($str->string);
|
|
|
252 |
|
|
|
253 |
return $str;
|
|
|
254 |
}
|
|
|
255 |
|
|
|
256 |
public function slice(int $start = 0, int $length = null): static
|
|
|
257 |
{
|
|
|
258 |
$str = clone $this;
|
|
|
259 |
|
|
|
260 |
$str->string = (string) grapheme_substr($this->string, $start, $length ?? 2147483647);
|
|
|
261 |
|
|
|
262 |
return $str;
|
|
|
263 |
}
|
|
|
264 |
|
|
|
265 |
public function splice(string $replacement, int $start = 0, int $length = null): static
|
|
|
266 |
{
|
|
|
267 |
$str = clone $this;
|
|
|
268 |
|
|
|
269 |
$start = $start ? \strlen(grapheme_substr($this->string, 0, $start)) : 0;
|
|
|
270 |
$length = $length ? \strlen(grapheme_substr($this->string, $start, $length ?? 2147483647)) : $length;
|
|
|
271 |
$str->string = substr_replace($this->string, $replacement, $start, $length ?? 2147483647);
|
|
|
272 |
normalizer_is_normalized($str->string) ?: $str->string = normalizer_normalize($str->string);
|
|
|
273 |
|
|
|
274 |
if (false === $str->string) {
|
|
|
275 |
throw new InvalidArgumentException('Invalid UTF-8 string.');
|
|
|
276 |
}
|
|
|
277 |
|
|
|
278 |
return $str;
|
|
|
279 |
}
|
|
|
280 |
|
|
|
281 |
public function split(string $delimiter, int $limit = null, int $flags = null): array
|
|
|
282 |
{
|
|
|
283 |
if (1 > $limit ??= 2147483647) {
|
|
|
284 |
throw new InvalidArgumentException('Split limit must be a positive integer.');
|
|
|
285 |
}
|
|
|
286 |
|
|
|
287 |
if ('' === $delimiter) {
|
|
|
288 |
throw new InvalidArgumentException('Split delimiter is empty.');
|
|
|
289 |
}
|
|
|
290 |
|
|
|
291 |
if (null !== $flags) {
|
|
|
292 |
return parent::split($delimiter.'u', $limit, $flags);
|
|
|
293 |
}
|
|
|
294 |
|
|
|
295 |
normalizer_is_normalized($delimiter) ?: $delimiter = normalizer_normalize($delimiter);
|
|
|
296 |
|
|
|
297 |
if (false === $delimiter) {
|
|
|
298 |
throw new InvalidArgumentException('Split delimiter is not a valid UTF-8 string.');
|
|
|
299 |
}
|
|
|
300 |
|
|
|
301 |
$str = clone $this;
|
|
|
302 |
$tail = $this->string;
|
|
|
303 |
$chunks = [];
|
|
|
304 |
$indexOf = $this->ignoreCase ? 'grapheme_stripos' : 'grapheme_strpos';
|
|
|
305 |
|
|
|
306 |
while (1 < $limit && false !== $i = $indexOf($tail, $delimiter)) {
|
|
|
307 |
$str->string = grapheme_substr($tail, 0, $i);
|
|
|
308 |
$chunks[] = clone $str;
|
|
|
309 |
$tail = substr($tail, \strlen($str->string) + \strlen($delimiter));
|
|
|
310 |
--$limit;
|
|
|
311 |
}
|
|
|
312 |
|
|
|
313 |
$str->string = $tail;
|
|
|
314 |
$chunks[] = clone $str;
|
|
|
315 |
|
|
|
316 |
return $chunks;
|
|
|
317 |
}
|
|
|
318 |
|
|
|
319 |
public function startsWith(string|iterable|AbstractString $prefix): bool
|
|
|
320 |
{
|
|
|
321 |
if ($prefix instanceof AbstractString) {
|
|
|
322 |
$prefix = $prefix->string;
|
|
|
323 |
} elseif (!\is_string($prefix)) {
|
|
|
324 |
return parent::startsWith($prefix);
|
|
|
325 |
}
|
|
|
326 |
|
|
|
327 |
$form = null === $this->ignoreCase ? \Normalizer::NFD : \Normalizer::NFC;
|
|
|
328 |
normalizer_is_normalized($prefix, $form) ?: $prefix = normalizer_normalize($prefix, $form);
|
|
|
329 |
|
|
|
330 |
if ('' === $prefix || false === $prefix) {
|
|
|
331 |
return false;
|
|
|
332 |
}
|
|
|
333 |
|
|
|
334 |
if ($this->ignoreCase) {
|
|
|
335 |
return 0 === mb_stripos(grapheme_extract($this->string, \strlen($prefix), \GRAPHEME_EXTR_MAXBYTES), $prefix, 0, 'UTF-8');
|
|
|
336 |
}
|
|
|
337 |
|
|
|
338 |
return $prefix === grapheme_extract($this->string, \strlen($prefix), \GRAPHEME_EXTR_MAXBYTES);
|
|
|
339 |
}
|
|
|
340 |
|
|
|
341 |
public function __wakeup()
|
|
|
342 |
{
|
|
|
343 |
if (!\is_string($this->string)) {
|
|
|
344 |
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
|
|
|
345 |
}
|
|
|
346 |
|
|
|
347 |
normalizer_is_normalized($this->string) ?: $this->string = normalizer_normalize($this->string);
|
|
|
348 |
}
|
|
|
349 |
|
|
|
350 |
public function __clone()
|
|
|
351 |
{
|
|
|
352 |
if (null === $this->ignoreCase) {
|
|
|
353 |
normalizer_is_normalized($this->string) ?: $this->string = normalizer_normalize($this->string);
|
|
|
354 |
}
|
|
|
355 |
|
|
|
356 |
$this->ignoreCase = false;
|
|
|
357 |
}
|
|
|
358 |
}
|