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