Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
LZW compression and decompression by @_sebastienp (not me)
/*
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)+"'")
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
Something went wrong with that request. Please try again.