Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selective node splitting. #4

Open
wants to merge 1 commit into
base: array-lookup
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions libs/lz-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var LZString = (function () {
streamDataPosition,
streamBitsPerChar,
streamGetCharFromInt,
breakCode=44,
reverseDict = {},
base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+",
Base64CharArray = (base + "/=").split(''),
Expand Down Expand Up @@ -61,7 +62,8 @@ var LZString = (function () {
node = _node(3), // first node will always be initialised like this.
nextNode,
dictSize = 3,
numBitsMask = 0b100;
numBitsMask = 0b100,
forceBreak=true;

if (uncompressed.length) {
// If there is a string, the first charCode is guaranteed to
Expand All @@ -87,8 +89,16 @@ var LZString = (function () {

for (j = 1; j < uncompressed.length; j++) {
c = uncompressed.charCodeAt(j);
// does the new charCode match an existing prefix?
nextNode = node.d[c];

// split Node at defined split symbol (do not split if the node starts with the symbol)
if (c===breakCode && forceBreak) {
nextNode=false;
forceBreak=false;
} else {
// does the new charCode match an existing prefix?
nextNode = node.d[c];
}

if (nextNode) {
// continue with next prefix
node = nextNode;
Expand All @@ -100,6 +110,11 @@ var LZString = (function () {
// Then we set node to the root node matching
// the charCode.

// mark the node if it starts not with the split symbol
if (c!==breakCode) {
forceBreak=true;
}

if (freshNode) {
// Prefix is a freshly added character token,
// which was already written to the bitstream
Expand Down Expand Up @@ -369,7 +384,9 @@ var LZString = (function () {
return _decompress(compressed.length, 16, function (index) { return compressed.charCodeAt(index); });
},

decompressFromArray: _decompressFromArray
decompressFromArray: _decompressFromArray,
setBreakCode: function (a) { breakCode=a; },
getBreakCode: function () { return breakCode; }
};
})();

Expand Down