PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.2.0
Independent Analytics – WordPress Analytics Plugin v2.2.0
2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / vendor / symfony / string / UnicodeString.php
independent-analytics / vendor / symfony / string Last commit date
Exception 2 years ago Inflector 2 years ago Resources 2 years ago Slugger 2 years ago AbstractString.php 2 years ago AbstractUnicodeString.php 2 years ago ByteString.php 2 years ago CodePointString.php 2 years ago LazyString.php 2 years ago UnicodeString.php 2 years ago
UnicodeString.php
304 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 namespace IAWP_SCOPED\Symfony\Component\String;
12
13 use IAWP_SCOPED\Symfony\Component\String\Exception\ExceptionInterface;
14 use IAWP_SCOPED\Symfony\Component\String\Exception\InvalidArgumentException;
15 /**
16 * Represents a string of Unicode grapheme clusters encoded as UTF-8.
17 *
18 * A letter followed by combining characters (accents typically) form what Unicode defines
19 * as a grapheme cluster: a character as humans mean it in written texts. This class knows
20 * about the concept and won't split a letter apart from its combining accents. It also
21 * ensures all string comparisons happen on their canonically-composed representation,
22 * ignoring e.g. the order in which accents are listed when a letter has many of them.
23 *
24 * @see https://unicode.org/reports/tr15/
25 *
26 * @author Nicolas Grekas <p@tchwork.com>
27 * @author Hugo Hamon <hugohamon@neuf.fr>
28 *
29 * @throws ExceptionInterface
30 * @internal
31 */
32 class UnicodeString extends AbstractUnicodeString
33 {
34 public function __construct(string $string = '')
35 {
36 $this->string = \normalizer_is_normalized($string) ? $string : \normalizer_normalize($string);
37 if (\false === $this->string) {
38 throw new InvalidArgumentException('Invalid UTF-8 string.');
39 }
40 }
41 public function append(string ...$suffix) : AbstractString
42 {
43 $str = clone $this;
44 $str->string = $this->string . (1 >= \count($suffix) ? $suffix[0] ?? '' : \implode('', $suffix));
45 \normalizer_is_normalized($str->string) ?: ($str->string = \normalizer_normalize($str->string));
46 if (\false === $str->string) {
47 throw new InvalidArgumentException('Invalid UTF-8 string.');
48 }
49 return $str;
50 }
51 public function chunk(int $length = 1) : array
52 {
53 if (1 > $length) {
54 throw new InvalidArgumentException('The chunk length must be greater than zero.');
55 }
56 if ('' === $this->string) {
57 return [];
58 }
59 $rx = '/(';
60 while (65535 < $length) {
61 $rx .= '\\X{65535}';
62 $length -= 65535;
63 }
64 $rx .= '\\X{' . $length . '})/u';
65 $str = clone $this;
66 $chunks = [];
67 foreach (\preg_split($rx, $this->string, -1, \PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY) as $chunk) {
68 $str->string = $chunk;
69 $chunks[] = clone $str;
70 }
71 return $chunks;
72 }
73 public function endsWith($suffix) : bool
74 {
75 if ($suffix instanceof AbstractString) {
76 $suffix = $suffix->string;
77 } elseif (\is_array($suffix) || $suffix instanceof \Traversable) {
78 return parent::endsWith($suffix);
79 } else {
80 $suffix = (string) $suffix;
81 }
82 $form = null === $this->ignoreCase ? \Normalizer::NFD : \Normalizer::NFC;
83 \normalizer_is_normalized($suffix, $form) ?: ($suffix = \normalizer_normalize($suffix, $form));
84 if ('' === $suffix || \false === $suffix) {
85 return \false;
86 }
87 if ($this->ignoreCase) {
88 return 0 === \mb_stripos(\grapheme_extract($this->string, \strlen($suffix), \GRAPHEME_EXTR_MAXBYTES, \strlen($this->string) - \strlen($suffix)), $suffix, 0, 'UTF-8');
89 }
90 return $suffix === \grapheme_extract($this->string, \strlen($suffix), \GRAPHEME_EXTR_MAXBYTES, \strlen($this->string) - \strlen($suffix));
91 }
92 public function equalsTo($string) : bool
93 {
94 if ($string instanceof AbstractString) {
95 $string = $string->string;
96 } elseif (\is_array($string) || $string instanceof \Traversable) {
97 return parent::equalsTo($string);
98 } else {
99 $string = (string) $string;
100 }
101 $form = null === $this->ignoreCase ? \Normalizer::NFD : \Normalizer::NFC;
102 \normalizer_is_normalized($string, $form) ?: ($string = \normalizer_normalize($string, $form));
103 if ('' !== $string && \false !== $string && $this->ignoreCase) {
104 return \strlen($string) === \strlen($this->string) && 0 === \mb_stripos($this->string, $string, 0, 'UTF-8');
105 }
106 return $string === $this->string;
107 }
108 public function indexOf($needle, int $offset = 0) : ?int
109 {
110 if ($needle instanceof AbstractString) {
111 $needle = $needle->string;
112 } elseif (\is_array($needle) || $needle instanceof \Traversable) {
113 return parent::indexOf($needle, $offset);
114 } else {
115 $needle = (string) $needle;
116 }
117 $form = null === $this->ignoreCase ? \Normalizer::NFD : \Normalizer::NFC;
118 \normalizer_is_normalized($needle, $form) ?: ($needle = \normalizer_normalize($needle, $form));
119 if ('' === $needle || \false === $needle) {
120 return null;
121 }
122 try {
123 $i = $this->ignoreCase ? \grapheme_stripos($this->string, $needle, $offset) : \grapheme_strpos($this->string, $needle, $offset);
124 } catch (\ValueError $e) {
125 return null;
126 }
127 return \false === $i ? null : $i;
128 }
129 public function indexOfLast($needle, int $offset = 0) : ?int
130 {
131 if ($needle instanceof AbstractString) {
132 $needle = $needle->string;
133 } elseif (\is_array($needle) || $needle instanceof \Traversable) {
134 return parent::indexOfLast($needle, $offset);
135 } else {
136 $needle = (string) $needle;
137 }
138 $form = null === $this->ignoreCase ? \Normalizer::NFD : \Normalizer::NFC;
139 \normalizer_is_normalized($needle, $form) ?: ($needle = \normalizer_normalize($needle, $form));
140 if ('' === $needle || \false === $needle) {
141 return null;
142 }
143 $string = $this->string;
144 if (0 > $offset) {
145 // workaround https://bugs.php.net/74264
146 if (0 > ($offset += \grapheme_strlen($needle))) {
147 $string = \grapheme_substr($string, 0, $offset);
148 }
149 $offset = 0;
150 }
151 $i = $this->ignoreCase ? \grapheme_strripos($string, $needle, $offset) : \grapheme_strrpos($string, $needle, $offset);
152 return \false === $i ? null : $i;
153 }
154 public function join(array $strings, string $lastGlue = null) : AbstractString
155 {
156 $str = parent::join($strings, $lastGlue);
157 \normalizer_is_normalized($str->string) ?: ($str->string = \normalizer_normalize($str->string));
158 return $str;
159 }
160 public function length() : int
161 {
162 return \grapheme_strlen($this->string);
163 }
164 /**
165 * @return static
166 */
167 public function normalize(int $form = self::NFC) : parent
168 {
169 $str = clone $this;
170 if (\in_array($form, [self::NFC, self::NFKC], \true)) {
171 \normalizer_is_normalized($str->string, $form) ?: ($str->string = \normalizer_normalize($str->string, $form));
172 } elseif (!\in_array($form, [self::NFD, self::NFKD], \true)) {
173 throw new InvalidArgumentException('Unsupported normalization form.');
174 } elseif (!\normalizer_is_normalized($str->string, $form)) {
175 $str->string = \normalizer_normalize($str->string, $form);
176 $str->ignoreCase = null;
177 }
178 return $str;
179 }
180 public function prepend(string ...$prefix) : AbstractString
181 {
182 $str = clone $this;
183 $str->string = (1 >= \count($prefix) ? $prefix[0] ?? '' : \implode('', $prefix)) . $this->string;
184 \normalizer_is_normalized($str->string) ?: ($str->string = \normalizer_normalize($str->string));
185 if (\false === $str->string) {
186 throw new InvalidArgumentException('Invalid UTF-8 string.');
187 }
188 return $str;
189 }
190 public function replace(string $from, string $to) : AbstractString
191 {
192 $str = clone $this;
193 \normalizer_is_normalized($from) ?: ($from = \normalizer_normalize($from));
194 if ('' !== $from && \false !== $from) {
195 $tail = $str->string;
196 $result = '';
197 $indexOf = $this->ignoreCase ? 'grapheme_stripos' : 'grapheme_strpos';
198 while ('' !== $tail && \false !== ($i = $indexOf($tail, $from))) {
199 $slice = \grapheme_substr($tail, 0, $i);
200 $result .= $slice . $to;
201 $tail = \substr($tail, \strlen($slice) + \strlen($from));
202 }
203 $str->string = $result . $tail;
204 \normalizer_is_normalized($str->string) ?: ($str->string = \normalizer_normalize($str->string));
205 if (\false === $str->string) {
206 throw new InvalidArgumentException('Invalid UTF-8 string.');
207 }
208 }
209 return $str;
210 }
211 public function replaceMatches(string $fromRegexp, $to) : AbstractString
212 {
213 $str = parent::replaceMatches($fromRegexp, $to);
214 \normalizer_is_normalized($str->string) ?: ($str->string = \normalizer_normalize($str->string));
215 return $str;
216 }
217 public function slice(int $start = 0, int $length = null) : AbstractString
218 {
219 $str = clone $this;
220 if (\PHP_VERSION_ID < 80000 && 0 > $start && \grapheme_strlen($this->string) < -$start) {
221 $start = 0;
222 }
223 $str->string = (string) \grapheme_substr($this->string, $start, $length ?? 2147483647);
224 return $str;
225 }
226 public function splice(string $replacement, int $start = 0, int $length = null) : AbstractString
227 {
228 $str = clone $this;
229 if (\PHP_VERSION_ID < 80000 && 0 > $start && \grapheme_strlen($this->string) < -$start) {
230 $start = 0;
231 }
232 $start = $start ? \strlen(\grapheme_substr($this->string, 0, $start)) : 0;
233 $length = $length ? \strlen(\grapheme_substr($this->string, $start, $length ?? 2147483647)) : $length;
234 $str->string = \substr_replace($this->string, $replacement, $start, $length ?? 2147483647);
235 \normalizer_is_normalized($str->string) ?: ($str->string = \normalizer_normalize($str->string));
236 if (\false === $str->string) {
237 throw new InvalidArgumentException('Invalid UTF-8 string.');
238 }
239 return $str;
240 }
241 public function split(string $delimiter, int $limit = null, int $flags = null) : array
242 {
243 if (1 > ($limit = $limit ?? 2147483647)) {
244 throw new InvalidArgumentException('Split limit must be a positive integer.');
245 }
246 if ('' === $delimiter) {
247 throw new InvalidArgumentException('Split delimiter is empty.');
248 }
249 if (null !== $flags) {
250 return parent::split($delimiter . 'u', $limit, $flags);
251 }
252 \normalizer_is_normalized($delimiter) ?: ($delimiter = \normalizer_normalize($delimiter));
253 if (\false === $delimiter) {
254 throw new InvalidArgumentException('Split delimiter is not a valid UTF-8 string.');
255 }
256 $str = clone $this;
257 $tail = $this->string;
258 $chunks = [];
259 $indexOf = $this->ignoreCase ? 'grapheme_stripos' : 'grapheme_strpos';
260 while (1 < $limit && \false !== ($i = $indexOf($tail, $delimiter))) {
261 $str->string = \grapheme_substr($tail, 0, $i);
262 $chunks[] = clone $str;
263 $tail = \substr($tail, \strlen($str->string) + \strlen($delimiter));
264 --$limit;
265 }
266 $str->string = $tail;
267 $chunks[] = clone $str;
268 return $chunks;
269 }
270 public function startsWith($prefix) : bool
271 {
272 if ($prefix instanceof AbstractString) {
273 $prefix = $prefix->string;
274 } elseif (\is_array($prefix) || $prefix instanceof \Traversable) {
275 return parent::startsWith($prefix);
276 } else {
277 $prefix = (string) $prefix;
278 }
279 $form = null === $this->ignoreCase ? \Normalizer::NFD : \Normalizer::NFC;
280 \normalizer_is_normalized($prefix, $form) ?: ($prefix = \normalizer_normalize($prefix, $form));
281 if ('' === $prefix || \false === $prefix) {
282 return \false;
283 }
284 if ($this->ignoreCase) {
285 return 0 === \mb_stripos(\grapheme_extract($this->string, \strlen($prefix), \GRAPHEME_EXTR_MAXBYTES), $prefix, 0, 'UTF-8');
286 }
287 return $prefix === \grapheme_extract($this->string, \strlen($prefix), \GRAPHEME_EXTR_MAXBYTES);
288 }
289 public function __wakeup()
290 {
291 if (!\is_string($this->string)) {
292 throw new \BadMethodCallException('Cannot unserialize ' . __CLASS__);
293 }
294 \normalizer_is_normalized($this->string) ?: ($this->string = \normalizer_normalize($this->string));
295 }
296 public function __clone()
297 {
298 if (null === $this->ignoreCase) {
299 \normalizer_is_normalized($this->string) ?: ($this->string = \normalizer_normalize($this->string));
300 }
301 $this->ignoreCase = \false;
302 }
303 }
304