Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
initital commit
  • Loading branch information
Stephen Woods committed Oct 3, 2009
0 parents commit b450e7d
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
Empty file added README
Empty file.
105 changes: 105 additions & 0 deletions lzw.js
@@ -0,0 +1,105 @@
(function(){

var dictionary = function(){

var result = {};
for (var i=0; i < 256; i++) {
var ch = String.fromCharCode(i);
result[ch] = i;
};
return result;
};


var dic = false;
function getDic(){

return dictionary();

}

function encode(str){
var index = 256;
var dictionary = getDic();
var outStr = [];
var s = '';

var len = str.length;
s = str[0];
for(var i=1; i < len; i++){
var c = str[i];
if(dictionary[s+c]){
s = s+c;
}else{
//console.log(ch + 'sec');
var code = ++index;
outStr.push(String.fromCharCode(dictionary[s]));
dictionary[s+c] = code;
s = c;
}
}
for(var c in s ){
outStr.push(s[c]);
}

return outStr.join('');
};

function tTable(){
var result = {};
for (var i=0; i < 256; i++) {
var ch = String.fromCharCode(i);
result[i] = ch;
};
return result;
}

var prefix_code = [];
var append_character = [];
function decode(str){

var next_code,
new_code,
old_code,
character,
counter,
string,
out_str;
next_code = 256;
counter = 0;
//old_code = str[0];
character = old_code;
out_str = old_code;
for(var c in str){
new_code = str[c];
string = decode_string(new_code);
out_str+= string;

if(next_code < 100000){
prefix_code[next_code] = old_code;
append_character[next_code] = character;
next_code++;
}
old_code = new_code;
}

return out_str;
}

function decode_string(str){
var i = 0;
var buffer = '';
var code = str.charCodeAt(0);
while(code > 255){
buffer += append_character[code];
code = prefix_code[code];
}

buffer = code;
return buffer;
}

function size(str){

return encodeURIComponent(str).replace(/%../g, 'x').length;
}

0 comments on commit b450e7d

Please sign in to comment.