SWAG98

LZW-Decoding (decompressing) (javascript) (no commentary)

Jan 18th, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Decompress an LZW-encoded string
  2. var lzw_decode = function lzw_decode(s,log) {
  3.     if(log !== false){log = true}
  4.     var dict = {};
  5.     var data = (s + "").split("");
  6.     var code = data[0].charCodeAt(0);
  7.     var ocode=code;
  8.     data.splice(0,1);
  9.     var currChar = data[0];
  10.     var oldPhrase = currChar;
  11.     var out = [currChar];
  12.     var phrase;
  13.     load = {now:0,max:data.length,before:0,character:'#'}
  14.     process.stdout.clearLine()
  15.     process.stdout.cursorTo(0)
  16.     if(log){process.stdout.write('decompressing...\n')}
  17.     for (var i=1; i<data.length; i++) {
  18.         load.now = Math.round(100*(i/load.max))
  19.         if(load.before !== load.now && log){
  20.             process.stdout.clearLine()
  21.             process.stdout.cursorTo(0)
  22.             process.stdout.write(load.now+'%')
  23.             process.stdout.cursorTo(5)
  24.             process.stdout.write((load.character).repeat(Math.round(load.now/4)))
  25.             load.before = load.now
  26.         }
  27.         var currCode = data[i].charCodeAt(0);
  28.         if (currCode < ocode) {
  29.             phrase = data[i];
  30.         }
  31.         else {
  32.            phrase = dict[currCode] ? dict[currCode] : (oldPhrase + currChar);
  33.         }
  34.         out.push(phrase);
  35.         currChar = phrase.charAt(0);
  36.         dict[code] = oldPhrase + currChar;
  37.         code++;
  38.         oldPhrase = phrase;
  39.     }
  40.     process.stdout.clearLine()
  41.     process.stdout.cursorTo(0)
  42.     return out.join("");
  43. }
Advertisement
Add Comment
Please, Sign In to add comment