AbstractUnicodeString.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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. use Symfony\Component\String\Exception\RuntimeException;
  14. /**
  15. * Represents a string of abstract Unicode characters.
  16. *
  17. * Unicode defines 3 types of "characters" (bytes, code points and grapheme clusters).
  18. * This class is the abstract type to use as a type-hint when the logic you want to
  19. * implement is Unicode-aware but doesn't care about code points vs grapheme clusters.
  20. *
  21. * @author Nicolas Grekas <p@tchwork.com>
  22. *
  23. * @throws ExceptionInterface
  24. */
  25. abstract class AbstractUnicodeString extends AbstractString
  26. {
  27. public const NFC = \Normalizer::NFC;
  28. public const NFD = \Normalizer::NFD;
  29. public const NFKC = \Normalizer::NFKC;
  30. public const NFKD = \Normalizer::NFKD;
  31. // all ASCII letters sorted by typical frequency of occurrence
  32. private const ASCII = "\x20\x65\x69\x61\x73\x6E\x74\x72\x6F\x6C\x75\x64\x5D\x5B\x63\x6D\x70\x27\x0A\x67\x7C\x68\x76\x2E\x66\x62\x2C\x3A\x3D\x2D\x71\x31\x30\x43\x32\x2A\x79\x78\x29\x28\x4C\x39\x41\x53\x2F\x50\x22\x45\x6A\x4D\x49\x6B\x33\x3E\x35\x54\x3C\x44\x34\x7D\x42\x7B\x38\x46\x77\x52\x36\x37\x55\x47\x4E\x3B\x4A\x7A\x56\x23\x48\x4F\x57\x5F\x26\x21\x4B\x3F\x58\x51\x25\x59\x5C\x09\x5A\x2B\x7E\x5E\x24\x40\x60\x7F\x00\x01\x02\x03\x04\x05\x06\x07\x08\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F";
  33. // the subset of folded case mappings that is not in lower case mappings
  34. private const FOLD_FROM = ['İ', 'µ', 'ſ', "\xCD\x85", 'ς', 'ϐ', 'ϑ', 'ϕ', 'ϖ', 'ϰ', 'ϱ', 'ϵ', 'ẛ', "\xE1\xBE\xBE", 'ß', 'ʼn', 'ǰ', 'ΐ', 'ΰ', 'և', 'ẖ', 'ẗ', 'ẘ', 'ẙ', 'ẚ', 'ẞ', 'ὐ', 'ὒ', 'ὔ', 'ὖ', 'ᾀ', 'ᾁ', 'ᾂ', 'ᾃ', 'ᾄ', 'ᾅ', 'ᾆ', 'ᾇ', 'ᾈ', 'ᾉ', 'ᾊ', 'ᾋ', 'ᾌ', 'ᾍ', 'ᾎ', 'ᾏ', 'ᾐ', 'ᾑ', 'ᾒ', 'ᾓ', 'ᾔ', 'ᾕ', 'ᾖ', 'ᾗ', 'ᾘ', 'ᾙ', 'ᾚ', 'ᾛ', 'ᾜ', 'ᾝ', 'ᾞ', 'ᾟ', 'ᾠ', 'ᾡ', 'ᾢ', 'ᾣ', 'ᾤ', 'ᾥ', 'ᾦ', 'ᾧ', 'ᾨ', 'ᾩ', 'ᾪ', 'ᾫ', 'ᾬ', 'ᾭ', 'ᾮ', 'ᾯ', 'ᾲ', 'ᾳ', 'ᾴ', 'ᾶ', 'ᾷ', 'ᾼ', 'ῂ', 'ῃ', 'ῄ', 'ῆ', 'ῇ', 'ῌ', 'ῒ', 'ῖ', 'ῗ', 'ῢ', 'ῤ', 'ῦ', 'ῧ', 'ῲ', 'ῳ', 'ῴ', 'ῶ', 'ῷ', 'ῼ', 'ff', 'fi', 'fl', 'ffi', 'ffl', 'ſt', 'st', 'ﬓ', 'ﬔ', 'ﬕ', 'ﬖ', 'ﬗ'];
  35. private const FOLD_TO = ['i̇', 'μ', 's', 'ι', 'σ', 'β', 'θ', 'φ', 'π', 'κ', 'ρ', 'ε', 'ṡ', 'ι', 'ss', 'ʼn', 'ǰ', 'ΐ', 'ΰ', 'եւ', 'ẖ', 'ẗ', 'ẘ', 'ẙ', 'aʾ', 'ss', 'ὐ', 'ὒ', 'ὔ', 'ὖ', 'ἀι', 'ἁι', 'ἂι', 'ἃι', 'ἄι', 'ἅι', 'ἆι', 'ἇι', 'ἀι', 'ἁι', 'ἂι', 'ἃι', 'ἄι', 'ἅι', 'ἆι', 'ἇι', 'ἠι', 'ἡι', 'ἢι', 'ἣι', 'ἤι', 'ἥι', 'ἦι', 'ἧι', 'ἠι', 'ἡι', 'ἢι', 'ἣι', 'ἤι', 'ἥι', 'ἦι', 'ἧι', 'ὠι', 'ὡι', 'ὢι', 'ὣι', 'ὤι', 'ὥι', 'ὦι', 'ὧι', 'ὠι', 'ὡι', 'ὢι', 'ὣι', 'ὤι', 'ὥι', 'ὦι', 'ὧι', 'ὰι', 'αι', 'άι', 'ᾶ', 'ᾶι', 'αι', 'ὴι', 'ηι', 'ήι', 'ῆ', 'ῆι', 'ηι', 'ῒ', 'ῖ', 'ῗ', 'ῢ', 'ῤ', 'ῦ', 'ῧ', 'ὼι', 'ωι', 'ώι', 'ῶ', 'ῶι', 'ωι', 'ff', 'fi', 'fl', 'ffi', 'ffl', 'st', 'st', 'մն', 'մե', 'մի', 'վն', 'մխ'];
  36. // the subset of upper case mappings that map one code point to many code points
  37. private const UPPER_FROM = ['ß', 'ff', 'fi', 'fl', 'ffi', 'ffl', 'ſt', 'st', 'և', 'ﬓ', 'ﬔ', 'ﬕ', 'ﬖ', 'ﬗ', 'ʼn', 'ΐ', 'ΰ', 'ǰ', 'ẖ', 'ẗ', 'ẘ', 'ẙ', 'ẚ', 'ὐ', 'ὒ', 'ὔ', 'ὖ', 'ᾶ', 'ῆ', 'ῒ', 'ΐ', 'ῖ', 'ῗ', 'ῢ', 'ΰ', 'ῤ', 'ῦ', 'ῧ', 'ῶ'];
  38. private const UPPER_TO = ['SS', 'FF', 'FI', 'FL', 'FFI', 'FFL', 'ST', 'ST', 'ԵՒ', 'ՄՆ', 'ՄԵ', 'ՄԻ', 'ՎՆ', 'ՄԽ', 'ʼN', 'Ϊ́', 'Ϋ́', 'J̌', 'H̱', 'T̈', 'W̊', 'Y̊', 'Aʾ', 'Υ̓', 'Υ̓̀', 'Υ̓́', 'Υ̓͂', 'Α͂', 'Η͂', 'Ϊ̀', 'Ϊ́', 'Ι͂', 'Ϊ͂', 'Ϋ̀', 'Ϋ́', 'Ρ̓', 'Υ͂', 'Ϋ͂', 'Ω͂'];
  39. // the subset of https://github.com/unicode-org/cldr/blob/master/common/transforms/Latin-ASCII.xml that is not in NFKD
  40. private const TRANSLIT_FROM = ['Æ', 'Ð', 'Ø', 'Þ', 'ß', 'æ', 'ð', 'ø', 'þ', 'Đ', 'đ', 'Ħ', 'ħ', 'ı', 'ĸ', 'Ŀ', 'ŀ', 'Ł', 'ł', 'ʼn', 'Ŋ', 'ŋ', 'Œ', 'œ', 'Ŧ', 'ŧ', 'ƀ', 'Ɓ', 'Ƃ', 'ƃ', 'Ƈ', 'ƈ', 'Ɖ', 'Ɗ', 'Ƌ', 'ƌ', 'Ɛ', 'Ƒ', 'ƒ', 'Ɠ', 'ƕ', 'Ɩ', 'Ɨ', 'Ƙ', 'ƙ', 'ƚ', 'Ɲ', 'ƞ', 'Ƣ', 'ƣ', 'Ƥ', 'ƥ', 'ƫ', 'Ƭ', 'ƭ', 'Ʈ', 'Ʋ', 'Ƴ', 'ƴ', 'Ƶ', 'ƶ', 'DŽ', 'Dž', 'dž', 'Ǥ', 'ǥ', 'ȡ', 'Ȥ', 'ȥ', 'ȴ', 'ȵ', 'ȶ', 'ȷ', 'ȸ', 'ȹ', 'Ⱥ', 'Ȼ', 'ȼ', 'Ƚ', 'Ⱦ', 'ȿ', 'ɀ', 'Ƀ', 'Ʉ', 'Ɇ', 'ɇ', 'Ɉ', 'ɉ', 'Ɍ', 'ɍ', 'Ɏ', 'ɏ', 'ɓ', 'ɕ', 'ɖ', 'ɗ', 'ɛ', 'ɟ', 'ɠ', 'ɡ', 'ɢ', 'ɦ', 'ɧ', 'ɨ', 'ɪ', 'ɫ', 'ɬ', 'ɭ', 'ɱ', 'ɲ', 'ɳ', 'ɴ', 'ɶ', 'ɼ', 'ɽ', 'ɾ', 'ʀ', 'ʂ', 'ʈ', 'ʉ', 'ʋ', 'ʏ', 'ʐ', 'ʑ', 'ʙ', 'ʛ', 'ʜ', 'ʝ', 'ʟ', 'ʠ', 'ʣ', 'ʥ', 'ʦ', 'ʪ', 'ʫ', 'ᴀ', 'ᴁ', 'ᴃ', 'ᴄ', 'ᴅ', 'ᴆ', 'ᴇ', 'ᴊ', 'ᴋ', 'ᴌ', 'ᴍ', 'ᴏ', 'ᴘ', 'ᴛ', 'ᴜ', 'ᴠ', 'ᴡ', 'ᴢ', 'ᵫ', 'ᵬ', 'ᵭ', 'ᵮ', 'ᵯ', 'ᵰ', 'ᵱ', 'ᵲ', 'ᵳ', 'ᵴ', 'ᵵ', 'ᵶ', 'ᵺ', 'ᵻ', 'ᵽ', 'ᵾ', 'ᶀ', 'ᶁ', 'ᶂ', 'ᶃ', 'ᶄ', 'ᶅ', 'ᶆ', 'ᶇ', 'ᶈ', 'ᶉ', 'ᶊ', 'ᶌ', 'ᶍ', 'ᶎ', 'ᶏ', 'ᶑ', 'ᶒ', 'ᶓ', 'ᶖ', 'ᶙ', 'ẚ', 'ẜ', 'ẝ', 'ẞ', 'Ỻ', 'ỻ', 'Ỽ', 'ỽ', 'Ỿ', 'ỿ', '©', '®', '₠', '₢', '₣', '₤', '₧', '₺', '₹', 'ℌ', '℞', '㎧', '㎮', '㏆', '㏗', '㏞', '㏟', '¼', '½', '¾', '⅓', '⅔', '⅕', '⅖', '⅗', '⅘', '⅙', '⅚', '⅛', '⅜', '⅝', '⅞', '⅟', '〇', '‘', '’', '‚', '‛', '“', '”', '„', '‟', '′', '″', '〝', '〞', '«', '»', '‹', '›', '‐', '‑', '‒', '–', '—', '―', '︱', '︲', '﹘', '‖', '⁄', '⁅', '⁆', '⁎', '、', '。', '〈', '〉', '《', '》', '〔', '〕', '〘', '〙', '〚', '〛', '︑', '︒', '︹', '︺', '︽', '︾', '︿', '﹀', '﹑', '﹝', '﹞', '⦅', '⦆', '。', '、', '×', '÷', '−', '∕', '∖', '∣', '∥', '≪', '≫', '⦅', '⦆'];
  41. private const TRANSLIT_TO = ['AE', 'D', 'O', 'TH', 'ss', 'ae', 'd', 'o', 'th', 'D', 'd', 'H', 'h', 'i', 'q', 'L', 'l', 'L', 'l', '\'n', 'N', 'n', 'OE', 'oe', 'T', 't', 'b', 'B', 'B', 'b', 'C', 'c', 'D', 'D', 'D', 'd', 'E', 'F', 'f', 'G', 'hv', 'I', 'I', 'K', 'k', 'l', 'N', 'n', 'OI', 'oi', 'P', 'p', 't', 'T', 't', 'T', 'V', 'Y', 'y', 'Z', 'z', 'DZ', 'Dz', 'dz', 'G', 'g', 'd', 'Z', 'z', 'l', 'n', 't', 'j', 'db', 'qp', 'A', 'C', 'c', 'L', 'T', 's', 'z', 'B', 'U', 'E', 'e', 'J', 'j', 'R', 'r', 'Y', 'y', 'b', 'c', 'd', 'd', 'e', 'j', 'g', 'g', 'G', 'h', 'h', 'i', 'I', 'l', 'l', 'l', 'm', 'n', 'n', 'N', 'OE', 'r', 'r', 'r', 'R', 's', 't', 'u', 'v', 'Y', 'z', 'z', 'B', 'G', 'H', 'j', 'L', 'q', 'dz', 'dz', 'ts', 'ls', 'lz', 'A', 'AE', 'B', 'C', 'D', 'D', 'E', 'J', 'K', 'L', 'M', 'O', 'P', 'T', 'U', 'V', 'W', 'Z', 'ue', 'b', 'd', 'f', 'm', 'n', 'p', 'r', 'r', 's', 't', 'z', 'th', 'I', 'p', 'U', 'b', 'd', 'f', 'g', 'k', 'l', 'm', 'n', 'p', 'r', 's', 'v', 'x', 'z', 'a', 'd', 'e', 'e', 'i', 'u', 'a', 's', 's', 'SS', 'LL', 'll', 'V', 'v', 'Y', 'y', '(C)', '(R)', 'CE', 'Cr', 'Fr.', 'L.', 'Pts', 'TL', 'Rs', 'x', 'Rx', 'm/s', 'rad/s', 'C/kg', 'pH', 'V/m', 'A/m', ' 1/4', ' 1/2', ' 3/4', ' 1/3', ' 2/3', ' 1/5', ' 2/5', ' 3/5', ' 4/5', ' 1/6', ' 5/6', ' 1/8', ' 3/8', ' 5/8', ' 7/8', ' 1/', '0', '\'', '\'', ',', '\'', '"', '"', ',,', '"', '\'', '"', '"', '"', '<<', '>>', '<', '>', '-', '-', '-', '-', '-', '-', '-', '-', '-', '||', '/', '[', ']', '*', ',', '.', '<', '>', '<<', '>>', '[', ']', '[', ']', '[', ']', ',', '.', '[', ']', '<<', '>>', '<', '>', ',', '[', ']', '((', '))', '.', ',', '*', '/', '-', '/', '\\', '|', '||', '<<', '>>', '((', '))'];
  42. private static $transliterators = [];
  43. private static $tableZero;
  44. private static $tableWide;
  45. /**
  46. * @return static
  47. */
  48. public static function fromCodePoints(int ...$codes): self
  49. {
  50. $string = '';
  51. foreach ($codes as $code) {
  52. if (0x80 > $code %= 0x200000) {
  53. $string .= \chr($code);
  54. } elseif (0x800 > $code) {
  55. $string .= \chr(0xC0 | $code >> 6).\chr(0x80 | $code & 0x3F);
  56. } elseif (0x10000 > $code) {
  57. $string .= \chr(0xE0 | $code >> 12).\chr(0x80 | $code >> 6 & 0x3F).\chr(0x80 | $code & 0x3F);
  58. } else {
  59. $string .= \chr(0xF0 | $code >> 18).\chr(0x80 | $code >> 12 & 0x3F).\chr(0x80 | $code >> 6 & 0x3F).\chr(0x80 | $code & 0x3F);
  60. }
  61. }
  62. return new static($string);
  63. }
  64. /**
  65. * Generic UTF-8 to ASCII transliteration.
  66. *
  67. * Install the intl extension for best results.
  68. *
  69. * @param string[]|\Transliterator[]|\Closure[] $rules See "*-Latin" rules from Transliterator::listIDs()
  70. */
  71. public function ascii(array $rules = []): self
  72. {
  73. $str = clone $this;
  74. $s = $str->string;
  75. $str->string = '';
  76. array_unshift($rules, 'nfd');
  77. $rules[] = 'latin-ascii';
  78. if (\function_exists('transliterator_transliterate')) {
  79. $rules[] = 'any-latin/bgn';
  80. }
  81. $rules[] = 'nfkd';
  82. $rules[] = '[:nonspacing mark:] remove';
  83. while (\strlen($s) - 1 > $i = strspn($s, self::ASCII)) {
  84. if (0 < --$i) {
  85. $str->string .= substr($s, 0, $i);
  86. $s = substr($s, $i);
  87. }
  88. if (!$rule = array_shift($rules)) {
  89. $rules = []; // An empty rule interrupts the next ones
  90. }
  91. if ($rule instanceof \Transliterator) {
  92. $s = $rule->transliterate($s);
  93. } elseif ($rule instanceof \Closure) {
  94. $s = $rule($s);
  95. } elseif ($rule) {
  96. if ('nfd' === $rule = strtolower($rule)) {
  97. normalizer_is_normalized($s, self::NFD) ?: $s = normalizer_normalize($s, self::NFD);
  98. } elseif ('nfkd' === $rule) {
  99. normalizer_is_normalized($s, self::NFKD) ?: $s = normalizer_normalize($s, self::NFKD);
  100. } elseif ('[:nonspacing mark:] remove' === $rule) {
  101. $s = preg_replace('/\p{Mn}++/u', '', $s);
  102. } elseif ('latin-ascii' === $rule) {
  103. $s = str_replace(self::TRANSLIT_FROM, self::TRANSLIT_TO, $s);
  104. } elseif ('de-ascii' === $rule) {
  105. $s = preg_replace("/([AUO])\u{0308}(?=\p{Ll})/u", '$1e', $s);
  106. $s = str_replace(["a\u{0308}", "o\u{0308}", "u\u{0308}", "A\u{0308}", "O\u{0308}", "U\u{0308}"], ['ae', 'oe', 'ue', 'AE', 'OE', 'UE'], $s);
  107. } elseif (\function_exists('transliterator_transliterate')) {
  108. if (null === $transliterator = self::$transliterators[$rule] ?? self::$transliterators[$rule] = \Transliterator::create($rule)) {
  109. if ('any-latin/bgn' === $rule) {
  110. $rule = 'any-latin';
  111. $transliterator = self::$transliterators[$rule] ?? self::$transliterators[$rule] = \Transliterator::create($rule);
  112. }
  113. if (null === $transliterator) {
  114. throw new InvalidArgumentException(sprintf('Unknown transliteration rule "%s".', $rule));
  115. }
  116. self::$transliterators['any-latin/bgn'] = $transliterator;
  117. }
  118. $s = $transliterator->transliterate($s);
  119. }
  120. } elseif (!\function_exists('iconv')) {
  121. $s = preg_replace('/[^\x00-\x7F]/u', '?', $s);
  122. } else {
  123. $s = @preg_replace_callback('/[^\x00-\x7F]/u', static function ($c) {
  124. $c = (string) iconv('UTF-8', 'ASCII//TRANSLIT', $c[0]);
  125. if ('' === $c && '' === iconv('UTF-8', 'ASCII//TRANSLIT', '²')) {
  126. throw new \LogicException(sprintf('"%s" requires a translit-able iconv implementation, try installing "gnu-libiconv" if you\'re using Alpine Linux.', static::class));
  127. }
  128. return 1 < \strlen($c) ? ltrim($c, '\'`"^~') : ('' !== $c ? $c : '?');
  129. }, $s);
  130. }
  131. }
  132. $str->string .= $s;
  133. return $str;
  134. }
  135. public function camel(): parent
  136. {
  137. $str = clone $this;
  138. $str->string = str_replace(' ', '', preg_replace_callback('/\b.(?![A-Z]{2,})/u', static function ($m) use (&$i) {
  139. return 1 === ++$i ? ('İ' === $m[0] ? 'i̇' : mb_strtolower($m[0], 'UTF-8')) : mb_convert_case($m[0], \MB_CASE_TITLE, 'UTF-8');
  140. }, preg_replace('/[^\pL0-9]++/u', ' ', $this->string)));
  141. return $str;
  142. }
  143. /**
  144. * @return int[]
  145. */
  146. public function codePointsAt(int $offset): array
  147. {
  148. $str = $this->slice($offset, 1);
  149. if ('' === $str->string) {
  150. return [];
  151. }
  152. $codePoints = [];
  153. foreach (preg_split('//u', $str->string, -1, \PREG_SPLIT_NO_EMPTY) as $c) {
  154. $codePoints[] = mb_ord($c, 'UTF-8');
  155. }
  156. return $codePoints;
  157. }
  158. public function folded(bool $compat = true): parent
  159. {
  160. $str = clone $this;
  161. if (!$compat || \PHP_VERSION_ID < 70300 || !\defined('Normalizer::NFKC_CF')) {
  162. $str->string = normalizer_normalize($str->string, $compat ? \Normalizer::NFKC : \Normalizer::NFC);
  163. $str->string = mb_strtolower(str_replace(self::FOLD_FROM, self::FOLD_TO, $this->string), 'UTF-8');
  164. } else {
  165. $str->string = normalizer_normalize($str->string, \Normalizer::NFKC_CF);
  166. }
  167. return $str;
  168. }
  169. public function join(array $strings, string $lastGlue = null): parent
  170. {
  171. $str = clone $this;
  172. $tail = null !== $lastGlue && 1 < \count($strings) ? $lastGlue.array_pop($strings) : '';
  173. $str->string = implode($this->string, $strings).$tail;
  174. if (!preg_match('//u', $str->string)) {
  175. throw new InvalidArgumentException('Invalid UTF-8 string.');
  176. }
  177. return $str;
  178. }
  179. public function lower(): parent
  180. {
  181. $str = clone $this;
  182. $str->string = mb_strtolower(str_replace('İ', 'i̇', $str->string), 'UTF-8');
  183. return $str;
  184. }
  185. public function match(string $regexp, int $flags = 0, int $offset = 0): array
  186. {
  187. $match = ((\PREG_PATTERN_ORDER | \PREG_SET_ORDER) & $flags) ? 'preg_match_all' : 'preg_match';
  188. if ($this->ignoreCase) {
  189. $regexp .= 'i';
  190. }
  191. set_error_handler(static function ($t, $m) { throw new InvalidArgumentException($m); });
  192. try {
  193. if (false === $match($regexp.'u', $this->string, $matches, $flags | \PREG_UNMATCHED_AS_NULL, $offset)) {
  194. $lastError = preg_last_error();
  195. foreach (get_defined_constants(true)['pcre'] as $k => $v) {
  196. if ($lastError === $v && '_ERROR' === substr($k, -6)) {
  197. throw new RuntimeException('Matching failed with '.$k.'.');
  198. }
  199. }
  200. throw new RuntimeException('Matching failed with unknown error code.');
  201. }
  202. } finally {
  203. restore_error_handler();
  204. }
  205. return $matches;
  206. }
  207. /**
  208. * @return static
  209. */
  210. public function normalize(int $form = self::NFC): self
  211. {
  212. if (!\in_array($form, [self::NFC, self::NFD, self::NFKC, self::NFKD])) {
  213. throw new InvalidArgumentException('Unsupported normalization form.');
  214. }
  215. $str = clone $this;
  216. normalizer_is_normalized($str->string, $form) ?: $str->string = normalizer_normalize($str->string, $form);
  217. return $str;
  218. }
  219. public function padBoth(int $length, string $padStr = ' '): parent
  220. {
  221. if ('' === $padStr || !preg_match('//u', $padStr)) {
  222. throw new InvalidArgumentException('Invalid UTF-8 string.');
  223. }
  224. $pad = clone $this;
  225. $pad->string = $padStr;
  226. return $this->pad($length, $pad, \STR_PAD_BOTH);
  227. }
  228. public function padEnd(int $length, string $padStr = ' '): parent
  229. {
  230. if ('' === $padStr || !preg_match('//u', $padStr)) {
  231. throw new InvalidArgumentException('Invalid UTF-8 string.');
  232. }
  233. $pad = clone $this;
  234. $pad->string = $padStr;
  235. return $this->pad($length, $pad, \STR_PAD_RIGHT);
  236. }
  237. public function padStart(int $length, string $padStr = ' '): parent
  238. {
  239. if ('' === $padStr || !preg_match('//u', $padStr)) {
  240. throw new InvalidArgumentException('Invalid UTF-8 string.');
  241. }
  242. $pad = clone $this;
  243. $pad->string = $padStr;
  244. return $this->pad($length, $pad, \STR_PAD_LEFT);
  245. }
  246. public function replaceMatches(string $fromRegexp, $to): parent
  247. {
  248. if ($this->ignoreCase) {
  249. $fromRegexp .= 'i';
  250. }
  251. if (\is_array($to) || $to instanceof \Closure) {
  252. if (!\is_callable($to)) {
  253. throw new \TypeError(sprintf('Argument 2 passed to "%s::replaceMatches()" must be callable, array given.', static::class));
  254. }
  255. $replace = 'preg_replace_callback';
  256. $to = static function (array $m) use ($to): string {
  257. $to = $to($m);
  258. if ('' !== $to && (!\is_string($to) || !preg_match('//u', $to))) {
  259. throw new InvalidArgumentException('Replace callback must return a valid UTF-8 string.');
  260. }
  261. return $to;
  262. };
  263. } elseif ('' !== $to && !preg_match('//u', $to)) {
  264. throw new InvalidArgumentException('Invalid UTF-8 string.');
  265. } else {
  266. $replace = 'preg_replace';
  267. }
  268. set_error_handler(static function ($t, $m) { throw new InvalidArgumentException($m); });
  269. try {
  270. if (null === $string = $replace($fromRegexp.'u', $to, $this->string)) {
  271. $lastError = preg_last_error();
  272. foreach (get_defined_constants(true)['pcre'] as $k => $v) {
  273. if ($lastError === $v && '_ERROR' === substr($k, -6)) {
  274. throw new RuntimeException('Matching failed with '.$k.'.');
  275. }
  276. }
  277. throw new RuntimeException('Matching failed with unknown error code.');
  278. }
  279. } finally {
  280. restore_error_handler();
  281. }
  282. $str = clone $this;
  283. $str->string = $string;
  284. return $str;
  285. }
  286. public function reverse(): parent
  287. {
  288. $str = clone $this;
  289. $str->string = implode('', array_reverse(preg_split('/(\X)/u', $str->string, -1, \PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY)));
  290. return $str;
  291. }
  292. public function snake(): parent
  293. {
  294. $str = $this->camel();
  295. $str->string = mb_strtolower(preg_replace(['/(\p{Lu}+)(\p{Lu}\p{Ll})/u', '/([\p{Ll}0-9])(\p{Lu})/u'], '\1_\2', $str->string), 'UTF-8');
  296. return $str;
  297. }
  298. public function title(bool $allWords = false): parent
  299. {
  300. $str = clone $this;
  301. $limit = $allWords ? -1 : 1;
  302. $str->string = preg_replace_callback('/\b./u', static function (array $m): string {
  303. return mb_convert_case($m[0], \MB_CASE_TITLE, 'UTF-8');
  304. }, $str->string, $limit);
  305. return $str;
  306. }
  307. public function trim(string $chars = " \t\n\r\0\x0B\x0C\u{A0}\u{FEFF}"): parent
  308. {
  309. if (" \t\n\r\0\x0B\x0C\u{A0}\u{FEFF}" !== $chars && !preg_match('//u', $chars)) {
  310. throw new InvalidArgumentException('Invalid UTF-8 chars.');
  311. }
  312. $chars = preg_quote($chars);
  313. $str = clone $this;
  314. $str->string = preg_replace("{^[$chars]++|[$chars]++$}uD", '', $str->string);
  315. return $str;
  316. }
  317. public function trimEnd(string $chars = " \t\n\r\0\x0B\x0C\u{A0}\u{FEFF}"): parent
  318. {
  319. if (" \t\n\r\0\x0B\x0C\u{A0}\u{FEFF}" !== $chars && !preg_match('//u', $chars)) {
  320. throw new InvalidArgumentException('Invalid UTF-8 chars.');
  321. }
  322. $chars = preg_quote($chars);
  323. $str = clone $this;
  324. $str->string = preg_replace("{[$chars]++$}uD", '', $str->string);
  325. return $str;
  326. }
  327. public function trimPrefix($prefix): parent
  328. {
  329. if (!$this->ignoreCase) {
  330. return parent::trimPrefix($prefix);
  331. }
  332. $str = clone $this;
  333. if ($prefix instanceof \Traversable) {
  334. $prefix = iterator_to_array($prefix, false);
  335. } elseif ($prefix instanceof parent) {
  336. $prefix = $prefix->string;
  337. }
  338. $prefix = implode('|', array_map('preg_quote', (array) $prefix));
  339. $str->string = preg_replace("{^(?:$prefix)}iuD", '', $this->string);
  340. return $str;
  341. }
  342. public function trimStart(string $chars = " \t\n\r\0\x0B\x0C\u{A0}\u{FEFF}"): parent
  343. {
  344. if (" \t\n\r\0\x0B\x0C\u{A0}\u{FEFF}" !== $chars && !preg_match('//u', $chars)) {
  345. throw new InvalidArgumentException('Invalid UTF-8 chars.');
  346. }
  347. $chars = preg_quote($chars);
  348. $str = clone $this;
  349. $str->string = preg_replace("{^[$chars]++}uD", '', $str->string);
  350. return $str;
  351. }
  352. public function trimSuffix($suffix): parent
  353. {
  354. if (!$this->ignoreCase) {
  355. return parent::trimSuffix($suffix);
  356. }
  357. $str = clone $this;
  358. if ($suffix instanceof \Traversable) {
  359. $suffix = iterator_to_array($suffix, false);
  360. } elseif ($suffix instanceof parent) {
  361. $suffix = $suffix->string;
  362. }
  363. $suffix = implode('|', array_map('preg_quote', (array) $suffix));
  364. $str->string = preg_replace("{(?:$suffix)$}iuD", '', $this->string);
  365. return $str;
  366. }
  367. public function upper(): parent
  368. {
  369. $str = clone $this;
  370. $str->string = mb_strtoupper($str->string, 'UTF-8');
  371. if (\PHP_VERSION_ID < 70300) {
  372. $str->string = str_replace(self::UPPER_FROM, self::UPPER_TO, $str->string);
  373. }
  374. return $str;
  375. }
  376. public function width(bool $ignoreAnsiDecoration = true): int
  377. {
  378. $width = 0;
  379. $s = str_replace(["\x00", "\x05", "\x07"], '', $this->string);
  380. if (false !== strpos($s, "\r")) {
  381. $s = str_replace(["\r\n", "\r"], "\n", $s);
  382. }
  383. if (!$ignoreAnsiDecoration) {
  384. $s = preg_replace('/[\p{Cc}\x7F]++/u', '', $s);
  385. }
  386. foreach (explode("\n", $s) as $s) {
  387. if ($ignoreAnsiDecoration) {
  388. $s = preg_replace('/(?:\x1B(?:
  389. \[ [\x30-\x3F]*+ [\x20-\x2F]*+ [\x40-\x7E]
  390. | [P\]X^_] .*? \x1B\\\\
  391. | [\x41-\x7E]
  392. )|[\p{Cc}\x7F]++)/xu', '', $s);
  393. }
  394. $lineWidth = $this->wcswidth($s);
  395. if ($lineWidth > $width) {
  396. $width = $lineWidth;
  397. }
  398. }
  399. return $width;
  400. }
  401. /**
  402. * @return static
  403. */
  404. private function pad(int $len, self $pad, int $type): parent
  405. {
  406. $sLen = $this->length();
  407. if ($len <= $sLen) {
  408. return clone $this;
  409. }
  410. $padLen = $pad->length();
  411. $freeLen = $len - $sLen;
  412. $len = $freeLen % $padLen;
  413. switch ($type) {
  414. case \STR_PAD_RIGHT:
  415. return $this->append(str_repeat($pad->string, intdiv($freeLen, $padLen)).($len ? $pad->slice(0, $len) : ''));
  416. case \STR_PAD_LEFT:
  417. return $this->prepend(str_repeat($pad->string, intdiv($freeLen, $padLen)).($len ? $pad->slice(0, $len) : ''));
  418. case \STR_PAD_BOTH:
  419. $freeLen /= 2;
  420. $rightLen = ceil($freeLen);
  421. $len = $rightLen % $padLen;
  422. $str = $this->append(str_repeat($pad->string, intdiv($rightLen, $padLen)).($len ? $pad->slice(0, $len) : ''));
  423. $leftLen = floor($freeLen);
  424. $len = $leftLen % $padLen;
  425. return $str->prepend(str_repeat($pad->string, intdiv($leftLen, $padLen)).($len ? $pad->slice(0, $len) : ''));
  426. default:
  427. throw new InvalidArgumentException('Invalid padding type.');
  428. }
  429. }
  430. /**
  431. * Based on https://github.com/jquast/wcwidth, a Python implementation of https://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c.
  432. */
  433. private function wcswidth(string $string): int
  434. {
  435. $width = 0;
  436. foreach (preg_split('//u', $string, -1, \PREG_SPLIT_NO_EMPTY) as $c) {
  437. $codePoint = mb_ord($c, 'UTF-8');
  438. if (0 === $codePoint // NULL
  439. || 0x034F === $codePoint // COMBINING GRAPHEME JOINER
  440. || (0x200B <= $codePoint && 0x200F >= $codePoint) // ZERO WIDTH SPACE to RIGHT-TO-LEFT MARK
  441. || 0x2028 === $codePoint // LINE SEPARATOR
  442. || 0x2029 === $codePoint // PARAGRAPH SEPARATOR
  443. || (0x202A <= $codePoint && 0x202E >= $codePoint) // LEFT-TO-RIGHT EMBEDDING to RIGHT-TO-LEFT OVERRIDE
  444. || (0x2060 <= $codePoint && 0x2063 >= $codePoint) // WORD JOINER to INVISIBLE SEPARATOR
  445. ) {
  446. continue;
  447. }
  448. // Non printable characters
  449. if (32 > $codePoint // C0 control characters
  450. || (0x07F <= $codePoint && 0x0A0 > $codePoint) // C1 control characters and DEL
  451. ) {
  452. return -1;
  453. }
  454. if (null === self::$tableZero) {
  455. self::$tableZero = require __DIR__.'/Resources/data/wcswidth_table_zero.php';
  456. }
  457. if ($codePoint >= self::$tableZero[0][0] && $codePoint <= self::$tableZero[$ubound = \count(self::$tableZero) - 1][1]) {
  458. $lbound = 0;
  459. while ($ubound >= $lbound) {
  460. $mid = floor(($lbound + $ubound) / 2);
  461. if ($codePoint > self::$tableZero[$mid][1]) {
  462. $lbound = $mid + 1;
  463. } elseif ($codePoint < self::$tableZero[$mid][0]) {
  464. $ubound = $mid - 1;
  465. } else {
  466. continue 2;
  467. }
  468. }
  469. }
  470. if (null === self::$tableWide) {
  471. self::$tableWide = require __DIR__.'/Resources/data/wcswidth_table_wide.php';
  472. }
  473. if ($codePoint >= self::$tableWide[0][0] && $codePoint <= self::$tableWide[$ubound = \count(self::$tableWide) - 1][1]) {
  474. $lbound = 0;
  475. while ($ubound >= $lbound) {
  476. $mid = floor(($lbound + $ubound) / 2);
  477. if ($codePoint > self::$tableWide[$mid][1]) {
  478. $lbound = $mid + 1;
  479. } elseif ($codePoint < self::$tableWide[$mid][0]) {
  480. $ubound = $mid - 1;
  481. } else {
  482. $width += 2;
  483. continue 2;
  484. }
  485. }
  486. }
  487. ++$width;
  488. }
  489. return $width;
  490. }
  491. }