-
-
Save sebastien-p/1027389 to your computer and use it in GitHub Desktop.
LZW compression
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
// Please, see http://rosettacode.org/wiki/LZW_compression#JavaScript | |
// and http://en.wikipedia.org/wiki/Lempel–Ziv–Welch for more infos. | |
function ( | |
a // String to compress and placeholder for 'wc'. | |
){ | |
for ( | |
var b = a + "Ā", // Append first "illegal" character (charCode === 256). | |
c = [], // dictionary | |
d = 0, // dictionary size | |
e = d, // iterator | |
f = c, // w | |
g = c, // result | |
h; // c | |
h = b.charAt(e++); | |
) | |
c[h] = h.charCodeAt(), // Fill in the dictionary ... | |
f = 1 + c[a = f + h] ? a : (g[d++] = c[f], c[a] = d + 255, h); // ... and use it to compress data. | |
return g // Array of compressed data. | |
} |
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
function(a){for(var b=a+"Ā",c=[],d=0,e=d,f=c,g=c,h;h=b.charAt(e++);)c[h]=h.charCodeAt(),f=1+c[a=f+h]?a:(g[d++]=c[f],c[a]=d+255,h);return g} |
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
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Sebastien P. https://twitter.com/#!/_sebastienp | |
Special thanks to @subzey (you rock) and @kbjr ! | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
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
{ | |
"name": "LZWcompress", | |
"description": "JavaScript implementation of the Lempel–Ziv–Welch universal lossless data compression algorithm.", | |
"keywords": [ | |
"LZW", | |
"lossless", | |
"data", | |
"compression" | |
] | |
} |
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
<!DOCTYPE html> | |
<title>Foo</title> | |
<div>Expected value: <b>84,79,66,69,79,82,78,79,84,256,258,260,265,259,261,263</b></div> | |
<div>Actual value: <b id="ret"></b></div> | |
<script> | |
var LZWcompress = function(a){for(var b=a+"Ā",c=[],d=0,e=d,f=c,g=c,h;h=b.charAt(e++);)c[h]=h.charCodeAt(),f=1+c[a=f+h]?a:(g[d++]=c[f],c[a]=d+255,h);return g} | |
document.getElementById("ret").innerHTML = LZWcompress("TOBEORNOTTOBEORTOBEORNOT") | |
</script> |
Hey, @sebastien-p
Great that you've started this! Thank you!
If you don't care for compatibility with IE7 or older, you can omit .charAt and use b[e++], thus saving 7 bytes.
@jed: could it be official canon that IE-compatibility may start with version 8?
Greetings, atk
@atk : thanks. I know about b[e++]
but why not offering IE<8 compatibility if we can ?
Like we used to say: One byte saved is one byte earned... maybe we can get enough space for String.fromCharCode.
@atk : I don't understand, there's no need of String.fromCharCode
anymore, where do you want it to be ?
@sebastien-p : Depends on what you expect as output.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My bad. I was checking using…
data:text/html;charset=utf-8,<!DOCTYPE%20html><title>Foo</title><div>Expected%20value%3A%20<b>84,79,66,69,79,82,78,79,84,256,258,260,265,259,261,263</b></div><div>Actual%20value%3A%20<b%20id="ret"></b></div><script>var%20LZWcompress%20=%20function(a){for(var%20b=a+"%C4%80",c=%5B%5D,d=0,e=d,f=c,g=c,h;h=b.charAt(e++);)c%5Bh%5D=h.charCodeAt(),f=1+c%5Ba=f+h%5D%3Fa%3A(g%5Bd++%5D=c%5Bf%5D,c%5Ba%5D=d+255,h);return%20g}document.getElementById("ret").innerHTML%20=%20LZWcompress("TOBEORNOTTOBEORTOBEORNOT")</script>
(New lines got cut out while pasting in my location bar – and because of the missing semicolons the whole thing broke.)