-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutf.js
More file actions
131 lines (123 loc) · 3.6 KB
/
utf.js
File metadata and controls
131 lines (123 loc) · 3.6 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
utf = new function()
{
this.unpackUTF16 = function(_str)
{
var i, utf16=[];
for (i=0; i<_str.length; i++) utf16[i] = _str.charCodeAt(i);
return utf16;
}
this.unpackChar = function(_str)
{
var utf16 = this.unpackUTF16(_str);
var i,n, tmp = [];
for (n=i=0; i<utf16.length; i++) {
if (utf16[i]<=0xff) tmp[n++] = utf16[i];
else {
tmp[n++] = utf16[i] >> 8;
tmp[n++] = utf16[i] & 0xff;
}
}
return tmp;
}
this.packChar =
this.packUTF16 = function(_utf16)
{
var i, str = "";
for (i in _utf16) str += String.fromCharCode(_utf16[i]);
return str;
}
this.unpackUTF8 = function(_str)
{
return this.toUTF8( this.unpackUTF16(_str) );
}
this.packUTF8 = function(_utf8)
{
return this.packUTF16( this.toUTF16(_utf8) );
}
this.toUTF8 = function(_utf16)
{
var utf8 = [];
var idx = 0;
var i, j, c;
for (i=0; i<_utf16.length; i++)
{
c = _utf16[i];
if (c <= 0x7f) utf8[idx++] = c;
else if (c <= 0x7ff)
{
utf8[idx++] = 0xc0 | (c >>> 6 );
utf8[idx++] = 0x80 | (c & 0x3f);
}
else if (c <= 0xffff)
{
utf8[idx++] = 0xe0 | (c >>> 12 );
utf8[idx++] = 0x80 | ((c >>> 6 ) & 0x3f);
utf8[idx++] = 0x80 | (c & 0x3f);
}
else
{
j = 4;
while (c >> (6*j)) j++;
utf8[idx++] = ((0xff00 >>> j) & 0xff) | (c >>> (6*--j) );
while (j--)
utf8[idx++] = 0x80 | ((c >>> (6*j)) & 0x3f);
}
}
return utf8;
}
this.toUTF16 = function(_utf8)
{
var utf16 = [];
var idx = 0;
var i,s;
for (i=0; i<_utf8.length; i++, idx++)
{
if (_utf8[i] <= 0x7f) utf16[idx] = _utf8[i];
else
{
if ( (_utf8[i]>>5) == 0x6)
{
utf16[idx] = ( (_utf8[i] & 0x1f) << 6 )
| ( _utf8[++i] & 0x3f );
}
else if ( (_utf8[i]>>4) == 0xe)
{
utf16[idx] = ( (_utf8[i] & 0xf) << 12 )
| ( (_utf8[++i] & 0x3f) << 6 )
| ( _utf8[++i] & 0x3f );
}
else
{
s = 1;
while (_utf8[i] & (0x20 >>> s) ) s++;
utf16[idx] = _utf8[i] & (0x1f >>> s);
while (s-->=0) utf16[idx] = (utf16[idx] << 6) ^ (_utf8[++i] & 0x3f);
}
}
}
return utf16;
}
this.URLencode = function(_str)
{
return _str.replace(/([^a-zA-Z0-9_\-\.])/g, function(_tmp, _c)
{
p if (_c == "\x20") return "+";
var tmp = utf.toUTF8( [_c.charCodeAt(0)] );
var c = "";
for (var i in tmp)
{
i = tmp[i].toString(16);
if (i.length == 1) i = "0"+ i;
c += "%"+ i;
}
return c;
} );
}
this.URLdecode = function(_dat)
{
_dat = _dat.replace(/\+/g, "\x20");
_dat = _dat.replace( /%([a-fA-F0-9][a-fA-F0-9])/g,
function(_tmp, _hex){ return String.fromCharCode( parseInt(_hex, 16) ) } );
return this.packChar( this.toUTF16( this.unpackUTF16(_dat) ) );
}
}