-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunicode.js
213 lines (197 loc) · 6.07 KB
/
unicode.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Unicode.js provides functions to convert to and from various bases and encoding commonly used in Unicode.
// Only unicodeConvert and datatypesEnum should be publicly used.
// Depends on jQuery.
// Provides integer constant to indicate the type of an input.
var datatypesEnum = {
BIN : 0,
INT : 1,
HEX : 2,
UTF8 : 3
// UTF16 : 4,
// UTF32 : 5
};
// Converts 'val' with type 'fromType' to type 'toType'.
// fromType and toType are values from datatypesEnum.
var unicodeConvert = function (val, fromType, toType) {
return fromBin(toBin(val, fromType), toType);
};
// toBin converts a string to a numeric value specified by type.
// If the input is invalid, it returns -1.
//
// TODO: Will strip off leading zeroes.
var toBin = function (val, type) {
if (typeof val !== 'string') {
return -1;
}
if (val < 0) {
return -1;
}
var digits = '0123456789';
var letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
switch (type) {
case datatypesEnum.BIN:
if (!validateCharacters(val, '01')) {
return -1;
}
return val;
case datatypesEnum.INT:
if (!validateCharacters(val, digits)) {
return -1;
}
return parseInt((val).toString(), 10).toString(2);
case datatypesEnum.HEX:
if (!validateCharacters(val, digits + letters)) {
return -1;
}
return parseInt((val).toString(), 16).toString(2);
case datatypesEnum.UTF8:
if (!validateCharacters(val, '01')) {
return -1;
}
return utf8ToBin(val);
// case datatypesEnum.UTF16:
// // not implemented
// if (!validateCharacters(val, '01')) {
// return -1;
// }
// case datatypesEnum.UTF32:
// // not implemented
// if (!validateCharacters(val, '01')) {
// return -1;
// }
default:
return -1;
}
};
// fromBin converts a valid binary string to a numeric string value specified by type.
// Returns -1 if val is not a valid binary string.
var fromBin = function(val, type) {
if (typeof val !== 'string') {
return -1;
}
if (val < 0) {
return -1;
}
if (!validateCharacters(val, '01')) {
return -1;
}
switch (type) {
case datatypesEnum.BIN:
val = parseInt(val, 2);
return val.toString(2);
case datatypesEnum.INT:
val = parseInt(val, 2);
return val.toString(10);
case datatypesEnum.HEX:
val = parseInt(val, 2);
return val.toString(16);
case datatypesEnum.UTF8:
return binToUtf8(val);
// case datatypesEnum.UTF16:
// // not implemented
// break;
// case datatypesEnum.UTF32:
// // not implemented
// break;
default:
return -1;
}
};
// Converts a UTF-8 binary string to Unicode binary string.
// Assumes input is a valid UTF-8 binary string.
var utf8ToBin = function (val) {
var utf8CodePoints = tokenizeUtf8(val);
var res = [];
for (var i = 0; i < utf8CodePoints.length; i++) {
res.push(utf8ToUnicode(utf8CodePoints[i]));
}
return res.join('');
};
// Splits a UTF-8 binary string into an array of binary string code points,
// still with padding.
var tokenizeUtf8 = function (val) {
var res = [];
for (var i = 0; i < val.length / 8; i++) {
res.push(val.substr(i*8, 8));
}
return res;
};
// Converts a single valid UTF-8 binary code point to a Unicode binary code point.
var utf8ToUnicode = function (val) {
if (val.indexOf('11110') === 0) {
return val.substr(5, 3) + val.substr(10, 6) + val.substr(18, 6) + val.substr(26, 6);
}
if (val.indexOf('1110') === 0) {
return val.substr(4, 4) + val.substr(10, 6) + val.substr(18, 6);
}
if (val.indexOf('110') === 0) {
return val.substr(3, 5) + val.substr(10, 6);
}
if (val.indexOf('10') === 0) {
return val.substr(2, 6);
}
if (val.indexOf('0') === 0) {
return val.substr(1, 7);
}
};
// Converts a valid binary string to UTF-8 binary.
var binToUtf8Bytes = function (val) {
if (val.length > 16) {
padding = 21 - val.length;
return [val.substr(0, 3 - padding), val.substr(3 - padding, 6), val.substr(9 - padding, 6), val.substr(15 - padding)];
}
if (val.length > 11) {
padding = 16 - val.length;
return [val.substr(0, 4 - padding), val.substr(4 - padding, 6), val.substr(10 - padding)];
}
if (val.length > 7) {
padding = 11 - val.length;
return [val.substr(0, 5 - padding), val.substr(5 - padding, 6)];
}
return [val];
};
var binToUtf8BytesPadded = function (val) {
var padding;
if (val.length > 3) {
padding = 21 - val.join('').length;
return ['11110' + '0'.repeat(padding) + val[0], '10' + val[1], '10' + val[2], '10' + val[3]];
}
if (val.length > 2) {
padding = 16 - val.join('').length;
return ['1110' + '0'.repeat(padding) + val[0], '10' + val[1], '10' + val[2]];
}
if (val.length > 1) {
padding = 11 - val.join('').length;
return ['110' + '0'.repeat(padding) + val[0], '10' + val[1]];
}
return ['0' + val[0]];
};
var binToUtf8 = function (val) {
return binToUtf8BytesPadded(binToUtf8Bytes(val)).join('');
};
var utf16ToBin = function (val) {
// not implemented
};
var binToUtf16 = function (val) {
// not implemented
};
var utf32ToBin = function (val) {
// not implemented
};
var binToUtf32 = function (val) {
// not implemented
};
var validateCharacters = function (val, valid) {
valid = valid.split('');
for (var i = 0; i < val.length; i++) {
if ($.inArray(val[i], valid) < 0) {
return false;
}
}
return true;
};
// http://stackoverflow.com/a/202627/1559886
String.prototype.repeat = function( num )
{
return new Array( num + 1 ).join( this );
};