comparison vendor/symfony/polyfill-mbstring/Mbstring.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents c2387f117808
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
42 * - mb_stripos - Finds position of first occurrence of a string within another, case insensitive 42 * - mb_stripos - Finds position of first occurrence of a string within another, case insensitive
43 * - mb_stristr - Finds first occurrence of a string within another, case insensitive 43 * - mb_stristr - Finds first occurrence of a string within another, case insensitive
44 * - mb_strrchr - Finds the last occurrence of a character in a string within another 44 * - mb_strrchr - Finds the last occurrence of a character in a string within another
45 * - mb_strrichr - Finds the last occurrence of a character in a string within another, case insensitive 45 * - mb_strrichr - Finds the last occurrence of a character in a string within another, case insensitive
46 * - mb_strripos - Finds position of last occurrence of a string within another, case insensitive 46 * - mb_strripos - Finds position of last occurrence of a string within another, case insensitive
47 * - mb_strstr - Finds first occurrence of a string within anothers 47 * - mb_strstr - Finds first occurrence of a string within another
48 * - mb_strwidth - Return width of string 48 * - mb_strwidth - Return width of string
49 * - mb_substr_count - Count the number of substring occurrences 49 * - mb_substr_count - Count the number of substring occurrences
50 * 50 *
51 * Not implemented: 51 * Not implemented:
52 * - mb_convert_kana - Convert "kana" one from another ("zen-kaku", "han-kaku" and more) 52 * - mb_convert_kana - Convert "kana" one from another ("zen-kaku", "han-kaku" and more)
70 70
71 private static $encodingList = array('ASCII', 'UTF-8'); 71 private static $encodingList = array('ASCII', 'UTF-8');
72 private static $language = 'neutral'; 72 private static $language = 'neutral';
73 private static $internalEncoding = 'UTF-8'; 73 private static $internalEncoding = 'UTF-8';
74 private static $caseFold = array( 74 private static $caseFold = array(
75 array('µ','ſ',"\xCD\x85",'ς',"\xCF\x90","\xCF\x91","\xCF\x95","\xCF\x96","\xCF\xB0","\xCF\xB1","\xCF\xB5","\xE1\xBA\x9B","\xE1\xBE\xBE"), 75 array('µ', 'ſ', "\xCD\x85", 'ς', "\xCF\x90", "\xCF\x91", "\xCF\x95", "\xCF\x96", "\xCF\xB0", "\xCF\xB1", "\xCF\xB5", "\xE1\xBA\x9B", "\xE1\xBE\xBE"),
76 array('μ','s','ι', 'σ','β', 'θ', 'φ', 'π', 'κ', 'ρ', 'ε', "\xE1\xB9\xA1",'ι'), 76 array('μ', 's', 'ι', 'σ', 'β', 'θ', 'φ', 'π', 'κ', 'ρ', 'ε', "\xE1\xB9\xA1", 'ι'),
77 ); 77 );
78 78
79 public static function mb_convert_encoding($s, $toEncoding, $fromEncoding = null) 79 public static function mb_convert_encoding($s, $toEncoding, $fromEncoding = null)
80 { 80 {
81 if (\is_array($fromEncoding) || false !== strpos($fromEncoding, ',')) { 81 if (\is_array($fromEncoding) || false !== strpos($fromEncoding, ',')) {
139 } 139 }
140 140
141 public static function mb_decode_numericentity($s, $convmap, $encoding = null) 141 public static function mb_decode_numericentity($s, $convmap, $encoding = null)
142 { 142 {
143 if (null !== $s && !\is_scalar($s) && !(\is_object($s) && \method_exists($s, '__toString'))) { 143 if (null !== $s && !\is_scalar($s) && !(\is_object($s) && \method_exists($s, '__toString'))) {
144 trigger_error('mb_decode_numericentity() expects parameter 1 to be string, '.gettype($s).' given', E_USER_WARNING); 144 trigger_error('mb_decode_numericentity() expects parameter 1 to be string, '.\gettype($s).' given', E_USER_WARNING);
145
145 return null; 146 return null;
146 } 147 }
147 148
148 if (!\is_array($convmap) || !$convmap) { 149 if (!\is_array($convmap) || !$convmap) {
149 return false; 150 return false;
150 } 151 }
151 152
152 if (null !== $encoding && !\is_scalar($encoding)) { 153 if (null !== $encoding && !\is_scalar($encoding)) {
153 trigger_error('mb_decode_numericentity() expects parameter 3 to be string, '.gettype($s).' given', E_USER_WARNING); 154 trigger_error('mb_decode_numericentity() expects parameter 3 to be string, '.\gettype($s).' given', E_USER_WARNING);
155
154 return ''; // Instead of null (cf. mb_encode_numericentity). 156 return ''; // Instead of null (cf. mb_encode_numericentity).
155 } 157 }
156 158
157 $s = (string) $s; 159 $s = (string) $s;
158 if ('' === $s) { 160 if ('' === $s) {
183 for ($i = 0; $i < $cnt; $i += 4) { 185 for ($i = 0; $i < $cnt; $i += 4) {
184 if ($c >= $convmap[$i] && $c <= $convmap[$i + 1]) { 186 if ($c >= $convmap[$i] && $c <= $convmap[$i + 1]) {
185 return Mbstring::mb_chr($c - $convmap[$i + 2]); 187 return Mbstring::mb_chr($c - $convmap[$i + 2]);
186 } 188 }
187 } 189 }
190
188 return $m[0]; 191 return $m[0];
189 }, $s); 192 }, $s);
190 193
191 if (null === $encoding) { 194 if (null === $encoding) {
192 return $s; 195 return $s;
196 } 199 }
197 200
198 public static function mb_encode_numericentity($s, $convmap, $encoding = null, $is_hex = false) 201 public static function mb_encode_numericentity($s, $convmap, $encoding = null, $is_hex = false)
199 { 202 {
200 if (null !== $s && !\is_scalar($s) && !(\is_object($s) && \method_exists($s, '__toString'))) { 203 if (null !== $s && !\is_scalar($s) && !(\is_object($s) && \method_exists($s, '__toString'))) {
201 trigger_error('mb_encode_numericentity() expects parameter 1 to be string, '.gettype($s).' given', E_USER_WARNING); 204 trigger_error('mb_encode_numericentity() expects parameter 1 to be string, '.\gettype($s).' given', E_USER_WARNING);
205
202 return null; 206 return null;
203 } 207 }
204 208
205 if (!\is_array($convmap) || !$convmap) { 209 if (!\is_array($convmap) || !$convmap) {
206 return false; 210 return false;
207 } 211 }
208 212
209 if (null !== $encoding && !\is_scalar($encoding)) { 213 if (null !== $encoding && !\is_scalar($encoding)) {
210 trigger_error('mb_encode_numericentity() expects parameter 3 to be string, '.gettype($s).' given', E_USER_WARNING); 214 trigger_error('mb_encode_numericentity() expects parameter 3 to be string, '.\gettype($s).' given', E_USER_WARNING);
215
211 return null; // Instead of '' (cf. mb_decode_numericentity). 216 return null; // Instead of '' (cf. mb_decode_numericentity).
212 } 217 }
213 218
214 if (null !== $is_hex && !\is_scalar($is_hex)) { 219 if (null !== $is_hex && !\is_scalar($is_hex)) {
215 trigger_error('mb_encode_numericentity() expects parameter 4 to be boolean, '.gettype($s).' given', E_USER_WARNING); 220 trigger_error('mb_encode_numericentity() expects parameter 4 to be boolean, '.\gettype($s).' given', E_USER_WARNING);
221
216 return null; 222 return null;
217 } 223 }
218 224
219 $s = (string) $s; 225 $s = (string) $s;
220 if ('' === $s) { 226 if ('' === $s) {
279 } else { 285 } else {
280 $s = iconv($encoding, 'UTF-8//IGNORE', $s); 286 $s = iconv($encoding, 'UTF-8//IGNORE', $s);
281 } 287 }
282 288
283 if (MB_CASE_TITLE == $mode) { 289 if (MB_CASE_TITLE == $mode) {
284 $s = preg_replace_callback('/\b\p{Ll}/u', array(__CLASS__, 'title_case_upper'), $s); 290 static $titleRegexp = null;
285 $s = preg_replace_callback('/\B[\p{Lu}\p{Lt}]+/u', array(__CLASS__, 'title_case_lower'), $s); 291 if (null === $titleRegexp) {
292 $titleRegexp = self::getData('titleCaseRegexp');
293 }
294 $s = preg_replace_callback($titleRegexp, array(__CLASS__, 'title_case'), $s);
286 } else { 295 } else {
287 if (MB_CASE_UPPER == $mode) { 296 if (MB_CASE_UPPER == $mode) {
288 static $upper = null; 297 static $upper = null;
289 if (null === $upper) { 298 if (null === $upper) {
290 $upper = self::getData('upperCase'); 299 $upper = self::getData('upperCase');
322 $s[--$nlen] = $uchr[--$ulen]; 331 $s[--$nlen] = $uchr[--$ulen];
323 } while ($ulen); 332 } while ($ulen);
324 } else { 333 } else {
325 $s = substr_replace($s, $uchr, $i - $ulen, $ulen); 334 $s = substr_replace($s, $uchr, $i - $ulen, $ulen);
326 $len += $nlen - $ulen; 335 $len += $nlen - $ulen;
327 $i += $nlen - $ulen; 336 $i += $nlen - $ulen;
328 } 337 }
329 } 338 }
330 } 339 }
331 } 340 }
332 341
450 switch ($enc) { 459 switch ($enc) {
451 default: 460 default:
452 if (strncmp($enc, 'ISO-8859-', 9)) { 461 if (strncmp($enc, 'ISO-8859-', 9)) {
453 return false; 462 return false;
454 } 463 }
464 // no break
455 case 'ASCII': 465 case 'ASCII':
456 case 'UTF8': 466 case 'UTF8':
457 case 'UTF-8': 467 case 'UTF-8':
458 } 468 }
459 } 469 }
699 { 709 {
700 if ('UTF-8' !== $encoding = self::getEncoding($encoding)) { 710 if ('UTF-8' !== $encoding = self::getEncoding($encoding)) {
701 $s = mb_convert_encoding($s, 'UTF-8', $encoding); 711 $s = mb_convert_encoding($s, 'UTF-8', $encoding);
702 } 712 }
703 713
714 if (1 === \strlen($s)) {
715 return \ord($s);
716 }
717
704 $code = ($s = unpack('C*', substr($s, 0, 4))) ? $s[1] : 0; 718 $code = ($s = unpack('C*', substr($s, 0, 4))) ? $s[1] : 0;
705 if (0xF0 <= $code) { 719 if (0xF0 <= $code) {
706 return (($code - 0xF0) << 18) + (($s[2] - 0x80) << 12) + (($s[3] - 0x80) << 6) + $s[4] - 0x80; 720 return (($code - 0xF0) << 18) + (($s[2] - 0x80) << 12) + (($s[3] - 0x80) << 6) + $s[4] - 0x80;
707 } 721 }
708 if (0xE0 <= $code) { 722 if (0xE0 <= $code) {
750 } 764 }
751 765
752 return $entities; 766 return $entities;
753 } 767 }
754 768
755 private static function title_case_lower(array $s) 769 private static function title_case(array $s)
756 { 770 {
757 return self::mb_convert_case($s[0], MB_CASE_LOWER, 'UTF-8'); 771 return self::mb_convert_case($s[1], MB_CASE_UPPER, 'UTF-8').self::mb_convert_case($s[2], MB_CASE_LOWER, 'UTF-8');
758 }
759
760 private static function title_case_upper(array $s)
761 {
762 return self::mb_convert_case($s[0], MB_CASE_UPPER, 'UTF-8');
763 } 772 }
764 773
765 private static function getData($file) 774 private static function getData($file)
766 { 775 {
767 if (file_exists($file = __DIR__.'/Resources/unidata/'.$file.'.php')) { 776 if (file_exists($file = __DIR__.'/Resources/unidata/'.$file.'.php')) {