mol_vary 0.0.89 → 0.0.90

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 $;
@@ -50,16 +60,6 @@ var $;
50
60
  $.$mol_bigint_encode = $mol_bigint_encode;
51
61
  })($ || ($ = {}));
52
62
 
53
- ;
54
- "use strict";
55
- var $;
56
- (function ($) {
57
- function $mol_fail(error) {
58
- throw error;
59
- }
60
- $.$mol_fail = $mol_fail;
61
- })($ || ($ = {}));
62
-
63
63
  ;
64
64
  "use strict";
65
65
  var $;
@@ -160,59 +160,6 @@ var $;
160
160
  $.$mol_charset_decode = $mol_charset_decode;
161
161
  })($ || ($ = {}));
162
162
 
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
163
  ;
217
164
  "use strict";
218
165
  var $;
@@ -323,8 +270,21 @@ var $;
323
270
  const release = (size) => {
324
271
  capacity -= size;
325
272
  };
326
- const dump_unum = (tip, val) => {
327
- if (val < $mol_vary_len.L1) {
273
+ const calc_size = (val) => {
274
+ if (val < $mol_vary_len.L1)
275
+ return 1;
276
+ if (val < 2 ** 8)
277
+ return 2;
278
+ if (val < 2 ** 16)
279
+ return 3;
280
+ if (val < 2 ** 32)
281
+ return 5;
282
+ if (val < 2n ** 64n)
283
+ return 9;
284
+ return $mol_fail(new Error('Too large number'));
285
+ };
286
+ const dump_unum = (tip, val, max = val) => {
287
+ if (max < $mol_vary_len.L1) {
328
288
  this.array[pos++] = tip | Number(val);
329
289
  release(8);
330
290
  return;
@@ -334,24 +294,24 @@ var $;
334
294
  if (offset !== undefined)
335
295
  return dump_unum($mol_vary_tip.link, offset);
336
296
  }
337
- if (val < 2 ** 8) {
297
+ if (max < 2 ** 8) {
338
298
  this.array[pos++] = tip | $mol_vary_len.L1;
339
299
  this.array[pos++] = Number(val);
340
300
  release(7);
341
301
  }
342
- else if (val < 2 ** 16) {
302
+ else if (max < 2 ** 16) {
343
303
  this.array[pos++] = tip | $mol_vary_len.L2;
344
304
  this.buffer.setUint16(pos, Number(val), true);
345
305
  pos += 2;
346
306
  release(6);
347
307
  }
348
- else if (val < 2 ** 32) {
308
+ else if (max < 2 ** 32) {
349
309
  this.array[pos++] = tip | $mol_vary_len.L4;
350
310
  this.buffer.setUint32(pos, Number(val), true);
351
311
  pos += 4;
352
312
  release(4);
353
313
  }
354
- else if (val < 2n ** 64n) {
314
+ else if (max < 2n ** 64n) {
355
315
  this.array[pos++] = tip | $mol_vary_len.L8;
356
316
  this.buffer.setBigUint64(pos, BigInt(val), true);
357
317
  pos += 8;
@@ -422,11 +382,13 @@ var $;
422
382
  const offset = offsets.get(val);
423
383
  if (offset !== undefined)
424
384
  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);
385
+ const len_max = val.length * 3;
386
+ const len_size = calc_size(len_max);
387
+ acquire(len_max);
388
+ const len = $mol_charset_encode_to(val, this.array, pos + len_size);
389
+ dump_unum($mol_vary_tip.text, len, len_max);
428
390
  pos += len;
429
- release(val.length * 3 - len);
391
+ release(len_max - len);
430
392
  offsets.set(val, offsets.size);
431
393
  return;
432
394
  };
@@ -632,8 +594,8 @@ var $;
632
594
  };
633
595
  const read_text = (kind) => {
634
596
  const len = read_unum(kind);
635
- const [text, bytes] = $mol_charset_decode_from(array, pos, len);
636
- pos += bytes;
597
+ const text = $mol_charset_decode(new Uint8Array(array.buffer, array.byteOffset + pos, len));
598
+ pos += len;
637
599
  stream.push(text);
638
600
  return text;
639
601
  };
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]);