-
Notifications
You must be signed in to change notification settings - Fork 2
/
stringpack.js
345 lines (289 loc) · 8.52 KB
/
stringpack.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
StringPack serializes almost all JavaScript objects in valid UTF8 strings.
It supports objects constructed from custom classes, undefined, null, ±0,
±Infinity,
Some amount of human debuggability (but not readability) is a design goal:
- Strings are untouched
- Numbers up to 15 have their hexadecimal representations
*/
var ALPH_S = 33, ALPH_L = 94, // Start & Length of the encoding character set
STR_1S = 33, STR_1E = 47, // Start & End of the first string range
STR_2S = 58, STR_2E = 64, STR_2O = 15,
STR_3S = 91, STR_3E = 94, STR_3O = 22,
INT_1S = 48, INT_1E = 57,
INT_2S = 65, INT_2E = 90, INT_2O = 10,
FLT3 = 95, FLT6 = 96,
ARR_S = 97, ARR_E = 104,
MAP_S = 105, MAP_E = 112,
SPL_S = 113, SPL_E = 120,
STR = 124, INT = 123, FLT = 125,
ARR = 121, MAP = 122, SPL = 126,
NULL = 33, FALSE = 34, TRUE = 35,
INFN = 36, INFP = 37, NAN = 38,
UNDEF = 39, FUNC = 40,
SPL1_S = 41, SPL1_E = 126, SPL1_O = 8,
UINT1_R = 31, UINT_LS = [2, 3, 5],
INT1_R = 11, INT_LS = [2, 3, 5, 7],
INT_MX = INT1_R * Math.pow(ALPH_L, 6) - 1,
FLT_B = 370, FLT_LS = [2, 5, 10];
module.exports = function StringPack (classes) {
classes = classes || [];
function decode(string) {
var i = 0;
function digit() {
if (i > string.length) { throw Error("DEC_UNEXP_END"); }
return string.charCodeAt(i++) - ALPH_S;
}
function int() {
var val = digit(),
sign = val % 2 ? -1 : 1,
last = i + INT_LS[(val = Math.floor(val / 2)) % 4] - 1;
val = Math.floor(val / 4);
while (i < last) {
val = val * ALPH_L + digit();
}
return sign * val;
}
function uint() {
var val = digit(),
last = i + UINT_LS[val % 3] - 1;
val = Math.floor(val / 3);
while (i < last) {
val = val * ALPH_L + digit();
}
return val;
}
function flt() {
var exp1 = digit(), exp2 = digit(),
sign = exp2 % 2 ? -1 : 1,
man = (exp2 = Math.floor(exp2 / 2)) % 2,
last = i + FLT_LS[exp1 % 3] - 1,
exp1 = Math.floor(exp1 / 3);
exp2 = Math.floor(exp2 / 2);
while (i < last) {
man = man * ALPH_L + digit();
}
return sign * parseFloat(man + "E" + (exp1 * 23 + exp2 - FLT_B));
// should be sign * man * Math.pow(10, exp1 * 23 + exp2 - FLT_B);
// but Math.pow(10, -x) sometimes rounds incorrectly while 1e-x doesn't.
}
function spl(index) {
var cls = classes[index],
args = [],
len = uint(),
object = Object.create(cls.prototype);
for (var j = 0; j < len; j++) { args.push(next()); }
cls.apply(object, args);
return object;
}
function next () {
var arr, map, len, j,
code = string.charCodeAt(i++);
if (isNaN(code)) { throw Error("DEC_UNEXP_END"); }
if (code >= INT_1S && code <= INT_1E) {
return code - INT_1S;
} else if (code >= INT_2S && code <= INT_2E) {
return code - INT_2S + INT_2O;
} else if (code >= STR_1S && code <= STR_1E) {
return string.substring(i, i += (code - STR_1S));
} else if (code >= STR_2S && code <= STR_2E) {
return string.substring(i, i += (code - STR_2S + STR_2O));
} else if (code >= STR_3S && code <= STR_3E) {
return string.substring(i, i += (code - STR_3S + STR_3O));
} else if (code >= SPL_S && code <= SPL_E) {
return spl(code - SPL_S);
} else if (code >= ARR_S && code <= ARR_E) {
arr = []; len = code - ARR_S;
for (j = 0; j < len; j++) { arr.push(next()); }
return arr;
} else if (code >= MAP_S && code <= MAP_E) {
map = {}; len = code - MAP_S;
for (j = 0; j < len; j++) { map[next()] = next(); }
return map;
} else {
switch(code) {
case FLT3: case FLT6:
return int() * (code === FLT3 ? 1E-3 : 1E-6);
case INT:
return int();
case FLT:
return flt();
case STR:
len = uint();
return string.substring(i, i += len);
case ARR:
arr = []; len = uint();
for (j = 0; j < len; j++) { arr.push(next()); }
return arr;
case MAP:
map = {}, len = uint();
for (j = 0; j < len; j++) { map[next()] = next(); }
return map;
case SPL:
code = string.charCodeAt(i++);
switch(code) {
case UNDEF: return undefined;
case NULL: return null;
case TRUE: return true;
case FALSE: return false;
case INFP: return +Infinity;
case INFN: return -Infinity;
case NAN: return NaN;
default: return spl(code - SPL1_S + SPL1_O + 1);
}
}
}
}
try {
return next();
} catch (e) {
throw new Error(
"ERR_DECODE\n" +
string.substr(Math.max(0, i - 10), 10) +
"←" + string.substr(i, 10) + "\n" +
e.message
);
}
}
function encode (object) {
var prefix, len, i, keys;
function digit(code) {
return String.fromCharCode(ALPH_S + code)
}
function code(code) {
return String.fromCharCode(code);
}
function int(val, carry, snaps) {
var str = "", size = 0;
while (val >= carry || str.length < snaps[size] - 1) {
str = digit(val % ALPH_L) + str;
val = Math.floor(val / ALPH_L);
if (str.length > snaps[size] - 1) { size++; }
}
return [val, size, str];
}
function uint(val) {
var ret = int(val, UINT1_R, UINT_LS);
return digit(ret[0] * 3 + ret[1]) + ret[2];
}
function num(val) {
var sign, str, dec, exp, ret;
if (val < 0 || val === 0 && 1 / val < 0) {
sign = 1;
val = -val;
} else {
sign = 0;
}
str = val.toExponential().split("e");
exp = parseInt(str[1]);
dec = Math.max(0, str[0].length - 2);
val *= Math.pow(10, dec - exp);
exp -= dec;
function big() {
console.log("BIG");
ret = int(val, 2, FLT_LS);
exp += FLT_B;
return code(FLT) +
digit(Math.floor(exp / 23) * 3 + ret[1]) +
digit((exp % 23) * 4 + ret[0] * 2 + sign) +
ret[2];
}
if (exp > 2 || exp < -6) {
return big();
} else if (exp < -3) {
if (val * Math.pow(10, exp + 6) > INT_MX) return big();
ret = int(val * Math.pow(10, exp + 6), INT1_R, INT_LS);
return code(FLT6) +
digit(ret[0] * 8 + ret[1] * 2 + sign) +
ret[2];
} else if (exp < 0) {
if (val * Math.pow(10, exp + 3) > INT_MX) return big();
ret = int(val * Math.pow(10, exp + 3), INT1_R, INT_LS);
return code(FLT3) +
digit(ret[0] * 8 + ret[1] * 2 + sign) +
ret[2];
} else {
if (val * Math.pow(10, exp) > INT_MX) return big();
ret = int(val * Math.pow(10, exp), INT1_R, INT_LS);
return code(INT) +
digit(ret[0] * 8 + ret[1] * 2 + sign) +
ret[2];
}
}
if (typeof object === "undefined") {
return code(SPL) + code(UNDEF);
} if (typeof object === "boolean") {
return code(SPL) + code(object ? TRUE : FALSE);
} else if (typeof object === "number") {
if (isNaN(object)) { return code(SPL) + code(NAN); }
if (object === -Infinity) { return code(SPL) + code(INFN); }
if (object === +Infinity) { return code(SPL) + code(INFP); }
if (object > 0 && object % 1 === 0) {
if (object <= INT_1E - INT_1S) {
return code(INT_1S + object);
} else if (object - INT_2O < INT_2E - INT_2S) {
return code(INT_2S + (object - INT_2O));
}
}
return num(object);
} else if (typeof object === "string") {
if (object.length <= STR_1E - STR_1S) {
prefix = code(STR_1S + object.length);
} else if (object.length - STR_2O <= STR_2E - STR_2S) {
prefix = code(STR_2S + (object.length - STR_2O));
} else if (object.length - STR_3O <= STR_3E - STR_3S) {
prefix = code(STR_3S + (object.length - STR_3O));
} else {
prefix = code(STR) + uint(object.length);
}
return prefix + object;
} else if (typeof object === "object") {
if (object === null) {
return code(SPL) + code(NULL);
}
if (Array.isArray(object)) {
len = object.length;
if (len < ARR_E - ARR_S) {
prefix = code(ARR_S + len);
} else {
prefix = code(ARR) + uint(len);
}
for (i = 0; i < len; i++) {
prefix += encode(object[i]);
}
return prefix;
}
if (typeof object.packArguments === "function") {
i = classes.indexOf(object.constructor);
if (i < 0) {
throw Error("ENC_SPL_CONSTRUCTOR_NOT_FOUND");
} else if (i <= SPL_E - SPL_S) {
prefix = code(SPL_S + i);
} else if (i - SPL1_O - 1 <= SPL1_E - SPL1_S) {
prefix = code(SPL) + code(SPL1_S + i - SPL1_O - 1);
} else {
throw Error("ENC_SPL_OUTSIDE_BOUNDS");
}
object = object.packArguments();
if (!Array.isArray(object)) { throw Error("ENC_SPL_ARGS_NOT_ARRAY"); }
prefix += uint(object.length);
for (i = 0, len = object.length; i < len; i++) {
prefix += encode(object[i]);
}
return prefix;
}
keys = Object.keys(object).sort();
len = keys.length;
if (len < MAP_E - MAP_S) {
prefix = code(MAP_S + len);
} else {
prefix = code(MAP) + uint(len);
}
for (i = 0; i < len; i++) {
prefix += encode(keys[i]) + encode(object[keys[i]]);
}
return prefix;
}
}
return { encode: encode, decode: decode };
};