← All changes
|
vendor_prefixed/twig/symfony/polyfill-mbstring/Mbstring.php
+39
-89
4.3.0
→
4.2.1
View file →
| @@ -75,9 +75,8 @@ | ||
| 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; | |
| 80 | 79 | public static function mb_convert_encoding($s, $toEncoding, $fromEncoding = null) |
| 81 | 80 | { |
| 82 | 81 | if (\is_array($s)) { |
| 83 | 82 | $r = []; |
| @@ -103,43 +102,17 @@ | ||
| 103 | 102 | if ('HTML-ENTITIES' === $fromEncoding || 'HTML' === $fromEncoding) { |
| 104 | 103 | $fromEncoding = 'Windows-1252'; |
| 105 | 104 | } |
| 106 | 105 | if ('UTF-8' !== $fromEncoding) { |
| 107 | - $s = self::iconv($fromEncoding, 'UTF-8', $s); | |
| 106 | + $s = \iconv($fromEncoding, 'UTF-8//IGNORE', $s); | |
| 108 | 107 | } |
| 109 | 108 | return \preg_replace_callback('/[\\x80-\\xFF]+/', [__CLASS__, 'html_encoding_callback'], $s); |
| 110 | 109 | } |
| 111 | 110 | if ('HTML-ENTITIES' === $fromEncoding) { |
| 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 | - } | |
| 111 | + $s = \html_entity_decode($s, \ENT_COMPAT, 'UTF-8'); | |
| 139 | 112 | $fromEncoding = 'UTF-8'; |
| 140 | 113 | } |
| 141 | - return self::iconv($fromEncoding, $toEncoding, $s); | |
| 114 | + return \iconv($fromEncoding, $toEncoding . '//IGNORE', $s); | |
| 142 | 115 | } |
| 143 | 116 | public static function mb_convert_variables($toEncoding, $fromEncoding, &...$vars) |
| 144 | 117 | { |
| 145 | 118 | $ok = \true; |
| @@ -179,12 +152,12 @@ | ||
| 179 | 152 | $encoding = self::getEncoding($encoding); |
| 180 | 153 | if ('UTF-8' === $encoding) { |
| 181 | 154 | $encoding = null; |
| 182 | 155 | if (!\preg_match('//u', $s)) { |
| 183 | - $s = @self::iconv('UTF-8', 'UTF-8', $s); | |
| 156 | + $s = @\iconv('UTF-8', 'UTF-8//IGNORE', $s); | |
| 184 | 157 | } |
| 185 | 158 | } else { |
| 186 | - $s = self::iconv($encoding, 'UTF-8', $s); | |
| 159 | + $s = \iconv($encoding, 'UTF-8//IGNORE', $s); | |
| 187 | 160 | } |
| 188 | 161 | $cnt = \floor(\count($convmap) / 4) * 4; |
| 189 | 162 | for ($i = 0; $i < $cnt; $i += 4) { |
| 190 | 163 | // collector_decode_htmlnumericentity ignores $convmap[$i + 3] |
| @@ -202,9 +175,9 @@ | ||
| 202 | 175 | }, $s); |
| 203 | 176 | if (null === $encoding) { |
| 204 | 177 | return $s; |
| 205 | 178 | } |
| 206 | - return self::iconv('UTF-8', $encoding, $s); | |
| 179 | + return \iconv('UTF-8', $encoding . '//IGNORE', $s); | |
| 207 | 180 | } |
| 208 | 181 | public static function mb_encode_numericentity($s, $convmap, $encoding = null, $is_hex = \false) |
| 209 | 182 | { |
| 210 | 183 | if (null !== $s && !\is_scalar($s) && !(\is_object($s) && \method_exists($s, '__toString'))) { |
| @@ -230,12 +203,12 @@ | ||
| 230 | 203 | $encoding = self::getEncoding($encoding); |
| 231 | 204 | if ('UTF-8' === $encoding) { |
| 232 | 205 | $encoding = null; |
| 233 | 206 | if (!\preg_match('//u', $s)) { |
| 234 | - $s = @self::iconv('UTF-8', 'UTF-8', $s); | |
| 207 | + $s = @\iconv('UTF-8', 'UTF-8//IGNORE', $s); | |
| 235 | 208 | } |
| 236 | 209 | } else { |
| 237 | - $s = self::iconv($encoding, 'UTF-8', $s); | |
| 210 | + $s = \iconv($encoding, 'UTF-8//IGNORE', $s); | |
| 238 | 211 | } |
| 239 | 212 | static $ulenMask = ["\xc0" => 2, "\xd0" => 2, "\xe0" => 3, "\xf0" => 4]; |
| 240 | 213 | $cnt = \floor(\count($convmap) / 4) * 4; |
| 241 | 214 | $i = 0; |
| @@ -257,9 +230,9 @@ | ||
| 257 | 230 | } |
| 258 | 231 | if (null === $encoding) { |
| 259 | 232 | return $result; |
| 260 | 233 | } |
| 261 | - return self::iconv('UTF-8', $encoding, $result); | |
| 234 | + return \iconv('UTF-8', $encoding . '//IGNORE', $result); | |
| 262 | 235 | } |
| 263 | 236 | public static function mb_convert_case($s, $mode, $encoding = null) |
| 264 | 237 | { |
| 265 | 238 | $s = (string) $s; |
| @@ -269,12 +242,12 @@ | ||
| 269 | 242 | $encoding = self::getEncoding($encoding); |
| 270 | 243 | if ('UTF-8' === $encoding) { |
| 271 | 244 | $encoding = null; |
| 272 | 245 | if (!\preg_match('//u', $s)) { |
| 273 | - $s = @self::iconv('UTF-8', 'UTF-8', $s); | |
| 246 | + $s = @\iconv('UTF-8', 'UTF-8//IGNORE', $s); | |
| 274 | 247 | } |
| 275 | 248 | } else { |
| 276 | - $s = self::iconv($encoding, 'UTF-8', $s); | |
| 249 | + $s = \iconv($encoding, 'UTF-8//IGNORE', $s); | |
| 277 | 250 | } |
| 278 | 251 | if (\MB_CASE_TITLE == $mode) { |
| 279 | 252 | static $titleRegexp = null; |
| 280 | 253 | if (null === $titleRegexp) { |
| @@ -327,9 +300,9 @@ | ||
| 327 | 300 | } |
| 328 | 301 | if (null === $encoding) { |
| 329 | 302 | return $s; |
| 330 | 303 | } |
| 331 | - return self::iconv('UTF-8', $encoding, $s); | |
| 304 | + return \iconv('UTF-8', $encoding . '//IGNORE', $s); | |
| 332 | 305 | } |
| 333 | 306 | public static function mb_internal_encoding($encoding = null) |
| 334 | 307 | { |
| 335 | 308 | if (null === $encoding) { |
| @@ -455,15 +428,9 @@ | ||
| 455 | 428 | $encoding = self::getEncoding($encoding); |
| 456 | 429 | if ('CP850' === $encoding || 'ASCII' === $encoding) { |
| 457 | 430 | return \strlen($s); |
| 458 | 431 | } |
| 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); | |
| 432 | + return @\iconv_strlen($s, $encoding); | |
| 466 | 433 | } |
| 467 | 434 | public static function mb_strpos($haystack, $needle, $offset = 0, $encoding = null) |
| 468 | 435 | { |
| 469 | 436 | $encoding = self::getEncoding($encoding); |
| @@ -647,9 +614,9 @@ | ||
| 647 | 614 | public static function mb_strwidth($s, $encoding = null) |
| 648 | 615 | { |
| 649 | 616 | $encoding = self::getEncoding($encoding); |
| 650 | 617 | if ('UTF-8' !== $encoding) { |
| 651 | - $s = self::iconv($encoding, 'UTF-8', $s); | |
| 618 | + $s = \iconv($encoding, 'UTF-8//IGNORE', $s); | |
| 652 | 619 | } |
| 653 | 620 | $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); |
| 654 | 621 | return ($wide << 1) + \iconv_strlen($s, 'UTF-8'); |
| 655 | 622 | } |
| @@ -697,20 +664,17 @@ | ||
| 697 | 664 | } |
| 698 | 665 | return $code; |
| 699 | 666 | } |
| 700 | 667 | /** @return string|false */ |
| 701 | - public static function mb_scrub(?string $string, ?string $encoding = null) : string | |
| 668 | + public static function mb_str_pad(string $string, int $length, string $pad_string = ' ', int $pad_type = \STR_PAD_RIGHT, ?string $encoding = null) | |
| 702 | 669 | { |
| 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; | |
| 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'); | |
| 707 | 676 | } |
| 708 | - return self::mb_convert_encoding((string) $string, $encoding, $encoding); | |
| 709 | - } | |
| 710 | - /** @return string|false */ | |
| 711 | - public static function mb_str_pad(string $string, int $length, string $pad_string = ' ', int $pad_type = \STR_PAD_RIGHT, ?string $encoding = null) | |
| 712 | - { | |
| 713 | 677 | if (null === $encoding) { |
| 714 | 678 | $encoding = self::mb_internal_encoding(); |
| 715 | 679 | } elseif (!self::assertEncoding($encoding, 'mb_str_pad(): Argument #5 ($encoding) must be a valid encoding, "%s" given')) { |
| 716 | 680 | return \false; |
| @@ -721,15 +685,8 @@ | ||
| 721 | 685 | return \false; |
| 722 | 686 | } |
| 723 | 687 | throw new \ValueError('mb_str_pad(): Argument #3 ($pad_string) must be a non-empty string'); |
| 724 | 688 | } |
| 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 | - } | |
| 732 | 689 | $paddingRequired = $length - self::mb_strlen($string, $encoding); |
| 733 | 690 | if ($paddingRequired < 1) { |
| 734 | 691 | return $string; |
| 735 | 692 | } |
| @@ -767,23 +724,8 @@ | ||
| 767 | 724 | $firstChar = \mb_substr($string, 0, 1, $encoding); |
| 768 | 725 | $firstChar = \mb_convert_case($firstChar, \MB_CASE_LOWER, $encoding); |
| 769 | 726 | return $firstChar . \mb_substr($string, 1, null, $encoding); |
| 770 | 727 | } |
| 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 | - } | |
| 786 | 728 | private static function getSubpart($pos, $part, $haystack, $encoding) |
| 787 | 729 | { |
| 788 | 730 | if (\false === $pos) { |
| 789 | 731 | return \false; |
| @@ -847,16 +789,24 @@ | ||
| 847 | 789 | return 'UTF-16BE'; |
| 848 | 790 | } |
| 849 | 791 | return $encoding; |
| 850 | 792 | } |
| 851 | - private static function iconv($fromEncoding, $toEncoding, $s) | |
| 793 | + /** @return string|false */ | |
| 794 | + public static function mb_trim(string $string, ?string $characters = null, ?string $encoding = null) | |
| 852 | 795 | { |
| 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); | |
| 796 | + return self::mb_internal_trim('{^[%s]+|[%1$s]+$}Du', $string, $characters, $encoding, __FUNCTION__); | |
| 857 | 797 | } |
| 858 | 798 | /** @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 */ | |
| 859 | 809 | private static function mb_internal_trim(string $regex, string $string, ?string $characters, ?string $encoding, string $function) |
| 860 | 810 | { |
| 861 | 811 | if (null === $encoding) { |
| 862 | 812 | $encoding = self::mb_internal_encoding(); |
| @@ -868,17 +818,17 @@ | ||
| 868 | 818 | } |
| 869 | 819 | if ('UTF-8' === $encoding) { |
| 870 | 820 | $encoding = null; |
| 871 | 821 | if (!\preg_match('//u', $string)) { |
| 872 | - $string = @self::iconv('UTF-8', 'UTF-8', $string); | |
| 822 | + $string = @\iconv('UTF-8', 'UTF-8//IGNORE', $string); | |
| 873 | 823 | } |
| 874 | 824 | if (null !== $characters && !\preg_match('//u', $characters)) { |
| 875 | - $characters = @self::iconv('UTF-8', 'UTF-8', $characters); | |
| 825 | + $characters = @\iconv('UTF-8', 'UTF-8//IGNORE', $characters); | |
| 876 | 826 | } |
| 877 | 827 | } else { |
| 878 | - $string = self::iconv($encoding, 'UTF-8', $string); | |
| 828 | + $string = \iconv($encoding, 'UTF-8//IGNORE', $string); | |
| 879 | 829 | if (null !== $characters) { |
| 880 | - $characters = self::iconv($encoding, 'UTF-8', $characters); | |
| 830 | + $characters = \iconv($encoding, 'UTF-8//IGNORE', $characters); | |
| 881 | 831 | } |
| 882 | 832 | } |
| 883 | 833 | if (null === $characters) { |
| 884 | 834 | $characters = "\\0 \f\n\r\t\v "; |
| @@ -888,9 +838,9 @@ | ||
| 888 | 838 | $string = \preg_replace(\sprintf($regex, $characters), '', $string); |
| 889 | 839 | if (null === $encoding) { |
| 890 | 840 | return $string; |
| 891 | 841 | } |
| 892 | - return self::iconv('UTF-8', $encoding, $string); | |
| 842 | + return \iconv('UTF-8', $encoding . '//IGNORE', $string); | |
| 893 | 843 | } |
| 894 | 844 | private static function assertEncoding(string $encoding, string $errorFormat) : bool |
| 895 | 845 | { |
| 896 | 846 | try { |