Created
June 9, 2011 10:32
-
-
Save christopherdebeer/1016495 to your computer and use it in GitHub Desktop.
LZW compression and decompression by @_sebastienp (not me)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright (c) 2011 Sebastien P. | |
http://twitter.com/_sebastienp | |
MIT licensed. | |
--- | |
LZW compression/decompression attempt for 140byt.es. | |
--- | |
See http://rosettacode.org/wiki/LZW_compression#JavaScript ! | |
*/ | |
window.LZW = { | |
// 163 bytes | |
compress: function ( | |
a, // String to compress | |
b, // Placeholder for dictionary size | |
c, // Placeholder for dictionary | |
d, // Placeholder for iterator | |
e, // Placeholder for w | |
f, // Placeholder for result | |
g, // Placeholder for c | |
h // Placeholder for wc | |
) { | |
for (b = d = 256, c = {}; d--;) | |
c[String.fromCharCode(d)] = d; | |
for (f = []; g = a[++d];) | |
//for (e = "", f = []; g = a[++d];) // Using this one is 1 byte longer ... | |
e = c[h = e ? e + g : g] ? h : (f.push(c[e]), c[h] = b++, g); | |
//e = c[h = e + g] ? h : (f.push(c[e]), c[h] = b++, g); // ... but maybe faster ? | |
e && f.push(c[e]); | |
return f | |
}, | |
// 158 bytes | |
decompress: function ( | |
a, // Array to decompress | |
b, // Placeholder for dictionary size | |
c, // Placeholder for dictionary | |
d, // Placeholder for iterator | |
e, // Placeholder for w | |
f, // Placeholder for result | |
g, // Placeholder for entry | |
h // Placeholder for k | |
) { | |
for (b = d = 256, c = [], e = String.fromCharCode; d--;) | |
c[d] = e(d); | |
for (e = f = e(a[d = 0]); (h = a[++d]) <= b;) | |
g = c[h] || e + e[0], c[b++] = e + g[0], f += e = g; | |
return f | |
} | |
}; | |
// 22 bytes --- String.fromCharCode(x) | |
// 28 bytes --- unescape("%"+x.toString(16)) | |
// 29 bytes --- eval("'\\"+x.toString(8)+"'") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.LZW = { | |
comp: function(a,b,c,d,e,f,g,h){for(b=d=256,c={};d--;)c[String.fromCharCode(d)]=d;for(f=[];g= a[++d];)e=c[h=e?e+g:g]?h:(f.push(c[e]),c[h]=b++,g);e&&f.push(c[e]);return f}, | |
decomp: function(a,b,c,d,e,f,g,h){for(b=d=256,c=[],e=String.fromCharCode;d--;)c[d]=e(d);for(e=f=e(a[d=0]);(h=a[++d])<=b;)g=c[h]||e+e[0],c[b++]=e+g[0],f+=e=g;return f} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
;)