Exception
1 year ago
Inflector
1 year ago
Resources
1 year ago
Slugger
1 year ago
AbstractString.php
1 year ago
AbstractUnicodeString.php
1 year ago
ByteString.php
1 year ago
CHANGELOG.md
1 year ago
CodePointString.php
1 year ago
LICENSE
1 year ago
LazyString.php
1 year ago
README.md
1 year ago
UnicodeString.php
1 year ago
composer.json
1 year ago
CodePointString.php
271 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 | |
| 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 code points encoded as UTF-8. |
| 19 | * |
| 20 | * @author Nicolas Grekas <p@tchwork.com> |
| 21 | * @author Hugo Hamon <hugohamon@neuf.fr> |
| 22 | * |
| 23 | * @throws ExceptionInterface |
| 24 | */ |
| 25 | class CodePointString extends AbstractUnicodeString |
| 26 | { |
| 27 | public function __construct(string $string = '') |
| 28 | { |
| 29 | if ('' !== $string && !preg_match('//u', $string)) { |
| 30 | throw new InvalidArgumentException('Invalid UTF-8 string.'); |
| 31 | } |
| 32 | |
| 33 | $this->string = $string; |
| 34 | } |
| 35 | |
| 36 | public function append(string ...$suffix): AbstractString |
| 37 | { |
| 38 | $str = clone $this; |
| 39 | $str->string .= 1 >= \count($suffix) ? ($suffix[0] ?? '') : implode('', $suffix); |
| 40 | |
| 41 | if (!preg_match('//u', $str->string)) { |
| 42 | throw new InvalidArgumentException('Invalid UTF-8 string.'); |
| 43 | } |
| 44 | |
| 45 | return $str; |
| 46 | } |
| 47 | |
| 48 | public function chunk(int $length = 1): array |
| 49 | { |
| 50 | if (1 > $length) { |
| 51 | throw new InvalidArgumentException('The chunk length must be greater than zero.'); |
| 52 | } |
| 53 | |
| 54 | if ('' === $this->string) { |
| 55 | return []; |
| 56 | } |
| 57 | |
| 58 | $rx = '/('; |
| 59 | while (65535 < $length) { |
| 60 | $rx .= '.{65535}'; |
| 61 | $length -= 65535; |
| 62 | } |
| 63 | $rx .= '.{'.$length.'})/us'; |
| 64 | |
| 65 | $str = clone $this; |
| 66 | $chunks = []; |
| 67 | |
| 68 | foreach (preg_split($rx, $this->string, -1, \PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY) as $chunk) { |
| 69 | $str->string = $chunk; |
| 70 | $chunks[] = clone $str; |
| 71 | } |
| 72 | |
| 73 | return $chunks; |
| 74 | } |
| 75 | |
| 76 | public function codePointsAt(int $offset): array |
| 77 | { |
| 78 | $str = $offset ? $this->slice($offset, 1) : $this; |
| 79 | |
| 80 | return '' === $str->string ? [] : [mb_ord($str->string, 'UTF-8')]; |
| 81 | } |
| 82 | |
| 83 | public function endsWith($suffix): bool |
| 84 | { |
| 85 | if ($suffix instanceof AbstractString) { |
| 86 | $suffix = $suffix->string; |
| 87 | } elseif (\is_array($suffix) || $suffix instanceof \Traversable) { |
| 88 | return parent::endsWith($suffix); |
| 89 | } else { |
| 90 | $suffix = (string) $suffix; |
| 91 | } |
| 92 | |
| 93 | if ('' === $suffix || !preg_match('//u', $suffix)) { |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | if ($this->ignoreCase) { |
| 98 | return preg_match('{'.preg_quote($suffix).'$}iuD', $this->string); |
| 99 | } |
| 100 | |
| 101 | return \strlen($this->string) >= \strlen($suffix) && 0 === substr_compare($this->string, $suffix, -\strlen($suffix)); |
| 102 | } |
| 103 | |
| 104 | public function equalsTo($string): bool |
| 105 | { |
| 106 | if ($string instanceof AbstractString) { |
| 107 | $string = $string->string; |
| 108 | } elseif (\is_array($string) || $string instanceof \Traversable) { |
| 109 | return parent::equalsTo($string); |
| 110 | } else { |
| 111 | $string = (string) $string; |
| 112 | } |
| 113 | |
| 114 | if ('' !== $string && $this->ignoreCase) { |
| 115 | return \strlen($string) === \strlen($this->string) && 0 === mb_stripos($this->string, $string, 0, 'UTF-8'); |
| 116 | } |
| 117 | |
| 118 | return $string === $this->string; |
| 119 | } |
| 120 | |
| 121 | public function indexOf($needle, int $offset = 0): ?int |
| 122 | { |
| 123 | if ($needle instanceof AbstractString) { |
| 124 | $needle = $needle->string; |
| 125 | } elseif (\is_array($needle) || $needle instanceof \Traversable) { |
| 126 | return parent::indexOf($needle, $offset); |
| 127 | } else { |
| 128 | $needle = (string) $needle; |
| 129 | } |
| 130 | |
| 131 | if ('' === $needle) { |
| 132 | return null; |
| 133 | } |
| 134 | |
| 135 | $i = $this->ignoreCase ? mb_stripos($this->string, $needle, $offset, 'UTF-8') : mb_strpos($this->string, $needle, $offset, 'UTF-8'); |
| 136 | |
| 137 | return false === $i ? null : $i; |
| 138 | } |
| 139 | |
| 140 | public function indexOfLast($needle, int $offset = 0): ?int |
| 141 | { |
| 142 | if ($needle instanceof AbstractString) { |
| 143 | $needle = $needle->string; |
| 144 | } elseif (\is_array($needle) || $needle instanceof \Traversable) { |
| 145 | return parent::indexOfLast($needle, $offset); |
| 146 | } else { |
| 147 | $needle = (string) $needle; |
| 148 | } |
| 149 | |
| 150 | if ('' === $needle) { |
| 151 | return null; |
| 152 | } |
| 153 | |
| 154 | $i = $this->ignoreCase ? mb_strripos($this->string, $needle, $offset, 'UTF-8') : mb_strrpos($this->string, $needle, $offset, 'UTF-8'); |
| 155 | |
| 156 | return false === $i ? null : $i; |
| 157 | } |
| 158 | |
| 159 | public function length(): int |
| 160 | { |
| 161 | return mb_strlen($this->string, 'UTF-8'); |
| 162 | } |
| 163 | |
| 164 | public function prepend(string ...$prefix): AbstractString |
| 165 | { |
| 166 | $str = clone $this; |
| 167 | $str->string = (1 >= \count($prefix) ? ($prefix[0] ?? '') : implode('', $prefix)).$this->string; |
| 168 | |
| 169 | if (!preg_match('//u', $str->string)) { |
| 170 | throw new InvalidArgumentException('Invalid UTF-8 string.'); |
| 171 | } |
| 172 | |
| 173 | return $str; |
| 174 | } |
| 175 | |
| 176 | public function replace(string $from, string $to): AbstractString |
| 177 | { |
| 178 | $str = clone $this; |
| 179 | |
| 180 | if ('' === $from || !preg_match('//u', $from)) { |
| 181 | return $str; |
| 182 | } |
| 183 | |
| 184 | if ('' !== $to && !preg_match('//u', $to)) { |
| 185 | throw new InvalidArgumentException('Invalid UTF-8 string.'); |
| 186 | } |
| 187 | |
| 188 | if ($this->ignoreCase) { |
| 189 | $str->string = implode($to, preg_split('{'.preg_quote($from).'}iuD', $this->string)); |
| 190 | } else { |
| 191 | $str->string = str_replace($from, $to, $this->string); |
| 192 | } |
| 193 | |
| 194 | return $str; |
| 195 | } |
| 196 | |
| 197 | public function slice(int $start = 0, ?int $length = null): AbstractString |
| 198 | { |
| 199 | $str = clone $this; |
| 200 | $str->string = mb_substr($this->string, $start, $length, 'UTF-8'); |
| 201 | |
| 202 | return $str; |
| 203 | } |
| 204 | |
| 205 | public function splice(string $replacement, int $start = 0, ?int $length = null): AbstractString |
| 206 | { |
| 207 | if (!preg_match('//u', $replacement)) { |
| 208 | throw new InvalidArgumentException('Invalid UTF-8 string.'); |
| 209 | } |
| 210 | |
| 211 | $str = clone $this; |
| 212 | $start = $start ? \strlen(mb_substr($this->string, 0, $start, 'UTF-8')) : 0; |
| 213 | $length = $length ? \strlen(mb_substr($this->string, $start, $length, 'UTF-8')) : $length; |
| 214 | $str->string = substr_replace($this->string, $replacement, $start, $length ?? \PHP_INT_MAX); |
| 215 | |
| 216 | return $str; |
| 217 | } |
| 218 | |
| 219 | public function split(string $delimiter, ?int $limit = null, ?int $flags = null): array |
| 220 | { |
| 221 | if (1 > $limit = $limit ?? \PHP_INT_MAX) { |
| 222 | throw new InvalidArgumentException('Split limit must be a positive integer.'); |
| 223 | } |
| 224 | |
| 225 | if ('' === $delimiter) { |
| 226 | throw new InvalidArgumentException('Split delimiter is empty.'); |
| 227 | } |
| 228 | |
| 229 | if (null !== $flags) { |
| 230 | return parent::split($delimiter.'u', $limit, $flags); |
| 231 | } |
| 232 | |
| 233 | if (!preg_match('//u', $delimiter)) { |
| 234 | throw new InvalidArgumentException('Split delimiter is not a valid UTF-8 string.'); |
| 235 | } |
| 236 | |
| 237 | $str = clone $this; |
| 238 | $chunks = $this->ignoreCase |
| 239 | ? preg_split('{'.preg_quote($delimiter).'}iuD', $this->string, $limit) |
| 240 | : explode($delimiter, $this->string, $limit); |
| 241 | |
| 242 | foreach ($chunks as &$chunk) { |
| 243 | $str->string = $chunk; |
| 244 | $chunk = clone $str; |
| 245 | } |
| 246 | |
| 247 | return $chunks; |
| 248 | } |
| 249 | |
| 250 | public function startsWith($prefix): bool |
| 251 | { |
| 252 | if ($prefix instanceof AbstractString) { |
| 253 | $prefix = $prefix->string; |
| 254 | } elseif (\is_array($prefix) || $prefix instanceof \Traversable) { |
| 255 | return parent::startsWith($prefix); |
| 256 | } else { |
| 257 | $prefix = (string) $prefix; |
| 258 | } |
| 259 | |
| 260 | if ('' === $prefix || !preg_match('//u', $prefix)) { |
| 261 | return false; |
| 262 | } |
| 263 | |
| 264 | if ($this->ignoreCase) { |
| 265 | return 0 === mb_stripos($this->string, $prefix, 0, 'UTF-8'); |
| 266 | } |
| 267 | |
| 268 | return 0 === strncmp($this->string, $prefix, \strlen($prefix)); |
| 269 | } |
| 270 | } |
| 271 |