mirror of
https://github.com/icecoder/ICEcoder.git
synced 2026-03-03 07:13:59 +01:00
LZCompress now including UTF16
This commit is contained in:
@@ -6,12 +6,12 @@ class LZContext
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public $dictionary = [];
|
||||
public $dictionary = array();
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public $dictionaryToCreate = [];
|
||||
public $dictionaryToCreate = array();
|
||||
|
||||
/**
|
||||
* @var string
|
||||
|
||||
@@ -25,6 +25,18 @@ class LZString
|
||||
});
|
||||
}
|
||||
|
||||
public static function compressToUTF16($input) {
|
||||
return self::_compress($input, 15, function($a) {
|
||||
return LZUtil16::fromCharCode($a+32);
|
||||
}) . LZUtil16::utf16_chr(32);
|
||||
}
|
||||
|
||||
public static function decompressFromUTF16($input) {
|
||||
return self::_decompress($input, 16384, function($feed, $index) {
|
||||
return LZUtil16::charCodeAt($feed, $index)-32;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $uncompressed
|
||||
* @return string
|
||||
@@ -273,7 +285,7 @@ class LZString
|
||||
}
|
||||
|
||||
$result .= $entry;
|
||||
$dictionary->addEntry($w . $entry{0});
|
||||
$dictionary->addEntry($w . LZUtil::utf8_charAt($entry, 0));
|
||||
$w = $entry;
|
||||
|
||||
$enlargeIn--;
|
||||
|
||||
@@ -16,7 +16,7 @@ class LZUtil
|
||||
*/
|
||||
public static $keyStrBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
|
||||
public static $keyStrUriSafe = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-$";
|
||||
private static $baseReverseDic = [];
|
||||
private static $baseReverseDic = array();
|
||||
|
||||
/**
|
||||
* @param string $alphabet
|
||||
@@ -26,7 +26,7 @@ class LZUtil
|
||||
public static function getBaseValue($alphabet, $character)
|
||||
{
|
||||
if(!array_key_exists($alphabet, self::$baseReverseDic)) {
|
||||
self::$baseReverseDic[$alphabet] = [];
|
||||
self::$baseReverseDic[$alphabet] = array();
|
||||
for($i=0; $i<strlen($alphabet); $i++) {
|
||||
self::$baseReverseDic[$alphabet][$alphabet{$i}] = $i;
|
||||
}
|
||||
@@ -107,6 +107,4 @@ class LZUtil
|
||||
public static function utf8_strlen($str) {
|
||||
return mb_strlen($str, 'UTF-8');
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
90
LZCompressor/LZUtil16.php
Normal file
90
LZCompressor/LZUtil16.php
Normal file
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: sics
|
||||
* Date: 27.02.2016
|
||||
* Time: 15:54
|
||||
*/
|
||||
|
||||
namespace LZCompressor;
|
||||
|
||||
|
||||
class LZUtil16
|
||||
{
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public static function fromCharCode()
|
||||
{
|
||||
return array_reduce(func_get_args(), function ($a, $b) {
|
||||
$a .= self::utf16_chr($b);
|
||||
return $a;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Phps chr() equivalent for UTF-16 encoding
|
||||
*
|
||||
* @param int|string $u
|
||||
* @return string
|
||||
*/
|
||||
public static function utf16_chr($u)
|
||||
{
|
||||
return mb_convert_encoding('&#' . intval($u) . ';', 'UTF-16', 'HTML-ENTITIES');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $str
|
||||
* @param int $num
|
||||
*
|
||||
* @return bool|integer
|
||||
*/
|
||||
public static function charCodeAt($str, $num=0)
|
||||
{
|
||||
return self::utf16_ord(self::utf16_charAt($str, $num));
|
||||
}
|
||||
|
||||
/**
|
||||
* @source http://blog.sarabande.jp/post/35970262740
|
||||
* @param string $ch
|
||||
* @return bool|integer
|
||||
*/
|
||||
function utf16_ord($ch) {
|
||||
$length = strlen($ch);
|
||||
if (2 === $length) {
|
||||
return hexdec(bin2hex($ch));
|
||||
} else if (4 === $length) {
|
||||
$w1 = $ch[0].$ch[1];
|
||||
$w2 = $ch[2].$ch[3];
|
||||
if ($w1 < "\xD8\x00" || "\xDF\xFF" < $w1 || $w2 < "\xDC\x00" || "\xDF\xFF" < $w2) {
|
||||
return false;
|
||||
}
|
||||
$w1 = (hexdec(bin2hex($w1)) & 0x3ff) << 10;
|
||||
$w2 = hexdec(bin2hex($w2)) & 0x3ff;
|
||||
return $w1 + $w2 + 0x10000;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $str
|
||||
* @param integer $num
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function utf16_charAt($str, $num)
|
||||
{
|
||||
return mb_substr($str, $num, 1, 'UTF-16');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $str
|
||||
* @return integer
|
||||
*/
|
||||
public static function utf16_strlen($str)
|
||||
{
|
||||
return mb_strlen($str, 'UTF-16');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,6 +10,7 @@ include(dirname(__FILE__)."/../LZCompressor/LZData.php");
|
||||
include(dirname(__FILE__)."/../LZCompressor/LZReverseDictionary.php");
|
||||
include(dirname(__FILE__)."/../LZCompressor/LZString.php");
|
||||
include(dirname(__FILE__)."/../LZCompressor/LZUtil.php");
|
||||
include(dirname(__FILE__)."/../LZCompressor/LZUtil16.php");
|
||||
use LZCompressor\LZString as LZString;
|
||||
|
||||
// ===============================
|
||||
|
||||
@@ -10,6 +10,7 @@ include(dirname(__FILE__)."/../LZCompressor/LZData.php");
|
||||
include(dirname(__FILE__)."/../LZCompressor/LZReverseDictionary.php");
|
||||
include(dirname(__FILE__)."/../LZCompressor/LZString.php");
|
||||
include(dirname(__FILE__)."/../LZCompressor/LZUtil.php");
|
||||
include(dirname(__FILE__)."/../LZCompressor/LZUtil16.php");
|
||||
use LZCompressor\LZString as LZString;
|
||||
?>
|
||||
<?php if ($_SESSION['githubDiff']) { ?>
|
||||
|
||||
Reference in New Issue
Block a user