Skip to content

Commit

Permalink
version 1.5 beta
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre Grimaud committed May 10, 2015
1 parent cf58a8e commit c8ac062
Showing 1 changed file with 50 additions and 22 deletions.
72 changes: 50 additions & 22 deletions libs/lz-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// For more information, the home page:
// http://pieroxy.net/blog/pages/lz-string/testing.html
//
// LZ-based compression algorithm, version 1.4.3
// LZ-based compression algorithm, version 1.5 beta
var LZString = (function() {

// private property
Expand All @@ -15,6 +15,34 @@ var keyStrBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567
var keyStrUriSafe = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-$";
var baseReverseDic = {};

var lzs_GetMap = function() {
return new Map();
}

if (typeof Map === "undefined") {
var lzs_NewCompatMap = function() {
this.data = {};
}

lzs_NewCompatMap.prototype.get = function(key) {
if (this.data.hasOwnProperty(key)) return this.data[key];
return null;
}
lzs_NewCompatMap.prototype.set = function(key,value) {
this.data[key] = value;
}
lzs_NewCompatMap.prototype.has = function(key) {
return this.data.hasOwnProperty(key);
}
lzs_NewCompatMap.prototype["delete"] = function(key) {
delete this.data[key];
}

lzs_GetMap = function() {
return new lzs_NewCompatMap();
};
}

function getBaseValue(alphabet, character) {
if (!baseReverseDic[alphabet]) {
baseReverseDic[alphabet] = {};
Expand Down Expand Up @@ -109,8 +137,8 @@ var LZString = {
_compress: function (uncompressed, bitsPerChar, getCharFromInt) {
if (uncompressed == null) return "";
var i, value,
context_dictionary= {},
context_dictionaryToCreate= {},
context_dictionary= lzs_GetMap(),
context_dictionaryToCreate= lzs_GetMap(),
context_c="",
context_wc="",
context_w="",
Expand All @@ -124,16 +152,16 @@ var LZString = {

for (ii = 0; ii < uncompressed.length; ii += 1) {
context_c = uncompressed[ii];
if (!Object.prototype.hasOwnProperty.call(context_dictionary,context_c)) {
context_dictionary[context_c] = context_dictSize++;
context_dictionaryToCreate[context_c] = true;
if (!context_dictionary.has(context_c)) {
context_dictionary.set(context_c,context_dictSize++);
context_dictionaryToCreate.set(context_c, true);
}

context_wc = context_w + context_c;
if (Object.prototype.hasOwnProperty.call(context_dictionary,context_wc)) {
if (context_dictionary.has(context_wc)) {
context_w = context_wc;
} else {
if (Object.prototype.hasOwnProperty.call(context_dictionaryToCreate,context_w)) {
if (context_dictionaryToCreate.has(context_w)) {
if (context_w.charCodeAt(0)<256) {
for (i=0 ; i<context_numBits ; i++) {
context_data_val = (context_data_val << 1);
Expand Down Expand Up @@ -188,9 +216,9 @@ var LZString = {
context_enlargeIn = Math.pow(2, context_numBits);
context_numBits++;
}
delete context_dictionaryToCreate[context_w];
context_dictionaryToCreate["delete"](context_w);
} else {
value = context_dictionary[context_w];
value = context_dictionary.get(context_w);
for (i=0 ; i<context_numBits ; i++) {
context_data_val = (context_data_val << 1) | (value&1);
if (context_data_position == bitsPerChar-1) {
Expand All @@ -211,14 +239,14 @@ var LZString = {
context_numBits++;
}
// Add wc to the dictionary.
context_dictionary[context_wc] = context_dictSize++;
context_dictionary.set(context_wc, context_dictSize++);
context_w = String(context_c);
}
}

// Output the code for w.
if (context_w !== "") {
if (Object.prototype.hasOwnProperty.call(context_dictionaryToCreate,context_w)) {
if (context_dictionaryToCreate.has(context_w)) {
if (context_w.charCodeAt(0)<256) {
for (i=0 ; i<context_numBits ; i++) {
context_data_val = (context_data_val << 1);
Expand Down Expand Up @@ -273,9 +301,9 @@ var LZString = {
context_enlargeIn = Math.pow(2, context_numBits);
context_numBits++;
}
delete context_dictionaryToCreate[context_w];
context_dictionaryToCreate["delete"](context_w);
} else {
value = context_dictionary[context_w];
value = context_dictionary.get(context_w);
for (i=0 ; i<context_numBits ; i++) {
context_data_val = (context_data_val << 1) | (value&1);
if (context_data_position == bitsPerChar-1) {
Expand Down Expand Up @@ -330,7 +358,7 @@ var LZString = {
},

_decompress: function (length, resetValue, getNextValue) {
var dictionary = [],
var dictionary = lzs_GetMap(),
next,
enlargeIn = 4,
dictSize = 4,
Expand All @@ -344,7 +372,7 @@ var LZString = {
data = {val:getNextValue(0), position:resetValue, index:1};

for (i = 0; i < 3; i += 1) {
dictionary[i] = i;
dictionary.set(i, i);
}

bits = 0;
Expand Down Expand Up @@ -397,7 +425,7 @@ var LZString = {
case 2:
return "";
}
dictionary[3] = c;
dictionary.set(3, c);
w = c;
result.push(c);
while (true) {
Expand Down Expand Up @@ -435,7 +463,7 @@ var LZString = {
power <<= 1;
}

dictionary[dictSize++] = f(bits);
dictionary.set(dictSize++, f(bits));
c = dictSize-1;
enlargeIn--;
break;
Expand All @@ -453,7 +481,7 @@ var LZString = {
bits |= (resb>0 ? 1 : 0) * power;
power <<= 1;
}
dictionary[dictSize++] = f(bits);
dictionary.set(dictSize++, f(bits));
c = dictSize-1;
enlargeIn--;
break;
Expand All @@ -466,8 +494,8 @@ var LZString = {
numBits++;
}

if (dictionary[c]) {
entry = dictionary[c];
if (dictionary.get(c)) {
entry = dictionary.get(c);
} else {
if (c === dictSize) {
entry = w + w[0];
Expand All @@ -478,7 +506,7 @@ var LZString = {
result.push(entry);

// Add w+entry[0] to the dictionary.
dictionary[dictSize++] = w + entry[0];
dictionary.set(dictSize++, w + entry[0]);
enlargeIn--;

w = entry;
Expand Down

0 comments on commit c8ac062

Please sign in to comment.