PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.15.0
Independent Analytics – WordPress Analytics Plugin v2.15.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 5 days ago Resources 5 days ago Slugger 5 days ago AbstractString.php 5 days ago AbstractUnicodeString.php 5 days ago ByteString.php 5 days ago CodePointString.php 5 days ago LazyString.php 5 days ago UnicodeString.php 5 days ago
UnicodeString.php
285 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 IAWPSCOPED\Symfony\Component\String;
12
13 use IAWPSCOPED\Symfony\Component\String\Exception\ExceptionInterface;
14 use IAWPSCOPED\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) : static
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(string|iterable|AbstractString $suffix) : bool
74 {
75 if ($suffix instanceof AbstractString) {
76 $suffix = $suffix->string;
77 } elseif (!\is_string($suffix)) {
78 return parent::endsWith($suffix);
79 }
80 $form = null === $this->ignoreCase ? \Normalizer::NFD : \Normalizer::NFC;
81 \normalizer_is_normalized($suffix, $form) ?: ($suffix = \normalizer_normalize($suffix, $form));
82 if ('' === $suffix || \false === $suffix) {
83 return \false;
84 }
85 if ($this->ignoreCase) {
86 return 0 === \mb_stripos(\grapheme_extract($this->string, \strlen($suffix), \GRAPHEME_EXTR_MAXBYTES, \strlen($this->string) - \strlen($suffix)), $suffix, 0, 'UTF-8');
87 }
88 return $suffix === \grapheme_extract($this->string, \strlen($suffix), \GRAPHEME_EXTR_MAXBYTES, \strlen($this->string) - \strlen($suffix));
89 }
90 public function equalsTo(string|iterable|AbstractString $string) : bool
91 {
92 if ($string instanceof AbstractString) {
93 $string = $string->string;
94 } elseif (!\is_string($string)) {
95 return parent::equalsTo($string);
96 }
97 $form = null === $this->ignoreCase ? \Normalizer::NFD : \Normalizer::NFC;
98 \normalizer_is_normalized($string, $form) ?: ($string = \normalizer_normalize($string, $form));
99 if ('' !== $string && \false !== $string && $this->ignoreCase) {
100 return \strlen($string) === \strlen($this->string) && 0 === \mb_stripos($this->string, $string, 0, 'UTF-8');
101 }
102 return $string === $this->string;
103 }
104 public function indexOf(string|iterable|AbstractString $needle, int $offset = 0) : ?int
105 {
106 if ($needle instanceof AbstractString) {
107 $needle = $needle->string;
108 } elseif (!\is_string($needle)) {
109 return parent::indexOf($needle, $offset);
110 }
111 $form = null === $this->ignoreCase ? \Normalizer::NFD : \Normalizer::NFC;
112 \normalizer_is_normalized($needle, $form) ?: ($needle = \normalizer_normalize($needle, $form));
113 if ('' === $needle || \false === $needle) {
114 return null;
115 }
116 try {
117 $i = $this->ignoreCase ? \grapheme_stripos($this->string, $needle, $offset) : \grapheme_strpos($this->string, $needle, $offset);
118 } catch (\ValueError $e) {
119 return null;
120 }
121 return \false === $i ? null : $i;
122 }
123 public function indexOfLast(string|iterable|AbstractString $needle, int $offset = 0) : ?int
124 {
125 if ($needle instanceof AbstractString) {
126 $needle = $needle->string;
127 } elseif (!\is_string($needle)) {
128 return parent::indexOfLast($needle, $offset);
129 }
130 $form = null === $this->ignoreCase ? \Normalizer::NFD : \Normalizer::NFC;
131 \normalizer_is_normalized($needle, $form) ?: ($needle = \normalizer_normalize($needle, $form));
132 if ('' === $needle || \false === $needle) {
133 return null;
134 }
135 $string = $this->string;
136 if (0 > $offset) {
137 // workaround https://bugs.php.net/74264
138 if (0 > ($offset += \grapheme_strlen($needle))) {
139 $string = \grapheme_substr($string, 0, $offset);
140 }
141 $offset = 0;
142 }
143 $i = $this->ignoreCase ? \grapheme_strripos($string, $needle, $offset) : \grapheme_strrpos($string, $needle, $offset);
144 return \false === $i ? null : $i;
145 }
146 public function join(array $strings, string $lastGlue = null) : static
147 {
148 $str = parent::join($strings, $lastGlue);
149 \normalizer_is_normalized($str->string) ?: ($str->string = \normalizer_normalize($str->string));
150 return $str;
151 }
152 public function length() : int
153 {
154 return \grapheme_strlen($this->string);
155 }
156 public function normalize(int $form = self::NFC) : static
157 {
158 $str = clone $this;
159 if (\in_array($form, [self::NFC, self::NFKC], \true)) {
160 \normalizer_is_normalized($str->string, $form) ?: ($str->string = \normalizer_normalize($str->string, $form));
161 } elseif (!\in_array($form, [self::NFD, self::NFKD], \true)) {
162 throw new InvalidArgumentException('Unsupported normalization form.');
163 } elseif (!\normalizer_is_normalized($str->string, $form)) {
164 $str->string = \normalizer_normalize($str->string, $form);
165 $str->ignoreCase = null;
166 }
167 return $str;
168 }
169 public function prepend(string ...$prefix) : static
170 {
171 $str = clone $this;
172 $str->string = (1 >= \count($prefix) ? $prefix[0] ?? '' : \implode('', $prefix)) . $this->string;
173 \normalizer_is_normalized($str->string) ?: ($str->string = \normalizer_normalize($str->string));
174 if (\false === $str->string) {
175 throw new InvalidArgumentException('Invalid UTF-8 string.');
176 }
177 return $str;
178 }
179 public function replace(string $from, string $to) : static
180 {
181 $str = clone $this;
182 \normalizer_is_normalized($from) ?: ($from = \normalizer_normalize($from));
183 if ('' !== $from && \false !== $from) {
184 $tail = $str->string;
185 $result = '';
186 $indexOf = $this->ignoreCase ? 'grapheme_stripos' : 'grapheme_strpos';
187 while ('' !== $tail && \false !== ($i = $indexOf($tail, $from))) {
188 $slice = \grapheme_substr($tail, 0, $i);
189 $result .= $slice . $to;
190 $tail = \substr($tail, \strlen($slice) + \strlen($from));
191 }
192 $str->string = $result . $tail;
193 \normalizer_is_normalized($str->string) ?: ($str->string = \normalizer_normalize($str->string));
194 if (\false === $str->string) {
195 throw new InvalidArgumentException('Invalid UTF-8 string.');
196 }
197 }
198 return $str;
199 }
200 public function replaceMatches(string $fromRegexp, string|callable $to) : static
201 {
202 $str = parent::replaceMatches($fromRegexp, $to);
203 \normalizer_is_normalized($str->string) ?: ($str->string = \normalizer_normalize($str->string));
204 return $str;
205 }
206 public function slice(int $start = 0, int $length = null) : static
207 {
208 $str = clone $this;
209 $str->string = (string) \grapheme_substr($this->string, $start, $length ?? 2147483647);
210 return $str;
211 }
212 public function splice(string $replacement, int $start = 0, int $length = null) : static
213 {
214 $str = clone $this;
215 $start = $start ? \strlen(\grapheme_substr($this->string, 0, $start)) : 0;
216 $length = $length ? \strlen(\grapheme_substr($this->string, $start, $length ?? 2147483647)) : $length;
217 $str->string = \substr_replace($this->string, $replacement, $start, $length ?? 2147483647);
218 \normalizer_is_normalized($str->string) ?: ($str->string = \normalizer_normalize($str->string));
219 if (\false === $str->string) {
220 throw new InvalidArgumentException('Invalid UTF-8 string.');
221 }
222 return $str;
223 }
224 public function split(string $delimiter, int $limit = null, int $flags = null) : array
225 {
226 if (1 > ($limit = $limit ?? 2147483647)) {
227 throw new InvalidArgumentException('Split limit must be a positive integer.');
228 }
229 if ('' === $delimiter) {
230 throw new InvalidArgumentException('Split delimiter is empty.');
231 }
232 if (null !== $flags) {
233 return parent::split($delimiter . 'u', $limit, $flags);
234 }
235 \normalizer_is_normalized($delimiter) ?: ($delimiter = \normalizer_normalize($delimiter));
236 if (\false === $delimiter) {
237 throw new InvalidArgumentException('Split delimiter is not a valid UTF-8 string.');
238 }
239 $str = clone $this;
240 $tail = $this->string;
241 $chunks = [];
242 $indexOf = $this->ignoreCase ? 'grapheme_stripos' : 'grapheme_strpos';
243 while (1 < $limit && \false !== ($i = $indexOf($tail, $delimiter))) {
244 $str->string = \grapheme_substr($tail, 0, $i);
245 $chunks[] = clone $str;
246 $tail = \substr($tail, \strlen($str->string) + \strlen($delimiter));
247 --$limit;
248 }
249 $str->string = $tail;
250 $chunks[] = clone $str;
251 return $chunks;
252 }
253 public function startsWith(string|iterable|AbstractString $prefix) : bool
254 {
255 if ($prefix instanceof AbstractString) {
256 $prefix = $prefix->string;
257 } elseif (!\is_string($prefix)) {
258 return parent::startsWith($prefix);
259 }
260 $form = null === $this->ignoreCase ? \Normalizer::NFD : \Normalizer::NFC;
261 \normalizer_is_normalized($prefix, $form) ?: ($prefix = \normalizer_normalize($prefix, $form));
262 if ('' === $prefix || \false === $prefix) {
263 return \false;
264 }
265 if ($this->ignoreCase) {
266 return 0 === \mb_stripos(\grapheme_extract($this->string, \strlen($prefix), \GRAPHEME_EXTR_MAXBYTES), $prefix, 0, 'UTF-8');
267 }
268 return $prefix === \grapheme_extract($this->string, \strlen($prefix), \GRAPHEME_EXTR_MAXBYTES);
269 }
270 public function __wakeup()
271 {
272 if (!\is_string($this->string)) {
273 throw new \BadMethodCallException('Cannot unserialize ' . __CLASS__);
274 }
275 \normalizer_is_normalized($this->string) ?: ($this->string = \normalizer_normalize($this->string));
276 }
277 public function __clone()
278 {
279 if (null === $this->ignoreCase) {
280 \normalizer_is_normalized($this->string) ?: ($this->string = \normalizer_normalize($this->string));
281 }
282 $this->ignoreCase = \false;
283 }
284 }
285