comparison vendor/symfony/polyfill-mbstring/Mbstring.php @ 12:7a779792577d

Update Drupal core to v8.4.5 (via Composer)
author Chris Cannam
date Fri, 23 Feb 2018 15:52:07 +0000
parents 4c8ae668cc8c
children c2387f117808
comparison
equal deleted inserted replaced
11:bfffd8d7479a 12:7a779792577d
18 * - mb_chr - Returns a specific character from its Unicode code point 18 * - mb_chr - Returns a specific character from its Unicode code point
19 * - mb_convert_encoding - Convert character encoding 19 * - mb_convert_encoding - Convert character encoding
20 * - mb_convert_variables - Convert character code in variable(s) 20 * - mb_convert_variables - Convert character code in variable(s)
21 * - mb_decode_mimeheader - Decode string in MIME header field 21 * - mb_decode_mimeheader - Decode string in MIME header field
22 * - mb_encode_mimeheader - Encode string for MIME header XXX NATIVE IMPLEMENTATION IS REALLY BUGGED 22 * - mb_encode_mimeheader - Encode string for MIME header XXX NATIVE IMPLEMENTATION IS REALLY BUGGED
23 * - mb_decode_numericentity - Decode HTML numeric string reference to character
24 * - mb_encode_numericentity - Encode character to HTML numeric string reference
23 * - mb_convert_case - Perform case folding on a string 25 * - mb_convert_case - Perform case folding on a string
26 * - mb_detect_encoding - Detect character encoding
24 * - mb_get_info - Get internal settings of mbstring 27 * - mb_get_info - Get internal settings of mbstring
25 * - mb_http_input - Detect HTTP input character encoding 28 * - mb_http_input - Detect HTTP input character encoding
26 * - mb_http_output - Set/Get HTTP output character encoding 29 * - mb_http_output - Set/Get HTTP output character encoding
27 * - mb_internal_encoding - Set/Get internal character encoding 30 * - mb_internal_encoding - Set/Get internal character encoding
28 * - mb_list_encodings - Returns an array of all supported encodings 31 * - mb_list_encodings - Returns an array of all supported encodings
45 * - mb_strwidth - Return width of string 48 * - mb_strwidth - Return width of string
46 * - mb_substr_count - Count the number of substring occurrences 49 * - mb_substr_count - Count the number of substring occurrences
47 * 50 *
48 * Not implemented: 51 * Not implemented:
49 * - 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)
50 * - mb_decode_numericentity - Decode HTML numeric string reference to character
51 * - mb_encode_numericentity - Encode character to HTML numeric string reference
52 * - mb_ereg_* - Regular expression with multibyte support 53 * - mb_ereg_* - Regular expression with multibyte support
53 * - mb_parse_str - Parse GET/POST/COOKIE data and set global variable 54 * - mb_parse_str - Parse GET/POST/COOKIE data and set global variable
54 * - mb_preferred_mime_name - Get MIME charset string 55 * - mb_preferred_mime_name - Get MIME charset string
55 * - mb_regex_encoding - Returns current encoding for multibyte regex as string 56 * - mb_regex_encoding - Returns current encoding for multibyte regex as string
56 * - mb_regex_set_options - Set/Get the default options for mbregex functions 57 * - mb_regex_set_options - Set/Get the default options for mbregex functions
135 public static function mb_encode_mimeheader($s, $charset = null, $transferEncoding = null, $linefeed = null, $indent = null) 136 public static function mb_encode_mimeheader($s, $charset = null, $transferEncoding = null, $linefeed = null, $indent = null)
136 { 137 {
137 trigger_error('mb_encode_mimeheader() is bugged. Please use iconv_mime_encode() instead', E_USER_WARNING); 138 trigger_error('mb_encode_mimeheader() is bugged. Please use iconv_mime_encode() instead', E_USER_WARNING);
138 } 139 }
139 140
140 public static function mb_convert_case($s, $mode, $encoding = null) 141 public static function mb_decode_numericentity($s, $convmap, $encoding = null)
141 { 142 {
142 if ('' === $s .= '') { 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);
145 return null;
146 }
147
148 if (!is_array($convmap) || !$convmap) {
149 return false;
150 }
151
152 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 return ''; // Instead of null (cf. mb_encode_numericentity).
155 }
156
157 $s = (string) $s;
158 if ('' === $s) {
143 return ''; 159 return '';
144 } 160 }
145 161
146 $encoding = self::getEncoding($encoding); 162 $encoding = self::getEncoding($encoding);
147 163
148 if ('UTF-8' === $encoding) { 164 if ('UTF-8' === $encoding) {
149 $encoding = null; 165 $encoding = null;
166 if (!preg_match('//u', $s)) {
167 $s = @iconv('UTF-8', 'UTF-8//IGNORE', $s);
168 }
169 } else {
170 $s = iconv($encoding, 'UTF-8//IGNORE', $s);
171 }
172
173 $cnt = floor(count($convmap) / 4) * 4;
174
175 for ($i = 0; $i < $cnt; $i += 4) {
176 // collector_decode_htmlnumericentity ignores $convmap[$i + 3]
177 $convmap[$i] += $convmap[$i + 2];
178 $convmap[$i + 1] += $convmap[$i + 2];
179 }
180
181 $s = preg_replace_callback('/&#(?:0*([0-9]+)|x0*([0-9a-fA-F]+))(?!&);?/', function (array $m) use ($cnt, $convmap) {
182 $c = isset($m[2]) ? (int) hexdec($m[2]) : $m[1];
183 for ($i = 0; $i < $cnt; $i += 4) {
184 if ($c >= $convmap[$i] && $c <= $convmap[$i + 1]) {
185 return Mbstring::mb_chr($c - $convmap[$i + 2]);
186 }
187 }
188 return $m[0];
189 }, $s);
190
191 if (null === $encoding) {
192 return $s;
193 }
194
195 return iconv('UTF-8', $encoding.'//IGNORE', $s);
196 }
197
198 public static function mb_encode_numericentity($s, $convmap, $encoding = null, $is_hex = false)
199 {
200 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);
202 return null;
203 }
204
205 if (!is_array($convmap) || !$convmap) {
206 return false;
207 }
208
209 if (null !== $encoding && !is_scalar($encoding)) {
210 trigger_error('mb_encode_numericentity() expects parameter 3 to be string, '.gettype($s).' given', E_USER_WARNING);
211 return null; // Instead of '' (cf. mb_decode_numericentity).
212 }
213
214 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);
216 return null;
217 }
218
219 $s = (string) $s;
220 if ('' === $s) {
221 return '';
222 }
223
224 $encoding = self::getEncoding($encoding);
225
226 if ('UTF-8' === $encoding) {
227 $encoding = null;
228 if (!preg_match('//u', $s)) {
229 $s = @iconv('UTF-8', 'UTF-8//IGNORE', $s);
230 }
231 } else {
232 $s = iconv($encoding, 'UTF-8//IGNORE', $s);
233 }
234
235 static $ulenMask = array("\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4);
236
237 $cnt = floor(count($convmap) / 4) * 4;
238 $i = 0;
239 $len = strlen($s);
240 $result = '';
241
242 while ($i < $len) {
243 $ulen = $s[$i] < "\x80" ? 1 : $ulenMask[$s[$i] & "\xF0"];
244 $uchr = substr($s, $i, $ulen);
245 $i += $ulen;
246 $c = self::mb_ord($uchr);
247
248 for ($j = 0; $j < $cnt; $j += 4) {
249 if ($c >= $convmap[$j] && $c <= $convmap[$j + 1]) {
250 $cOffset = ($c + $convmap[$j + 2]) & $convmap[$j + 3];
251 $result .= $is_hex ? sprintf('&#x%X;', $cOffset) : '&#'.$cOffset.';';
252 continue 2;
253 }
254 }
255 $result .= $uchr;
256 }
257
258 if (null === $encoding) {
259 return $result;
260 }
261
262 return iconv('UTF-8', $encoding.'//IGNORE', $result);
263 }
264
265 public static function mb_convert_case($s, $mode, $encoding = null)
266 {
267 $s = (string) $s;
268 if ('' === $s) {
269 return '';
270 }
271
272 $encoding = self::getEncoding($encoding);
273
274 if ('UTF-8' === $encoding) {
275 $encoding = null;
276 if (!preg_match('//u', $s)) {
277 $s = @iconv('UTF-8', 'UTF-8//IGNORE', $s);
278 }
150 } else { 279 } else {
151 $s = iconv($encoding, 'UTF-8//IGNORE', $s); 280 $s = iconv($encoding, 'UTF-8//IGNORE', $s);
152 } 281 }
153 282
154 if (MB_CASE_TITLE == $mode) { 283 if (MB_CASE_TITLE == $mode) {
334 return true; 463 return true;
335 } 464 }
336 465
337 public static function mb_strlen($s, $encoding = null) 466 public static function mb_strlen($s, $encoding = null)
338 { 467 {
339 switch ($encoding = self::getEncoding($encoding)) { 468 $encoding = self::getEncoding($encoding);
340 case 'ASCII': 469 if ('CP850' === $encoding || 'ASCII' === $encoding) {
341 case 'CP850': 470 return strlen($s);
342 return strlen($s);
343 } 471 }
344 472
345 return @iconv_strlen($s, $encoding); 473 return @iconv_strlen($s, $encoding);
346 } 474 }
347 475
348 public static function mb_strpos($haystack, $needle, $offset = 0, $encoding = null) 476 public static function mb_strpos($haystack, $needle, $offset = 0, $encoding = null)
349 { 477 {
350 $encoding = self::getEncoding($encoding); 478 $encoding = self::getEncoding($encoding);
351 479 if ('CP850' === $encoding || 'ASCII' === $encoding) {
352 if ('' === $needle .= '') { 480 return strpos($haystack, $needle, $offset);
481 }
482
483 $needle = (string) $needle;
484 if ('' === $needle) {
353 trigger_error(__METHOD__.': Empty delimiter', E_USER_WARNING); 485 trigger_error(__METHOD__.': Empty delimiter', E_USER_WARNING);
354 486
355 return false; 487 return false;
356 } 488 }
357 489
359 } 491 }
360 492
361 public static function mb_strrpos($haystack, $needle, $offset = 0, $encoding = null) 493 public static function mb_strrpos($haystack, $needle, $offset = 0, $encoding = null)
362 { 494 {
363 $encoding = self::getEncoding($encoding); 495 $encoding = self::getEncoding($encoding);
496 if ('CP850' === $encoding || 'ASCII' === $encoding) {
497 return strrpos($haystack, $needle, $offset);
498 }
364 499
365 if ($offset != (int) $offset) { 500 if ($offset != (int) $offset) {
366 $offset = 0; 501 $offset = 0;
367 } elseif ($offset = (int) $offset) { 502 } elseif ($offset = (int) $offset) {
368 if ($offset < 0) { 503 if ($offset < 0) {
398 } 533 }
399 534
400 public static function mb_substr($s, $start, $length = null, $encoding = null) 535 public static function mb_substr($s, $start, $length = null, $encoding = null)
401 { 536 {
402 $encoding = self::getEncoding($encoding); 537 $encoding = self::getEncoding($encoding);
538 if ('CP850' === $encoding || 'ASCII' === $encoding) {
539 return substr($s, $start, null === $length ? 2147483647 : $length);
540 }
403 541
404 if ($start < 0) { 542 if ($start < 0) {
405 $start = iconv_strlen($s, $encoding) + $start; 543 $start = iconv_strlen($s, $encoding) + $start;
406 if ($start < 0) { 544 if ($start < 0) {
407 $start = 0; 545 $start = 0;
415 if ($length < 0) { 553 if ($length < 0) {
416 return ''; 554 return '';
417 } 555 }
418 } 556 }
419 557
420 return iconv_substr($s, $start, $length, $encoding).''; 558 return (string) iconv_substr($s, $start, $length, $encoding);
421 } 559 }
422 560
423 public static function mb_stripos($haystack, $needle, $offset = 0, $encoding = null) 561 public static function mb_stripos($haystack, $needle, $offset = 0, $encoding = null)
424 { 562 {
425 $haystack = self::mb_convert_case($haystack, self::MB_CASE_FOLD, $encoding); 563 $haystack = self::mb_convert_case($haystack, self::MB_CASE_FOLD, $encoding);
436 } 574 }
437 575
438 public static function mb_strrchr($haystack, $needle, $part = false, $encoding = null) 576 public static function mb_strrchr($haystack, $needle, $part = false, $encoding = null)
439 { 577 {
440 $encoding = self::getEncoding($encoding); 578 $encoding = self::getEncoding($encoding);
579 if ('CP850' === $encoding || 'ASCII' === $encoding) {
580 return strrchr($haystack, $needle, $part);
581 }
441 $needle = self::mb_substr($needle, 0, 1, $encoding); 582 $needle = self::mb_substr($needle, 0, 1, $encoding);
442 $pos = iconv_strrpos($haystack, $needle, $encoding); 583 $pos = iconv_strrpos($haystack, $needle, $encoding);
443 584
444 return self::getSubpart($pos, $part, $haystack, $encoding); 585 return self::getSubpart($pos, $part, $haystack, $encoding);
445 } 586 }
584 } 725 }
585 726
586 return self::mb_substr($haystack, $pos, null, $encoding); 727 return self::mb_substr($haystack, $pos, null, $encoding);
587 } 728 }
588 729
589 private static function html_encoding_callback($m) 730 private static function html_encoding_callback(array $m)
590 { 731 {
591 $i = 1; 732 $i = 1;
592 $entities = ''; 733 $entities = '';
593 $m = unpack('C*', htmlentities($m[0], ENT_COMPAT, 'UTF-8')); 734 $m = unpack('C*', htmlentities($m[0], ENT_COMPAT, 'UTF-8'));
594 735
609 } 750 }
610 751
611 return $entities; 752 return $entities;
612 } 753 }
613 754
614 private static function title_case_lower($s) 755 private static function title_case_lower(array $s)
615 { 756 {
616 return self::mb_convert_case($s[0], MB_CASE_LOWER, 'UTF-8'); 757 return self::mb_convert_case($s[0], MB_CASE_LOWER, 'UTF-8');
617 } 758 }
618 759
619 private static function title_case_upper($s) 760 private static function title_case_upper(array $s)
620 { 761 {
621 return self::mb_convert_case($s[0], MB_CASE_UPPER, 'UTF-8'); 762 return self::mb_convert_case($s[0], MB_CASE_UPPER, 'UTF-8');
622 } 763 }
623 764
624 private static function getData($file) 765 private static function getData($file)