UnicodeString.php 13 KB

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