← All changes
|
vendor_prefixed/twig/symfony/polyfill-mbstring/Mbstring.php
+89
-39
4.1.0-dev3
→
4.3.1
View file →
| @@ -75,8 +75,9 @@ | ||
| 75 | 75 | private const SIMPLE_CASE_FOLD = [['µ', 'ſ', "ͅ", 'ς', "ϐ", "ϑ", "ϕ", "ϖ", "ϰ", "ϱ", "ϵ", "ẛ", "ι"], ['μ', 's', 'ι', 'σ', 'β', 'θ', 'φ', 'π', 'κ', 'ρ', 'ε', "ṡ", 'ι']]; |
| 76 | 76 | private static $encodingList = ['ASCII', 'UTF-8']; |
| 77 | 77 | private static $language = 'neutral'; |
| 78 | 78 | private static $internalEncoding = 'UTF-8'; |
| 79 | + private static $iconvSupportsIgnore; | |
| 79 | 80 | public static function mb_convert_encoding($s, $toEncoding, $fromEncoding = null) |
| 80 | 81 | { |
| 81 | 82 | if (\is_array($s)) { |
| 82 | 83 | $r = []; |
| @@ -102,17 +103,43 @@ | ||
| 102 | 103 | if ('HTML-ENTITIES' === $fromEncoding || 'HTML' === $fromEncoding) { |
| 103 | 104 | $fromEncoding = 'Windows-1252'; |
| 104 | 105 | } |
| 105 | 106 | if ('UTF-8' !== $fromEncoding) { |
| 106 | - $s = \iconv($fromEncoding, 'UTF-8//IGNORE', $s); | |
| 107 | + $s = self::iconv($fromEncoding, 'UTF-8', $s); | |
| 107 | 108 | } |
| 108 | 109 | return \preg_replace_callback('/[\\x80-\\xFF]+/', [__CLASS__, 'html_encoding_callback'], $s); |
| 109 | 110 | } |
| 110 | 111 | if ('HTML-ENTITIES' === $fromEncoding) { |
| 111 | - $s = \html_entity_decode($s, \ENT_COMPAT, 'UTF-8'); | |
| 112 | + $decodeControlChars = static function ($m) { | |
| 113 | + $code = '' !== ($m[2] ?? '') ? \hexdec($m[2]) : (int) $m[1]; | |
| 114 | + if ($code < 32 || 127 === $code) { | |
| 115 | + return \chr($code); | |
| 116 | + } | |
| 117 | + if (128 <= $code && $code <= 159) { | |
| 118 | + return "\xc2" . \chr(0x80 | $code & 0x3f); | |
| 119 | + } | |
| 120 | + return $m[0]; | |
| 121 | + }; | |
| 122 | + if (\PHP_VERSION_ID >= 70400) { | |
| 123 | + $s = \html_entity_decode($s, \ENT_QUOTES, 'UTF-8'); | |
| 124 | + // html_entity_decode() leaves numeric entities for C0/C1 control | |
| 125 | + // characters as-is (HTML spec), but mb_convert_encoding() decodes | |
| 126 | + // them. Catch what html_entity_decode() missed. | |
| 127 | + if (\false !== \strpos($s, '&#')) { | |
| 128 | + $s = \preg_replace_callback('/&#(?:0*([0-9]++)|[xX]0*([0-9a-fA-F]++));/', $decodeControlChars, $s); | |
| 129 | + } | |
| 130 | + } else { | |
| 131 | + // PHP < 7.4: html_entity_decode() truncates strings at NUL bytes, | |
| 132 | + // so decode the control character entities first then call | |
| 133 | + // html_entity_decode() on each NUL-delimited chunk independently. | |
| 134 | + $s = \preg_replace_callback('/&#(?:0*([0-9]++)|[xX]0*([0-9a-fA-F]++));/', $decodeControlChars, $s); | |
| 135 | + $s = \implode("\x00", \array_map(static function ($chunk) { | |
| 136 | + return \html_entity_decode($chunk, \ENT_QUOTES, 'UTF-8'); | |
| 137 | + }, \explode("\x00", $s))); | |
| 138 | + } | |
| 112 | 139 | $fromEncoding = 'UTF-8'; |
| 113 | 140 | } |
| 114 | - return \iconv($fromEncoding, $toEncoding . '//IGNORE', $s); | |
| 141 | + return self::iconv($fromEncoding, $toEncoding, $s); | |
| 115 | 142 | } |
| 116 | 143 | public static function mb_convert_variables($toEncoding, $fromEncoding, &...$vars) |
| 117 | 144 | { |
| 118 | 145 | $ok = \true; |
| @@ -152,12 +179,12 @@ | ||
| 152 | 179 | $encoding = self::getEncoding($encoding); |
| 153 | 180 | if ('UTF-8' === $encoding) { |
| 154 | 181 | $encoding = null; |
| 155 | 182 | if (!\preg_match('//u', $s)) { |
| 156 | - $s = @\iconv('UTF-8', 'UTF-8//IGNORE', $s); | |
| 183 | + $s = @self::iconv('UTF-8', 'UTF-8', $s); | |
| 157 | 184 | } |
| 158 | 185 | } else { |
| 159 | - $s = \iconv($encoding, 'UTF-8//IGNORE', $s); | |
| 186 | + $s = self::iconv($encoding, 'UTF-8', $s); | |
| 160 | 187 | } |
| 161 | 188 | $cnt = \floor(\count($convmap) / 4) * 4; |
| 162 | 189 | for ($i = 0; $i < $cnt; $i += 4) { |
| 163 | 190 | // collector_decode_htmlnumericentity ignores $convmap[$i + 3] |
| @@ -175,9 +202,9 @@ | ||
| 175 | 202 | }, $s); |
| 176 | 203 | if (null === $encoding) { |
| 177 | 204 | return $s; |
| 178 | 205 | } |
| 179 | - return \iconv('UTF-8', $encoding . '//IGNORE', $s); | |
| 206 | + return self::iconv('UTF-8', $encoding, $s); | |
| 180 | 207 | } |
| 181 | 208 | public static function mb_encode_numericentity($s, $convmap, $encoding = null, $is_hex = \false) |
| 182 | 209 | { |
| 183 | 210 | if (null !== $s && !\is_scalar($s) && !(\is_object($s) && \method_exists($s, '__toString'))) { |
| @@ -203,12 +230,12 @@ | ||
| 203 | 230 | $encoding = self::getEncoding($encoding); |
| 204 | 231 | if ('UTF-8' === $encoding) { |
| 205 | 232 | $encoding = null; |
| 206 | 233 | if (!\preg_match('//u', $s)) { |
| 207 | - $s = @\iconv('UTF-8', 'UTF-8//IGNORE', $s); | |
| 234 | + $s = @self::iconv('UTF-8', 'UTF-8', $s); | |
| 208 | 235 | } |
| 209 | 236 | } else { |
| 210 | - $s = \iconv($encoding, 'UTF-8//IGNORE', $s); | |
| 237 | + $s = self::iconv($encoding, 'UTF-8', $s); | |
| 211 | 238 | } |
| 212 | 239 | static $ulenMask = ["\xc0" => 2, "\xd0" => 2, "\xe0" => 3, "\xf0" => 4]; |
| 213 | 240 | $cnt = \floor(\count($convmap) / 4) * 4; |
| 214 | 241 | $i = 0; |
| @@ -230,9 +257,9 @@ | ||
| 230 | 257 | } |
| 231 | 258 | if (null === $encoding) { |
| 232 | 259 | return $result; |
| 233 | 260 | } |
| 234 | - return \iconv('UTF-8', $encoding . '//IGNORE', $result); | |
| 261 | + return self::iconv('UTF-8', $encoding, $result); | |
| 235 | 262 | } |
| 236 | 263 | public static function mb_convert_case($s, $mode, $encoding = null) |
| 237 | 264 | { |
| 238 | 265 | $s = (string) $s; |
| @@ -242,12 +269,12 @@ | ||
| 242 | 269 | $encoding = self::getEncoding($encoding); |
| 243 | 270 | if ('UTF-8' === $encoding) { |
| 244 | 271 | $encoding = null; |
| 245 | 272 | if (!\preg_match('//u', $s)) { |
| 246 | - $s = @\iconv('UTF-8', 'UTF-8//IGNORE', $s); | |
| 273 | + $s = @self::iconv('UTF-8', 'UTF-8', $s); | |
| 247 | 274 | } |
| 248 | 275 | } else { |
| 249 | - $s = \iconv($encoding, 'UTF-8//IGNORE', $s); | |
| 276 | + $s = self::iconv($encoding, 'UTF-8', $s); | |
| 250 | 277 | } |
| 251 | 278 | if (\MB_CASE_TITLE == $mode) { |
| 252 | 279 | static $titleRegexp = null; |
| 253 | 280 | if (null === $titleRegexp) { |
| @@ -300,9 +327,9 @@ | ||
| 300 | 327 | } |
| 301 | 328 | if (null === $encoding) { |
| 302 | 329 | return $s; |
| 303 | 330 | } |
| 304 | - return \iconv('UTF-8', $encoding . '//IGNORE', $s); | |
| 331 | + return self::iconv('UTF-8', $encoding, $s); | |
| 305 | 332 | } |
| 306 | 333 | public static function mb_internal_encoding($encoding = null) |
| 307 | 334 | { |
| 308 | 335 | if (null === $encoding) { |
| @@ -428,9 +455,15 @@ | ||
| 428 | 455 | $encoding = self::getEncoding($encoding); |
| 429 | 456 | if ('CP850' === $encoding || 'ASCII' === $encoding) { |
| 430 | 457 | return \strlen($s); |
| 431 | 458 | } |
| 432 | - return @\iconv_strlen($s, $encoding); | |
| 459 | + if (\false !== ($len = @\iconv_strlen($s, $encoding))) { | |
| 460 | + return $len; | |
| 461 | + } | |
| 462 | + if ('UTF-8' !== $encoding) { | |
| 463 | + return $len; | |
| 464 | + } | |
| 465 | + return \preg_match_all('/[\\x00-\\x7F]|[\\xC0-\\xDF][\\x80-\\xBF]?|[\\xE0-\\xEF][\\x80-\\xBF]{0,2}|[\\xF0-\\xF7][\\x80-\\xBF]{0,3}|[\\xF8-\\xFB][\\x80-\\xBF]{0,4}|[\\xFC-\\xFD][\\x80-\\xBF]{0,5}|[\\x80-\\xBF\\xFE\\xFF]/s', $s); | |
| 433 | 466 | } |
| 434 | 467 | public static function mb_strpos($haystack, $needle, $offset = 0, $encoding = null) |
| 435 | 468 | { |
| 436 | 469 | $encoding = self::getEncoding($encoding); |
| @@ -614,9 +647,9 @@ | ||
| 614 | 647 | public static function mb_strwidth($s, $encoding = null) |
| 615 | 648 | { |
| 616 | 649 | $encoding = self::getEncoding($encoding); |
| 617 | 650 | if ('UTF-8' !== $encoding) { |
| 618 | - $s = \iconv($encoding, 'UTF-8//IGNORE', $s); | |
| 651 | + $s = self::iconv($encoding, 'UTF-8', $s); | |
| 619 | 652 | } |
| 620 | 653 | $s = \preg_replace('/[\\x{1100}-\\x{115F}\\x{2329}\\x{232A}\\x{2E80}-\\x{303E}\\x{3040}-\\x{A4CF}\\x{AC00}-\\x{D7A3}\\x{F900}-\\x{FAFF}\\x{FE10}-\\x{FE19}\\x{FE30}-\\x{FE6F}\\x{FF00}-\\x{FF60}\\x{FFE0}-\\x{FFE6}\\x{20000}-\\x{2FFFD}\\x{30000}-\\x{3FFFD}]/u', '', $s, -1, $wide); |
| 621 | 654 | return ($wide << 1) + \iconv_strlen($s, 'UTF-8'); |
| 622 | 655 | } |
| @@ -664,17 +697,20 @@ | ||
| 664 | 697 | } |
| 665 | 698 | return $code; |
| 666 | 699 | } |
| 667 | 700 | /** @return string|false */ |
| 701 | + public static function mb_scrub(?string $string, ?string $encoding = null) : string | |
| 702 | + { | |
| 703 | + if (null === $encoding) { | |
| 704 | + $encoding = self::mb_internal_encoding(); | |
| 705 | + } elseif (!self::assertEncoding($encoding, 'mb_scrub(): Argument #2 ($encoding) must be a valid encoding, "%s" given')) { | |
| 706 | + return \false; | |
| 707 | + } | |
| 708 | + return self::mb_convert_encoding((string) $string, $encoding, $encoding); | |
| 709 | + } | |
| 710 | + /** @return string|false */ | |
| 668 | 711 | public static function mb_str_pad(string $string, int $length, string $pad_string = ' ', int $pad_type = \STR_PAD_RIGHT, ?string $encoding = null) |
| 669 | 712 | { |
| 670 | - if (!\in_array($pad_type, [\STR_PAD_RIGHT, \STR_PAD_LEFT, \STR_PAD_BOTH], \true)) { | |
| 671 | - if (\PHP_VERSION_ID < 80000) { | |
| 672 | - \trigger_error('mb_str_pad(): Argument #4 ($pad_type) must be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH', \E_USER_WARNING); | |
| 673 | - return \false; | |
| 674 | - } | |
| 675 | - throw new \ValueError('mb_str_pad(): Argument #4 ($pad_type) must be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH'); | |
| 676 | - } | |
| 677 | 713 | if (null === $encoding) { |
| 678 | 714 | $encoding = self::mb_internal_encoding(); |
| 679 | 715 | } elseif (!self::assertEncoding($encoding, 'mb_str_pad(): Argument #5 ($encoding) must be a valid encoding, "%s" given')) { |
| 680 | 716 | return \false; |
| @@ -685,8 +721,15 @@ | ||
| 685 | 721 | return \false; |
| 686 | 722 | } |
| 687 | 723 | throw new \ValueError('mb_str_pad(): Argument #3 ($pad_string) must be a non-empty string'); |
| 688 | 724 | } |
| 725 | + if (!\in_array($pad_type, [\STR_PAD_RIGHT, \STR_PAD_LEFT, \STR_PAD_BOTH], \true)) { | |
| 726 | + if (\PHP_VERSION_ID < 80000) { | |
| 727 | + \trigger_error('mb_str_pad(): Argument #4 ($pad_type) must be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH', \E_USER_WARNING); | |
| 728 | + return \false; | |
| 729 | + } | |
| 730 | + throw new \ValueError('mb_str_pad(): Argument #4 ($pad_type) must be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH'); | |
| 731 | + } | |
| 689 | 732 | $paddingRequired = $length - self::mb_strlen($string, $encoding); |
| 690 | 733 | if ($paddingRequired < 1) { |
| 691 | 734 | return $string; |
| 692 | 735 | } |
| @@ -724,8 +767,23 @@ | ||
| 724 | 767 | $firstChar = \mb_substr($string, 0, 1, $encoding); |
| 725 | 768 | $firstChar = \mb_convert_case($firstChar, \MB_CASE_LOWER, $encoding); |
| 726 | 769 | return $firstChar . \mb_substr($string, 1, null, $encoding); |
| 727 | 770 | } |
| 771 | + /** @return string|false */ | |
| 772 | + public static function mb_trim(string $string, ?string $characters = null, ?string $encoding = null) | |
| 773 | + { | |
| 774 | + return self::mb_internal_trim('{^[%s]+|[%1$s]+$}Du', $string, $characters, $encoding, __FUNCTION__); | |
| 775 | + } | |
| 776 | + /** @return string|false */ | |
| 777 | + public static function mb_ltrim(string $string, ?string $characters = null, ?string $encoding = null) | |
| 778 | + { | |
| 779 | + return self::mb_internal_trim('{^[%s]+}Du', $string, $characters, $encoding, __FUNCTION__); | |
| 780 | + } | |
| 781 | + /** @return string|false */ | |
| 782 | + public static function mb_rtrim(string $string, ?string $characters = null, ?string $encoding = null) | |
| 783 | + { | |
| 784 | + return self::mb_internal_trim('{[%s]+$}Du', $string, $characters, $encoding, __FUNCTION__); | |
| 785 | + } | |
| 728 | 786 | private static function getSubpart($pos, $part, $haystack, $encoding) |
| 729 | 787 | { |
| 730 | 788 | if (\false === $pos) { |
| 731 | 789 | return \false; |
| @@ -789,24 +847,16 @@ | ||
| 789 | 847 | return 'UTF-16BE'; |
| 790 | 848 | } |
| 791 | 849 | return $encoding; |
| 792 | 850 | } |
| 793 | - /** @return string|false */ | |
| 794 | - public static function mb_trim(string $string, ?string $characters = null, ?string $encoding = null) | |
| 851 | + private static function iconv($fromEncoding, $toEncoding, $s) | |
| 795 | 852 | { |
| 796 | - return self::mb_internal_trim('{^[%s]+|[%1$s]+$}Du', $string, $characters, $encoding, __FUNCTION__); | |
| 853 | + if (null === self::$iconvSupportsIgnore) { | |
| 854 | + self::$iconvSupportsIgnore = \false !== @\iconv('UTF-8', 'UTF-8//IGNORE', ''); | |
| 855 | + } | |
| 856 | + return self::$iconvSupportsIgnore ? \iconv($fromEncoding, $toEncoding . '//IGNORE', $s) : \iconv($fromEncoding, $toEncoding, $s); | |
| 797 | 857 | } |
| 798 | 858 | /** @return string|false */ |
| 799 | - public static function mb_ltrim(string $string, ?string $characters = null, ?string $encoding = null) | |
| 800 | - { | |
| 801 | - return self::mb_internal_trim('{^[%s]+}Du', $string, $characters, $encoding, __FUNCTION__); | |
| 802 | - } | |
| 803 | - /** @return string|false */ | |
| 804 | - public static function mb_rtrim(string $string, ?string $characters = null, ?string $encoding = null) | |
| 805 | - { | |
| 806 | - return self::mb_internal_trim('{[%s]+$}Du', $string, $characters, $encoding, __FUNCTION__); | |
| 807 | - } | |
| 808 | - /** @return string|false */ | |
| 809 | 859 | private static function mb_internal_trim(string $regex, string $string, ?string $characters, ?string $encoding, string $function) |
| 810 | 860 | { |
| 811 | 861 | if (null === $encoding) { |
| 812 | 862 | $encoding = self::mb_internal_encoding(); |
| @@ -818,17 +868,17 @@ | ||
| 818 | 868 | } |
| 819 | 869 | if ('UTF-8' === $encoding) { |
| 820 | 870 | $encoding = null; |
| 821 | 871 | if (!\preg_match('//u', $string)) { |
| 822 | - $string = @\iconv('UTF-8', 'UTF-8//IGNORE', $string); | |
| 872 | + $string = @self::iconv('UTF-8', 'UTF-8', $string); | |
| 823 | 873 | } |
| 824 | 874 | if (null !== $characters && !\preg_match('//u', $characters)) { |
| 825 | - $characters = @\iconv('UTF-8', 'UTF-8//IGNORE', $characters); | |
| 875 | + $characters = @self::iconv('UTF-8', 'UTF-8', $characters); | |
| 826 | 876 | } |
| 827 | 877 | } else { |
| 828 | - $string = \iconv($encoding, 'UTF-8//IGNORE', $string); | |
| 878 | + $string = self::iconv($encoding, 'UTF-8', $string); | |
| 829 | 879 | if (null !== $characters) { |
| 830 | - $characters = \iconv($encoding, 'UTF-8//IGNORE', $characters); | |
| 880 | + $characters = self::iconv($encoding, 'UTF-8', $characters); | |
| 831 | 881 | } |
| 832 | 882 | } |
| 833 | 883 | if (null === $characters) { |
| 834 | 884 | $characters = "\\0 \f\n\r\t\v "; |
| @@ -838,9 +888,9 @@ | ||
| 838 | 888 | $string = \preg_replace(\sprintf($regex, $characters), '', $string); |
| 839 | 889 | if (null === $encoding) { |
| 840 | 890 | return $string; |
| 841 | 891 | } |
| 842 | - return \iconv('UTF-8', $encoding . '//IGNORE', $string); | |
| 892 | + return self::iconv('UTF-8', $encoding, $string); | |
| 843 | 893 | } |
| 844 | 894 | private static function assertEncoding(string $encoding, string $errorFormat) : bool |
| 845 | 895 | { |
| 846 | 896 | try { |