| @@ -1,975 +1,0 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | - | |
| 4 | -namespace Symfony\Polyfill\Mbstring; | |
| 5 | - | |
| 6 | -final class Mbstring | |
| 7 | -{ | |
| 8 | - public const MB_CASE_FOLD = \PHP_INT_MAX; | |
| 9 | - | |
| 10 | - private const SIMPLE_CASE_FOLD = [ | |
| 11 | - ['µ', 'ſ', "\xCD\x85", 'ς', "\xCF\x90", "\xCF\x91", "\xCF\x95", "\xCF\x96", "\xCF\xB0", "\xCF\xB1", "\xCF\xB5", "\xE1\xBA\x9B", "\xE1\xBE\xBE"], | |
| 12 | - ['μ', 's', 'ι', 'σ', 'β', 'θ', 'φ', 'π', 'κ', 'ρ', 'ε', "\xE1\xB9\xA1", 'ι'], | |
| 13 | - ]; | |
| 14 | - | |
| 15 | - private static $encodingList = ['ASCII', 'UTF-8']; | |
| 16 | - private static $language = 'neutral'; | |
| 17 | - private static $internalEncoding = 'UTF-8'; | |
| 18 | - | |
| 19 | - public static function mb_convert_encoding($s, $toEncoding, $fromEncoding = null) | |
| 20 | - { | |
| 21 | - if (\is_array($s)) { | |
| 22 | - $r = []; | |
| 23 | - foreach ($s as $str) { | |
| 24 | - $r[] = self::mb_convert_encoding($str, $toEncoding, $fromEncoding); | |
| 25 | - } | |
| 26 | - | |
| 27 | - return $r; | |
| 28 | - } | |
| 29 | - | |
| 30 | - if (\is_array($fromEncoding) || (null !== $fromEncoding && false !== strpos($fromEncoding, ','))) { | |
| 31 | - $fromEncoding = self::mb_detect_encoding($s, $fromEncoding); | |
| 32 | - } else { | |
| 33 | - $fromEncoding = self::getEncoding($fromEncoding); | |
| 34 | - } | |
| 35 | - | |
| 36 | - $toEncoding = self::getEncoding($toEncoding); | |
| 37 | - | |
| 38 | - if ('BASE64' === $fromEncoding) { | |
| 39 | - $s = base64_decode($s); | |
| 40 | - $fromEncoding = $toEncoding; | |
| 41 | - } | |
| 42 | - | |
| 43 | - if ('BASE64' === $toEncoding) { | |
| 44 | - return base64_encode($s); | |
| 45 | - } | |
| 46 | - | |
| 47 | - if ('HTML-ENTITIES' === $toEncoding || 'HTML' === $toEncoding) { | |
| 48 | - if ('HTML-ENTITIES' === $fromEncoding || 'HTML' === $fromEncoding) { | |
| 49 | - $fromEncoding = 'Windows-1252'; | |
| 50 | - } | |
| 51 | - if ('UTF-8' !== $fromEncoding) { | |
| 52 | - $s = iconv($fromEncoding, 'UTF-8//IGNORE', $s); | |
| 53 | - } | |
| 54 | - | |
| 55 | - return preg_replace_callback('/[\x80-\xFF]+/', [__CLASS__, 'html_encoding_callback'], $s); | |
| 56 | - } | |
| 57 | - | |
| 58 | - if ('HTML-ENTITIES' === $fromEncoding) { | |
| 59 | - $s = html_entity_decode($s, \ENT_COMPAT, 'UTF-8'); | |
| 60 | - $fromEncoding = 'UTF-8'; | |
| 61 | - } | |
| 62 | - | |
| 63 | - return iconv($fromEncoding, $toEncoding.'//IGNORE', $s); | |
| 64 | - } | |
| 65 | - | |
| 66 | - public static function mb_convert_variables($toEncoding, $fromEncoding, &...$vars) | |
| 67 | - { | |
| 68 | - $ok = true; | |
| 69 | - array_walk_recursive($vars, function (&$v) use (&$ok, $toEncoding, $fromEncoding) { | |
| 70 | - if (false === $v = self::mb_convert_encoding($v, $toEncoding, $fromEncoding)) { | |
| 71 | - $ok = false; | |
| 72 | - } | |
| 73 | - }); | |
| 74 | - | |
| 75 | - return $ok ? $fromEncoding : false; | |
| 76 | - } | |
| 77 | - | |
| 78 | - public static function mb_decode_mimeheader($s) | |
| 79 | - { | |
| 80 | - return iconv_mime_decode($s, 2, self::$internalEncoding); | |
| 81 | - } | |
| 82 | - | |
| 83 | - public static function mb_encode_mimeheader($s, $charset = null, $transferEncoding = null, $linefeed = null, $indent = null) | |
| 84 | - { | |
| 85 | - trigger_error('mb_encode_mimeheader() is bugged. Please use iconv_mime_encode() instead', \E_USER_WARNING); | |
| 86 | - } | |
| 87 | - | |
| 88 | - public static function mb_decode_numericentity($s, $convmap, $encoding = null) | |
| 89 | - { | |
| 90 | - if (null !== $s && !\is_scalar($s) && !(\is_object($s) && method_exists($s, '__toString'))) { | |
| 91 | - trigger_error('mb_decode_numericentity() expects parameter 1 to be string, '.\gettype($s).' given', \E_USER_WARNING); | |
| 92 | - | |
| 93 | - return null; | |
| 94 | - } | |
| 95 | - | |
| 96 | - if (!\is_array($convmap) || (80000 > \PHP_VERSION_ID && !$convmap)) { | |
| 97 | - return false; | |
| 98 | - } | |
| 99 | - | |
| 100 | - if (null !== $encoding && !\is_scalar($encoding)) { | |
| 101 | - trigger_error('mb_decode_numericentity() expects parameter 3 to be string, '.\gettype($s).' given', \E_USER_WARNING); | |
| 102 | - | |
| 103 | - return ''; // Instead of null (cf. mb_encode_numericentity). | |
| 104 | - } | |
| 105 | - | |
| 106 | - $s = (string) $s; | |
| 107 | - if ('' === $s) { | |
| 108 | - return ''; | |
| 109 | - } | |
| 110 | - | |
| 111 | - $encoding = self::getEncoding($encoding); | |
| 112 | - | |
| 113 | - if ('UTF-8' === $encoding) { | |
| 114 | - $encoding = null; | |
| 115 | - if (!preg_match('//u', $s)) { | |
| 116 | - $s = @iconv('UTF-8', 'UTF-8//IGNORE', $s); | |
| 117 | - } | |
| 118 | - } else { | |
| 119 | - $s = iconv($encoding, 'UTF-8//IGNORE', $s); | |
| 120 | - } | |
| 121 | - | |
| 122 | - $cnt = floor(\count($convmap) / 4) * 4; | |
| 123 | - | |
| 124 | - for ($i = 0; $i < $cnt; $i += 4) { | |
| 125 | - $convmap[$i] += $convmap[$i + 2]; | |
| 126 | - $convmap[$i + 1] += $convmap[$i + 2]; | |
| 127 | - } | |
| 128 | - | |
| 129 | - $s = preg_replace_callback('/&#(?:0*([0-9]+)|x0*([0-9a-fA-F]+))(?!&);?/', function (array $m) use ($cnt, $convmap) { | |
| 130 | - $c = isset($m[2]) ? (int) hexdec($m[2]) : $m[1]; | |
| 131 | - for ($i = 0; $i < $cnt; $i += 4) { | |
| 132 | - if ($c >= $convmap[$i] && $c <= $convmap[$i + 1]) { | |
| 133 | - return self::mb_chr($c - $convmap[$i + 2]); | |
| 134 | - } | |
| 135 | - } | |
| 136 | - | |
| 137 | - return $m[0]; | |
| 138 | - }, $s); | |
| 139 | - | |
| 140 | - if (null === $encoding) { | |
| 141 | - return $s; | |
| 142 | - } | |
| 143 | - | |
| 144 | - return iconv('UTF-8', $encoding.'//IGNORE', $s); | |
| 145 | - } | |
| 146 | - | |
| 147 | - public static function mb_encode_numericentity($s, $convmap, $encoding = null, $is_hex = false) | |
| 148 | - { | |
| 149 | - if (null !== $s && !\is_scalar($s) && !(\is_object($s) && method_exists($s, '__toString'))) { | |
| 150 | - trigger_error('mb_encode_numericentity() expects parameter 1 to be string, '.\gettype($s).' given', \E_USER_WARNING); | |
| 151 | - | |
| 152 | - return null; | |
| 153 | - } | |
| 154 | - | |
| 155 | - if (!\is_array($convmap) || (80000 > \PHP_VERSION_ID && !$convmap)) { | |
| 156 | - return false; | |
| 157 | - } | |
| 158 | - | |
| 159 | - if (null !== $encoding && !\is_scalar($encoding)) { | |
| 160 | - trigger_error('mb_encode_numericentity() expects parameter 3 to be string, '.\gettype($s).' given', \E_USER_WARNING); | |
| 161 | - | |
| 162 | - return null; // Instead of '' (cf. mb_decode_numericentity). | |
| 163 | - } | |
| 164 | - | |
| 165 | - if (null !== $is_hex && !\is_scalar($is_hex)) { | |
| 166 | - trigger_error('mb_encode_numericentity() expects parameter 4 to be boolean, '.\gettype($s).' given', \E_USER_WARNING); | |
| 167 | - | |
| 168 | - return null; | |
| 169 | - } | |
| 170 | - | |
| 171 | - $s = (string) $s; | |
| 172 | - if ('' === $s) { | |
| 173 | - return ''; | |
| 174 | - } | |
| 175 | - | |
| 176 | - $encoding = self::getEncoding($encoding); | |
| 177 | - | |
| 178 | - if ('UTF-8' === $encoding) { | |
| 179 | - $encoding = null; | |
| 180 | - if (!preg_match('//u', $s)) { | |
| 181 | - $s = @iconv('UTF-8', 'UTF-8//IGNORE', $s); | |
| 182 | - } | |
| 183 | - } else { | |
| 184 | - $s = iconv($encoding, 'UTF-8//IGNORE', $s); | |
| 185 | - } | |
| 186 | - | |
| 187 | - static $ulenMask = ["\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4]; | |
| 188 | - | |
| 189 | - $cnt = floor(\count($convmap) / 4) * 4; | |
| 190 | - $i = 0; | |
| 191 | - $len = \strlen($s); | |
| 192 | - $result = ''; | |
| 193 | - | |
| 194 | - while ($i < $len) { | |
| 195 | - $ulen = $s[$i] < "\x80" ? 1 : $ulenMask[$s[$i] & "\xF0"]; | |
| 196 | - $uchr = substr($s, $i, $ulen); | |
| 197 | - $i += $ulen; | |
| 198 | - $c = self::mb_ord($uchr); | |
| 199 | - | |
| 200 | - for ($j = 0; $j < $cnt; $j += 4) { | |
| 201 | - if ($c >= $convmap[$j] && $c <= $convmap[$j + 1]) { | |
| 202 | - $cOffset = ($c + $convmap[$j + 2]) & $convmap[$j + 3]; | |
| 203 | - $result .= $is_hex ? sprintf('&#x%X;', $cOffset) : '&#'.$cOffset.';'; | |
| 204 | - continue 2; | |
| 205 | - } | |
| 206 | - } | |
| 207 | - $result .= $uchr; | |
| 208 | - } | |
| 209 | - | |
| 210 | - if (null === $encoding) { | |
| 211 | - return $result; | |
| 212 | - } | |
| 213 | - | |
| 214 | - return iconv('UTF-8', $encoding.'//IGNORE', $result); | |
| 215 | - } | |
| 216 | - | |
| 217 | - public static function mb_convert_case($s, $mode, $encoding = null) | |
| 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 | - if (\MB_CASE_TITLE == $mode) { | |
| 236 | - static $titleRegexp = null; | |
| 237 | - if (null === $titleRegexp) { | |
| 238 | - $titleRegexp = self::getData('titleCaseRegexp'); | |
| 239 | - } | |
| 240 | - $s = preg_replace_callback($titleRegexp, [__CLASS__, 'title_case'], $s); | |
| 241 | - } else { | |
| 242 | - if (\MB_CASE_UPPER == $mode) { | |
| 243 | - static $upper = null; | |
| 244 | - if (null === $upper) { | |
| 245 | - $upper = self::getData('upperCase'); | |
| 246 | - } | |
| 247 | - $map = $upper; | |
| 248 | - } else { | |
| 249 | - if (self::MB_CASE_FOLD === $mode) { | |
| 250 | - static $caseFolding = null; | |
| 251 | - if (null === $caseFolding) { | |
| 252 | - $caseFolding = self::getData('caseFolding'); | |
| 253 | - } | |
| 254 | - $s = strtr($s, $caseFolding); | |
| 255 | - } | |
| 256 | - | |
| 257 | - static $lower = null; | |
| 258 | - if (null === $lower) { | |
| 259 | - $lower = self::getData('lowerCase'); | |
| 260 | - } | |
| 261 | - $map = $lower; | |
| 262 | - } | |
| 263 | - | |
| 264 | - static $ulenMask = ["\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4]; | |
| 265 | - | |
| 266 | - $i = 0; | |
| 267 | - $len = \strlen($s); | |
| 268 | - | |
| 269 | - while ($i < $len) { | |
| 270 | - $ulen = $s[$i] < "\x80" ? 1 : $ulenMask[$s[$i] & "\xF0"]; | |
| 271 | - $uchr = substr($s, $i, $ulen); | |
| 272 | - $i += $ulen; | |
| 273 | - | |
| 274 | - if (isset($map[$uchr])) { | |
| 275 | - $uchr = $map[$uchr]; | |
| 276 | - $nlen = \strlen($uchr); | |
| 277 | - | |
| 278 | - if ($nlen == $ulen) { | |
| 279 | - $nlen = $i; | |
| 280 | - do { | |
| 281 | - $s[--$nlen] = $uchr[--$ulen]; | |
| 282 | - } while ($ulen); | |
| 283 | - } else { | |
| 284 | - $s = substr_replace($s, $uchr, $i - $ulen, $ulen); | |
| 285 | - $len += $nlen - $ulen; | |
| 286 | - $i += $nlen - $ulen; | |
| 287 | - } | |
| 288 | - } | |
| 289 | - } | |
| 290 | - } | |
| 291 | - | |
| 292 | - if (null === $encoding) { | |
| 293 | - return $s; | |
| 294 | - } | |
| 295 | - | |
| 296 | - return iconv('UTF-8', $encoding.'//IGNORE', $s); | |
| 297 | - } | |
| 298 | - | |
| 299 | - public static function mb_internal_encoding($encoding = null) | |
| 300 | - { | |
| 301 | - if (null === $encoding) { | |
| 302 | - return self::$internalEncoding; | |
| 303 | - } | |
| 304 | - | |
| 305 | - $normalizedEncoding = self::getEncoding($encoding); | |
| 306 | - | |
| 307 | - if ('UTF-8' === $normalizedEncoding || false !== @iconv($normalizedEncoding, $normalizedEncoding, ' ')) { | |
| 308 | - self::$internalEncoding = $normalizedEncoding; | |
| 309 | - | |
| 310 | - return true; | |
| 311 | - } | |
| 312 | - | |
| 313 | - if (80000 > \PHP_VERSION_ID) { | |
| 314 | - return false; | |
| 315 | - } | |
| 316 | - | |
| 317 | - throw new \ValueError(sprintf('Argument #1 ($encoding) must be a valid encoding, "%s" given', $encoding)); | |
| 318 | - } | |
| 319 | - | |
| 320 | - public static function mb_language($lang = null) | |
| 321 | - { | |
| 322 | - if (null === $lang) { | |
| 323 | - return self::$language; | |
| 324 | - } | |
| 325 | - | |
| 326 | - switch ($normalizedLang = strtolower($lang)) { | |
| 327 | - case 'uni': | |
| 328 | - case 'neutral': | |
| 329 | - self::$language = $normalizedLang; | |
| 330 | - | |
| 331 | - return true; | |
| 332 | - } | |
| 333 | - | |
| 334 | - if (80000 > \PHP_VERSION_ID) { | |
| 335 | - return false; | |
| 336 | - } | |
| 337 | - | |
| 338 | - throw new \ValueError(sprintf('Argument #1 ($language) must be a valid language, "%s" given', $lang)); | |
| 339 | - } | |
| 340 | - | |
| 341 | - public static function mb_list_encodings() | |
| 342 | - { | |
| 343 | - return ['UTF-8']; | |
| 344 | - } | |
| 345 | - | |
| 346 | - public static function mb_encoding_aliases($encoding) | |
| 347 | - { | |
| 348 | - switch (strtoupper($encoding)) { | |
| 349 | - case 'UTF8': | |
| 350 | - case 'UTF-8': | |
| 351 | - return ['utf8']; | |
| 352 | - } | |
| 353 | - | |
| 354 | - return false; | |
| 355 | - } | |
| 356 | - | |
| 357 | - public static function mb_check_encoding($var = null, $encoding = null) | |
| 358 | - { | |
| 359 | - if (null === $encoding) { | |
| 360 | - if (null === $var) { | |
| 361 | - return false; | |
| 362 | - } | |
| 363 | - $encoding = self::$internalEncoding; | |
| 364 | - } | |
| 365 | - | |
| 366 | - if (!\is_array($var)) { | |
| 367 | - return self::mb_detect_encoding($var, [$encoding]) || false !== @iconv($encoding, $encoding, $var); | |
| 368 | - } | |
| 369 | - | |
| 370 | - foreach ($var as $key => $value) { | |
| 371 | - if (!self::mb_check_encoding($key, $encoding)) { | |
| 372 | - return false; | |
| 373 | - } | |
| 374 | - if (!self::mb_check_encoding($value, $encoding)) { | |
| 375 | - return false; | |
| 376 | - } | |
| 377 | - } | |
| 378 | - | |
| 379 | - return true; | |
| 380 | - } | |
| 381 | - | |
| 382 | - public static function mb_detect_encoding($str, $encodingList = null, $strict = false) | |
| 383 | - { | |
| 384 | - if (null === $encodingList) { | |
| 385 | - $encodingList = self::$encodingList; | |
| 386 | - } else { | |
| 387 | - if (!\is_array($encodingList)) { | |
| 388 | - $encodingList = array_map('trim', explode(',', $encodingList)); | |
| 389 | - } | |
| 390 | - $encodingList = array_map('strtoupper', $encodingList); | |
| 391 | - } | |
| 392 | - | |
| 393 | - foreach ($encodingList as $enc) { | |
| 394 | - switch ($enc) { | |
| 395 | - case 'ASCII': | |
| 396 | - if (!preg_match('/[\x80-\xFF]/', $str)) { | |
| 397 | - return $enc; | |
| 398 | - } | |
| 399 | - break; | |
| 400 | - | |
| 401 | - case 'UTF8': | |
| 402 | - case 'UTF-8': | |
| 403 | - if (preg_match('//u', $str)) { | |
| 404 | - return 'UTF-8'; | |
| 405 | - } | |
| 406 | - break; | |
| 407 | - | |
| 408 | - default: | |
| 409 | - if (0 === strncmp($enc, 'ISO-8859-', 9)) { | |
| 410 | - return $enc; | |
| 411 | - } | |
| 412 | - } | |
| 413 | - } | |
| 414 | - | |
| 415 | - return false; | |
| 416 | - } | |
| 417 | - | |
| 418 | - public static function mb_detect_order($encodingList = null) | |
| 419 | - { | |
| 420 | - if (null === $encodingList) { | |
| 421 | - return self::$encodingList; | |
| 422 | - } | |
| 423 | - | |
| 424 | - if (!\is_array($encodingList)) { | |
| 425 | - $encodingList = array_map('trim', explode(',', $encodingList)); | |
| 426 | - } | |
| 427 | - $encodingList = array_map('strtoupper', $encodingList); | |
| 428 | - | |
| 429 | - foreach ($encodingList as $enc) { | |
| 430 | - switch ($enc) { | |
| 431 | - default: | |
| 432 | - if (strncmp($enc, 'ISO-8859-', 9)) { | |
| 433 | - return false; | |
| 434 | - } | |
| 435 | - case 'ASCII': | |
| 436 | - case 'UTF8': | |
| 437 | - case 'UTF-8': | |
| 438 | - } | |
| 439 | - } | |
| 440 | - | |
| 441 | - self::$encodingList = $encodingList; | |
| 442 | - | |
| 443 | - return true; | |
| 444 | - } | |
| 445 | - | |
| 446 | - public static function mb_strlen($s, $encoding = null) | |
| 447 | - { | |
| 448 | - $encoding = self::getEncoding($encoding); | |
| 449 | - if ('CP850' === $encoding || 'ASCII' === $encoding) { | |
| 450 | - return \strlen($s); | |
| 451 | - } | |
| 452 | - | |
| 453 | - return @iconv_strlen($s, $encoding); | |
| 454 | - } | |
| 455 | - | |
| 456 | - public static function mb_strpos($haystack, $needle, $offset = 0, $encoding = null) | |
| 457 | - { | |
| 458 | - $encoding = self::getEncoding($encoding); | |
| 459 | - if ('CP850' === $encoding || 'ASCII' === $encoding) { | |
| 460 | - return strpos($haystack, $needle, $offset); | |
| 461 | - } | |
| 462 | - | |
| 463 | - $needle = (string) $needle; | |
| 464 | - if ('' === $needle) { | |
| 465 | - if (80000 > \PHP_VERSION_ID) { | |
| 466 | - trigger_error(__METHOD__.': Empty delimiter', \E_USER_WARNING); | |
| 467 | - | |
| 468 | - return false; | |
| 469 | - } | |
| 470 | - | |
| 471 | - return 0; | |
| 472 | - } | |
| 473 | - | |
| 474 | - return iconv_strpos($haystack, $needle, $offset, $encoding); | |
| 475 | - } | |
| 476 | - | |
| 477 | - public static function mb_strrpos($haystack, $needle, $offset = 0, $encoding = null) | |
| 478 | - { | |
| 479 | - $encoding = self::getEncoding($encoding); | |
| 480 | - if ('CP850' === $encoding || 'ASCII' === $encoding) { | |
| 481 | - return strrpos($haystack, $needle, $offset); | |
| 482 | - } | |
| 483 | - | |
| 484 | - if ($offset != (int) $offset) { | |
| 485 | - $offset = 0; | |
| 486 | - } elseif ($offset = (int) $offset) { | |
| 487 | - if ($offset < 0) { | |
| 488 | - if (0 > $offset += self::mb_strlen($needle)) { | |
| 489 | - $haystack = self::mb_substr($haystack, 0, $offset, $encoding); | |
| 490 | - } | |
| 491 | - $offset = 0; | |
| 492 | - } else { | |
| 493 | - $haystack = self::mb_substr($haystack, $offset, 2147483647, $encoding); | |
| 494 | - } | |
| 495 | - } | |
| 496 | - | |
| 497 | - $pos = '' !== $needle || 80000 > \PHP_VERSION_ID | |
| 498 | - ? iconv_strrpos($haystack, $needle, $encoding) | |
| 499 | - : self::mb_strlen($haystack, $encoding); | |
| 500 | - | |
| 501 | - return false !== $pos ? $offset + $pos : false; | |
| 502 | - } | |
| 503 | - | |
| 504 | - public static function mb_str_split($string, $split_length = 1, $encoding = null) | |
| 505 | - { | |
| 506 | - if (null !== $string && !\is_scalar($string) && !(\is_object($string) && method_exists($string, '__toString'))) { | |
| 507 | - trigger_error('mb_str_split() expects parameter 1 to be string, '.\gettype($string).' given', \E_USER_WARNING); | |
| 508 | - | |
| 509 | - return null; | |
| 510 | - } | |
| 511 | - | |
| 512 | - if (1 > $split_length = (int) $split_length) { | |
| 513 | - if (80000 > \PHP_VERSION_ID) { | |
| 514 | - trigger_error('The length of each segment must be greater than zero', \E_USER_WARNING); | |
| 515 | - | |
| 516 | - return false; | |
| 517 | - } | |
| 518 | - | |
| 519 | - throw new \ValueError('Argument #2 ($length) must be greater than 0'); | |
| 520 | - } | |
| 521 | - | |
| 522 | - if (null === $encoding) { | |
| 523 | - $encoding = mb_internal_encoding(); | |
| 524 | - } | |
| 525 | - | |
| 526 | - if ('UTF-8' === $encoding = self::getEncoding($encoding)) { | |
| 527 | - $rx = '/('; | |
| 528 | - while (65535 < $split_length) { | |
| 529 | - $rx .= '.{65535}'; | |
| 530 | - $split_length -= 65535; | |
| 531 | - } | |
| 532 | - $rx .= '.{'.$split_length.'})/us'; | |
| 533 | - | |
| 534 | - return preg_split($rx, $string, -1, \PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY); | |
| 535 | - } | |
| 536 | - | |
| 537 | - $result = []; | |
| 538 | - $length = mb_strlen($string, $encoding); | |
| 539 | - | |
| 540 | - for ($i = 0; $i < $length; $i += $split_length) { | |
| 541 | - $result[] = mb_substr($string, $i, $split_length, $encoding); | |
| 542 | - } | |
| 543 | - | |
| 544 | - return $result; | |
| 545 | - } | |
| 546 | - | |
| 547 | - public static function mb_strtolower($s, $encoding = null) | |
| 548 | - { | |
| 549 | - return self::mb_convert_case($s, \MB_CASE_LOWER, $encoding); | |
| 550 | - } | |
| 551 | - | |
| 552 | - public static function mb_strtoupper($s, $encoding = null) | |
| 553 | - { | |
| 554 | - return self::mb_convert_case($s, \MB_CASE_UPPER, $encoding); | |
| 555 | - } | |
| 556 | - | |
| 557 | - public static function mb_substitute_character($c = null) | |
| 558 | - { | |
| 559 | - if (null === $c) { | |
| 560 | - return 'none'; | |
| 561 | - } | |
| 562 | - if (0 === strcasecmp($c, 'none')) { | |
| 563 | - return true; | |
| 564 | - } | |
| 565 | - if (80000 > \PHP_VERSION_ID) { | |
| 566 | - return false; | |
| 567 | - } | |
| 568 | - if (\is_int($c) || 'long' === $c || 'entity' === $c) { | |
| 569 | - return false; | |
| 570 | - } | |
| 571 | - | |
| 572 | - throw new \ValueError('Argument #1 ($substitute_character) must be "none", "long", "entity" or a valid codepoint'); | |
| 573 | - } | |
| 574 | - | |
| 575 | - public static function mb_substr($s, $start, $length = null, $encoding = null) | |
| 576 | - { | |
| 577 | - $encoding = self::getEncoding($encoding); | |
| 578 | - if ('CP850' === $encoding || 'ASCII' === $encoding) { | |
| 579 | - return (string) substr($s, $start, null === $length ? 2147483647 : $length); | |
| 580 | - } | |
| 581 | - | |
| 582 | - if ($start < 0) { | |
| 583 | - $start = iconv_strlen($s, $encoding) + $start; | |
| 584 | - if ($start < 0) { | |
| 585 | - $start = 0; | |
| 586 | - } | |
| 587 | - } | |
| 588 | - | |
| 589 | - if (null === $length) { | |
| 590 | - $length = 2147483647; | |
| 591 | - } elseif ($length < 0) { | |
| 592 | - $length = iconv_strlen($s, $encoding) + $length - $start; | |
| 593 | - if ($length < 0) { | |
| 594 | - return ''; | |
| 595 | - } | |
| 596 | - } | |
| 597 | - | |
| 598 | - return (string) iconv_substr($s, $start, $length, $encoding); | |
| 599 | - } | |
| 600 | - | |
| 601 | - public static function mb_stripos($haystack, $needle, $offset = 0, $encoding = null) | |
| 602 | - { | |
| 603 | - [$haystack, $needle] = str_replace(self::SIMPLE_CASE_FOLD[0], self::SIMPLE_CASE_FOLD[1], [ | |
| 604 | - self::mb_convert_case($haystack, \MB_CASE_LOWER, $encoding), | |
| 605 | - self::mb_convert_case($needle, \MB_CASE_LOWER, $encoding), | |
| 606 | - ]); | |
| 607 | - | |
| 608 | - return self::mb_strpos($haystack, $needle, $offset, $encoding); | |
| 609 | - } | |
| 610 | - | |
| 611 | - public static function mb_stristr($haystack, $needle, $part = false, $encoding = null) | |
| 612 | - { | |
| 613 | - $pos = self::mb_stripos($haystack, $needle, 0, $encoding); | |
| 614 | - | |
| 615 | - return self::getSubpart($pos, $part, $haystack, $encoding); | |
| 616 | - } | |
| 617 | - | |
| 618 | - public static function mb_strrchr($haystack, $needle, $part = false, $encoding = null) | |
| 619 | - { | |
| 620 | - $encoding = self::getEncoding($encoding); | |
| 621 | - if ('CP850' === $encoding || 'ASCII' === $encoding) { | |
| 622 | - $pos = strrpos($haystack, $needle); | |
| 623 | - } else { | |
| 624 | - $needle = self::mb_substr($needle, 0, 1, $encoding); | |
| 625 | - $pos = iconv_strrpos($haystack, $needle, $encoding); | |
| 626 | - } | |
| 627 | - | |
| 628 | - return self::getSubpart($pos, $part, $haystack, $encoding); | |
| 629 | - } | |
| 630 | - | |
| 631 | - public static function mb_strrichr($haystack, $needle, $part = false, $encoding = null) | |
| 632 | - { | |
| 633 | - $needle = self::mb_substr($needle, 0, 1, $encoding); | |
| 634 | - $pos = self::mb_strripos($haystack, $needle, $encoding); | |
| 635 | - | |
| 636 | - return self::getSubpart($pos, $part, $haystack, $encoding); | |
| 637 | - } | |
| 638 | - | |
| 639 | - public static function mb_strripos($haystack, $needle, $offset = 0, $encoding = null) | |
| 640 | - { | |
| 641 | - $haystack = self::mb_convert_case($haystack, \MB_CASE_LOWER, $encoding); | |
| 642 | - $needle = self::mb_convert_case($needle, \MB_CASE_LOWER, $encoding); | |
| 643 | - | |
| 644 | - $haystack = str_replace(self::SIMPLE_CASE_FOLD[0], self::SIMPLE_CASE_FOLD[1], $haystack); | |
| 645 | - $needle = str_replace(self::SIMPLE_CASE_FOLD[0], self::SIMPLE_CASE_FOLD[1], $needle); | |
| 646 | - | |
| 647 | - return self::mb_strrpos($haystack, $needle, $offset, $encoding); | |
| 648 | - } | |
| 649 | - | |
| 650 | - public static function mb_strstr($haystack, $needle, $part = false, $encoding = null) | |
| 651 | - { | |
| 652 | - $pos = strpos($haystack, $needle); | |
| 653 | - if (false === $pos) { | |
| 654 | - return false; | |
| 655 | - } | |
| 656 | - if ($part) { | |
| 657 | - return substr($haystack, 0, $pos); | |
| 658 | - } | |
| 659 | - | |
| 660 | - return substr($haystack, $pos); | |
| 661 | - } | |
| 662 | - | |
| 663 | - public static function mb_get_info($type = 'all') | |
| 664 | - { | |
| 665 | - $info = [ | |
| 666 | - 'internal_encoding' => self::$internalEncoding, | |
| 667 | - 'http_output' => 'pass', | |
| 668 | - 'http_output_conv_mimetypes' => '^(text/|application/xhtml\+xml)', | |
| 669 | - 'func_overload' => 0, | |
| 670 | - 'func_overload_list' => 'no overload', | |
| 671 | - 'mail_charset' => 'UTF-8', | |
| 672 | - 'mail_header_encoding' => 'BASE64', | |
| 673 | - 'mail_body_encoding' => 'BASE64', | |
| 674 | - 'illegal_chars' => 0, | |
| 675 | - 'encoding_translation' => 'Off', | |
| 676 | - 'language' => self::$language, | |
| 677 | - 'detect_order' => self::$encodingList, | |
| 678 | - 'substitute_character' => 'none', | |
| 679 | - 'strict_detection' => 'Off', | |
| 680 | - ]; | |
| 681 | - | |
| 682 | - if ('all' === $type) { | |
| 683 | - return $info; | |
| 684 | - } | |
| 685 | - if (isset($info[$type])) { | |
| 686 | - return $info[$type]; | |
| 687 | - } | |
| 688 | - | |
| 689 | - return false; | |
| 690 | - } | |
| 691 | - | |
| 692 | - public static function mb_http_input($type = '') | |
| 693 | - { | |
| 694 | - return false; | |
| 695 | - } | |
| 696 | - | |
| 697 | - public static function mb_http_output($encoding = null) | |
| 698 | - { | |
| 699 | - return null !== $encoding ? 'pass' === $encoding : 'pass'; | |
| 700 | - } | |
| 701 | - | |
| 702 | - public static function mb_strwidth($s, $encoding = null) | |
| 703 | - { | |
| 704 | - $encoding = self::getEncoding($encoding); | |
| 705 | - | |
| 706 | - if ('UTF-8' !== $encoding) { | |
| 707 | - $s = iconv($encoding, 'UTF-8//IGNORE', $s); | |
| 708 | - } | |
| 709 | - | |
| 710 | - $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); | |
| 711 | - | |
| 712 | - return ($wide << 1) + iconv_strlen($s, 'UTF-8'); | |
| 713 | - } | |
| 714 | - | |
| 715 | - public static function mb_substr_count($haystack, $needle, $encoding = null) | |
| 716 | - { | |
| 717 | - return substr_count($haystack, $needle); | |
| 718 | - } | |
| 719 | - | |
| 720 | - public static function mb_output_handler($contents, $status) | |
| 721 | - { | |
| 722 | - return $contents; | |
| 723 | - } | |
| 724 | - | |
| 725 | - public static function mb_chr($code, $encoding = null) | |
| 726 | - { | |
| 727 | - if (0x80 > $code %= 0x200000) { | |
| 728 | - $s = \chr($code); | |
| 729 | - } elseif (0x800 > $code) { | |
| 730 | - $s = \chr(0xC0 | $code >> 6).\chr(0x80 | $code & 0x3F); | |
| 731 | - } elseif (0x10000 > $code) { | |
| 732 | - $s = \chr(0xE0 | $code >> 12).\chr(0x80 | $code >> 6 & 0x3F).\chr(0x80 | $code & 0x3F); | |
| 733 | - } else { | |
| 734 | - $s = \chr(0xF0 | $code >> 18).\chr(0x80 | $code >> 12 & 0x3F).\chr(0x80 | $code >> 6 & 0x3F).\chr(0x80 | $code & 0x3F); | |
| 735 | - } | |
| 736 | - | |
| 737 | - if ('UTF-8' !== $encoding = self::getEncoding($encoding)) { | |
| 738 | - $s = mb_convert_encoding($s, $encoding, 'UTF-8'); | |
| 739 | - } | |
| 740 | - | |
| 741 | - return $s; | |
| 742 | - } | |
| 743 | - | |
| 744 | - public static function mb_ord($s, $encoding = null) | |
| 745 | - { | |
| 746 | - if ('UTF-8' !== $encoding = self::getEncoding($encoding)) { | |
| 747 | - $s = mb_convert_encoding($s, 'UTF-8', $encoding); | |
| 748 | - } | |
| 749 | - | |
| 750 | - if (1 === \strlen($s)) { | |
| 751 | - return \ord($s); | |
| 752 | - } | |
| 753 | - | |
| 754 | - $code = ($s = unpack('C*', substr($s, 0, 4))) ? $s[1] : 0; | |
| 755 | - if (0xF0 <= $code) { | |
| 756 | - return (($code - 0xF0) << 18) + (($s[2] - 0x80) << 12) + (($s[3] - 0x80) << 6) + $s[4] - 0x80; | |
| 757 | - } | |
| 758 | - if (0xE0 <= $code) { | |
| 759 | - return (($code - 0xE0) << 12) + (($s[2] - 0x80) << 6) + $s[3] - 0x80; | |
| 760 | - } | |
| 761 | - if (0xC0 <= $code) { | |
| 762 | - return (($code - 0xC0) << 6) + $s[2] - 0x80; | |
| 763 | - } | |
| 764 | - | |
| 765 | - return $code; | |
| 766 | - } | |
| 767 | - | |
| 768 | - public static function mb_str_pad(string $string, int $length, string $pad_string = ' ', int $pad_type = \STR_PAD_RIGHT, ?string $encoding = null): string | |
| 769 | - { | |
| 770 | - if (!\in_array($pad_type, [\STR_PAD_RIGHT, \STR_PAD_LEFT, \STR_PAD_BOTH], true)) { | |
| 771 | - throw new \ValueError('mb_str_pad(): Argument #4 ($pad_type) must be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH'); | |
| 772 | - } | |
| 773 | - | |
| 774 | - if (null === $encoding) { | |
| 775 | - $encoding = self::mb_internal_encoding(); | |
| 776 | - } else { | |
| 777 | - self::assertEncoding($encoding, 'mb_str_pad(): Argument #5 ($encoding) must be a valid encoding, "%s" given'); | |
| 778 | - } | |
| 779 | - | |
| 780 | - if (self::mb_strlen($pad_string, $encoding) <= 0) { | |
| 781 | - throw new \ValueError('mb_str_pad(): Argument #3 ($pad_string) must be a non-empty string'); | |
| 782 | - } | |
| 783 | - | |
| 784 | - $paddingRequired = $length - self::mb_strlen($string, $encoding); | |
| 785 | - | |
| 786 | - if ($paddingRequired < 1) { | |
| 787 | - return $string; | |
| 788 | - } | |
| 789 | - | |
| 790 | - switch ($pad_type) { | |
| 791 | - case \STR_PAD_LEFT: | |
| 792 | - return self::mb_substr(str_repeat($pad_string, $paddingRequired), 0, $paddingRequired, $encoding).$string; | |
| 793 | - case \STR_PAD_RIGHT: | |
| 794 | - return $string.self::mb_substr(str_repeat($pad_string, $paddingRequired), 0, $paddingRequired, $encoding); | |
| 795 | - default: | |
| 796 | - $leftPaddingLength = floor($paddingRequired / 2); | |
| 797 | - $rightPaddingLength = $paddingRequired - $leftPaddingLength; | |
| 798 | - | |
| 799 | - return self::mb_substr(str_repeat($pad_string, $leftPaddingLength), 0, $leftPaddingLength, $encoding).$string.self::mb_substr(str_repeat($pad_string, $rightPaddingLength), 0, $rightPaddingLength, $encoding); | |
| 800 | - } | |
| 801 | - } | |
| 802 | - | |
| 803 | - public static function mb_ucfirst(string $string, ?string $encoding = null): string | |
| 804 | - { | |
| 805 | - if (null === $encoding) { | |
| 806 | - $encoding = self::mb_internal_encoding(); | |
| 807 | - } else { | |
| 808 | - self::assertEncoding($encoding, 'mb_ucfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given'); | |
| 809 | - } | |
| 810 | - | |
| 811 | - $firstChar = mb_substr($string, 0, 1, $encoding); | |
| 812 | - $firstChar = mb_convert_case($firstChar, \MB_CASE_TITLE, $encoding); | |
| 813 | - | |
| 814 | - return $firstChar.mb_substr($string, 1, null, $encoding); | |
| 815 | - } | |
| 816 | - | |
| 817 | - public static function mb_lcfirst(string $string, ?string $encoding = null): string | |
| 818 | - { | |
| 819 | - if (null === $encoding) { | |
| 820 | - $encoding = self::mb_internal_encoding(); | |
| 821 | - } else { | |
| 822 | - self::assertEncoding($encoding, 'mb_lcfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given'); | |
| 823 | - } | |
| 824 | - | |
| 825 | - $firstChar = mb_substr($string, 0, 1, $encoding); | |
| 826 | - $firstChar = mb_convert_case($firstChar, \MB_CASE_LOWER, $encoding); | |
| 827 | - | |
| 828 | - return $firstChar.mb_substr($string, 1, null, $encoding); | |
| 829 | - } | |
| 830 | - | |
| 831 | - private static function getSubpart($pos, $part, $haystack, $encoding) | |
| 832 | - { | |
| 833 | - if (false === $pos) { | |
| 834 | - return false; | |
| 835 | - } | |
| 836 | - if ($part) { | |
| 837 | - return self::mb_substr($haystack, 0, $pos, $encoding); | |
| 838 | - } | |
| 839 | - | |
| 840 | - return self::mb_substr($haystack, $pos, null, $encoding); | |
| 841 | - } | |
| 842 | - | |
| 843 | - private static function html_encoding_callback(array $m) | |
| 844 | - { | |
| 845 | - $i = 1; | |
| 846 | - $entities = ''; | |
| 847 | - $m = unpack('C*', htmlentities($m[0], \ENT_COMPAT, 'UTF-8')); | |
| 848 | - | |
| 849 | - while (isset($m[$i])) { | |
| 850 | - if (0x80 > $m[$i]) { | |
| 851 | - $entities .= \chr($m[$i++]); | |
| 852 | - continue; | |
| 853 | - } | |
| 854 | - if (0xF0 <= $m[$i]) { | |
| 855 | - $c = (($m[$i++] - 0xF0) << 18) + (($m[$i++] - 0x80) << 12) + (($m[$i++] - 0x80) << 6) + $m[$i++] - 0x80; | |
| 856 | - } elseif (0xE0 <= $m[$i]) { | |
| 857 | - $c = (($m[$i++] - 0xE0) << 12) + (($m[$i++] - 0x80) << 6) + $m[$i++] - 0x80; | |
| 858 | - } else { | |
| 859 | - $c = (($m[$i++] - 0xC0) << 6) + $m[$i++] - 0x80; | |
| 860 | - } | |
| 861 | - | |
| 862 | - $entities .= '&#'.$c.';'; | |
| 863 | - } | |
| 864 | - | |
| 865 | - return $entities; | |
| 866 | - } | |
| 867 | - | |
| 868 | - private static function title_case(array $s) | |
| 869 | - { | |
| 870 | - return self::mb_convert_case($s[1], \MB_CASE_UPPER, 'UTF-8').self::mb_convert_case($s[2], \MB_CASE_LOWER, 'UTF-8'); | |
| 871 | - } | |
| 872 | - | |
| 873 | - private static function getData($file) | |
| 874 | - { | |
| 875 | - if (file_exists($file = __DIR__.'/Resources/unidata/'.$file.'.php')) { | |
| 876 | - return require $file; | |
| 877 | - } | |
| 878 | - | |
| 879 | - return false; | |
| 880 | - } | |
| 881 | - | |
| 882 | - private static function getEncoding($encoding) | |
| 883 | - { | |
| 884 | - if (null === $encoding) { | |
| 885 | - return self::$internalEncoding; | |
| 886 | - } | |
| 887 | - | |
| 888 | - if ('UTF-8' === $encoding) { | |
| 889 | - return 'UTF-8'; | |
| 890 | - } | |
| 891 | - | |
| 892 | - $encoding = strtoupper($encoding); | |
| 893 | - | |
| 894 | - if ('8BIT' === $encoding || 'BINARY' === $encoding) { | |
| 895 | - return 'CP850'; | |
| 896 | - } | |
| 897 | - | |
| 898 | - if ('UTF8' === $encoding) { | |
| 899 | - return 'UTF-8'; | |
| 900 | - } | |
| 901 | - | |
| 902 | - return $encoding; | |
| 903 | - } | |
| 904 | - | |
| 905 | - public static function mb_trim(string $string, ?string $characters = null, ?string $encoding = null): string | |
| 906 | - { | |
| 907 | - return self::mb_internal_trim('{^[%s]+|[%1$s]+$}Du', $string, $characters, $encoding, __FUNCTION__); | |
| 908 | - } | |
| 909 | - | |
| 910 | - public static function mb_ltrim(string $string, ?string $characters = null, ?string $encoding = null): string | |
| 911 | - { | |
| 912 | - return self::mb_internal_trim('{^[%s]+}Du', $string, $characters, $encoding, __FUNCTION__); | |
| 913 | - } | |
| 914 | - | |
| 915 | - public static function mb_rtrim(string $string, ?string $characters = null, ?string $encoding = null): string | |
| 916 | - { | |
| 917 | - return self::mb_internal_trim('{[%s]+$}D', $string, $characters, $encoding, __FUNCTION__); | |
| 918 | - } | |
| 919 | - | |
| 920 | - private static function mb_internal_trim(string $regex, string $string, ?string $characters, ?string $encoding, string $function): string | |
| 921 | - { | |
| 922 | - if (null === $encoding) { | |
| 923 | - $encoding = self::mb_internal_encoding(); | |
| 924 | - } else { | |
| 925 | - self::assertEncoding($encoding, $function.'(): Argument #3 ($encoding) must be a valid encoding, "%s" given'); | |
| 926 | - } | |
| 927 | - | |
| 928 | - if ('' === $characters) { | |
| 929 | - return null === $encoding ? $string : self::mb_convert_encoding($string, $encoding); | |
| 930 | - } | |
| 931 | - | |
| 932 | - if ('UTF-8' === $encoding) { | |
| 933 | - $encoding = null; | |
| 934 | - if (!preg_match('//u', $string)) { | |
| 935 | - $string = @iconv('UTF-8', 'UTF-8//IGNORE', $string); | |
| 936 | - } | |
| 937 | - if (null !== $characters && !preg_match('//u', $characters)) { | |
| 938 | - $characters = @iconv('UTF-8', 'UTF-8//IGNORE', $characters); | |
| 939 | - } | |
| 940 | - } else { | |
| 941 | - $string = iconv($encoding, 'UTF-8//IGNORE', $string); | |
| 942 | - | |
| 943 | - if (null !== $characters) { | |
| 944 | - $characters = iconv($encoding, 'UTF-8//IGNORE', $characters); | |
| 945 | - } | |
| 946 | - } | |
| 947 | - | |
| 948 | - if (null === $characters) { | |
| 949 | - $characters = "\\0 \f\n\r\t\v\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}"; | |
| 950 | - } else { | |
| 951 | - $characters = preg_quote($characters); | |
| 952 | - } | |
| 953 | - | |
| 954 | - $string = preg_replace(sprintf($regex, $characters), '', $string); | |
| 955 | - | |
| 956 | - if (null === $encoding) { | |
| 957 | - return $string; | |
| 958 | - } | |
| 959 | - | |
| 960 | - return iconv('UTF-8', $encoding.'//IGNORE', $string); | |
| 961 | - } | |
| 962 | - | |
| 963 | - private static function assertEncoding(string $encoding, string $errorFormat): void | |
| 964 | - { | |
| 965 | - try { | |
| 966 | - $validEncoding = @self::mb_check_encoding('', $encoding); | |
| 967 | - } catch (\ValueError $e) { | |
| 968 | - throw new \ValueError(sprintf($errorFormat, $encoding)); | |
| 969 | - } | |
| 970 | - | |
| 971 | - if (!$validEncoding) { | |
| 972 | - throw new \ValueError(sprintf($errorFormat, $encoding)); | |
| 973 | - } | |
| 974 | - } | |
| 975 | -} | |