_polyfills.php 1.7 KB

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