mol_crypto_lib 0.1.1660 → 0.1.1662

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/node.mjs CHANGED
@@ -231,6 +231,11 @@ var $;
231
231
  return this.setBigUint64(offset, next, true), next;
232
232
  $mol_fail(new Error(`Wrong uint64 value ${next}`));
233
233
  }
234
+ float16(offset, next) {
235
+ if (next !== undefined)
236
+ this.setFloat16(offset, next, true);
237
+ return this.getFloat16(offset, true);
238
+ }
234
239
  float32(offset, next) {
235
240
  if (next !== undefined)
236
241
  this.setFloat32(offset, next, true);
@@ -2577,12 +2582,41 @@ var $;
2577
2582
  "use strict";
2578
2583
  var $;
2579
2584
  (function ($) {
2580
- const TextEncoder = globalThis.TextEncoder ?? $node.util.TextEncoder;
2581
- const encoder = new TextEncoder();
2582
- function $mol_charset_encode(value) {
2583
- return encoder.encode(value);
2585
+ let buf = new Uint8Array(2 ** 12);
2586
+ function $mol_charset_encode(str) {
2587
+ const capacity = str.length * 3;
2588
+ if (buf.byteLength < capacity)
2589
+ buf = new Uint8Array(capacity);
2590
+ return buf.slice(0, $mol_charset_encode_to(str, buf));
2584
2591
  }
2585
2592
  $.$mol_charset_encode = $mol_charset_encode;
2593
+ function $mol_charset_encode_to(str, buf, from = 0) {
2594
+ let pos = from;
2595
+ for (let i = 0; i < str.length; i++) {
2596
+ let code = str.charCodeAt(i);
2597
+ if (code < 0x80) {
2598
+ buf[pos++] = code;
2599
+ }
2600
+ else if (code < 0x800) {
2601
+ buf[pos++] = 0xc0 | (code >> 6);
2602
+ buf[pos++] = 0x80 | (code & 0x3f);
2603
+ }
2604
+ else if (code < 0xd800 || code >= 0xe000) {
2605
+ buf[pos++] = 0xe0 | (code >> 12);
2606
+ buf[pos++] = 0x80 | ((code >> 6) & 0x3f);
2607
+ buf[pos++] = 0x80 | (code & 0x3f);
2608
+ }
2609
+ else {
2610
+ const point = ((code - 0xd800) << 10) + str.charCodeAt(++i) + 0x2400;
2611
+ buf[pos++] = 0xf0 | (point >> 18);
2612
+ buf[pos++] = 0x80 | ((point >> 12) & 0x3f);
2613
+ buf[pos++] = 0x80 | ((point >> 6) & 0x3f);
2614
+ buf[pos++] = 0x80 | (point & 0x3f);
2615
+ }
2616
+ }
2617
+ return pos - from;
2618
+ }
2619
+ $.$mol_charset_encode_to = $mol_charset_encode_to;
2586
2620
  })($ || ($ = {}));
2587
2621
 
2588
2622
  ;
package/node.test.js CHANGED
@@ -222,6 +222,11 @@ var $;
222
222
  return this.setBigUint64(offset, next, true), next;
223
223
  $mol_fail(new Error(`Wrong uint64 value ${next}`));
224
224
  }
225
+ float16(offset, next) {
226
+ if (next !== undefined)
227
+ this.setFloat16(offset, next, true);
228
+ return this.getFloat16(offset, true);
229
+ }
225
230
  float32(offset, next) {
226
231
  if (next !== undefined)
227
232
  this.setFloat32(offset, next, true);
@@ -2568,12 +2573,41 @@ var $;
2568
2573
  "use strict";
2569
2574
  var $;
2570
2575
  (function ($) {
2571
- const TextEncoder = globalThis.TextEncoder ?? $node.util.TextEncoder;
2572
- const encoder = new TextEncoder();
2573
- function $mol_charset_encode(value) {
2574
- return encoder.encode(value);
2576
+ let buf = new Uint8Array(2 ** 12);
2577
+ function $mol_charset_encode(str) {
2578
+ const capacity = str.length * 3;
2579
+ if (buf.byteLength < capacity)
2580
+ buf = new Uint8Array(capacity);
2581
+ return buf.slice(0, $mol_charset_encode_to(str, buf));
2575
2582
  }
2576
2583
  $.$mol_charset_encode = $mol_charset_encode;
2584
+ function $mol_charset_encode_to(str, buf, from = 0) {
2585
+ let pos = from;
2586
+ for (let i = 0; i < str.length; i++) {
2587
+ let code = str.charCodeAt(i);
2588
+ if (code < 0x80) {
2589
+ buf[pos++] = code;
2590
+ }
2591
+ else if (code < 0x800) {
2592
+ buf[pos++] = 0xc0 | (code >> 6);
2593
+ buf[pos++] = 0x80 | (code & 0x3f);
2594
+ }
2595
+ else if (code < 0xd800 || code >= 0xe000) {
2596
+ buf[pos++] = 0xe0 | (code >> 12);
2597
+ buf[pos++] = 0x80 | ((code >> 6) & 0x3f);
2598
+ buf[pos++] = 0x80 | (code & 0x3f);
2599
+ }
2600
+ else {
2601
+ const point = ((code - 0xd800) << 10) + str.charCodeAt(++i) + 0x2400;
2602
+ buf[pos++] = 0xf0 | (point >> 18);
2603
+ buf[pos++] = 0x80 | ((point >> 12) & 0x3f);
2604
+ buf[pos++] = 0x80 | ((point >> 6) & 0x3f);
2605
+ buf[pos++] = 0x80 | (point & 0x3f);
2606
+ }
2607
+ }
2608
+ return pos - from;
2609
+ }
2610
+ $.$mol_charset_encode_to = $mol_charset_encode_to;
2577
2611
  })($ || ($ = {}));
2578
2612
 
2579
2613
  ;
@@ -4417,10 +4451,23 @@ var $;
4417
4451
  var $;
4418
4452
  (function ($) {
4419
4453
  $mol_test({
4420
- 'encode utf8 string'() {
4421
- const str = 'Hello, ΧΨΩЫ';
4422
- const encoded = new Uint8Array([72, 101, 108, 108, 111, 44, 32, 206, 167, 206, 168, 206, 169, 208, 171]);
4423
- $mol_assert_like($mol_charset_encode(str), encoded);
4454
+ 'encode empty'() {
4455
+ $mol_assert_equal($mol_charset_encode(''), new Uint8Array([]));
4456
+ },
4457
+ 'encode 1 octet'() {
4458
+ $mol_assert_equal($mol_charset_encode('F'), new Uint8Array([0x46]));
4459
+ },
4460
+ 'encode 2 octet'() {
4461
+ $mol_assert_equal($mol_charset_encode('Б'), new Uint8Array([0xd0, 0x91]));
4462
+ },
4463
+ 'encode 3 octet'() {
4464
+ $mol_assert_equal($mol_charset_encode('ह'), new Uint8Array([0xe0, 0xa4, 0xb9]));
4465
+ },
4466
+ 'encode 4 octet'() {
4467
+ $mol_assert_equal($mol_charset_encode('𐍈'), new Uint8Array([0xf0, 0x90, 0x8d, 0x88]));
4468
+ },
4469
+ 'encode surrogate pair'() {
4470
+ $mol_assert_equal($mol_charset_encode('😀'), new Uint8Array([0xf0, 0x9f, 0x98, 0x80]));
4424
4471
  },
4425
4472
  });
4426
4473
  })($ || ($ = {}));