3 * 1st char: code of rep prefix, then:
8 var f
, res
= "", used
= [], ch
;
9 /* find least used char */
10 for (f
= 0; f
<= 255; f
++) used
[f
] = 0;
11 for (f
= 0; f
< s
.length
; f
++) {
14 if (ch >= 32 && ch <= 126) print('"'+String.fromCharCode(ch)+'"');
15 else print("0x"+ch.toString(16).toUpperCase());
20 var repChar
= 35, repCnt
= used
[repChar
], repASCII
= true;
21 for (f
= 0; f
<= 255; f
++) {
23 if (uc
< repCnt
|| (!repASCII
&& uc
<= repCnt
)) {
24 repChar
= f
; repCnt
= uc
;
25 repASCII
= (f
>= 32 && f
<= 126 && f
!= 34 && f
!= 92);
30 if (repASCII) print('"'+String.fromCharCode(repChar)+'"');
31 else print("0x"+repChar.charCodeAt(0).toString(16).toUpperCase());
32 print("cnt: "+repCnt);
33 print("used: "+used[repChar]);
35 function AddCode (c
, isRep
) {
36 if (!isRep
&& c
== repChar
) {
39 AddCode(repChar
, true);
42 if (c
>= 32 && c
<= 126) {
43 if (c
== 34 || c
== 92) res
+= '\\';
44 res
+= String
.fromCharCode(c
);
46 var h
= c
.toString(16).toLowerCase();
47 while (h
.length
< 2) h
= "0"+h
;
52 AddCode(repChar
, true); /* repchar */
53 for (f
= 0; f
< s
.length
; ) {
55 if (f
>= s
.length
-2) { AddCode(c
); f
++; continue; }
56 if (s
[f
+1] == c
&& (c
== repChar
|| s
[f
+2] == c
)) {
58 AddCode(repChar
, true);
59 var cnt
= -1; /* 0 means at least one repeat */
60 while (cnt
< 255 && f
< s
.length
&& s
[f
] == c
) { cnt
++; f
++; }
61 AddCode(cnt
, true); /* repcount */
62 AddCode(c
, true); /* char */
63 /*print("rep "+(cnt+1)+"; char: "+c);*/
64 } else { AddCode(c
); f
++; }
72 * 1st char: code of rep prefix, then:
74 * repprefix,count,code
76 function RLEUnpack (s
) {
77 var res
= [], rc
= s
.charCodeAt(0);
79 for (var f
= 1; f
< s
.length
; ) {
80 var cc
= s
.charCodeAt(f
++);
82 var cnt
= s
.charCodeAt(f
++);
83 cc
= s
.charCodeAt(f
++);
84 /*print("pos: "+(f-3)+"; rep "+(cnt+1)+"; char: "+cc+"; npos: "+f+"; nchar: "+s.charCodeAt(f));*/
85 while (cnt
-- > 0) res
.push(cc
);
93 function PackArray (arr
) {
98 function DUnpackStr (s
) {