-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstringCode.js
108 lines (96 loc) · 4.11 KB
/
stringCode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
(function(root){
function toUnicode(str){
if(!str) return '';
return str.toString().replace(/[^\x00-\xff]/g,function(s){
return '\\u'+s.charCodeAt(0).toString(16);
});
}
function unicode2Char(str){
if(!str) return '';
return str.replace(/\\u(\w{1,4})/g,function(a,s){
return String.fromCharCode(parseInt(s,16));
});
}
root.enCode=function(strNormalString) {
var strCompressedString = "",
intLeftOver = 0,
intOutputCode = 0,
pcode = 0,
ccode = 0,
k = 0,
strNormalString=toUnicode(strNormalString),
len = strNormalString.length,
getCode=String.fromCharCode;
for (var i = 0; i < len; i++) {
ccode = strNormalString.charCodeAt(i);
k = (pcode << 8) | ccode;
intLeftOver += 12;
intOutputCode <<= 12;
intOutputCode |= pcode;
pcode = ccode;
if (intLeftOver >= 16) {
strCompressedString += getCode(intOutputCode >> (intLeftOver - 16));
intOutputCode &= (Math.pow(2, (intLeftOver - 16)) - 1);
intLeftOver -= 16;
}
}
if (pcode != 0) {
intLeftOver += 12;
intOutputCode <<= 12;
intOutputCode |= pcode;
}
if (intLeftOver >= 16) {
strCompressedString += getCode(intOutputCode >> (intLeftOver - 16));
intOutputCode &= (Math.pow(2, (intLeftOver - 16)) - 1);
intLeftOver -= 16;
}
if (intLeftOver > 0) {
intOutputCode <<= (16 - intLeftOver);
strCompressedString += getCode(intOutputCode);
}
return strCompressedString;
};
root.deCode=function(strCompressedString){
var strNormalString = "",
ht = [],
used = 128,
intLeftOver = 0,
intOutputCode = 0,
ccode = 0,
pcode = 0,
key = 0,
len = strCompressedString.length,
getCode=String.fromCharCode;
for (var i = 0; i < used; i++) {
ht[i] = getCode(i);
}
for (i = 0; i < len; i++) {
intLeftOver += 16;
intOutputCode <<= 16;
intOutputCode |= strCompressedString.charCodeAt(i);
while (1) {
if (intLeftOver >= 12) {
ccode = intOutputCode >> (intLeftOver - 12);
if (typeof (key = ht[ccode]) != "undefined") {
strNormalString += key;
if (used > 128) {
ht[ht.length] = ht[pcode] + key.substr(0, 1);
}
pcode = ccode;
}else {
key = ht[pcode] + ht[pcode].substr(0, 1);
strNormalString += key;
ht[ht.length] = ht[pcode] + key.substr(0, 1);
pcode = ht.length - 1;
}
used++;
intLeftOver -= 12;
intOutputCode &= (Math.pow(2, intLeftOver) - 1);
}else {
break;
}
}
}
return unicode2Char(strNormalString.substr(1,strNormalString.length-2));
}
}(this));