Advertisement
Lusien_Lashans

RLE

Mar 5th, 2017
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function code(str){
  2.     var res = '';
  3.     var n = 1;
  4.     for (var i = 0; i < str.length; i++){
  5.         if (n >= 127){
  6.             res += '#' + String.fromCharCode(127) + str.charAt(i);
  7.             n -= 127;
  8.             continue;
  9.         }
  10.  
  11.        
  12.         if (str.charCodeAt(i) == str.charCodeAt(i+1)){
  13.             n++;
  14.             continue;
  15.         }
  16.        
  17.         if (n > 3  || str.charAt(i) == '#'){
  18.             res += '#' + String.fromCharCode(n) + str.charAt(i);
  19.             n = 1;
  20.         }
  21.         else{
  22.             res += str.slice(i - n + 1, i + 1);
  23.             n = 1;
  24.         }  
  25.     }
  26.     return res;
  27. }
  28.  
  29. function decode(str){
  30.     var res = '';
  31.     for (var i = 0; i < str.length; i++){
  32.        
  33.         if (str.charAt(i) == '#'){
  34.             if (str.charCodeAt(i+1) == 127){
  35.                 for (var j = 0; j < 128; j++) {
  36.                 res += str.charAt(i+2);
  37.             }
  38.             i += 2;
  39.             }  
  40.             else{
  41.             for (var j = 0; j < str.charCodeAt(i+1); j++) {
  42.                 res += str.charAt(i+2);
  43.             }
  44.             i += 2;
  45.             }
  46.         }
  47.         else{
  48.             res += str.charAt(i);
  49.         }
  50.     }
  51.     return res;
  52. }
  53.  
  54.  
  55. var fso = new ActiveXObject("Scripting.FileSystemObject");
  56. var name = WSH.Arguments(0);
  57. var ts = fso.OpenTextFile(WSH.Arguments(1));
  58. var str = ts.ReadAll();
  59. ts.Close();
  60.  
  61. if (name == 'code'){
  62.     str = code(str);
  63. }
  64. if (name == 'decode'){
  65.     str = decode(str);
  66. }
  67.  
  68. ts = fso.OpenTextFile(WSH.Arguments(2), 2, true);
  69. ts.Writeline(str);
  70. ts.Close();
  71. WSH.echo(str);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement