1 | (function() {
|
2 |
|
3 | var CND, VOID, badge, bytecount_date, bytecount_float, bytecount_singular, bytecount_typemarker, cast, debug, declare, grow_rbuffer, isa, rbuffer, rbuffer_max_size, rbuffer_min_size, release_extraneous_rbuffer_bytes, rpr, size_of, symbol_fallback, tm_date, tm_false, tm_hi, tm_list, tm_lo, tm_ninfinity, tm_nnumber, tm_null, tm_pinfinity, tm_pnumber, tm_private, tm_text, tm_true, tm_void, type_of, validate, warn;
|
4 |
|
5 | CND = require('cnd');
|
6 |
|
7 | rpr = CND.rpr;
|
8 |
|
9 | badge = 'HOLLERITH-CODEC/MAIN';
|
10 |
|
11 | debug = CND.get_logger('debug', badge);
|
12 |
|
13 | warn = CND.get_logger('warn', badge);
|
14 |
|
15 |
|
16 | this.types = require('./types');
|
17 |
|
18 | ({isa, validate, cast, declare, size_of, type_of} = this.types);
|
19 |
|
20 | VOID = Symbol('VOID');
|
21 |
|
22 |
|
23 | this['typemarkers'] = {};
|
24 |
|
25 |
|
26 | tm_lo = this['typemarkers']['lo'] = 0x00;
|
27 |
|
28 | tm_null = this['typemarkers']['null'] = 'B'.codePointAt(0);
|
29 |
|
30 | tm_false = this['typemarkers']['false'] = 'C'.codePointAt(0);
|
31 |
|
32 | tm_true = this['typemarkers']['true'] = 'D'.codePointAt(0);
|
33 |
|
34 | tm_list = this['typemarkers']['list'] = 'E'.codePointAt(0);
|
35 |
|
36 | tm_date = this['typemarkers']['date'] = 'G'.codePointAt(0);
|
37 |
|
38 | tm_ninfinity = this['typemarkers']['ninfinity'] = 'J'.codePointAt(0);
|
39 |
|
40 | tm_nnumber = this['typemarkers']['nnumber'] = 'K'.codePointAt(0);
|
41 |
|
42 | tm_void = this['typemarkers']['void'] = 'L'.codePointAt(0);
|
43 |
|
44 | tm_pnumber = this['typemarkers']['pnumber'] = 'M'.codePointAt(0);
|
45 |
|
46 | tm_pinfinity = this['typemarkers']['pinfinity'] = 'N'.codePointAt(0);
|
47 |
|
48 | tm_text = this['typemarkers']['text'] = 'T'.codePointAt(0);
|
49 |
|
50 | tm_private = this['typemarkers']['private'] = 'Z'.codePointAt(0);
|
51 |
|
52 | tm_hi = this['typemarkers']['hi'] = 0xff;
|
53 |
|
54 |
|
55 | this['bytecounts'] = {};
|
56 |
|
57 | bytecount_singular = this['bytecounts']['singular'] = 1;
|
58 |
|
59 | bytecount_typemarker = this['bytecounts']['typemarker'] = 1;
|
60 |
|
61 | bytecount_float = this['bytecounts']['float'] = 9;
|
62 |
|
63 | bytecount_date = this['bytecounts']['date'] = bytecount_float + 1;
|
64 |
|
65 |
|
66 | this['sentinels'] = {};
|
67 |
|
68 |
|
69 |
|
70 | this['sentinels']['firstdate'] = new Date(-8640000000000000);
|
71 |
|
72 | this['sentinels']['lastdate'] = new Date(+8640000000000000);
|
73 |
|
74 |
|
75 | this['keys'] = {};
|
76 |
|
77 |
|
78 | this['keys']['lo'] = Buffer.alloc(1, this['typemarkers']['lo']);
|
79 |
|
80 | this['keys']['hi'] = Buffer.alloc(1, this['typemarkers']['hi']);
|
81 |
|
82 |
|
83 | this['symbols'] = {};
|
84 |
|
85 | symbol_fallback = this['fallback'] = Symbol('fallback');
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | rbuffer_min_size = 1024;
|
91 |
|
92 | rbuffer_max_size = 65536;
|
93 |
|
94 | rbuffer = Buffer.alloc(rbuffer_min_size);
|
95 |
|
96 |
|
97 | grow_rbuffer = function() {
|
98 | var factor, new_result_buffer, new_size;
|
99 | factor = 2;
|
100 | new_size = Math.floor(rbuffer.length * factor + 0.5);
|
101 |
|
102 | new_result_buffer = Buffer.alloc(new_size);
|
103 | rbuffer.copy(new_result_buffer);
|
104 | rbuffer = new_result_buffer;
|
105 | return null;
|
106 | };
|
107 |
|
108 |
|
109 | release_extraneous_rbuffer_bytes = function() {
|
110 | if (rbuffer.length > rbuffer_max_size) {
|
111 |
|
112 | rbuffer = Buffer.alloc(rbuffer_max_size);
|
113 | }
|
114 | return null;
|
115 | };
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | this.write_singular = function(idx, value) {
|
121 | var typemarker;
|
122 | while (!(rbuffer.length >= idx + bytecount_singular)) {
|
123 | grow_rbuffer();
|
124 | }
|
125 | if (value === null) {
|
126 | typemarker = tm_null;
|
127 | } else if (value === false) {
|
128 | typemarker = tm_false;
|
129 | } else if (value === true) {
|
130 | typemarker = tm_true;
|
131 | } else if (value === VOID) {
|
132 | typemarker = tm_void;
|
133 | } else {
|
134 | throw new Error(`µ56733 unable to encode value of type ${type_of(value)}`);
|
135 | }
|
136 | rbuffer[idx] = typemarker;
|
137 | return idx + bytecount_singular;
|
138 | };
|
139 |
|
140 |
|
141 | this.read_singular = function(buffer, idx) {
|
142 | var typemarker, value;
|
143 | switch (typemarker = buffer[idx]) {
|
144 | case tm_null:
|
145 | value = null;
|
146 | break;
|
147 | case tm_false:
|
148 | value = false;
|
149 | break;
|
150 | case tm_true:
|
151 | value = true;
|
152 | break;
|
153 |
|
154 | case tm_void:
|
155 | value = VOID;
|
156 | break;
|
157 | default:
|
158 | throw new Error(`µ57564 unable to decode 0x${typemarker.toString(16)} at index ${idx} (${rpr(buffer)})`);
|
159 | }
|
160 | return [idx + bytecount_singular, value];
|
161 | };
|
162 |
|
163 |
|
164 |
|
165 |
|
166 | this.write_private = function(idx, value, encoder) {
|
167 | var encoded_value, proper_value, ref, type, wrapped_value;
|
168 | while (!(rbuffer.length >= idx + 3 * bytecount_typemarker)) {
|
169 | grow_rbuffer();
|
170 | }
|
171 |
|
172 | rbuffer[idx] = tm_private;
|
173 | idx += bytecount_typemarker;
|
174 |
|
175 | rbuffer[idx] = tm_list;
|
176 | idx += bytecount_typemarker;
|
177 |
|
178 | type = (ref = value['type']) != null ? ref : 'private';
|
179 | proper_value = value['value'];
|
180 |
|
181 | if (encoder != null) {
|
182 | encoded_value = encoder(type, proper_value, symbol_fallback);
|
183 | if (encoded_value !== symbol_fallback) {
|
184 | proper_value = encoded_value;
|
185 | }
|
186 |
|
187 | } else if (type.startsWith('-')) {
|
188 |
|
189 | switch (type) {
|
190 | case '-set':
|
191 | null;
|
192 | break;
|
193 | default:
|
194 | throw new Error(`µ58395 unknown built-in private type ${rpr(type)}`);
|
195 | }
|
196 | }
|
197 |
|
198 | wrapped_value = [type, proper_value];
|
199 | idx = this._encode(wrapped_value, idx);
|
200 |
|
201 | rbuffer[idx] = tm_lo;
|
202 | idx += bytecount_typemarker;
|
203 |
|
204 | return idx;
|
205 | };
|
206 |
|
207 |
|
208 | this.read_private = function(buffer, idx, decoder) {
|
209 |
|
210 | var R, type, value;
|
211 | idx += bytecount_typemarker;
|
212 | [idx, [type, value]] = this.read_list(buffer, idx);
|
213 |
|
214 | if (decoder != null) {
|
215 | R = decoder(type, value, symbol_fallback);
|
216 | if (R === void 0) {
|
217 | throw new Error("µ59226 encountered illegal value `undefined` when reading private type");
|
218 | }
|
219 | if (R === symbol_fallback) {
|
220 | R = {type, value};
|
221 | }
|
222 |
|
223 | } else if (type.startsWith('-')) {
|
224 |
|
225 | switch (type) {
|
226 | case '-set':
|
227 | R = new Set(value[0]);
|
228 | break;
|
229 | default:
|
230 | throw new Error(`µ60057 unknown built-in private type ${rpr(type)}`);
|
231 | }
|
232 | } else {
|
233 |
|
234 | R = {type, value};
|
235 | }
|
236 | return [idx, R];
|
237 | };
|
238 |
|
239 |
|
240 |
|
241 |
|
242 | this.write_number = function(idx, number) {
|
243 | var type;
|
244 | while (!(rbuffer.length >= idx + bytecount_float)) {
|
245 | grow_rbuffer();
|
246 | }
|
247 | if (number < 0) {
|
248 | type = tm_nnumber;
|
249 | number = -number;
|
250 | } else {
|
251 | type = tm_pnumber;
|
252 | }
|
253 | rbuffer[idx] = type;
|
254 | rbuffer.writeDoubleBE(number, idx + 1);
|
255 | if (type === tm_nnumber) {
|
256 | this._invert_buffer(rbuffer, idx);
|
257 | }
|
258 | return idx + bytecount_float;
|
259 | };
|
260 |
|
261 |
|
262 | this.write_infinity = function(idx, number) {
|
263 | while (!(rbuffer.length >= idx + bytecount_singular)) {
|
264 | grow_rbuffer();
|
265 | }
|
266 | rbuffer[idx] = number === -2e308 ? tm_ninfinity : tm_pinfinity;
|
267 | return idx + bytecount_singular;
|
268 | };
|
269 |
|
270 |
|
271 | this.read_nnumber = function(buffer, idx) {
|
272 | var copy;
|
273 | if (buffer[idx] !== tm_nnumber) {
|
274 | throw new Error(`µ60888 not a negative number at index ${idx}`);
|
275 | }
|
276 | copy = this._invert_buffer(Buffer.from(buffer.slice(idx, idx + bytecount_float)), 0);
|
277 | return [idx + bytecount_float, -(copy.readDoubleBE(1))];
|
278 | };
|
279 |
|
280 |
|
281 | this.read_pnumber = function(buffer, idx) {
|
282 | if (buffer[idx] !== tm_pnumber) {
|
283 | throw new Error(`µ61719 not a positive number at index ${idx}`);
|
284 | }
|
285 | return [idx + bytecount_float, buffer.readDoubleBE(idx + 1)];
|
286 | };
|
287 |
|
288 |
|
289 | this._invert_buffer = function(buffer, idx) {
|
290 | var i, j, ref, ref1;
|
291 | for (i = j = ref = idx + 1, ref1 = idx + 8; (ref <= ref1 ? j <= ref1 : j >= ref1); i = ref <= ref1 ? ++j : --j) {
|
292 | buffer[i] = ~buffer[i];
|
293 | }
|
294 | return buffer;
|
295 | };
|
296 |
|
297 |
|
298 |
|
299 |
|
300 | this.write_date = function(idx, date) {
|
301 | var number;
|
302 | while (!(rbuffer.length >= idx + bytecount_date)) {
|
303 | grow_rbuffer();
|
304 | }
|
305 | number = +date;
|
306 | rbuffer[idx] = tm_date;
|
307 | return this.write_number(idx + 1, number);
|
308 | };
|
309 |
|
310 |
|
311 | this.read_date = function(buffer, idx) {
|
312 | var type, value;
|
313 | if (buffer[idx] !== tm_date) {
|
314 | throw new Error(`µ62550 not a date at index ${idx}`);
|
315 | }
|
316 | switch (type = buffer[idx + 1]) {
|
317 | case tm_nnumber:
|
318 | [idx, value] = this.read_nnumber(buffer, idx + 1);
|
319 | break;
|
320 | case tm_pnumber:
|
321 | [idx, value] = this.read_pnumber(buffer, idx + 1);
|
322 | break;
|
323 | default:
|
324 | throw new Error(`µ63381 unknown date type marker 0x${type.toString(16)} at index ${idx}`);
|
325 | }
|
326 | return [idx, new Date(value)];
|
327 | };
|
328 |
|
329 |
|
330 |
|
331 |
|
332 | this.write_text = function(idx, text) {
|
333 | var bytecount_text;
|
334 | text = text.replace(/\x01/g, '\x01\x02');
|
335 | text = text.replace(/\x00/g, '\x01\x01');
|
336 | bytecount_text = (Buffer.byteLength(text, 'utf-8')) + 2;
|
337 | while (!(rbuffer.length >= idx + bytecount_text)) {
|
338 | grow_rbuffer();
|
339 | }
|
340 | rbuffer[idx] = tm_text;
|
341 | rbuffer.write(text, idx + 1);
|
342 | rbuffer[idx + bytecount_text - 1] = tm_lo;
|
343 | return idx + bytecount_text;
|
344 | };
|
345 |
|
346 |
|
347 | this.read_text = function(buffer, idx) {
|
348 | var R, byte, stop_idx;
|
349 | if (buffer[idx] !== tm_text) {
|
350 |
|
351 | throw new Error(`µ64212 not a text at index ${idx}`);
|
352 | }
|
353 | stop_idx = idx;
|
354 | while (true) {
|
355 | stop_idx += +1;
|
356 | if ((byte = buffer[stop_idx]) === tm_lo) {
|
357 | break;
|
358 | }
|
359 | if (byte == null) {
|
360 | throw new Error(`µ65043 runaway string at index ${idx}`);
|
361 | }
|
362 | }
|
363 | R = buffer.toString('utf-8', idx + 1, stop_idx);
|
364 | R = R.replace(/\x01\x01/g, '\x00');
|
365 | R = R.replace(/\x01\x02/g, '\x01');
|
366 | return [stop_idx + 1, R];
|
367 | };
|
368 |
|
369 |
|
370 |
|
371 |
|
372 | this.read_list = function(buffer, idx) {
|
373 | var R, byte, value;
|
374 | if (buffer[idx] !== tm_list) {
|
375 | throw new Error(`µ65874 not a list at index ${idx}`);
|
376 | }
|
377 | R = [];
|
378 | idx += +1;
|
379 | while (true) {
|
380 | if ((byte = buffer[idx]) === tm_lo) {
|
381 | break;
|
382 | }
|
383 | [idx, value] = this._decode(buffer, idx, true);
|
384 | R.push(value[0]);
|
385 | if (byte == null) {
|
386 | throw new Error(`µ66705 runaway list at index ${idx}`);
|
387 | }
|
388 | }
|
389 | return [idx + 1, R];
|
390 | };
|
391 |
|
392 |
|
393 |
|
394 |
|
395 | this.write = function(idx, value, encoder) {
|
396 | var type;
|
397 | if (value === VOID) {
|
398 | return this.write_singular(idx, value);
|
399 | }
|
400 | switch (type = type_of(value)) {
|
401 | case 'text':
|
402 | return this.write_text(idx, value);
|
403 | case 'float':
|
404 | return this.write_number(idx, value);
|
405 | case 'infinity':
|
406 | return this.write_infinity(idx, value);
|
407 | case 'date':
|
408 | return this.write_date(idx, value);
|
409 |
|
410 | case 'set':
|
411 |
|
412 | return this.write_private(idx, {
|
413 | type: '-set',
|
414 | value: [Array.from(value)]
|
415 | });
|
416 | }
|
417 | if (isa.object(value)) {
|
418 |
|
419 | return this.write_private(idx, value, encoder);
|
420 | }
|
421 | return this.write_singular(idx, value);
|
422 | };
|
423 |
|
424 |
|
425 |
|
426 |
|
427 | this.encode = function(key, encoder) {
|
428 | var R, idx, type;
|
429 | key = key.slice(0);
|
430 | key.push(VOID);
|
431 | rbuffer.fill(0x00);
|
432 | if ((type = type_of(key)) !== 'list') {
|
433 | throw new Error(`µ67536 expected a list, got a ${type}`);
|
434 | }
|
435 | idx = this._encode(key, 0, encoder);
|
436 | R = Buffer.alloc(idx);
|
437 | rbuffer.copy(R, 0, 0, idx);
|
438 | release_extraneous_rbuffer_bytes();
|
439 |
|
440 | return R;
|
441 | };
|
442 |
|
443 |
|
444 | this.encode_plus_hi = function(key, encoder) {
|
445 | var R, idx, type;
|
446 |
|
447 | rbuffer.fill(0x00);
|
448 | if ((type = type_of(key)) !== 'list') {
|
449 | throw new Error(`µ68367 expected a list, got a ${type}`);
|
450 | }
|
451 | idx = this._encode(key, 0, encoder);
|
452 | while (!(rbuffer.length >= idx + 1)) {
|
453 | grow_rbuffer();
|
454 | }
|
455 | rbuffer[idx] = tm_hi;
|
456 | idx += +1;
|
457 | R = Buffer.alloc(idx);
|
458 | rbuffer.copy(R, 0, 0, idx);
|
459 | release_extraneous_rbuffer_bytes();
|
460 |
|
461 | return R;
|
462 | };
|
463 |
|
464 |
|
465 | this._encode = function(key, idx, encoder) {
|
466 | var element, element_idx, error, j, k, key_rpr, l, last_element_idx, len, len1, len2, sub_element;
|
467 | last_element_idx = key.length - 1;
|
468 | for (element_idx = j = 0, len = key.length; j < len; element_idx = ++j) {
|
469 | element = key[element_idx];
|
470 | try {
|
471 | if (isa.list(element)) {
|
472 | rbuffer[idx] = tm_list;
|
473 | idx += +1;
|
474 | for (k = 0, len1 = element.length; k < len1; k++) {
|
475 | sub_element = element[k];
|
476 | idx = this._encode([sub_element], idx, encoder);
|
477 | }
|
478 | rbuffer[idx] = tm_lo;
|
479 | idx += +1;
|
480 | } else {
|
481 | idx = this.write(idx, element, encoder);
|
482 | }
|
483 | } catch (error1) {
|
484 | error = error1;
|
485 | key_rpr = [];
|
486 | for (l = 0, len2 = key.length; l < len2; l++) {
|
487 | element = key[l];
|
488 | if (isa.buffer(element)) {
|
489 | throw new Error("µ45533 unable to encode buffers");
|
490 | } else {
|
491 |
|
492 | key_rpr.push(rpr(element));
|
493 | }
|
494 | }
|
495 | warn(`µ44544 detected problem with key [ ${rpr(key_rpr.join(', '))} ]`);
|
496 | throw error;
|
497 | }
|
498 | }
|
499 |
|
500 | return idx;
|
501 | };
|
502 |
|
503 |
|
504 | this.decode = function(buffer, decoder) {
|
505 | buffer = buffer.slice(0, buffer.length - 1);
|
506 | return (this._decode(buffer, 0, false, decoder))[1];
|
507 | };
|
508 |
|
509 |
|
510 | this._decode = function(buffer, idx, single, decoder) {
|
511 | var R, last_idx, type, value;
|
512 | R = [];
|
513 | last_idx = buffer.length - 1;
|
514 | while (true) {
|
515 | if (idx > last_idx) {
|
516 | break;
|
517 | }
|
518 | switch (type = buffer[idx]) {
|
519 | case tm_list:
|
520 | [idx, value] = this.read_list(buffer, idx);
|
521 | break;
|
522 | case tm_text:
|
523 | [idx, value] = this.read_text(buffer, idx);
|
524 | break;
|
525 | case tm_nnumber:
|
526 | [idx, value] = this.read_nnumber(buffer, idx);
|
527 | break;
|
528 | case tm_ninfinity:
|
529 | [idx, value] = [idx + 1, -2e308];
|
530 | break;
|
531 | case tm_pnumber:
|
532 | [idx, value] = this.read_pnumber(buffer, idx);
|
533 | break;
|
534 | case tm_pinfinity:
|
535 | [idx, value] = [idx + 1, +2e308];
|
536 | break;
|
537 | case tm_date:
|
538 | [idx, value] = this.read_date(buffer, idx);
|
539 | break;
|
540 | case tm_private:
|
541 | [idx, value] = this.read_private(buffer, idx, decoder);
|
542 | break;
|
543 | default:
|
544 | [idx, value] = this.read_singular(buffer, idx);
|
545 | }
|
546 | R.push(value);
|
547 | if (single) {
|
548 | break;
|
549 | }
|
550 | }
|
551 |
|
552 | return [idx, R];
|
553 | };
|
554 |
|
555 |
|
556 |
|
557 |
|
558 |
|
559 |
|
560 |
|
561 | this.encodings = {
|
562 |
|
563 | dbcs2: `⓪①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳㉑㉒㉓㉔㉕㉖㉗㉘㉙㉚㉛
|
564 | ㉜!"#$%&'()*+,-./0123456789:;<=>?
|
565 | @ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_
|
566 | `abcdefghijklmnopqrstuvwxyz{|}~㉠
|
567 | ㉝㉞㉟㊱㊲㊳㊴㊵㊶㊷㊸㊹㊺㊻㊼㊽㊾㊿㋐㋑㋒㋓㋔㋕㋖㋗㋘㋙㋚㋛㋜㋝
|
568 | ㋞㋟㋠㋡㋢㋣㋤㋥㋦㋧㋨㋩㋪㋫㋬㋭㋮㋯㋰㋱㋲㋳㋴㋵㋶㋷㋸㋹㋺㋻㋼㋽
|
569 | ㋾㊊㊋㊌㊍㊎㊏㊐㊑㊒㊓㊔㊕㊖㊗㊘㊙㊚㊛㊜㊝㊞㊟㊠㊡㊢㊣㊤㊥㊦㊧㊨
|
570 | ㊩㊪㊫㊬㊭㊮㊯㊰㊀㊁㊂㊃㊄㊅㊆㊇㊈㊉㉈㉉㉊㉋㉌㉍㉎㉏⓵⓶⓷⓸⓹〓`,
|
571 |
|
572 | aleph: `БДИЛЦЧШЭЮƆƋƏƐƔƥƧƸψŐőŒœŊŁłЯɔɘɐɕəɞ
|
573 | ␣!"#$%&'()*+,-./0123456789:;<=>?
|
574 | @ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_
|
575 | \`abcdefghijklmnopqrstuvwxyz{|}~ω
|
576 | ΓΔΘΛΞΠΣΦΨΩαβγδεζηθικλμνξπρςστυφχ
|
577 | Ж¡¢£¤¥¦§¨©ª«¬Я®¯°±²³´µ¶·¸¹º»¼½¾¿
|
578 | ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞß
|
579 | àáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ`,
|
580 |
|
581 | rdctn: `∇≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡
|
582 | ␣!"#$%&'()*+,-./0123456789:;<=>?
|
583 | @ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_
|
584 | \`abcdefghijklmnopqrstuvwxyz{|}~≡
|
585 | ∃∃∃∃∃∃∃∃∃∃∃∃∃∃∃∃∃∃∃∃∃∃∃∃∃∃∃∃∃∃∃∃
|
586 | ∃∃¢£¤¥¦§¨©ª«¬Я®¯°±²³´µ¶·¸¹º»¼½¾¿
|
587 | ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞß
|
588 | àáâãäåæçèéêëìíîïðñò≢≢≢≢≢≢≢≢≢≢≢≢Δ`
|
589 | };
|
590 |
|
591 |
|
592 | this.rpr_of_buffer = function(buffer, encoding = 'rdctn') {
|
593 | return (rpr(buffer)) + ' ' + this._encode_buffer(buffer, encoding);
|
594 | };
|
595 |
|
596 |
|
597 | this._encode_buffer = function(buffer, encoding = 'rdctn') {
|
598 | var idx;
|
599 | if (!isa.list(encoding)) {
|
600 | encoding = this.encodings[encoding];
|
601 | }
|
602 | return ((function() {
|
603 | var j, ref, results;
|
604 | results = [];
|
605 | for (idx = j = 0, ref = buffer.length; (0 <= ref ? j < ref : j > ref); idx = 0 <= ref ? ++j : --j) {
|
606 | results.push(encoding[buffer[idx]]);
|
607 | }
|
608 | return results;
|
609 | })()).join('');
|
610 | };
|
611 |
|
612 |
|
613 | this._compile_encodings = function() {
|
614 | var chrs_of, encoding, length, name, ref;
|
615 |
|
616 | chrs_of = function(text) {
|
617 | var chr;
|
618 | text = text.split(/([\ud800-\udbff].|.)/);
|
619 | return (function() {
|
620 | var j, len, results;
|
621 | results = [];
|
622 | for (j = 0, len = text.length; j < len; j++) {
|
623 | chr = text[j];
|
624 | if (chr !== '') {
|
625 | results.push(chr);
|
626 | }
|
627 | }
|
628 | return results;
|
629 | })();
|
630 | };
|
631 | ref = this.encodings;
|
632 |
|
633 | for (name in ref) {
|
634 | encoding = ref[name];
|
635 | encoding = chrs_of(encoding.replace(/\n+/g, ''));
|
636 | if ((length = encoding.length) !== 256) {
|
637 | throw new Error(`µ69198 expected 256 characters, found ${length} in encoding ${rpr(name)}`);
|
638 | }
|
639 | this.encodings[name] = encoding;
|
640 | }
|
641 | return null;
|
642 | };
|
643 |
|
644 | this._compile_encodings();
|
645 |
|
646 |
|
647 | this.as_sortline = function(key, settings) {
|
648 | var bare, base, buffer, buffer_txt, idx, joiner, ref, ref1, ref2, ref3, stringify;
|
649 | joiner = (ref = settings != null ? settings['joiner'] : void 0) != null ? ref : ' ';
|
650 | base = (ref1 = settings != null ? settings['base'] : void 0) != null ? ref1 : 0x2800;
|
651 | stringify = (ref2 = settings != null ? settings['stringify'] : void 0) != null ? ref2 : JSON.stringify;
|
652 | bare = (ref3 = settings != null ? settings['bare'] : void 0) != null ? ref3 : false;
|
653 | buffer = this.encode(key);
|
654 | buffer_txt = ((function() {
|
655 | var j, ref4, results;
|
656 | results = [];
|
657 | for (idx = j = 0, ref4 = buffer.length - 1; (0 <= ref4 ? j < ref4 : j > ref4); idx = 0 <= ref4 ? ++j : --j) {
|
658 | results.push(String.fromCodePoint(base + buffer[idx]));
|
659 | }
|
660 | return results;
|
661 | })()).join('');
|
662 | if (bare) {
|
663 | return buffer_txt;
|
664 | }
|
665 | return buffer_txt + joiner + stringify(key);
|
666 | };
|
667 |
|
668 | }).call(this);
|
669 |
|
670 |
|
\ | No newline at end of file |