mol_view_tree2_lib 1.0.37 → 1.0.39

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
@@ -4326,12 +4326,41 @@ var $;
4326
4326
  "use strict";
4327
4327
  var $;
4328
4328
  (function ($) {
4329
- const TextEncoder = globalThis.TextEncoder ?? $node.util.TextEncoder;
4330
- const encoder = new TextEncoder();
4331
- function $mol_charset_encode(value) {
4332
- return encoder.encode(value);
4329
+ let buf = new Uint8Array(2 ** 12);
4330
+ function $mol_charset_encode(str) {
4331
+ const capacity = str.length * 3;
4332
+ if (buf.byteLength < capacity)
4333
+ buf = new Uint8Array(capacity);
4334
+ return buf.slice(0, $mol_charset_encode_to(str, buf));
4333
4335
  }
4334
4336
  $.$mol_charset_encode = $mol_charset_encode;
4337
+ function $mol_charset_encode_to(str, buf, from = 0) {
4338
+ let pos = from;
4339
+ for (let i = 0; i < str.length; i++) {
4340
+ let code = str.charCodeAt(i);
4341
+ if (code < 0x80) {
4342
+ buf[pos++] = code;
4343
+ }
4344
+ else if (code < 0x800) {
4345
+ buf[pos++] = 0xc0 | (code >> 6);
4346
+ buf[pos++] = 0x80 | (code & 0x3f);
4347
+ }
4348
+ else if (code < 0xd800 || code >= 0xe000) {
4349
+ buf[pos++] = 0xe0 | (code >> 12);
4350
+ buf[pos++] = 0x80 | ((code >> 6) & 0x3f);
4351
+ buf[pos++] = 0x80 | (code & 0x3f);
4352
+ }
4353
+ else {
4354
+ const point = ((code - 0xd800) << 10) + str.charCodeAt(++i) + 0x2400;
4355
+ buf[pos++] = 0xf0 | (point >> 18);
4356
+ buf[pos++] = 0x80 | ((point >> 12) & 0x3f);
4357
+ buf[pos++] = 0x80 | ((point >> 6) & 0x3f);
4358
+ buf[pos++] = 0x80 | (point & 0x3f);
4359
+ }
4360
+ }
4361
+ return pos - from;
4362
+ }
4363
+ $.$mol_charset_encode_to = $mol_charset_encode_to;
4335
4364
  })($ || ($ = {}));
4336
4365
 
4337
4366
  ;
package/node.test.js CHANGED
@@ -4317,12 +4317,41 @@ var $;
4317
4317
  "use strict";
4318
4318
  var $;
4319
4319
  (function ($) {
4320
- const TextEncoder = globalThis.TextEncoder ?? $node.util.TextEncoder;
4321
- const encoder = new TextEncoder();
4322
- function $mol_charset_encode(value) {
4323
- return encoder.encode(value);
4320
+ let buf = new Uint8Array(2 ** 12);
4321
+ function $mol_charset_encode(str) {
4322
+ const capacity = str.length * 3;
4323
+ if (buf.byteLength < capacity)
4324
+ buf = new Uint8Array(capacity);
4325
+ return buf.slice(0, $mol_charset_encode_to(str, buf));
4324
4326
  }
4325
4327
  $.$mol_charset_encode = $mol_charset_encode;
4328
+ function $mol_charset_encode_to(str, buf, from = 0) {
4329
+ let pos = from;
4330
+ for (let i = 0; i < str.length; i++) {
4331
+ let code = str.charCodeAt(i);
4332
+ if (code < 0x80) {
4333
+ buf[pos++] = code;
4334
+ }
4335
+ else if (code < 0x800) {
4336
+ buf[pos++] = 0xc0 | (code >> 6);
4337
+ buf[pos++] = 0x80 | (code & 0x3f);
4338
+ }
4339
+ else if (code < 0xd800 || code >= 0xe000) {
4340
+ buf[pos++] = 0xe0 | (code >> 12);
4341
+ buf[pos++] = 0x80 | ((code >> 6) & 0x3f);
4342
+ buf[pos++] = 0x80 | (code & 0x3f);
4343
+ }
4344
+ else {
4345
+ const point = ((code - 0xd800) << 10) + str.charCodeAt(++i) + 0x2400;
4346
+ buf[pos++] = 0xf0 | (point >> 18);
4347
+ buf[pos++] = 0x80 | ((point >> 12) & 0x3f);
4348
+ buf[pos++] = 0x80 | ((point >> 6) & 0x3f);
4349
+ buf[pos++] = 0x80 | (point & 0x3f);
4350
+ }
4351
+ }
4352
+ return pos - from;
4353
+ }
4354
+ $.$mol_charset_encode_to = $mol_charset_encode_to;
4326
4355
  })($ || ($ = {}));
4327
4356
 
4328
4357
  ;
@@ -9164,10 +9193,23 @@ var $;
9164
9193
  var $;
9165
9194
  (function ($) {
9166
9195
  $mol_test({
9167
- 'encode utf8 string'() {
9168
- const str = 'Hello, ΧΨΩЫ';
9169
- const encoded = new Uint8Array([72, 101, 108, 108, 111, 44, 32, 206, 167, 206, 168, 206, 169, 208, 171]);
9170
- $mol_assert_like($mol_charset_encode(str), encoded);
9196
+ 'encode empty'() {
9197
+ $mol_assert_equal($mol_charset_encode(''), new Uint8Array([]));
9198
+ },
9199
+ 'encode 1 octet'() {
9200
+ $mol_assert_equal($mol_charset_encode('F'), new Uint8Array([0x46]));
9201
+ },
9202
+ 'encode 2 octet'() {
9203
+ $mol_assert_equal($mol_charset_encode('Б'), new Uint8Array([0xd0, 0x91]));
9204
+ },
9205
+ 'encode 3 octet'() {
9206
+ $mol_assert_equal($mol_charset_encode('ह'), new Uint8Array([0xe0, 0xa4, 0xb9]));
9207
+ },
9208
+ 'encode 4 octet'() {
9209
+ $mol_assert_equal($mol_charset_encode('𐍈'), new Uint8Array([0xf0, 0x90, 0x8d, 0x88]));
9210
+ },
9211
+ 'encode surrogate pair'() {
9212
+ $mol_assert_equal($mol_charset_encode('😀'), new Uint8Array([0xf0, 0x9f, 0x98, 0x80]));
9171
9213
  },
9172
9214
  });
9173
9215
  })($ || ($ = {}));