UnicodeString.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\String;
  11. use Symfony\Component\String\Exception\ExceptionInterface;
  12. use Symfony\Component\String\Exception\InvalidArgumentException;
  13. /**
  14. * Represents a string of Unicode grapheme clusters encoded as UTF-8.
  15. *
  16. * A letter followed by combining characters (accents typically) form what Unicode defines
  17. * as a grapheme cluster: a character as humans mean it in written texts. This class knows
  18. * about the concept and won't split a letter apart from its combining accents. It also
  19. * ensures all string comparisons happen on their canonically-composed representation,
  20. * ignoring e.g. the order in which accents are listed when a letter has many of them.
  21. *
  22. * @see https://unicode.org/reports/tr15/
  23. *
  24. * @author Nicolas Grekas <p@tchwork.com>
  25. * @author Hugo Hamon <hugohamon@neuf.fr>
  26. *
  27. * @throws ExceptionInterface
  28. */
  29. class UnicodeString extends AbstractUnicodeString
  30. {
  31. public function __construct(string $string = '')
  32. {
  33. $this->string = normalizer_is_normalized($string) ? $string : normalizer_normalize($string);
  34. if (false === $this->string) {
  35. throw new InvalidArgumentException('Invalid UTF-8 string.');
  36. }
  37. }
  38. public function append(string ...$suffix): static
  39. {
  40. $str = clone $this;
  41. $str->string = $this->string.(1 >= \count($suffix) ? ($suffix[0] ?? '') : implode('', $suffix));
  42. normalizer_is_normalized($str->string) ?: $str->string = normalizer_normalize($str->string);
  43. if (false === $str->string) {
  44. throw new InvalidArgumentException('Invalid UTF-8 string.');
  45. }
  46. return $str;
  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. if ('' === $this->string) {
  54. return [];
  55. }
  56. $rx = '/(';
  57. while (65535 < $length) {
  58. $rx .= '\X{65535}';
  59. $length -= 65535;
  60. }
  61. $rx .= '\X{'.$length.'})/u';
  62. $str = clone $this;
  63. $chunks = [];
  64. foreach (preg_split($rx, $this->string, -1, \PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY) as $chunk) {
  65. $str->string = $chunk;
  66. $chunks[] = clone $str;
  67. }
  68. return $chunks;
  69. }
  70. public function endsWith(string|iterable|AbstractString $suffix): bool
  71. {
  72. if ($suffix instanceof AbstractString) {
  73. $suffix = $suffix->string;
  74. } elseif (!\is_string($suffix)) {
  75. return parent::endsWith($suffix);
  76. }
  77. $form = null === $this->ignoreCase ? \Normalizer::NFD : \Normalizer::NFC;
  78. normalizer_is_normalized($suffix, $form) ?: $suffix = normalizer_normalize($suffix, $form);
  79. if ('' === $suffix || false === $suffix) {
  80. return false;
  81. }
  82. if ($this->ignoreCase) {
  83. return 0 === mb_stripos(grapheme_extract($this->string, \strlen($suffix), \GRAPHEME_EXTR_MAXBYTES, \strlen($this->string) - \strlen($suffix)), $suffix, 0, 'UTF-8');
  84. }
  85. return $suffix === grapheme_extract($this->string, \strlen($suffix), \GRAPHEME_EXTR_MAXBYTES, \strlen($this->string) - \strlen($suffix));
  86. }
  87. public function equalsTo(string|iterable|AbstractString $string): bool
  88. {
  89. if ($string instanceof AbstractString) {
  90. $string = $string->string;
  91. } elseif (!\is_string($string)) {
  92. return parent::equalsTo($string);
  93. }
  94. $form = null === $this->ignoreCase ? \Normalizer::NFD : \Normalizer::NFC;
  95. normalizer_is_normalized($string, $form) ?: $string = normalizer_normalize($string, $form);
  96. if ('' !== $string && false !== $string && $this->ignoreCase) {
  97. return \strlen($string) === \strlen($this->string) && 0 === mb_stripos($this->string, $string, 0, 'UTF-8');
  98. }
  99. return $string === $this->string;
  100. }
  101. public function indexOf(string|iterable|AbstractString $needle, int $offset = 0): ?int
  102. {
  103. if ($needle instanceof AbstractString) {
  104. $needle = $needle->string;
  105. } elseif (!\is_string($needle)) {
  106. return parent::indexOf($needle, $offset);
  107. }
  108. $form = null === $this->ignoreCase ? \Normalizer::NFD : \Normalizer::NFC;
  109. normalizer_is_normalized($needle, $form) ?: $needle = normalizer_normalize($needle, $form);
  110. if ('' === $needle || false === $needle) {
  111. return null;
  112. }
  113. try {
  114. $i = $this->ignoreCase ? grapheme_stripos($this->string, $needle, $offset) : grapheme_strpos($this->string, $needle, $offset);
  115. } catch (\ValueError) {
  116. return null;
  117. }
  118. return false === $i ? null : $i;
  119. }
  120. public function indexOfLast(string|iterable|AbstractString $needle, int $offset = 0): ?int
  121. {
  122. if ($needle instanceof AbstractString) {
  123. $needle = $needle->string;
  124. } elseif (!\is_string($needle)) {
  125. return parent::indexOfLast($needle, $offset);
  126. }
  127. $form = null === $this->ignoreCase ? \Normalizer::NFD : \Normalizer::NFC;
  128. normalizer_is_normalized($needle, $form) ?: $needle = normalizer_normalize($needle, $form);
  129. if ('' === $needle || false === $needle) {
  130. return null;
  131. }
  132. $string = $this->string;
  133. if (0 > $offset) {
  134. // workaround https://bugs.php.net/74264
  135. if (0 > $offset += grapheme_strlen($needle)) {
  136. $string = grapheme_substr($string, 0, $offset);
  137. }
  138. $offset = 0;
  139. }
  140. $i = $this->ignoreCase ? grapheme_strripos($string, $needle, $offset) : grapheme_strrpos($string, $needle, $offset);
  141. return false === $i ? null : $i;
  142. }
  143. public function join(array $strings, string $lastGlue = null): static
  144. {
  145. $str = parent::join($strings, $lastGlue);
  146. normalizer_is_normalized($str->string) ?: $str->string = normalizer_normalize($str->string);
  147. return $str;
  148. }
  149. public function length(): int
  150. {
  151. return grapheme_strlen($this->string);
  152. }
  153. public function normalize(int $form = self::NFC): static
  154. {
  155. $str = clone $this;
  156. if (\in_array($form, [self::NFC, self::NFKC], true)) {
  157. normalizer_is_normalized($str->string, $form) ?: $str->string = normalizer_normalize($str->string, $form);
  158. } elseif (!\in_array($form, [self::NFD, self::NFKD], true)) {
  159. throw new InvalidArgumentException('Unsupported normalization form.');
  160. } elseif (!normalizer_is_normalized($str->string, $form)) {
  161. $str->string = normalizer_normalize($str->string, $form);
  162. $str->ignoreCase = null;
  163. }
  164. return $str;
  165. }
  166. public function prepend(string ...$prefix): static
  167. {
  168. $str = clone $this;
  169. $str->string = (1 >= \count($prefix) ? ($prefix[0] ?? '') : implode('', $prefix)).$this->string;
  170. normalizer_is_normalized($str->string) ?: $str->string = normalizer_normalize($str->string);
  171. if (false === $str->string) {
  172. throw new InvalidArgumentException('Invalid UTF-8 string.');
  173. }
  174. return $str;
  175. }
  176. public function replace(string $from, string $to): static
  177. {
  178. $str = clone $this;
  179. normalizer_is_normalized($from) ?: $from = normalizer_normalize($from);
  180. if ('' !== $from && false !== $from) {
  181. $tail = $str->string;
  182. $result = '';
  183. $indexOf = $this->ignoreCase ? 'grapheme_stripos' : 'grapheme_strpos';
  184. while ('' !== $tail && false !== $i = $indexOf($tail, $from)) {
  185. $slice = grapheme_substr($tail, 0, $i);
  186. $result .= $slice.$to;
  187. $tail = substr($tail, \strlen($slice) + \strlen($from));
  188. }
  189. $str->string = $result.$tail;
  190. normalizer_is_normalized($str->string) ?: $str->string = normalizer_normalize($str->string);
  191. if (false === $str->string) {
  192. throw new InvalidArgumentException('Invalid UTF-8 string.');
  193. }
  194. }
  195. return $str;
  196. }
  197. public function replaceMatches(string $fromRegexp, string|callable $to): static
  198. {
  199. $str = parent::replaceMatches($fromRegexp, $to);
  200. normalizer_is_normalized($str->string) ?: $str->string = normalizer_normalize($str->string);
  201. return $str;
  202. }
  203. public function slice(int $start = 0, int $length = null): static
  204. {
  205. $str = clone $this;
  206. $str->string = (string) grapheme_substr($this->string, $start, $length ?? 2147483647);
  207. return $str;
  208. }
  209. public function splice(string $replacement, int $start = 0, int $length = null): static
  210. {
  211. $str = clone $this;
  212. $start = $start ? \strlen(grapheme_substr($this->string, 0, $start)) : 0;
  213. $length = $length ? \strlen(grapheme_substr($this->string, $start, $length ?? 2147483647)) : $length;
  214. $str->string = substr_replace($this->string, $replacement, $start, $length ?? 2147483647);
  215. normalizer_is_normalized($str->string) ?: $str->string = normalizer_normalize($str->string);
  216. if (false === $str->string) {
  217. throw new InvalidArgumentException('Invalid UTF-8 string.');
  218. }
  219. return $str;
  220. }
  221. public function split(string $delimiter, int $limit = null, int $flags = null): array
  222. {
  223. if (1 > $limit ??= 2147483647) {
  224. throw new InvalidArgumentException('Split limit must be a positive integer.');
  225. }
  226. if ('' === $delimiter) {
  227. throw new InvalidArgumentException('Split delimiter is empty.');
  228. }
  229. if (null !== $flags) {
  230. return parent::split($delimiter.'u', $limit, $flags);
  231. }
  232. normalizer_is_normalized($delimiter) ?: $delimiter = normalizer_normalize($delimiter);
  233. if (false === $delimiter) {
  234. throw new InvalidArgumentException('Split delimiter is not a valid UTF-8 string.');
  235. }
  236. $str = clone $this;
  237. $tail = $this->string;
  238. $chunks = [];
  239. $indexOf = $this->ignoreCase ? 'grapheme_stripos' : 'grapheme_strpos';
  240. while (1 < $limit && false !== $i = $indexOf($tail, $delimiter)) {
  241. $str->string = grapheme_substr($tail, 0, $i);
  242. $chunks[] = clone $str;
  243. $tail = substr($tail, \strlen($str->string) + \strlen($delimiter));
  244. --$limit;
  245. }
  246. $str->string = $tail;
  247. $chunks[] = clone $str;
  248. return $chunks;
  249. }
  250. public function startsWith(string|iterable|AbstractString $prefix): bool
  251. {
  252. if ($prefix instanceof AbstractString) {
  253. $prefix = $prefix->string;
  254. } elseif (!\is_string($prefix)) {
  255. return parent::startsWith($prefix);
  256. }
  257. $form = null === $this->ignoreCase ? \Normalizer::NFD : \Normalizer::NFC;
  258. normalizer_is_normalized($prefix, $form) ?: $prefix = normalizer_normalize($prefix, $form);
  259. if ('' === $prefix || false === $prefix) {
  260. return false;
  261. }
  262. if ($this->ignoreCase) {
  263. return 0 === mb_stripos(grapheme_extract($this->string, \strlen($prefix), \GRAPHEME_EXTR_MAXBYTES), $prefix, 0, 'UTF-8');
  264. }
  265. return $prefix === grapheme_extract($this->string, \strlen($prefix), \GRAPHEME_EXTR_MAXBYTES);
  266. }
  267. public function __wakeup()
  268. {
  269. if (!\is_string($this->string)) {
  270. throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  271. }
  272. normalizer_is_normalized($this->string) ?: $this->string = normalizer_normalize($this->string);
  273. }
  274. public function __clone()
  275. {
  276. if (null === $this->ignoreCase) {
  277. normalizer_is_normalized($this->string) ?: $this->string = normalizer_normalize($this->string);
  278. }
  279. $this->ignoreCase = false;
  280. }
  281. }