mol_vary 0.0.89 → 0.0.91

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/web.mjs CHANGED
@@ -28,6 +28,16 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
28
28
  var $ = ( typeof module === 'object' ) ? ( module['export'+'s'] = globalThis ) : globalThis
29
29
  $.$$ = $
30
30
 
31
+ ;
32
+ "use strict";
33
+ var $;
34
+ (function ($) {
35
+ function $mol_fail(error) {
36
+ throw error;
37
+ }
38
+ $.$mol_fail = $mol_fail;
39
+ })($ || ($ = {}));
40
+
31
41
  ;
32
42
  "use strict";
33
43
  var $;
@@ -54,21 +64,21 @@ var $;
54
64
  "use strict";
55
65
  var $;
56
66
  (function ($) {
57
- function $mol_fail(error) {
58
- throw error;
67
+ let buf = new Uint8Array(2 ** 12);
68
+ function $mol_charset_buffer(size) {
69
+ if (buf.byteLength < size)
70
+ buf = new Uint8Array(size);
71
+ return buf;
59
72
  }
60
- $.$mol_fail = $mol_fail;
73
+ $.$mol_charset_buffer = $mol_charset_buffer;
61
74
  })($ || ($ = {}));
62
75
 
63
76
  ;
64
77
  "use strict";
65
78
  var $;
66
79
  (function ($) {
67
- let buf = new Uint8Array(2 ** 12);
68
80
  function $mol_charset_encode(str) {
69
- const capacity = str.length * 3;
70
- if (buf.byteLength < capacity)
71
- buf = new Uint8Array(capacity);
81
+ const buf = $mol_charset_buffer(str.length * 3);
72
82
  return buf.slice(0, $mol_charset_encode_to(str, buf));
73
83
  }
74
84
  $.$mol_charset_encode = $mol_charset_encode;
@@ -160,59 +170,6 @@ var $;
160
170
  $.$mol_charset_decode = $mol_charset_decode;
161
171
  })($ || ($ = {}));
162
172
 
163
- ;
164
- "use strict";
165
- var $;
166
- (function ($) {
167
- function $mol_charset_decode_from(buffer, from, count) {
168
- const codes = [];
169
- let pos = from;
170
- while (pos < buffer.length && codes.length < count) {
171
- const byte1 = buffer[pos++];
172
- if (byte1 <= 0x7F) {
173
- codes.push(byte1);
174
- }
175
- else if ((byte1 & 0xE0) === 0xC0) {
176
- if (pos >= buffer.length)
177
- break;
178
- const byte2 = buffer[pos++];
179
- const code = ((byte1 & 0x1F) << 6) | (byte2 & 0x3F);
180
- codes.push(code);
181
- }
182
- else if ((byte1 & 0xF0) === 0xE0) {
183
- if (pos + 1 >= buffer.length)
184
- break;
185
- const byte2 = buffer[pos++];
186
- const byte3 = buffer[pos++];
187
- const code = ((byte1 & 0x0F) << 12) | ((byte2 & 0x3F) << 6) | (byte3 & 0x3F);
188
- codes.push(code);
189
- }
190
- else if ((byte1 & 0xF8) === 0xF0) {
191
- if (pos + 2 >= buffer.length)
192
- break;
193
- const byte2 = buffer[pos++];
194
- const byte3 = buffer[pos++];
195
- const byte4 = buffer[pos++];
196
- let code = ((byte1 & 0x07) << 18) | ((byte2 & 0x3F) << 12) | ((byte3 & 0x3F) << 6) | (byte4 & 0x3F);
197
- if (code > 0xFFFF) {
198
- code -= 0x10000;
199
- const hi = 0xD800 + (code >> 10);
200
- const lo = 0xDC00 + (code & 0x3FF);
201
- codes.push(hi, lo);
202
- }
203
- else {
204
- codes.push(code);
205
- }
206
- }
207
- else {
208
- codes.push(0xFFFD);
209
- }
210
- }
211
- return [String.fromCharCode(...codes), pos - from];
212
- }
213
- $.$mol_charset_decode_from = $mol_charset_decode_from;
214
- })($ || ($ = {}));
215
-
216
173
  ;
217
174
  "use strict";
218
175
  var $;
@@ -323,8 +280,21 @@ var $;
323
280
  const release = (size) => {
324
281
  capacity -= size;
325
282
  };
326
- const dump_unum = (tip, val) => {
327
- if (val < $mol_vary_len.L1) {
283
+ const calc_size = (val) => {
284
+ if (val < $mol_vary_len.L1)
285
+ return 1;
286
+ if (val < 2 ** 8)
287
+ return 2;
288
+ if (val < 2 ** 16)
289
+ return 3;
290
+ if (val < 2 ** 32)
291
+ return 5;
292
+ if (val < 2n ** 64n)
293
+ return 9;
294
+ return $mol_fail(new Error('Too large number'));
295
+ };
296
+ const dump_unum = (tip, val, max = val) => {
297
+ if (max < $mol_vary_len.L1) {
328
298
  this.array[pos++] = tip | Number(val);
329
299
  release(8);
330
300
  return;
@@ -334,24 +304,24 @@ var $;
334
304
  if (offset !== undefined)
335
305
  return dump_unum($mol_vary_tip.link, offset);
336
306
  }
337
- if (val < 2 ** 8) {
307
+ if (max < 2 ** 8) {
338
308
  this.array[pos++] = tip | $mol_vary_len.L1;
339
309
  this.array[pos++] = Number(val);
340
310
  release(7);
341
311
  }
342
- else if (val < 2 ** 16) {
312
+ else if (max < 2 ** 16) {
343
313
  this.array[pos++] = tip | $mol_vary_len.L2;
344
314
  this.buffer.setUint16(pos, Number(val), true);
345
315
  pos += 2;
346
316
  release(6);
347
317
  }
348
- else if (val < 2 ** 32) {
318
+ else if (max < 2 ** 32) {
349
319
  this.array[pos++] = tip | $mol_vary_len.L4;
350
320
  this.buffer.setUint32(pos, Number(val), true);
351
321
  pos += 4;
352
322
  release(4);
353
323
  }
354
- else if (val < 2n ** 64n) {
324
+ else if (max < 2n ** 64n) {
355
325
  this.array[pos++] = tip | $mol_vary_len.L8;
356
326
  this.buffer.setBigUint64(pos, BigInt(val), true);
357
327
  pos += 8;
@@ -422,11 +392,13 @@ var $;
422
392
  const offset = offsets.get(val);
423
393
  if (offset !== undefined)
424
394
  return dump_unum($mol_vary_tip.link, offset);
425
- dump_unum($mol_vary_tip.text, val.length);
426
- acquire(val.length * 3);
427
- const len = $mol_charset_encode_to(val, this.array, pos);
395
+ const len_max = val.length * 3;
396
+ const len_size = calc_size(len_max);
397
+ acquire(len_max);
398
+ const len = $mol_charset_encode_to(val, this.array, pos + len_size);
399
+ dump_unum($mol_vary_tip.text, len, len_max);
428
400
  pos += len;
429
- release(val.length * 3 - len);
401
+ release(len_max - len);
430
402
  offsets.set(val, offsets.size);
431
403
  return;
432
404
  };
@@ -632,8 +604,8 @@ var $;
632
604
  };
633
605
  const read_text = (kind) => {
634
606
  const len = read_unum(kind);
635
- const [text, bytes] = $mol_charset_decode_from(array, pos, len);
636
- pos += bytes;
607
+ const text = $mol_charset_decode(new Uint8Array(array.buffer, array.byteOffset + pos, len));
608
+ pos += len;
637
609
  stream.push(text);
638
610
  return text;
639
611
  };
package/web.test.js CHANGED
@@ -1410,32 +1410,6 @@ var $;
1410
1410
  });
1411
1411
  })($ || ($ = {}));
1412
1412
 
1413
- ;
1414
- "use strict";
1415
- var $;
1416
- (function ($) {
1417
- $mol_test({
1418
- 'encode empty'() {
1419
- $mol_assert_equal($mol_charset_decode_from(new Uint8Array([]), 0, 0), ['', 0]);
1420
- },
1421
- 'encode 1 octet'() {
1422
- $mol_assert_equal($mol_charset_decode_from(new Uint8Array([0x46]), 0, 1), ['F', 1]);
1423
- },
1424
- 'encode 2 octet'() {
1425
- $mol_assert_equal($mol_charset_decode_from(new Uint8Array([0xd0, 0x91]), 0, 1), ['Б', 2]);
1426
- },
1427
- 'encode 3 octet'() {
1428
- $mol_assert_equal($mol_charset_decode_from(new Uint8Array([0xe0, 0xa4, 0xb9]), 0, 1), ['ह', 3]);
1429
- },
1430
- 'encode 4 octet'() {
1431
- $mol_assert_equal($mol_charset_decode_from(new Uint8Array([0xf0, 0x90, 0x8d, 0x88]), 0, 1), ['𐍈', 4]);
1432
- },
1433
- 'encode surrogate pair'() {
1434
- $mol_assert_equal($mol_charset_decode_from(new Uint8Array([0xf0, 0x9f, 0x98, 0x80]), 0, 2), ['😀', 4]);
1435
- },
1436
- });
1437
- })($ || ($ = {}));
1438
-
1439
1413
  ;
1440
1414
  "use strict";
1441
1415
  var $;
@@ -1679,8 +1653,11 @@ var $;
1679
1653
  },
1680
1654
  "vary pack text"($) {
1681
1655
  check(['foo'], [text | 3, ...str('foo')]);
1682
- const long = 'abcdefghijklmnopqrstuvwxyzЖЫ';
1683
- check([long], [text | L1, 28, ...str(long)]);
1656
+ check(['абв'], [text | 6, ...str('абв')]);
1657
+ const long_lat = 'abcdefghijklmnopqrst';
1658
+ check([long_lat], [text | L1, 20, ...str(long_lat)]);
1659
+ const long_cyr = 'абвгдеёжзийклмнопрст';
1660
+ check([long_cyr], [text | L1, 40, ...str(long_cyr)]);
1684
1661
  },
1685
1662
  "vary pack dedup text"($) {
1686
1663
  check([["f", "f"]], [list | 2, text | 1, ...str('f'), link | 0]);