| @@ -31,24 +31,24 @@ | ||
| 31 | 31 | class UnicodeString extends AbstractUnicodeString |
| 32 | 32 | { |
| 33 | 33 | public function __construct(string $string = '') |
| 34 | 34 | { |
| 35 | - $this->string = \normalizer_is_normalized($string) ? $string : \normalizer_normalize($string); | |
| 35 | + $this->string = normalizer_is_normalized($string) ? $string : normalizer_normalize($string); | |
| 36 | 36 | if (\false === $this->string) { |
| 37 | 37 | throw new InvalidArgumentException('Invalid UTF-8 string.'); |
| 38 | 38 | } |
| 39 | 39 | } |
| 40 | - public function append(string ...$suffix) : AbstractString | |
| 40 | + public function append(string ...$suffix): AbstractString | |
| 41 | 41 | { |
| 42 | 42 | $str = clone $this; |
| 43 | - $str->string = $this->string . (1 >= \count($suffix) ? $suffix[0] ?? '' : \implode('', $suffix)); | |
| 44 | - \normalizer_is_normalized($str->string) ?: ($str->string = \normalizer_normalize($str->string)); | |
| 43 | + $str->string = $this->string . (1 >= \count($suffix) ? $suffix[0] ?? '' : implode('', $suffix)); | |
| 44 | + normalizer_is_normalized($str->string) ?: $str->string = normalizer_normalize($str->string); | |
| 45 | 45 | if (\false === $str->string) { |
| 46 | 46 | throw new InvalidArgumentException('Invalid UTF-8 string.'); |
| 47 | 47 | } |
| 48 | 48 | return $str; |
| 49 | 49 | } |
| 50 | - public function chunk(int $length = 1) : array | |
| 50 | + public function chunk(int $length = 1): array | |
| 51 | 51 | { |
| 52 | 52 | if (1 > $length) { |
| 53 | 53 | throw new InvalidArgumentException('The chunk length must be greater than zero.'); |
| 54 | 54 | } |
| @@ -56,21 +56,21 @@ | ||
| 56 | 56 | return []; |
| 57 | 57 | } |
| 58 | 58 | $rx = '/('; |
| 59 | 59 | while (65535 < $length) { |
| 60 | - $rx .= '\\X{65535}'; | |
| 60 | + $rx .= '\X{65535}'; | |
| 61 | 61 | $length -= 65535; |
| 62 | 62 | } |
| 63 | - $rx .= '\\X{' . $length . '})/u'; | |
| 63 | + $rx .= '\X{' . $length . '})/u'; | |
| 64 | 64 | $str = clone $this; |
| 65 | 65 | $chunks = []; |
| 66 | - foreach (\preg_split($rx, $this->string, -1, \PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY) as $chunk) { | |
| 66 | + foreach (preg_split($rx, $this->string, -1, \PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY) as $chunk) { | |
| 67 | 67 | $str->string = $chunk; |
| 68 | 68 | $chunks[] = clone $str; |
| 69 | 69 | } |
| 70 | 70 | return $chunks; |
| 71 | 71 | } |
| 72 | - public function endsWith($suffix) : bool | |
| 72 | + public function endsWith($suffix): bool | |
| 73 | 73 | { |
| 74 | 74 | if ($suffix instanceof AbstractString) { |
| 75 | 75 | $suffix = $suffix->string; |
| 76 | 76 | } elseif (\is_array($suffix) || $suffix instanceof \Traversable) { |
| @@ -78,18 +78,18 @@ | ||
| 78 | 78 | } else { |
| 79 | 79 | $suffix = (string) $suffix; |
| 80 | 80 | } |
| 81 | 81 | $form = null === $this->ignoreCase ? \Normalizer::NFD : \Normalizer::NFC; |
| 82 | - \normalizer_is_normalized($suffix, $form) ?: ($suffix = \normalizer_normalize($suffix, $form)); | |
| 82 | + normalizer_is_normalized($suffix, $form) ?: $suffix = normalizer_normalize($suffix, $form); | |
| 83 | 83 | if ('' === $suffix || \false === $suffix) { |
| 84 | 84 | return \false; |
| 85 | 85 | } |
| 86 | 86 | if ($this->ignoreCase) { |
| 87 | - return 0 === \mb_stripos(\grapheme_extract($this->string, \strlen($suffix), \GRAPHEME_EXTR_MAXBYTES, \strlen($this->string) - \strlen($suffix)), $suffix, 0, 'UTF-8'); | |
| 87 | + return 0 === mb_stripos(grapheme_extract($this->string, \strlen($suffix), \GRAPHEME_EXTR_MAXBYTES, \strlen($this->string) - \strlen($suffix)), $suffix, 0, 'UTF-8'); | |
| 88 | 88 | } |
| 89 | - return $suffix === \grapheme_extract($this->string, \strlen($suffix), \GRAPHEME_EXTR_MAXBYTES, \strlen($this->string) - \strlen($suffix)); | |
| 89 | + return $suffix === grapheme_extract($this->string, \strlen($suffix), \GRAPHEME_EXTR_MAXBYTES, \strlen($this->string) - \strlen($suffix)); | |
| 90 | 90 | } |
| 91 | - public function equalsTo($string) : bool | |
| 91 | + public function equalsTo($string): bool | |
| 92 | 92 | { |
| 93 | 93 | if ($string instanceof AbstractString) { |
| 94 | 94 | $string = $string->string; |
| 95 | 95 | } elseif (\is_array($string) || $string instanceof \Traversable) { |
| @@ -97,15 +97,15 @@ | ||
| 97 | 97 | } else { |
| 98 | 98 | $string = (string) $string; |
| 99 | 99 | } |
| 100 | 100 | $form = null === $this->ignoreCase ? \Normalizer::NFD : \Normalizer::NFC; |
| 101 | - \normalizer_is_normalized($string, $form) ?: ($string = \normalizer_normalize($string, $form)); | |
| 101 | + normalizer_is_normalized($string, $form) ?: $string = normalizer_normalize($string, $form); | |
| 102 | 102 | if ('' !== $string && \false !== $string && $this->ignoreCase) { |
| 103 | - return \strlen($string) === \strlen($this->string) && 0 === \mb_stripos($this->string, $string, 0, 'UTF-8'); | |
| 103 | + return \strlen($string) === \strlen($this->string) && 0 === mb_stripos($this->string, $string, 0, 'UTF-8'); | |
| 104 | 104 | } |
| 105 | 105 | return $string === $this->string; |
| 106 | 106 | } |
| 107 | - public function indexOf($needle, int $offset = 0) : ?int | |
| 107 | + public function indexOf($needle, int $offset = 0): ?int | |
| 108 | 108 | { |
| 109 | 109 | if ($needle instanceof AbstractString) { |
| 110 | 110 | $needle = $needle->string; |
| 111 | 111 | } elseif (\is_array($needle) || $needle instanceof \Traversable) { |
| @@ -113,20 +113,20 @@ | ||
| 113 | 113 | } else { |
| 114 | 114 | $needle = (string) $needle; |
| 115 | 115 | } |
| 116 | 116 | $form = null === $this->ignoreCase ? \Normalizer::NFD : \Normalizer::NFC; |
| 117 | - \normalizer_is_normalized($needle, $form) ?: ($needle = \normalizer_normalize($needle, $form)); | |
| 117 | + normalizer_is_normalized($needle, $form) ?: $needle = normalizer_normalize($needle, $form); | |
| 118 | 118 | if ('' === $needle || \false === $needle) { |
| 119 | 119 | return null; |
| 120 | 120 | } |
| 121 | 121 | try { |
| 122 | - $i = $this->ignoreCase ? \grapheme_stripos($this->string, $needle, $offset) : \grapheme_strpos($this->string, $needle, $offset); | |
| 122 | + $i = $this->ignoreCase ? grapheme_stripos($this->string, $needle, $offset) : grapheme_strpos($this->string, $needle, $offset); | |
| 123 | 123 | } catch (\ValueError $e) { |
| 124 | 124 | return null; |
| 125 | 125 | } |
| 126 | 126 | return \false === $i ? null : $i; |
| 127 | 127 | } |
| 128 | - public function indexOfLast($needle, int $offset = 0) : ?int | |
| 128 | + public function indexOfLast($needle, int $offset = 0): ?int | |
| 129 | 129 | { |
| 130 | 130 | if ($needle instanceof AbstractString) { |
| 131 | 131 | $needle = $needle->string; |
| 132 | 132 | } elseif (\is_array($needle) || $needle instanceof \Traversable) { |
| @@ -134,9 +134,9 @@ | ||
| 134 | 134 | } else { |
| 135 | 135 | $needle = (string) $needle; |
| 136 | 136 | } |
| 137 | 137 | $form = null === $this->ignoreCase ? \Normalizer::NFD : \Normalizer::NFC; |
| 138 | - \normalizer_is_normalized($needle, $form) ?: ($needle = \normalizer_normalize($needle, $form)); | |
| 138 | + normalizer_is_normalized($needle, $form) ?: $needle = normalizer_normalize($needle, $form); | |
| 139 | 139 | if ('' === $needle || \false === $needle) { |
| 140 | 140 | return null; |
| 141 | 141 | } |
| 142 | 142 | $string = $this->string; |
| @@ -141,67 +141,67 @@ | ||
| 141 | 141 | } |
| 142 | 142 | $string = $this->string; |
| 143 | 143 | if (0 > $offset) { |
| 144 | 144 | // workaround https://bugs.php.net/74264 |
| 145 | - if (0 > ($offset += \grapheme_strlen($needle))) { | |
| 146 | - $string = \grapheme_substr($string, 0, $offset); | |
| 145 | + if (0 > $offset += grapheme_strlen($needle)) { | |
| 146 | + $string = grapheme_substr($string, 0, $offset); | |
| 147 | 147 | } |
| 148 | 148 | $offset = 0; |
| 149 | 149 | } |
| 150 | - $i = $this->ignoreCase ? \grapheme_strripos($string, $needle, $offset) : \grapheme_strrpos($string, $needle, $offset); | |
| 150 | + $i = $this->ignoreCase ? grapheme_strripos($string, $needle, $offset) : grapheme_strrpos($string, $needle, $offset); | |
| 151 | 151 | return \false === $i ? null : $i; |
| 152 | 152 | } |
| 153 | - public function join(array $strings, ?string $lastGlue = null) : AbstractString | |
| 153 | + public function join(array $strings, ?string $lastGlue = null): AbstractString | |
| 154 | 154 | { |
| 155 | 155 | $str = parent::join($strings, $lastGlue); |
| 156 | - \normalizer_is_normalized($str->string) ?: ($str->string = \normalizer_normalize($str->string)); | |
| 156 | + normalizer_is_normalized($str->string) ?: $str->string = normalizer_normalize($str->string); | |
| 157 | 157 | return $str; |
| 158 | 158 | } |
| 159 | - public function length() : int | |
| 159 | + public function length(): int | |
| 160 | 160 | { |
| 161 | - return \grapheme_strlen($this->string); | |
| 161 | + return grapheme_strlen($this->string); | |
| 162 | 162 | } |
| 163 | 163 | /** |
| 164 | 164 | * @return static |
| 165 | 165 | */ |
| 166 | - public function normalize(int $form = self::NFC) : parent | |
| 166 | + public function normalize(int $form = self::NFC): parent | |
| 167 | 167 | { |
| 168 | 168 | $str = clone $this; |
| 169 | 169 | if (\in_array($form, [self::NFC, self::NFKC], \true)) { |
| 170 | - \normalizer_is_normalized($str->string, $form) ?: ($str->string = \normalizer_normalize($str->string, $form)); | |
| 170 | + normalizer_is_normalized($str->string, $form) ?: $str->string = normalizer_normalize($str->string, $form); | |
| 171 | 171 | } elseif (!\in_array($form, [self::NFD, self::NFKD], \true)) { |
| 172 | 172 | throw new InvalidArgumentException('Unsupported normalization form.'); |
| 173 | - } elseif (!\normalizer_is_normalized($str->string, $form)) { | |
| 174 | - $str->string = \normalizer_normalize($str->string, $form); | |
| 173 | + } elseif (!normalizer_is_normalized($str->string, $form)) { | |
| 174 | + $str->string = normalizer_normalize($str->string, $form); | |
| 175 | 175 | $str->ignoreCase = null; |
| 176 | 176 | } |
| 177 | 177 | return $str; |
| 178 | 178 | } |
| 179 | - public function prepend(string ...$prefix) : AbstractString | |
| 179 | + public function prepend(string ...$prefix): AbstractString | |
| 180 | 180 | { |
| 181 | 181 | $str = clone $this; |
| 182 | - $str->string = (1 >= \count($prefix) ? $prefix[0] ?? '' : \implode('', $prefix)) . $this->string; | |
| 183 | - \normalizer_is_normalized($str->string) ?: ($str->string = \normalizer_normalize($str->string)); | |
| 182 | + $str->string = (1 >= \count($prefix) ? $prefix[0] ?? '' : implode('', $prefix)) . $this->string; | |
| 183 | + normalizer_is_normalized($str->string) ?: $str->string = normalizer_normalize($str->string); | |
| 184 | 184 | if (\false === $str->string) { |
| 185 | 185 | throw new InvalidArgumentException('Invalid UTF-8 string.'); |
| 186 | 186 | } |
| 187 | 187 | return $str; |
| 188 | 188 | } |
| 189 | - public function replace(string $from, string $to) : AbstractString | |
| 189 | + public function replace(string $from, string $to): AbstractString | |
| 190 | 190 | { |
| 191 | 191 | $str = clone $this; |
| 192 | - \normalizer_is_normalized($from) ?: ($from = \normalizer_normalize($from)); | |
| 192 | + normalizer_is_normalized($from) ?: $from = normalizer_normalize($from); | |
| 193 | 193 | if ('' !== $from && \false !== $from) { |
| 194 | 194 | $tail = $str->string; |
| 195 | 195 | $result = ''; |
| 196 | 196 | $indexOf = $this->ignoreCase ? 'grapheme_stripos' : 'grapheme_strpos'; |
| 197 | - while ('' !== $tail && \false !== ($i = $indexOf($tail, $from))) { | |
| 198 | - $slice = \grapheme_substr($tail, 0, $i); | |
| 197 | + while ('' !== $tail && \false !== $i = $indexOf($tail, $from)) { | |
| 198 | + $slice = grapheme_substr($tail, 0, $i); | |
| 199 | 199 | $result .= $slice . $to; |
| 200 | - $tail = \substr($tail, \strlen($slice) + \strlen($from)); | |
| 200 | + $tail = substr($tail, \strlen($slice) + \strlen($from)); | |
| 201 | 201 | } |
| 202 | 202 | $str->string = $result . $tail; |
| 203 | - \normalizer_is_normalized($str->string) ?: ($str->string = \normalizer_normalize($str->string)); | |
| 203 | + normalizer_is_normalized($str->string) ?: $str->string = normalizer_normalize($str->string); | |
| 204 | 204 | if (\false === $str->string) { |
| 205 | 205 | throw new InvalidArgumentException('Invalid UTF-8 string.'); |
| 206 | 206 | } |
| 207 | 207 | } |
| @@ -206,41 +206,41 @@ | ||
| 206 | 206 | } |
| 207 | 207 | } |
| 208 | 208 | return $str; |
| 209 | 209 | } |
| 210 | - public function replaceMatches(string $fromRegexp, $to) : AbstractString | |
| 210 | + public function replaceMatches(string $fromRegexp, $to): AbstractString | |
| 211 | 211 | { |
| 212 | 212 | $str = parent::replaceMatches($fromRegexp, $to); |
| 213 | - \normalizer_is_normalized($str->string) ?: ($str->string = \normalizer_normalize($str->string)); | |
| 213 | + normalizer_is_normalized($str->string) ?: $str->string = normalizer_normalize($str->string); | |
| 214 | 214 | return $str; |
| 215 | 215 | } |
| 216 | - public function slice(int $start = 0, ?int $length = null) : AbstractString | |
| 216 | + public function slice(int $start = 0, ?int $length = null): AbstractString | |
| 217 | 217 | { |
| 218 | 218 | $str = clone $this; |
| 219 | - if (\PHP_VERSION_ID < 80000 && 0 > $start && \grapheme_strlen($this->string) < -$start) { | |
| 219 | + if (\PHP_VERSION_ID < 80000 && 0 > $start && grapheme_strlen($this->string) < -$start) { | |
| 220 | 220 | $start = 0; |
| 221 | 221 | } |
| 222 | - $str->string = (string) \grapheme_substr($this->string, $start, $length ?? 2147483647); | |
| 222 | + $str->string = (string) grapheme_substr($this->string, $start, $length ?? 2147483647); | |
| 223 | 223 | return $str; |
| 224 | 224 | } |
| 225 | - public function splice(string $replacement, int $start = 0, ?int $length = null) : AbstractString | |
| 225 | + public function splice(string $replacement, int $start = 0, ?int $length = null): AbstractString | |
| 226 | 226 | { |
| 227 | 227 | $str = clone $this; |
| 228 | - if (\PHP_VERSION_ID < 80000 && 0 > $start && \grapheme_strlen($this->string) < -$start) { | |
| 228 | + if (\PHP_VERSION_ID < 80000 && 0 > $start && grapheme_strlen($this->string) < -$start) { | |
| 229 | 229 | $start = 0; |
| 230 | 230 | } |
| 231 | - $start = $start ? \strlen(\grapheme_substr($this->string, 0, $start)) : 0; | |
| 232 | - $length = $length ? \strlen(\grapheme_substr($this->string, $start, $length ?? 2147483647)) : $length; | |
| 233 | - $str->string = \substr_replace($this->string, $replacement, $start, $length ?? 2147483647); | |
| 234 | - \normalizer_is_normalized($str->string) ?: ($str->string = \normalizer_normalize($str->string)); | |
| 231 | + $start = $start ? \strlen(grapheme_substr($this->string, 0, $start)) : 0; | |
| 232 | + $length = $length ? \strlen(grapheme_substr($this->string, $start, $length ?? 2147483647)) : $length; | |
| 233 | + $str->string = substr_replace($this->string, $replacement, $start, $length ?? 2147483647); | |
| 234 | + normalizer_is_normalized($str->string) ?: $str->string = normalizer_normalize($str->string); | |
| 235 | 235 | if (\false === $str->string) { |
| 236 | 236 | throw new InvalidArgumentException('Invalid UTF-8 string.'); |
| 237 | 237 | } |
| 238 | 238 | return $str; |
| 239 | 239 | } |
| 240 | - public function split(string $delimiter, ?int $limit = null, ?int $flags = null) : array | |
| 240 | + public function split(string $delimiter, ?int $limit = null, ?int $flags = null): array | |
| 241 | 241 | { |
| 242 | - if (1 > ($limit = $limit ?? 2147483647)) { | |
| 242 | + if (1 > $limit = $limit ?? 2147483647) { | |
| 243 | 243 | throw new InvalidArgumentException('Split limit must be a positive integer.'); |
| 244 | 244 | } |
| 245 | 245 | if ('' === $delimiter) { |
| 246 | 246 | throw new InvalidArgumentException('Split delimiter is empty.'); |
| @@ -247,9 +247,9 @@ | ||
| 247 | 247 | } |
| 248 | 248 | if (null !== $flags) { |
| 249 | 249 | return parent::split($delimiter . 'u', $limit, $flags); |
| 250 | 250 | } |
| 251 | - \normalizer_is_normalized($delimiter) ?: ($delimiter = \normalizer_normalize($delimiter)); | |
| 251 | + normalizer_is_normalized($delimiter) ?: $delimiter = normalizer_normalize($delimiter); | |
| 252 | 252 | if (\false === $delimiter) { |
| 253 | 253 | throw new InvalidArgumentException('Split delimiter is not a valid UTF-8 string.'); |
| 254 | 254 | } |
| 255 | 255 | $str = clone $this; |
| @@ -255,12 +255,12 @@ | ||
| 255 | 255 | $str = clone $this; |
| 256 | 256 | $tail = $this->string; |
| 257 | 257 | $chunks = []; |
| 258 | 258 | $indexOf = $this->ignoreCase ? 'grapheme_stripos' : 'grapheme_strpos'; |
| 259 | - while (1 < $limit && \false !== ($i = $indexOf($tail, $delimiter))) { | |
| 260 | - $str->string = \grapheme_substr($tail, 0, $i); | |
| 259 | + while (1 < $limit && \false !== $i = $indexOf($tail, $delimiter)) { | |
| 260 | + $str->string = grapheme_substr($tail, 0, $i); | |
| 261 | 261 | $chunks[] = clone $str; |
| 262 | - $tail = \substr($tail, \strlen($str->string) + \strlen($delimiter)); | |
| 262 | + $tail = substr($tail, \strlen($str->string) + \strlen($delimiter)); | |
| 263 | 263 | --$limit; |
| 264 | 264 | } |
| 265 | 265 | $str->string = $tail; |
| 266 | 266 | $chunks[] = clone $str; |
| @@ -265,9 +265,9 @@ | ||
| 265 | 265 | $str->string = $tail; |
| 266 | 266 | $chunks[] = clone $str; |
| 267 | 267 | return $chunks; |
| 268 | 268 | } |
| 269 | - public function startsWith($prefix) : bool | |
| 269 | + public function startsWith($prefix): bool | |
| 270 | 270 | { |
| 271 | 271 | if ($prefix instanceof AbstractString) { |
| 272 | 272 | $prefix = $prefix->string; |
| 273 | 273 | } elseif (\is_array($prefix) || $prefix instanceof \Traversable) { |
| @@ -275,16 +275,16 @@ | ||
| 275 | 275 | } else { |
| 276 | 276 | $prefix = (string) $prefix; |
| 277 | 277 | } |
| 278 | 278 | $form = null === $this->ignoreCase ? \Normalizer::NFD : \Normalizer::NFC; |
| 279 | - \normalizer_is_normalized($prefix, $form) ?: ($prefix = \normalizer_normalize($prefix, $form)); | |
| 279 | + normalizer_is_normalized($prefix, $form) ?: $prefix = normalizer_normalize($prefix, $form); | |
| 280 | 280 | if ('' === $prefix || \false === $prefix) { |
| 281 | 281 | return \false; |
| 282 | 282 | } |
| 283 | 283 | if ($this->ignoreCase) { |
| 284 | - return 0 === \mb_stripos(\grapheme_extract($this->string, \strlen($prefix), \GRAPHEME_EXTR_MAXBYTES), $prefix, 0, 'UTF-8'); | |
| 284 | + return 0 === mb_stripos(grapheme_extract($this->string, \strlen($prefix), \GRAPHEME_EXTR_MAXBYTES), $prefix, 0, 'UTF-8'); | |
| 285 | 285 | } |
| 286 | - return $prefix === \grapheme_extract($this->string, \strlen($prefix), \GRAPHEME_EXTR_MAXBYTES); | |
| 286 | + return $prefix === grapheme_extract($this->string, \strlen($prefix), \GRAPHEME_EXTR_MAXBYTES); | |
| 287 | 287 | } |
| 288 | 288 | public function __wakeup() |
| 289 | 289 | { |
| 290 | 290 | if (!\is_string($this->string)) { |
| @@ -289,14 +289,14 @@ | ||
| 289 | 289 | { |
| 290 | 290 | if (!\is_string($this->string)) { |
| 291 | 291 | throw new \BadMethodCallException('Cannot unserialize ' . __CLASS__); |
| 292 | 292 | } |
| 293 | - \normalizer_is_normalized($this->string) ?: ($this->string = \normalizer_normalize($this->string)); | |
| 293 | + normalizer_is_normalized($this->string) ?: $this->string = normalizer_normalize($this->string); | |
| 294 | 294 | } |
| 295 | 295 | public function __clone() |
| 296 | 296 | { |
| 297 | 297 | if (null === $this->ignoreCase) { |
| 298 | - \normalizer_is_normalized($this->string) ?: ($this->string = \normalizer_normalize($this->string)); | |
| 298 | + normalizer_is_normalized($this->string) ?: $this->string = normalizer_normalize($this->string); | |
| 299 | 299 | } |
| 300 | 300 | $this->ignoreCase = \false; |
| 301 | 301 | } |
| 302 | 302 | } |