LZUtil.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: sics
  5. * Date: 27.02.2016
  6. * Time: 15:54
  7. */
  8. namespace App\Game\Services\LZCompressor;
  9. class LZUtil
  10. {
  11. /**
  12. * @var string
  13. */
  14. public static $keyStrBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  15. public static $keyStrUriSafe = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-$";
  16. private static $baseReverseDic = array();
  17. /**
  18. * @param string $alphabet
  19. * @param integer $character
  20. * @return string
  21. */
  22. public static function getBaseValue($alphabet, $character)
  23. {
  24. if(!array_key_exists($alphabet, self::$baseReverseDic)) {
  25. self::$baseReverseDic[$alphabet] = array();
  26. for($i=0; $i<strlen($alphabet); $i++) {
  27. self::$baseReverseDic[$alphabet][$alphabet[$i]] = $i;
  28. }
  29. }
  30. return self::$baseReverseDic[$alphabet][$character];
  31. }
  32. /**
  33. * @return string
  34. */
  35. public static function fromCharCode()
  36. {
  37. return array_reduce(func_get_args(), function ($a, $b) {
  38. $a .= self::utf8_chr($b);
  39. return $a;
  40. });
  41. }
  42. /**
  43. * Phps chr() equivalent for UTF-8 encoding
  44. *
  45. * @param int|string $u
  46. * @return string
  47. */
  48. public static function utf8_chr($u)
  49. {
  50. return mb_convert_encoding('&#' . intval($u) . ';', 'UTF-8', 'HTML-ENTITIES');
  51. }
  52. /**
  53. * @param string $str
  54. * @param int $num
  55. *
  56. * @return bool|integer
  57. */
  58. public static function charCodeAt($str, $num=0)
  59. {
  60. return self::utf8_ord(self::utf8_charAt($str, $num));
  61. }
  62. /**
  63. * @param string $ch
  64. *
  65. * @return bool|integer
  66. */
  67. public static function utf8_ord($ch)
  68. {
  69. // must remain php's strlen
  70. $len = strlen($ch);
  71. if ($len <= 0) {
  72. return -1;
  73. }
  74. $h = ord($ch[0]);
  75. if ($h <= 0x7F) return $h;
  76. if ($h < 0xC2) return -3;
  77. if ($h <= 0xDF && $len > 1) return ($h & 0x1F) << 6 | (ord($ch[1]) & 0x3F);
  78. if ($h <= 0xEF && $len > 2) return ($h & 0x0F) << 12 | (ord($ch[1]) & 0x3F) << 6 | (ord($ch[2]) & 0x3F);
  79. if ($h <= 0xF4 && $len > 3)
  80. return ($h & 0x0F) << 18 | (ord($ch[1]) & 0x3F) << 12 | (ord($ch[2]) & 0x3F) << 6 | (ord($ch[3]) & 0x3F);
  81. return -2;
  82. }
  83. /**
  84. * @param string $str
  85. * @param integer $num
  86. *
  87. * @return string
  88. */
  89. public static function utf8_charAt($str, $num)
  90. {
  91. return mb_substr($str, $num, 1, 'UTF-8');
  92. }
  93. /**
  94. * @param string $str
  95. * @return integer
  96. */
  97. public static function utf8_strlen($str) {
  98. return mb_strlen($str, 'UTF-8');
  99. }
  100. }