mol_vary 0.0.96 → 0.0.97

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
@@ -77,57 +77,140 @@ var $;
77
77
  "use strict";
78
78
  var $;
79
79
  (function ($) {
80
+ const ascii_set = [...`0123456789.,:;()'"- \n`].map(c => c.charCodeAt(0));
81
+ const ascii_map = new Array(0x80).fill(0);
82
+ for (let i = 0; i < ascii_set.length; ++i)
83
+ ascii_map[ascii_set[i]] = i | 0x80;
84
+ const diacr_set = [
85
+ 0x00, 0x01, 0x0F, 0x0B, 0x07, 0x08, 0x12, 0x13,
86
+ 0x02, 0x0C, 0x06, 0x11, 0x03, 0x09, 0x0A, 0x04,
87
+ 0x28, 0x31, 0x27, 0x26, 0x23,
88
+ ];
89
+ const diacr_map = new Array(0x80).fill(0);
90
+ for (let i = 0; i < diacr_set.length; ++i)
91
+ diacr_map[diacr_set[i]] = i | 0x80;
92
+ const wide_offset = 0x0E_00;
93
+ const wide_limit = 128 * 128 * 8 + wide_offset;
94
+ const tine_limit = 128 * 98;
95
+ const full_mode = 0x95;
96
+ const wide_mode = 0x96;
97
+ const tiny_mode = 0x9E;
80
98
  function $mol_charset_ucf_encode(str) {
81
99
  const buf = $mol_charset_buffer(str.length * 3);
82
100
  return buf.slice(0, $mol_charset_ucf_encode_to(str, buf));
83
101
  }
84
102
  $.$mol_charset_ucf_encode = $mol_charset_ucf_encode;
85
- const fast_char = `0123456789.,:;()?!-'" \n`;
86
- const fast_map = new Array(0x80).fill(0);
87
- for (let i = 0; i < fast_char.length; ++i)
88
- fast_map[fast_char[i].charCodeAt(0)] = i | 0x80;
89
103
  function $mol_charset_ucf_encode_to(str, buf, from = 0) {
90
104
  let pos = from;
91
- let mode = 0x9C;
105
+ let mode = tiny_mode;
106
+ const write_high = (code) => {
107
+ buf[pos++] = ((code + 128 - mode) & 0x7F) | 0x80;
108
+ };
109
+ const write_remap = (code) => {
110
+ const fast = ascii_map[code];
111
+ if (fast)
112
+ write_high(fast);
113
+ else
114
+ buf[pos++] = code;
115
+ };
116
+ const write_mode = (m) => {
117
+ write_high(m);
118
+ mode = m;
119
+ };
92
120
  for (let i = 0; i < str.length; i++) {
93
121
  let code = str.charCodeAt(i);
94
122
  if (code >= 0xd800 && code < 0xe000)
95
123
  code = ((code - 0xd800) << 10) + str.charCodeAt(++i) + 0x2400;
96
124
  if (code < 0x80) {
97
- if (mode !== 0x9C) {
98
- const fast = fast_map[code];
99
- if (fast)
100
- code = fast;
101
- else
102
- buf[pos++] = mode = 0x9C;
125
+ if (mode !== tiny_mode) {
126
+ const fast = ascii_map[code];
127
+ if (!fast)
128
+ write_mode(tiny_mode);
103
129
  }
104
130
  buf[pos++] = code;
105
131
  }
106
- else if (code < 0x32_00) {
107
- const page = (code >> 7) + 0x9C;
132
+ else if (code < tine_limit) {
133
+ const page = (code >> 7) + tiny_mode;
134
+ code &= 0x7F;
135
+ if (page === 164) {
136
+ const fast = diacr_map[code];
137
+ if (fast) {
138
+ if (mode !== tiny_mode)
139
+ write_mode(tiny_mode);
140
+ write_high(fast);
141
+ continue;
142
+ }
143
+ }
108
144
  if (mode !== page)
109
- buf[pos++] = mode = page;
110
- buf[pos++] = code & 0x7F;
145
+ write_mode(page);
146
+ write_remap(code);
111
147
  }
112
- else if (code < 0x04_20_00) {
113
- code -= 0x2000;
114
- const page = (code >> 15) + 0x98;
148
+ else if (code < wide_limit) {
149
+ code -= wide_offset;
150
+ const page = (code >> 14) + wide_mode;
115
151
  if (mode !== page)
116
- buf[pos++] = mode = page;
117
- buf[pos++] = code & 0x7F;
118
- buf[pos++] = code >> 7;
152
+ write_mode(page);
153
+ write_remap(code & 0x7F);
154
+ write_remap((code >> 7) & 0x7F);
119
155
  }
120
156
  else {
121
- if (mode !== 0x97)
122
- buf[pos++] = mode = 0x97;
123
- buf[pos++] = code & 0x7F;
124
- buf[pos++] = code >> 7;
125
- buf[pos++] = code >> 15;
157
+ if (mode !== full_mode)
158
+ write_mode(full_mode);
159
+ write_remap(code & 0x7F);
160
+ write_remap((code >> 7) & 0x7F);
161
+ write_remap(code >> 14);
126
162
  }
127
163
  }
164
+ if (mode !== tiny_mode)
165
+ write_mode(tiny_mode);
128
166
  return pos - from;
129
167
  }
130
168
  $.$mol_charset_ucf_encode_to = $mol_charset_ucf_encode_to;
169
+ function $mol_charset_ucf_decode(buffer, mode = tiny_mode) {
170
+ let text = '';
171
+ let pos = 0;
172
+ let page_offset = 0;
173
+ const read_remap = () => {
174
+ let code = buffer[pos++];
175
+ if (code > 0x80)
176
+ code = ((mode + code) & 0x7F) | 0x80;
177
+ return code;
178
+ };
179
+ while (pos < buffer.length) {
180
+ let code = read_remap();
181
+ if (code < full_mode) {
182
+ if (mode === tiny_mode) {
183
+ if (code > 0x80) {
184
+ code = diacr_set[code - 0x080] | (6 << 7);
185
+ }
186
+ }
187
+ else if (!ascii_map[code]) {
188
+ if (code >= 0x80)
189
+ code = ascii_set[code - 0x80];
190
+ if (mode < tiny_mode)
191
+ code |= read_remap() << 7;
192
+ if (mode === full_mode)
193
+ code |= read_remap() << 14;
194
+ code += page_offset;
195
+ }
196
+ text += String.fromCodePoint(code);
197
+ }
198
+ else if (code >= tiny_mode) {
199
+ mode = code;
200
+ page_offset = (mode - tiny_mode) << 7;
201
+ }
202
+ else if (code === full_mode) {
203
+ mode = code;
204
+ page_offset = 0;
205
+ }
206
+ else {
207
+ mode = code;
208
+ page_offset = ((mode - wide_mode) << 14) + wide_offset;
209
+ }
210
+ }
211
+ return text;
212
+ }
213
+ $.$mol_charset_ucf_decode = $mol_charset_ucf_decode;
131
214
  })($ || ($ = {}));
132
215
 
133
216
  ;
@@ -156,45 +239,6 @@ var $;
156
239
  $.$mol_bigint_decode = $mol_bigint_decode;
157
240
  })($ || ($ = {}));
158
241
 
159
- ;
160
- "use strict";
161
- var $;
162
- (function ($) {
163
- const fast_char = `0123456789.,:;()?!-'" \n`;
164
- function $mol_charset_ucf_decode(buffer, mode = 0x9C) {
165
- let text = '';
166
- let pos = 0;
167
- let page_offset = 0;
168
- while (pos < buffer.length) {
169
- let code = buffer[pos++];
170
- if (code < 0x80) {
171
- if (mode < 0x9C)
172
- code |= buffer[pos++] << 7;
173
- if (mode === 0x97)
174
- code |= buffer[pos++] << 15;
175
- text += String.fromCodePoint(page_offset + code);
176
- }
177
- else if (code < 0x97) {
178
- text += fast_char[code - 0x80];
179
- }
180
- else if (code >= 0x9C) {
181
- mode = code;
182
- page_offset = (mode - 0x9C) << 7;
183
- }
184
- else if (code === 0x97) {
185
- mode = code;
186
- page_offset = 0;
187
- }
188
- else {
189
- mode = code;
190
- page_offset = ((mode - 0x98) << 15) + 0x20_00;
191
- }
192
- }
193
- return text;
194
- }
195
- $.$mol_charset_ucf_decode = $mol_charset_ucf_decode;
196
- })($ || ($ = {}));
197
-
198
242
  ;
199
243
  "use strict";
200
244
  var $;
package/node.test.js CHANGED
@@ -68,57 +68,140 @@ var $;
68
68
  "use strict";
69
69
  var $;
70
70
  (function ($) {
71
+ const ascii_set = [...`0123456789.,:;()'"- \n`].map(c => c.charCodeAt(0));
72
+ const ascii_map = new Array(0x80).fill(0);
73
+ for (let i = 0; i < ascii_set.length; ++i)
74
+ ascii_map[ascii_set[i]] = i | 0x80;
75
+ const diacr_set = [
76
+ 0x00, 0x01, 0x0F, 0x0B, 0x07, 0x08, 0x12, 0x13,
77
+ 0x02, 0x0C, 0x06, 0x11, 0x03, 0x09, 0x0A, 0x04,
78
+ 0x28, 0x31, 0x27, 0x26, 0x23,
79
+ ];
80
+ const diacr_map = new Array(0x80).fill(0);
81
+ for (let i = 0; i < diacr_set.length; ++i)
82
+ diacr_map[diacr_set[i]] = i | 0x80;
83
+ const wide_offset = 0x0E_00;
84
+ const wide_limit = 128 * 128 * 8 + wide_offset;
85
+ const tine_limit = 128 * 98;
86
+ const full_mode = 0x95;
87
+ const wide_mode = 0x96;
88
+ const tiny_mode = 0x9E;
71
89
  function $mol_charset_ucf_encode(str) {
72
90
  const buf = $mol_charset_buffer(str.length * 3);
73
91
  return buf.slice(0, $mol_charset_ucf_encode_to(str, buf));
74
92
  }
75
93
  $.$mol_charset_ucf_encode = $mol_charset_ucf_encode;
76
- const fast_char = `0123456789.,:;()?!-'" \n`;
77
- const fast_map = new Array(0x80).fill(0);
78
- for (let i = 0; i < fast_char.length; ++i)
79
- fast_map[fast_char[i].charCodeAt(0)] = i | 0x80;
80
94
  function $mol_charset_ucf_encode_to(str, buf, from = 0) {
81
95
  let pos = from;
82
- let mode = 0x9C;
96
+ let mode = tiny_mode;
97
+ const write_high = (code) => {
98
+ buf[pos++] = ((code + 128 - mode) & 0x7F) | 0x80;
99
+ };
100
+ const write_remap = (code) => {
101
+ const fast = ascii_map[code];
102
+ if (fast)
103
+ write_high(fast);
104
+ else
105
+ buf[pos++] = code;
106
+ };
107
+ const write_mode = (m) => {
108
+ write_high(m);
109
+ mode = m;
110
+ };
83
111
  for (let i = 0; i < str.length; i++) {
84
112
  let code = str.charCodeAt(i);
85
113
  if (code >= 0xd800 && code < 0xe000)
86
114
  code = ((code - 0xd800) << 10) + str.charCodeAt(++i) + 0x2400;
87
115
  if (code < 0x80) {
88
- if (mode !== 0x9C) {
89
- const fast = fast_map[code];
90
- if (fast)
91
- code = fast;
92
- else
93
- buf[pos++] = mode = 0x9C;
116
+ if (mode !== tiny_mode) {
117
+ const fast = ascii_map[code];
118
+ if (!fast)
119
+ write_mode(tiny_mode);
94
120
  }
95
121
  buf[pos++] = code;
96
122
  }
97
- else if (code < 0x32_00) {
98
- const page = (code >> 7) + 0x9C;
123
+ else if (code < tine_limit) {
124
+ const page = (code >> 7) + tiny_mode;
125
+ code &= 0x7F;
126
+ if (page === 164) {
127
+ const fast = diacr_map[code];
128
+ if (fast) {
129
+ if (mode !== tiny_mode)
130
+ write_mode(tiny_mode);
131
+ write_high(fast);
132
+ continue;
133
+ }
134
+ }
99
135
  if (mode !== page)
100
- buf[pos++] = mode = page;
101
- buf[pos++] = code & 0x7F;
136
+ write_mode(page);
137
+ write_remap(code);
102
138
  }
103
- else if (code < 0x04_20_00) {
104
- code -= 0x2000;
105
- const page = (code >> 15) + 0x98;
139
+ else if (code < wide_limit) {
140
+ code -= wide_offset;
141
+ const page = (code >> 14) + wide_mode;
106
142
  if (mode !== page)
107
- buf[pos++] = mode = page;
108
- buf[pos++] = code & 0x7F;
109
- buf[pos++] = code >> 7;
143
+ write_mode(page);
144
+ write_remap(code & 0x7F);
145
+ write_remap((code >> 7) & 0x7F);
110
146
  }
111
147
  else {
112
- if (mode !== 0x97)
113
- buf[pos++] = mode = 0x97;
114
- buf[pos++] = code & 0x7F;
115
- buf[pos++] = code >> 7;
116
- buf[pos++] = code >> 15;
148
+ if (mode !== full_mode)
149
+ write_mode(full_mode);
150
+ write_remap(code & 0x7F);
151
+ write_remap((code >> 7) & 0x7F);
152
+ write_remap(code >> 14);
117
153
  }
118
154
  }
155
+ if (mode !== tiny_mode)
156
+ write_mode(tiny_mode);
119
157
  return pos - from;
120
158
  }
121
159
  $.$mol_charset_ucf_encode_to = $mol_charset_ucf_encode_to;
160
+ function $mol_charset_ucf_decode(buffer, mode = tiny_mode) {
161
+ let text = '';
162
+ let pos = 0;
163
+ let page_offset = 0;
164
+ const read_remap = () => {
165
+ let code = buffer[pos++];
166
+ if (code > 0x80)
167
+ code = ((mode + code) & 0x7F) | 0x80;
168
+ return code;
169
+ };
170
+ while (pos < buffer.length) {
171
+ let code = read_remap();
172
+ if (code < full_mode) {
173
+ if (mode === tiny_mode) {
174
+ if (code > 0x80) {
175
+ code = diacr_set[code - 0x080] | (6 << 7);
176
+ }
177
+ }
178
+ else if (!ascii_map[code]) {
179
+ if (code >= 0x80)
180
+ code = ascii_set[code - 0x80];
181
+ if (mode < tiny_mode)
182
+ code |= read_remap() << 7;
183
+ if (mode === full_mode)
184
+ code |= read_remap() << 14;
185
+ code += page_offset;
186
+ }
187
+ text += String.fromCodePoint(code);
188
+ }
189
+ else if (code >= tiny_mode) {
190
+ mode = code;
191
+ page_offset = (mode - tiny_mode) << 7;
192
+ }
193
+ else if (code === full_mode) {
194
+ mode = code;
195
+ page_offset = 0;
196
+ }
197
+ else {
198
+ mode = code;
199
+ page_offset = ((mode - wide_mode) << 14) + wide_offset;
200
+ }
201
+ }
202
+ return text;
203
+ }
204
+ $.$mol_charset_ucf_decode = $mol_charset_ucf_decode;
122
205
  })($ || ($ = {}));
123
206
 
124
207
  ;
@@ -147,45 +230,6 @@ var $;
147
230
  $.$mol_bigint_decode = $mol_bigint_decode;
148
231
  })($ || ($ = {}));
149
232
 
150
- ;
151
- "use strict";
152
- var $;
153
- (function ($) {
154
- const fast_char = `0123456789.,:;()?!-'" \n`;
155
- function $mol_charset_ucf_decode(buffer, mode = 0x9C) {
156
- let text = '';
157
- let pos = 0;
158
- let page_offset = 0;
159
- while (pos < buffer.length) {
160
- let code = buffer[pos++];
161
- if (code < 0x80) {
162
- if (mode < 0x9C)
163
- code |= buffer[pos++] << 7;
164
- if (mode === 0x97)
165
- code |= buffer[pos++] << 15;
166
- text += String.fromCodePoint(page_offset + code);
167
- }
168
- else if (code < 0x97) {
169
- text += fast_char[code - 0x80];
170
- }
171
- else if (code >= 0x9C) {
172
- mode = code;
173
- page_offset = (mode - 0x9C) << 7;
174
- }
175
- else if (code === 0x97) {
176
- mode = code;
177
- page_offset = 0;
178
- }
179
- else {
180
- mode = code;
181
- page_offset = ((mode - 0x98) << 15) + 0x20_00;
182
- }
183
- }
184
- return text;
185
- }
186
- $.$mol_charset_ucf_decode = $mol_charset_ucf_decode;
187
- })($ || ($ = {}));
188
-
189
233
  ;
190
234
  "use strict";
191
235
  var $;
@@ -4479,15 +4523,46 @@ var $;
4479
4523
  (function ($_1) {
4480
4524
  var $$;
4481
4525
  (function ($$) {
4526
+ function check(text, bytes) {
4527
+ const ideal = new Uint8Array(bytes);
4528
+ const actual = $mol_charset_ucf_encode(text);
4529
+ $mol_assert_equal(actual, ideal);
4530
+ $mol_assert_equal($mol_charset_ucf_decode(actual), text);
4531
+ }
4482
4532
  $mol_test({
4483
- "Complex UCF encoding"($) {
4484
- $mol_assert_equal($mol_charset_ucf_encode('hi мир, 美しい 世界 🏴‍☠\t\n'), new Uint8Array([
4485
- 0x68, 0x69, 0x20,
4486
- 0xA4, 0x3C, 0x38, 0x40, 0x8B, 0x95,
4487
- 0x98, 0x0E, 0xBF, 0xFC, 0x57, 0x44, 0x95,
4488
- 0x98, 0x16, 0x5C, 0x4C, 0xAA, 0x95,
4489
- 0x9B, 0x74, 0xA7, 0xDC, 0x0D, 0xE8, 0x20, 0x9C, 0x09, 0x0A,
4490
- ]));
4533
+ "Full ASCII compatible"($) {
4534
+ check('hi', [0x68, 0x69]);
4535
+ },
4536
+ "1B ASCII with diacritic"($) {
4537
+ check('allo\u0302', [0x61, 0x6C, 0x6C, 0x6F, 0xEA]);
4538
+ },
4539
+ "1B Cyrillic"($) {
4540
+ check('мир', [0x88, 0x3C, 0xE2, 0x40, 0xF8]);
4541
+ },
4542
+ "1B Cyrillic with nummbers and punctuation"($) {
4543
+ check('м.1', [0x88, 0x3C, 0x2E, 0x31, 0xF8]);
4544
+ },
4545
+ "2B Kanji"($) {
4546
+ check('美', [0xF9, 0x0E, 0x63, 0x87]);
4547
+ },
4548
+ "3B rare Kanji"($) {
4549
+ check('𲎯', [0xF7, 0x2F, 0x47, 0x0C, 0x89]);
4550
+ },
4551
+ "1B Kana"($) {
4552
+ check('しい', [0xE0, 0x57, 0x44, 0xA0]);
4553
+ },
4554
+ "2B Emoji with 1B modifiers"($) {
4555
+ check('🏴', [0xFF, 0x74, 0x4B, 0x81]);
4556
+ check('🏴‍☠', [0xFF, 0x74, 0x4B, 0xC1, 0x0D, 0x8C, 0xA9, 0xB4]);
4557
+ },
4558
+ "Mixed scripts"($) {
4559
+ check('allô 美しい мир, 🏴‍☠\n', [
4560
+ 0x61, 0x6C, 0x6C, 0x6F, 0xEA, 0x20,
4561
+ 0xF9, 0x0E, 0x63, 0xE7, 0x57, 0x44, 0x20,
4562
+ 0xA8, 0x3C, 0xE2, 0x40, 0x2C, 0x20,
4563
+ 0xF7, 0x74, 0x4B, 0xC1, 0x0D, 0x8C, 0xA9, 0x0A,
4564
+ 0xB4,
4565
+ ]);
4491
4566
  },
4492
4567
  });
4493
4568
  })($$ = $_1.$$ || ($_1.$$ = {}));
@@ -4533,26 +4608,6 @@ var $;
4533
4608
  })($$ = $_1.$$ || ($_1.$$ = {}));
4534
4609
  })($ || ($ = {}));
4535
4610
 
4536
- ;
4537
- "use strict";
4538
- var $;
4539
- (function ($_1) {
4540
- var $$;
4541
- (function ($$) {
4542
- $mol_test({
4543
- "Complex UCF eecoding"($) {
4544
- $mol_assert_equal('hi мир, 美しい 世界 🏴‍☠\t\n', $mol_charset_ucf_decode(new Uint8Array([
4545
- 0x68, 0x69, 0x20,
4546
- 0xA4, 0x3C, 0x38, 0x40, 0x8B, 0x95,
4547
- 0x98, 0x0E, 0xBF, 0xFC, 0x57, 0x44, 0x95,
4548
- 0x98, 0x16, 0x5C, 0x4C, 0xAA, 0x95,
4549
- 0x9B, 0x74, 0xA7, 0xDC, 0x0D, 0xE8, 0x20, 0x9C, 0x09, 0x0A,
4550
- ])));
4551
- },
4552
- });
4553
- })($$ = $_1.$$ || ($_1.$$ = {}));
4554
- })($ || ($ = {}));
4555
-
4556
4611
  ;
4557
4612
  "use strict";
4558
4613
  var $;
@@ -4997,11 +5052,11 @@ var $;
4997
5052
  },
4998
5053
  "vary pack text"($) {
4999
5054
  check(['foo'], [text | 3, ...str('foo')]);
5000
- check(['абв'], [text | 4, ...str('абв')]);
5055
+ check(['абв'], [text | 5, ...str('абв')]);
5001
5056
  const long_lat = 'abcdefghijklmnopqrst';
5002
5057
  check([long_lat], [text | L1, 20, ...str(long_lat)]);
5003
5058
  const long_cyr = 'абвгдеёжзийклмнопрст';
5004
- check([long_cyr], [text | L1, 21, ...str(long_cyr)]);
5059
+ check([long_cyr], [text | L1, 22, ...str(long_cyr)]);
5005
5060
  },
5006
5061
  "vary pack dedup text"($) {
5007
5062
  check([["f", "f"]], [list | 2, text | 1, ...str('f'), link | 0]);