UnicodeString.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. if ('' === $string || normalizer_is_normalized($this->string = $string)) {
  34. return;
  35. }
  36. if (false === $string = normalizer_normalize($string)) {
  37. throw new InvalidArgumentException('Invalid UTF-8 string.');
  38. }
  39. $this->string = $string;
  40. }
  41. public function append(string ...$suffix): static
  42. {
  43. $str = clone $this;
  44. $str->string = $this->string.(1 >= \count($suffix) ? ($suffix[0] ?? '') : implode('', $suffix));
  45. if (normalizer_is_normalized($str->string)) {
  46. return $str;
  47. }
  48. if (false === $string = normalizer_normalize($str->string)) {
  49. throw new InvalidArgumentException('Invalid UTF-8 string.');
  50. }
  51. $str->string = $string;
  52. return $str;
  53. }
  54. public function chunk(int $length = 1): array
  55. {
  56. if (1 > $length) {
  57. throw new InvalidArgumentException('The chunk length must be greater than zero.');
  58. }
  59. if ('' === $this->string) {
  60. return [];
  61. }
  62. $rx = '/(';
  63. while (65535 < $length) {
  64. $rx .= '\X{65535}';
  65. $length -= 65535;
  66. }
  67. $rx .= '\X{'.$length.'})/u';
  68. $str = clone $this;
  69. $chunks = [];
  70. foreach (preg_split($rx, $this->string, -1, \PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY) as $chunk) {
  71. $str->string = $chunk;
  72. $chunks[] = clone $str;
  73. }
  74. return $chunks;
  75. }
  76. public function endsWith(string|iterable|AbstractString $suffix): bool
  77. {
  78. if ($suffix instanceof AbstractString) {
  79. $suffix = $suffix->string;
  80. } elseif (!\is_string($suffix)) {
  81. return parent::endsWith($suffix);
  82. }
  83. $form = null === $this->ignoreCase ? \Normalizer::NFD : \Normalizer::NFC;
  84. normalizer_is_normalized($suffix, $form) ?: $suffix = normalizer_normalize($suffix, $form);
  85. if ('' === $suffix || false === $suffix) {
  86. return false;
  87. }
  88. if (false === $grapheme = grapheme_extract($this->string, \strlen($suffix), \GRAPHEME_EXTR_MAXBYTES, \strlen($this->string) - \strlen($suffix))) {
  89. $grapheme = '';
  90. }
  91. if ($this->ignoreCase) {
  92. return 0 === mb_stripos($grapheme, $suffix, 0, 'UTF-8');
  93. }
  94. return $suffix === $grapheme;
  95. }
  96. public function equalsTo(string|iterable|AbstractString $string): bool
  97. {
  98. if ($string instanceof AbstractString) {
  99. $string = $string->string;
  100. } elseif (!\is_string($string)) {
  101. return parent::equalsTo($string);
  102. }
  103. $form = null === $this->ignoreCase ? \Normalizer::NFD : \Normalizer::NFC;
  104. normalizer_is_normalized($string, $form) ?: $string = normalizer_normalize($string, $form);
  105. if ('' !== $string && false !== $string && $this->ignoreCase) {
  106. return \strlen($string) === \strlen($this->string) && 0 === mb_stripos($this->string, $string, 0, 'UTF-8');
  107. }
  108. return $string === $this->string;
  109. }
  110. public function indexOf(string|iterable|AbstractString $needle, int $offset = 0): ?int
  111. {
  112. if ($needle instanceof AbstractString) {
  113. $needle = $needle->string;
  114. } elseif (!\is_string($needle)) {
  115. return parent::indexOf($needle, $offset);
  116. }
  117. $form = null === $this->ignoreCase ? \Normalizer::NFD : \Normalizer::NFC;
  118. normalizer_is_normalized($needle, $form) ?: $needle = normalizer_normalize($needle, $form);
  119. if ('' === $needle || false === $needle) {
  120. return null;
  121. }
  122. try {
  123. $i = $this->ignoreCase ? grapheme_stripos($this->string, $needle, $offset) : grapheme_strpos($this->string, $needle, $offset);
  124. } catch (\ValueError) {
  125. return null;
  126. }
  127. return false === $i ? null : $i;
  128. }
  129. public function indexOfLast(string|iterable|AbstractString $needle, int $offset = 0): ?int
  130. {
  131. if ($needle instanceof AbstractString) {
  132. $needle = $needle->string;
  133. } elseif (!\is_string($needle)) {
  134. return parent::indexOfLast($needle, $offset);
  135. }
  136. $form = null === $this->ignoreCase ? \Normalizer::NFD : \Normalizer::NFC;
  137. normalizer_is_normalized($needle, $form) ?: $needle = normalizer_normalize($needle, $form);
  138. if ('' === $needle || false === $needle) {
  139. return null;
  140. }
  141. $string = $this->string;
  142. if (0 > $offset) {
  143. // workaround https://bugs.php.net/74264
  144. if (0 > $offset += grapheme_strlen($needle)) {
  145. $string = grapheme_substr($string, 0, $offset);
  146. }
  147. $offset = 0;
  148. }
  149. try {
  150. $i = $this->ignoreCase ? grapheme_strripos($string, $needle, $offset) : grapheme_strrpos($string, $needle, $offset);
  151. } catch (\ValueError) {
  152. return null;
  153. }
  154. return false === $i ? null : $i;
  155. }
  156. public function join(array $strings, ?string $lastGlue = null): static
  157. {
  158. $str = parent::join($strings, $lastGlue);
  159. normalizer_is_normalized($str->string) ?: $str->string = normalizer_normalize($str->string);
  160. return $str;
  161. }
  162. public function length(): int
  163. {
  164. return grapheme_strlen($this->string);
  165. }
  166. public function normalize(int $form = self::NFC): static
  167. {
  168. $str = clone $this;
  169. if (\in_array($form, [self::NFC, self::NFKC], true)) {
  170. normalizer_is_normalized($str->string, $form) ?: $str->string = normalizer_normalize($str->string, $form);
  171. } elseif (!\in_array($form, [self::NFD, self::NFKD], true)) {
  172. throw new InvalidArgumentException('Unsupported normalization form.');
  173. } elseif (!normalizer_is_normalized($str->string, $form)) {
  174. $str->string = normalizer_normalize($str->string, $form);
  175. $str->ignoreCase = null;
  176. }
  177. return $str;
  178. }
  179. public function prepend(string ...$prefix): static
  180. {
  181. $str = clone $this;
  182. $str->string = (1 >= \count($prefix) ? ($prefix[0] ?? '') : implode('', $prefix)).$this->string;
  183. if (normalizer_is_normalized($str->string)) {
  184. return $str;
  185. }
  186. if (false === $string = normalizer_normalize($str->string)) {
  187. throw new InvalidArgumentException('Invalid UTF-8 string.');
  188. }
  189. $str->string = $string;
  190. return $str;
  191. }
  192. public function replace(string $from, string $to): static
  193. {
  194. $str = clone $this;
  195. normalizer_is_normalized($from) ?: $from = normalizer_normalize($from);
  196. if ('' !== $from && false !== $from) {
  197. $tail = $str->string;
  198. $result = '';
  199. $indexOf = $this->ignoreCase ? 'grapheme_stripos' : 'grapheme_strpos';
  200. while ('' !== $tail && false !== $i = $indexOf($tail, $from)) {
  201. $slice = grapheme_substr($tail, 0, $i);
  202. $result .= $slice.$to;
  203. $tail = substr($tail, \strlen($slice) + \strlen($from));
  204. }
  205. $str->string = $result.$tail;
  206. if (normalizer_is_normalized($str->string)) {
  207. return $str;
  208. }
  209. if (false === $string = normalizer_normalize($str->string)) {
  210. throw new InvalidArgumentException('Invalid UTF-8 string.');
  211. }
  212. $str->string = $string;
  213. }
  214. return $str;
  215. }
  216. public function replaceMatches(string $fromRegexp, string|callable $to): static
  217. {
  218. $str = parent::replaceMatches($fromRegexp, $to);
  219. normalizer_is_normalized($str->string) ?: $str->string = normalizer_normalize($str->string);
  220. return $str;
  221. }
  222. public function slice(int $start = 0, ?int $length = null): static
  223. {
  224. $str = clone $this;
  225. $str->string = (string) grapheme_substr($this->string, $start, $length ?? 2147483647);
  226. return $str;
  227. }
  228. public function splice(string $replacement, int $start = 0, ?int $length = null): static
  229. {
  230. $str = clone $this;
  231. $start = $start ? \strlen(grapheme_substr($this->string, 0, $start)) : 0;
  232. $length = $length ? \strlen(grapheme_substr(substr($this->string, $start), 0, $length)) : $length;
  233. $str->string = substr_replace($this->string, $replacement, $start, $length ?? 2147483647);
  234. if (normalizer_is_normalized($str->string)) {
  235. return $str;
  236. }
  237. if (false === $string = normalizer_normalize($str->string)) {
  238. throw new InvalidArgumentException('Invalid UTF-8 string.');
  239. }
  240. $str->string = $string;
  241. return $str;
  242. }
  243. public function split(string $delimiter, ?int $limit = null, ?int $flags = null): array
  244. {
  245. if (1 > $limit ??= 2147483647) {
  246. throw new InvalidArgumentException('Split limit must be a positive integer.');
  247. }
  248. if ('' === $delimiter) {
  249. throw new InvalidArgumentException('Split delimiter is empty.');
  250. }
  251. if (null !== $flags) {
  252. return parent::split($delimiter.'u', $limit, $flags);
  253. }
  254. normalizer_is_normalized($delimiter) ?: $delimiter = normalizer_normalize($delimiter);
  255. if (false === $delimiter) {
  256. throw new InvalidArgumentException('Split delimiter is not a valid UTF-8 string.');
  257. }
  258. $str = clone $this;
  259. $tail = $this->string;
  260. $chunks = [];
  261. $indexOf = $this->ignoreCase ? 'grapheme_stripos' : 'grapheme_strpos';
  262. while (1 < $limit && false !== $i = $indexOf($tail, $delimiter)) {
  263. $str->string = grapheme_substr($tail, 0, $i);
  264. $chunks[] = clone $str;
  265. $tail = substr($tail, \strlen($str->string) + \strlen($delimiter));
  266. --$limit;
  267. }
  268. $str->string = $tail;
  269. $chunks[] = clone $str;
  270. return $chunks;
  271. }
  272. public function startsWith(string|iterable|AbstractString $prefix): bool
  273. {
  274. if ($prefix instanceof AbstractString) {
  275. $prefix = $prefix->string;
  276. } elseif (!\is_string($prefix)) {
  277. return parent::startsWith($prefix);
  278. }
  279. $form = null === $this->ignoreCase ? \Normalizer::NFD : \Normalizer::NFC;
  280. normalizer_is_normalized($prefix, $form) ?: $prefix = normalizer_normalize($prefix, $form);
  281. if ('' === $prefix || false === $prefix) {
  282. return false;
  283. }
  284. if (false === $grapheme = grapheme_extract($this->string, \strlen($prefix), \GRAPHEME_EXTR_MAXBYTES)) {
  285. $grapheme = '';
  286. }
  287. if ($this->ignoreCase) {
  288. return 0 === mb_stripos($grapheme, $prefix, 0, 'UTF-8');
  289. }
  290. return $prefix === $grapheme;
  291. }
  292. public function trimPrefix($prefix): static
  293. {
  294. if (\is_array($prefix) || $prefix instanceof \Traversable) {
  295. return parent::trimPrefix($prefix);
  296. }
  297. if ($prefix instanceof AbstractString) {
  298. $prefix = $prefix->string;
  299. } else {
  300. $prefix = (string) $prefix;
  301. }
  302. if (!normalizer_is_normalized($prefix, \Normalizer::NFC)) {
  303. $prefix = normalizer_normalize($prefix, \Normalizer::NFC);
  304. }
  305. return parent::trimPrefix($prefix);
  306. }
  307. public function trimSuffix($suffix): static
  308. {
  309. if (\is_array($suffix) || $suffix instanceof \Traversable) {
  310. return parent::trimSuffix($suffix);
  311. }
  312. if ($suffix instanceof AbstractString) {
  313. $suffix = $suffix->string;
  314. } else {
  315. $suffix = (string) $suffix;
  316. }
  317. if (!normalizer_is_normalized($suffix, \Normalizer::NFC)) {
  318. $suffix = normalizer_normalize($suffix, \Normalizer::NFC);
  319. }
  320. return parent::trimSuffix($suffix);
  321. }
  322. public function __unserialize(array $data): void
  323. {
  324. if ($wakeup = self::class !== (new \ReflectionMethod($this, '__wakeup'))->class && self::class === (new \ReflectionMethod($this, '__unserialize'))->class) {
  325. trigger_deprecation('symfony/string', '7.4', 'Implementing "%s::__wakeup()" is deprecated, use "__unserialize()" instead.', get_debug_type($this));
  326. }
  327. if (($data['string'] ?? null) instanceof \Stringable || ($data["\0*\0string"] ?? null) instanceof \Stringable) {
  328. throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  329. }
  330. try {
  331. if (\in_array(array_keys($data), [['string'], ["\0*\0string"]], true)) {
  332. $this->string = $data['string'] ?? $data["\0*\0string"];
  333. if ($wakeup) {
  334. $this->__wakeup();
  335. }
  336. return;
  337. }
  338. trigger_deprecation('symfony/string', '7.4', 'Passing more than just key "string" to "%s::__unserialize()" is deprecated, populate properties in "%s::__unserialize()" instead.', self::class, get_debug_type($this));
  339. \Closure::bind(function ($data) use ($wakeup) {
  340. foreach ($data as $key => $value) {
  341. $this->{("\0" === $key[0] ?? '') ? substr($key, 1 + strrpos($key, "\0")) : $key} = $value;
  342. }
  343. if ($wakeup) {
  344. $this->__wakeup();
  345. }
  346. }, $this, static::class)($data);
  347. } finally {
  348. if (!$wakeup) {
  349. normalizer_is_normalized($this->string) ?: $this->string = normalizer_normalize($this->string);
  350. }
  351. }
  352. }
  353. /**
  354. * @deprecated since Symfony 7.4, will be replaced by `__unserialize()` in 8.0
  355. */
  356. public function __wakeup(): void
  357. {
  358. trigger_deprecation('symfony/string', '7.4', 'Calling "%s::__wakeup()" is deprecated, use "__unserialize()" instead.', get_debug_type($this));
  359. if (!\is_string($this->string)) {
  360. throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  361. }
  362. normalizer_is_normalized($this->string) ?: $this->string = normalizer_normalize($this->string);
  363. }
  364. public function __clone()
  365. {
  366. if (null === $this->ignoreCase) {
  367. normalizer_is_normalized($this->string) ?: $this->string = normalizer_normalize($this->string);
  368. }
  369. $this->ignoreCase = false;
  370. }
  371. }