_polyfills.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. }