2
0

_polyfills.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * Polyfills for some functions, which are required by Haxe-generated code, but not available in PHP 7.0.
  4. * No Haxe-generated code is available at this point.
  5. * No code should be executed from this file.
  6. * Symbols declarations are the only code allowed here.
  7. */
  8. namespace { //Namespace declaration is required because this file is included under non-root namespace.
  9. /**
  10. * @see http://php.net/manual/en/function.mb-chr.php
  11. */
  12. if(!function_exists('mb_chr')) {
  13. function mb_chr($code, $encoding = null) {
  14. if($encoding && $encoding !== 'UTF-8') {
  15. throw new Exception("$encoding is not supported in mb_chr() polyfill.");
  16. }
  17. if (0x80 > $code %= 0x200000) {
  18. $s = chr($code);
  19. } elseif (0x800 > $code) {
  20. $s = chr(0xC0 | $code >> 6) . chr(0x80 | $code & 0x3F);
  21. } elseif (0x10000 > $code) {
  22. $s = chr(0xE0 | $code >> 12) . chr(0x80 | $code >> 6 & 0x3F) . chr(0x80 | $code & 0x3F);
  23. } else {
  24. $s = chr(0xF0 | $code >> 18) . chr(0x80 | $code >> 12 & 0x3F) . chr(0x80 | $code >> 6 & 0x3F) . chr(0x80 | $code & 0x3F);
  25. }
  26. return $s;
  27. }
  28. }
  29. /**
  30. * @see http://php.net/manual/en/function.mb-ord.php
  31. */
  32. if(!function_exists('mb_ord')) {
  33. function mb_ord($s, $encoding = null) {
  34. if($encoding && $encoding !== 'UTF-8') {
  35. throw new Exception("$encoding is not supported in mb_ord() polyfill.");
  36. }
  37. $code = ($s = unpack('C*', substr($s, 0, 4))) ? $s[1] : 0;
  38. if (0xF0 <= $code) {
  39. return (($code - 0xF0) << 18) + (($s[2] - 0x80) << 12) + (($s[3] - 0x80) << 6) + $s[4] - 0x80;
  40. }
  41. if (0xE0 <= $code) {
  42. return (($code - 0xE0) << 12) + (($s[2] - 0x80) << 6) + $s[3] - 0x80;
  43. }
  44. if (0xC0 <= $code) {
  45. return (($code - 0xC0) << 6) + $s[2] - 0x80;
  46. }
  47. return $code;
  48. }
  49. }
  50. /**
  51. * @see http://php.net/manual/en/function.mb-scrub.php
  52. */
  53. if(!function_exists('mb_scrub')) {
  54. function mb_scrub($s, $encoding = null) {
  55. $encoding = null === $encoding ? mb_internal_encoding() : $encoding;
  56. return mb_convert_encoding($s, $encoding, $encoding);
  57. }
  58. }
  59. /**
  60. * @see https://www.php.net/manual/en/function.str-starts-with.php
  61. */
  62. if (!function_exists('str_starts_with')) {
  63. function str_starts_with($str, $start) {
  64. return (@substr_compare($str, $start, 0, strlen($start))==0);
  65. }
  66. }
  67. }