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