@axiom-lattice/client-sdk 1.0.43 → 1.0.45

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.
@@ -0,0 +1,2897 @@
1
+ "use strict";
2
+ // This is free and unencumbered software released into the public domain.
3
+ // See LICENSE.md for more information.
4
+ /**
5
+ * @fileoverview Global |this| required for resolving indexes in node.
6
+ * @suppress {globalThis}
7
+ */
8
+ (function (global) {
9
+ 'use strict';
10
+ // If we're in node require encoding-indexes and attach it to the global.
11
+ if (typeof module !== "undefined" && module.exports &&
12
+ !global["encoding-indexes"]) {
13
+ global["encoding-indexes"] =
14
+ require("./encoding-indexes.js")["encoding-indexes"];
15
+ }
16
+ //
17
+ // Utilities
18
+ //
19
+ /**
20
+ * @param {number} a The number to test.
21
+ * @param {number} min The minimum value in the range, inclusive.
22
+ * @param {number} max The maximum value in the range, inclusive.
23
+ * @return {boolean} True if a >= min and a <= max.
24
+ */
25
+ function inRange(a, min, max) {
26
+ return min <= a && a <= max;
27
+ }
28
+ /**
29
+ * @param {!Array.<*>} array The array to check.
30
+ * @param {*} item The item to look for in the array.
31
+ * @return {boolean} True if the item appears in the array.
32
+ */
33
+ function includes(array, item) {
34
+ return array.indexOf(item) !== -1;
35
+ }
36
+ var floor = Math.floor;
37
+ /**
38
+ * @param {*} o
39
+ * @return {Object}
40
+ */
41
+ function ToDictionary(o) {
42
+ if (o === undefined)
43
+ return {};
44
+ if (o === Object(o))
45
+ return o;
46
+ throw TypeError('Could not convert argument to dictionary');
47
+ }
48
+ /**
49
+ * @param {string} string Input string of UTF-16 code units.
50
+ * @return {!Array.<number>} Code points.
51
+ */
52
+ function stringToCodePoints(string) {
53
+ // https://heycam.github.io/webidl/#dfn-obtain-unicode
54
+ // 1. Let S be the DOMString value.
55
+ var s = String(string);
56
+ // 2. Let n be the length of S.
57
+ var n = s.length;
58
+ // 3. Initialize i to 0.
59
+ var i = 0;
60
+ // 4. Initialize U to be an empty sequence of Unicode characters.
61
+ var u = [];
62
+ // 5. While i < n:
63
+ while (i < n) {
64
+ // 1. Let c be the code unit in S at index i.
65
+ var c = s.charCodeAt(i);
66
+ // 2. Depending on the value of c:
67
+ // c < 0xD800 or c > 0xDFFF
68
+ if (c < 0xD800 || c > 0xDFFF) {
69
+ // Append to U the Unicode character with code point c.
70
+ u.push(c);
71
+ }
72
+ // 0xDC00 ≤ c ≤ 0xDFFF
73
+ else if (0xDC00 <= c && c <= 0xDFFF) {
74
+ // Append to U a U+FFFD REPLACEMENT CHARACTER.
75
+ u.push(0xFFFD);
76
+ }
77
+ // 0xD800 ≤ c ≤ 0xDBFF
78
+ else if (0xD800 <= c && c <= 0xDBFF) {
79
+ // 1. If i = n−1, then append to U a U+FFFD REPLACEMENT
80
+ // CHARACTER.
81
+ if (i === n - 1) {
82
+ u.push(0xFFFD);
83
+ }
84
+ // 2. Otherwise, i < n−1:
85
+ else {
86
+ // 1. Let d be the code unit in S at index i+1.
87
+ var d = s.charCodeAt(i + 1);
88
+ // 2. If 0xDC00 ≤ d ≤ 0xDFFF, then:
89
+ if (0xDC00 <= d && d <= 0xDFFF) {
90
+ // 1. Let a be c & 0x3FF.
91
+ var a = c & 0x3FF;
92
+ // 2. Let b be d & 0x3FF.
93
+ var b = d & 0x3FF;
94
+ // 3. Append to U the Unicode character with code point
95
+ // 2^16+2^10*a+b.
96
+ u.push(0x10000 + (a << 10) + b);
97
+ // 4. Set i to i+1.
98
+ i += 1;
99
+ }
100
+ // 3. Otherwise, d < 0xDC00 or d > 0xDFFF. Append to U a
101
+ // U+FFFD REPLACEMENT CHARACTER.
102
+ else {
103
+ u.push(0xFFFD);
104
+ }
105
+ }
106
+ }
107
+ // 3. Set i to i+1.
108
+ i += 1;
109
+ }
110
+ // 6. Return U.
111
+ return u;
112
+ }
113
+ /**
114
+ * @param {!Array.<number>} code_points Array of code points.
115
+ * @return {string} string String of UTF-16 code units.
116
+ */
117
+ function codePointsToString(code_points) {
118
+ var s = '';
119
+ for (var i = 0; i < code_points.length; ++i) {
120
+ var cp = code_points[i];
121
+ if (cp <= 0xFFFF) {
122
+ s += String.fromCharCode(cp);
123
+ }
124
+ else {
125
+ cp -= 0x10000;
126
+ s += String.fromCharCode((cp >> 10) + 0xD800, (cp & 0x3FF) + 0xDC00);
127
+ }
128
+ }
129
+ return s;
130
+ }
131
+ //
132
+ // Implementation of Encoding specification
133
+ // https://encoding.spec.whatwg.org/
134
+ //
135
+ //
136
+ // 4. Terminology
137
+ //
138
+ /**
139
+ * An ASCII byte is a byte in the range 0x00 to 0x7F, inclusive.
140
+ * @param {number} a The number to test.
141
+ * @return {boolean} True if a is in the range 0x00 to 0x7F, inclusive.
142
+ */
143
+ function isASCIIByte(a) {
144
+ return 0x00 <= a && a <= 0x7F;
145
+ }
146
+ /**
147
+ * An ASCII code point is a code point in the range U+0000 to
148
+ * U+007F, inclusive.
149
+ */
150
+ var isASCIICodePoint = isASCIIByte;
151
+ /**
152
+ * End-of-stream is a special token that signifies no more tokens
153
+ * are in the stream.
154
+ * @const
155
+ */ var end_of_stream = -1;
156
+ /**
157
+ * A stream represents an ordered sequence of tokens.
158
+ *
159
+ * @constructor
160
+ * @param {!(Array.<number>|Uint8Array)} tokens Array of tokens that provide
161
+ * the stream.
162
+ */
163
+ function Stream(tokens) {
164
+ /** @type {!Array.<number>} */
165
+ this.tokens = [].slice.call(tokens);
166
+ // Reversed as push/pop is more efficient than shift/unshift.
167
+ this.tokens.reverse();
168
+ }
169
+ Stream.prototype = {
170
+ /**
171
+ * @return {boolean} True if end-of-stream has been hit.
172
+ */
173
+ endOfStream: function () {
174
+ return !this.tokens.length;
175
+ },
176
+ /**
177
+ * When a token is read from a stream, the first token in the
178
+ * stream must be returned and subsequently removed, and
179
+ * end-of-stream must be returned otherwise.
180
+ *
181
+ * @return {number} Get the next token from the stream, or
182
+ * end_of_stream.
183
+ */
184
+ read: function () {
185
+ if (!this.tokens.length)
186
+ return end_of_stream;
187
+ return this.tokens.pop();
188
+ },
189
+ /**
190
+ * When one or more tokens are prepended to a stream, those tokens
191
+ * must be inserted, in given order, before the first token in the
192
+ * stream.
193
+ *
194
+ * @param {(number|!Array.<number>)} token The token(s) to prepend to the
195
+ * stream.
196
+ */
197
+ prepend: function (token) {
198
+ if (Array.isArray(token)) {
199
+ var tokens = /**@type {!Array.<number>}*/ (token);
200
+ while (tokens.length)
201
+ this.tokens.push(tokens.pop());
202
+ }
203
+ else {
204
+ this.tokens.push(token);
205
+ }
206
+ },
207
+ /**
208
+ * When one or more tokens are pushed to a stream, those tokens
209
+ * must be inserted, in given order, after the last token in the
210
+ * stream.
211
+ *
212
+ * @param {(number|!Array.<number>)} token The tokens(s) to push to the
213
+ * stream.
214
+ */
215
+ push: function (token) {
216
+ if (Array.isArray(token)) {
217
+ var tokens = /**@type {!Array.<number>}*/ (token);
218
+ while (tokens.length)
219
+ this.tokens.unshift(tokens.shift());
220
+ }
221
+ else {
222
+ this.tokens.unshift(token);
223
+ }
224
+ }
225
+ };
226
+ //
227
+ // 5. Encodings
228
+ //
229
+ // 5.1 Encoders and decoders
230
+ /** @const */
231
+ var finished = -1;
232
+ /**
233
+ * @param {boolean} fatal If true, decoding errors raise an exception.
234
+ * @param {number=} opt_code_point Override the standard fallback code point.
235
+ * @return {number} The code point to insert on a decoding error.
236
+ */
237
+ function decoderError(fatal, opt_code_point) {
238
+ if (fatal)
239
+ throw TypeError('Decoder error');
240
+ return opt_code_point || 0xFFFD;
241
+ }
242
+ /**
243
+ * @param {number} code_point The code point that could not be encoded.
244
+ * @return {number} Always throws, no value is actually returned.
245
+ */
246
+ function encoderError(code_point) {
247
+ throw TypeError('The code point ' + code_point + ' could not be encoded.');
248
+ }
249
+ /** @interface */
250
+ function Decoder() { }
251
+ Decoder.prototype = {
252
+ /**
253
+ * @param {Stream} stream The stream of bytes being decoded.
254
+ * @param {number} bite The next byte read from the stream.
255
+ * @return {?(number|!Array.<number>)} The next code point(s)
256
+ * decoded, or null if not enough data exists in the input
257
+ * stream to decode a complete code point, or |finished|.
258
+ */
259
+ handler: function (stream, bite) { }
260
+ };
261
+ /** @interface */
262
+ function Encoder() { }
263
+ Encoder.prototype = {
264
+ /**
265
+ * @param {Stream} stream The stream of code points being encoded.
266
+ * @param {number} code_point Next code point read from the stream.
267
+ * @return {(number|!Array.<number>)} Byte(s) to emit, or |finished|.
268
+ */
269
+ handler: function (stream, code_point) { }
270
+ };
271
+ // 5.2 Names and labels
272
+ // TODO: Define @typedef for Encoding: {name:string,labels:Array.<string>}
273
+ // https://github.com/google/closure-compiler/issues/247
274
+ /**
275
+ * @param {string} label The encoding label.
276
+ * @return {?{name:string,labels:Array.<string>}}
277
+ */
278
+ function getEncoding(label) {
279
+ // 1. Remove any leading and trailing ASCII whitespace from label.
280
+ label = String(label).trim().toLowerCase();
281
+ // 2. If label is an ASCII case-insensitive match for any of the
282
+ // labels listed in the table below, return the corresponding
283
+ // encoding, and failure otherwise.
284
+ if (Object.prototype.hasOwnProperty.call(label_to_encoding, label)) {
285
+ return label_to_encoding[label];
286
+ }
287
+ return null;
288
+ }
289
+ /**
290
+ * Encodings table: https://encoding.spec.whatwg.org/encodings.json
291
+ * @const
292
+ * @type {!Array.<{
293
+ * heading: string,
294
+ * encodings: Array.<{name:string,labels:Array.<string>}>
295
+ * }>}
296
+ */
297
+ var encodings = [
298
+ {
299
+ "encodings": [
300
+ {
301
+ "labels": [
302
+ "unicode-1-1-utf-8",
303
+ "utf-8",
304
+ "utf8"
305
+ ],
306
+ "name": "UTF-8"
307
+ }
308
+ ],
309
+ "heading": "The Encoding"
310
+ },
311
+ {
312
+ "encodings": [
313
+ {
314
+ "labels": [
315
+ "866",
316
+ "cp866",
317
+ "csibm866",
318
+ "ibm866"
319
+ ],
320
+ "name": "IBM866"
321
+ },
322
+ {
323
+ "labels": [
324
+ "csisolatin2",
325
+ "iso-8859-2",
326
+ "iso-ir-101",
327
+ "iso8859-2",
328
+ "iso88592",
329
+ "iso_8859-2",
330
+ "iso_8859-2:1987",
331
+ "l2",
332
+ "latin2"
333
+ ],
334
+ "name": "ISO-8859-2"
335
+ },
336
+ {
337
+ "labels": [
338
+ "csisolatin3",
339
+ "iso-8859-3",
340
+ "iso-ir-109",
341
+ "iso8859-3",
342
+ "iso88593",
343
+ "iso_8859-3",
344
+ "iso_8859-3:1988",
345
+ "l3",
346
+ "latin3"
347
+ ],
348
+ "name": "ISO-8859-3"
349
+ },
350
+ {
351
+ "labels": [
352
+ "csisolatin4",
353
+ "iso-8859-4",
354
+ "iso-ir-110",
355
+ "iso8859-4",
356
+ "iso88594",
357
+ "iso_8859-4",
358
+ "iso_8859-4:1988",
359
+ "l4",
360
+ "latin4"
361
+ ],
362
+ "name": "ISO-8859-4"
363
+ },
364
+ {
365
+ "labels": [
366
+ "csisolatincyrillic",
367
+ "cyrillic",
368
+ "iso-8859-5",
369
+ "iso-ir-144",
370
+ "iso8859-5",
371
+ "iso88595",
372
+ "iso_8859-5",
373
+ "iso_8859-5:1988"
374
+ ],
375
+ "name": "ISO-8859-5"
376
+ },
377
+ {
378
+ "labels": [
379
+ "arabic",
380
+ "asmo-708",
381
+ "csiso88596e",
382
+ "csiso88596i",
383
+ "csisolatinarabic",
384
+ "ecma-114",
385
+ "iso-8859-6",
386
+ "iso-8859-6-e",
387
+ "iso-8859-6-i",
388
+ "iso-ir-127",
389
+ "iso8859-6",
390
+ "iso88596",
391
+ "iso_8859-6",
392
+ "iso_8859-6:1987"
393
+ ],
394
+ "name": "ISO-8859-6"
395
+ },
396
+ {
397
+ "labels": [
398
+ "csisolatingreek",
399
+ "ecma-118",
400
+ "elot_928",
401
+ "greek",
402
+ "greek8",
403
+ "iso-8859-7",
404
+ "iso-ir-126",
405
+ "iso8859-7",
406
+ "iso88597",
407
+ "iso_8859-7",
408
+ "iso_8859-7:1987",
409
+ "sun_eu_greek"
410
+ ],
411
+ "name": "ISO-8859-7"
412
+ },
413
+ {
414
+ "labels": [
415
+ "csiso88598e",
416
+ "csisolatinhebrew",
417
+ "hebrew",
418
+ "iso-8859-8",
419
+ "iso-8859-8-e",
420
+ "iso-ir-138",
421
+ "iso8859-8",
422
+ "iso88598",
423
+ "iso_8859-8",
424
+ "iso_8859-8:1988",
425
+ "visual"
426
+ ],
427
+ "name": "ISO-8859-8"
428
+ },
429
+ {
430
+ "labels": [
431
+ "csiso88598i",
432
+ "iso-8859-8-i",
433
+ "logical"
434
+ ],
435
+ "name": "ISO-8859-8-I"
436
+ },
437
+ {
438
+ "labels": [
439
+ "csisolatin6",
440
+ "iso-8859-10",
441
+ "iso-ir-157",
442
+ "iso8859-10",
443
+ "iso885910",
444
+ "l6",
445
+ "latin6"
446
+ ],
447
+ "name": "ISO-8859-10"
448
+ },
449
+ {
450
+ "labels": [
451
+ "iso-8859-13",
452
+ "iso8859-13",
453
+ "iso885913"
454
+ ],
455
+ "name": "ISO-8859-13"
456
+ },
457
+ {
458
+ "labels": [
459
+ "iso-8859-14",
460
+ "iso8859-14",
461
+ "iso885914"
462
+ ],
463
+ "name": "ISO-8859-14"
464
+ },
465
+ {
466
+ "labels": [
467
+ "csisolatin9",
468
+ "iso-8859-15",
469
+ "iso8859-15",
470
+ "iso885915",
471
+ "iso_8859-15",
472
+ "l9"
473
+ ],
474
+ "name": "ISO-8859-15"
475
+ },
476
+ {
477
+ "labels": [
478
+ "iso-8859-16"
479
+ ],
480
+ "name": "ISO-8859-16"
481
+ },
482
+ {
483
+ "labels": [
484
+ "cskoi8r",
485
+ "koi",
486
+ "koi8",
487
+ "koi8-r",
488
+ "koi8_r"
489
+ ],
490
+ "name": "KOI8-R"
491
+ },
492
+ {
493
+ "labels": [
494
+ "koi8-ru",
495
+ "koi8-u"
496
+ ],
497
+ "name": "KOI8-U"
498
+ },
499
+ {
500
+ "labels": [
501
+ "csmacintosh",
502
+ "mac",
503
+ "macintosh",
504
+ "x-mac-roman"
505
+ ],
506
+ "name": "macintosh"
507
+ },
508
+ {
509
+ "labels": [
510
+ "dos-874",
511
+ "iso-8859-11",
512
+ "iso8859-11",
513
+ "iso885911",
514
+ "tis-620",
515
+ "windows-874"
516
+ ],
517
+ "name": "windows-874"
518
+ },
519
+ {
520
+ "labels": [
521
+ "cp1250",
522
+ "windows-1250",
523
+ "x-cp1250"
524
+ ],
525
+ "name": "windows-1250"
526
+ },
527
+ {
528
+ "labels": [
529
+ "cp1251",
530
+ "windows-1251",
531
+ "x-cp1251"
532
+ ],
533
+ "name": "windows-1251"
534
+ },
535
+ {
536
+ "labels": [
537
+ "ansi_x3.4-1968",
538
+ "ascii",
539
+ "cp1252",
540
+ "cp819",
541
+ "csisolatin1",
542
+ "ibm819",
543
+ "iso-8859-1",
544
+ "iso-ir-100",
545
+ "iso8859-1",
546
+ "iso88591",
547
+ "iso_8859-1",
548
+ "iso_8859-1:1987",
549
+ "l1",
550
+ "latin1",
551
+ "us-ascii",
552
+ "windows-1252",
553
+ "x-cp1252"
554
+ ],
555
+ "name": "windows-1252"
556
+ },
557
+ {
558
+ "labels": [
559
+ "cp1253",
560
+ "windows-1253",
561
+ "x-cp1253"
562
+ ],
563
+ "name": "windows-1253"
564
+ },
565
+ {
566
+ "labels": [
567
+ "cp1254",
568
+ "csisolatin5",
569
+ "iso-8859-9",
570
+ "iso-ir-148",
571
+ "iso8859-9",
572
+ "iso88599",
573
+ "iso_8859-9",
574
+ "iso_8859-9:1989",
575
+ "l5",
576
+ "latin5",
577
+ "windows-1254",
578
+ "x-cp1254"
579
+ ],
580
+ "name": "windows-1254"
581
+ },
582
+ {
583
+ "labels": [
584
+ "cp1255",
585
+ "windows-1255",
586
+ "x-cp1255"
587
+ ],
588
+ "name": "windows-1255"
589
+ },
590
+ {
591
+ "labels": [
592
+ "cp1256",
593
+ "windows-1256",
594
+ "x-cp1256"
595
+ ],
596
+ "name": "windows-1256"
597
+ },
598
+ {
599
+ "labels": [
600
+ "cp1257",
601
+ "windows-1257",
602
+ "x-cp1257"
603
+ ],
604
+ "name": "windows-1257"
605
+ },
606
+ {
607
+ "labels": [
608
+ "cp1258",
609
+ "windows-1258",
610
+ "x-cp1258"
611
+ ],
612
+ "name": "windows-1258"
613
+ },
614
+ {
615
+ "labels": [
616
+ "x-mac-cyrillic",
617
+ "x-mac-ukrainian"
618
+ ],
619
+ "name": "x-mac-cyrillic"
620
+ }
621
+ ],
622
+ "heading": "Legacy single-byte encodings"
623
+ },
624
+ {
625
+ "encodings": [
626
+ {
627
+ "labels": [
628
+ "chinese",
629
+ "csgb2312",
630
+ "csiso58gb231280",
631
+ "gb2312",
632
+ "gb_2312",
633
+ "gb_2312-80",
634
+ "gbk",
635
+ "iso-ir-58",
636
+ "x-gbk"
637
+ ],
638
+ "name": "GBK"
639
+ },
640
+ {
641
+ "labels": [
642
+ "gb18030"
643
+ ],
644
+ "name": "gb18030"
645
+ }
646
+ ],
647
+ "heading": "Legacy multi-byte Chinese (simplified) encodings"
648
+ },
649
+ {
650
+ "encodings": [
651
+ {
652
+ "labels": [
653
+ "big5",
654
+ "big5-hkscs",
655
+ "cn-big5",
656
+ "csbig5",
657
+ "x-x-big5"
658
+ ],
659
+ "name": "Big5"
660
+ }
661
+ ],
662
+ "heading": "Legacy multi-byte Chinese (traditional) encodings"
663
+ },
664
+ {
665
+ "encodings": [
666
+ {
667
+ "labels": [
668
+ "cseucpkdfmtjapanese",
669
+ "euc-jp",
670
+ "x-euc-jp"
671
+ ],
672
+ "name": "EUC-JP"
673
+ },
674
+ {
675
+ "labels": [
676
+ "csiso2022jp",
677
+ "iso-2022-jp"
678
+ ],
679
+ "name": "ISO-2022-JP"
680
+ },
681
+ {
682
+ "labels": [
683
+ "csshiftjis",
684
+ "ms932",
685
+ "ms_kanji",
686
+ "shift-jis",
687
+ "shift_jis",
688
+ "sjis",
689
+ "windows-31j",
690
+ "x-sjis"
691
+ ],
692
+ "name": "Shift_JIS"
693
+ }
694
+ ],
695
+ "heading": "Legacy multi-byte Japanese encodings"
696
+ },
697
+ {
698
+ "encodings": [
699
+ {
700
+ "labels": [
701
+ "cseuckr",
702
+ "csksc56011987",
703
+ "euc-kr",
704
+ "iso-ir-149",
705
+ "korean",
706
+ "ks_c_5601-1987",
707
+ "ks_c_5601-1989",
708
+ "ksc5601",
709
+ "ksc_5601",
710
+ "windows-949"
711
+ ],
712
+ "name": "EUC-KR"
713
+ }
714
+ ],
715
+ "heading": "Legacy multi-byte Korean encodings"
716
+ },
717
+ {
718
+ "encodings": [
719
+ {
720
+ "labels": [
721
+ "csiso2022kr",
722
+ "hz-gb-2312",
723
+ "iso-2022-cn",
724
+ "iso-2022-cn-ext",
725
+ "iso-2022-kr"
726
+ ],
727
+ "name": "replacement"
728
+ },
729
+ {
730
+ "labels": [
731
+ "utf-16be"
732
+ ],
733
+ "name": "UTF-16BE"
734
+ },
735
+ {
736
+ "labels": [
737
+ "utf-16",
738
+ "utf-16le"
739
+ ],
740
+ "name": "UTF-16LE"
741
+ },
742
+ {
743
+ "labels": [
744
+ "x-user-defined"
745
+ ],
746
+ "name": "x-user-defined"
747
+ }
748
+ ],
749
+ "heading": "Legacy miscellaneous encodings"
750
+ }
751
+ ];
752
+ // Label to encoding registry.
753
+ /** @type {Object.<string,{name:string,labels:Array.<string>}>} */
754
+ var label_to_encoding = {};
755
+ encodings.forEach(function (category) {
756
+ category.encodings.forEach(function (encoding) {
757
+ encoding.labels.forEach(function (label) {
758
+ label_to_encoding[label] = encoding;
759
+ });
760
+ });
761
+ });
762
+ // Registry of of encoder/decoder factories, by encoding name.
763
+ /** @type {Object.<string, function({fatal:boolean}): Encoder>} */
764
+ var encoders = {};
765
+ /** @type {Object.<string, function({fatal:boolean}): Decoder>} */
766
+ var decoders = {};
767
+ //
768
+ // 6. Indexes
769
+ //
770
+ /**
771
+ * @param {number} pointer The |pointer| to search for.
772
+ * @param {(!Array.<?number>|undefined)} index The |index| to search within.
773
+ * @return {?number} The code point corresponding to |pointer| in |index|,
774
+ * or null if |code point| is not in |index|.
775
+ */
776
+ function indexCodePointFor(pointer, index) {
777
+ if (!index)
778
+ return null;
779
+ return index[pointer] || null;
780
+ }
781
+ /**
782
+ * @param {number} code_point The |code point| to search for.
783
+ * @param {!Array.<?number>} index The |index| to search within.
784
+ * @return {?number} The first pointer corresponding to |code point| in
785
+ * |index|, or null if |code point| is not in |index|.
786
+ */
787
+ function indexPointerFor(code_point, index) {
788
+ var pointer = index.indexOf(code_point);
789
+ return pointer === -1 ? null : pointer;
790
+ }
791
+ /**
792
+ * @param {string} name Name of the index.
793
+ * @return {(!Array.<number>|!Array.<Array.<number>>)}
794
+ * */
795
+ function index(name) {
796
+ if (!('encoding-indexes' in global)) {
797
+ throw Error("Indexes missing." +
798
+ " Did you forget to include encoding-indexes.js first?");
799
+ }
800
+ return global['encoding-indexes'][name];
801
+ }
802
+ /**
803
+ * @param {number} pointer The |pointer| to search for in the gb18030 index.
804
+ * @return {?number} The code point corresponding to |pointer| in |index|,
805
+ * or null if |code point| is not in the gb18030 index.
806
+ */
807
+ function indexGB18030RangesCodePointFor(pointer) {
808
+ // 1. If pointer is greater than 39419 and less than 189000, or
809
+ // pointer is greater than 1237575, return null.
810
+ if ((pointer > 39419 && pointer < 189000) || (pointer > 1237575))
811
+ return null;
812
+ // 2. If pointer is 7457, return code point U+E7C7.
813
+ if (pointer === 7457)
814
+ return 0xE7C7;
815
+ // 3. Let offset be the last pointer in index gb18030 ranges that
816
+ // is equal to or less than pointer and let code point offset be
817
+ // its corresponding code point.
818
+ var offset = 0;
819
+ var code_point_offset = 0;
820
+ var idx = index('gb18030-ranges');
821
+ var i;
822
+ for (i = 0; i < idx.length; ++i) {
823
+ /** @type {!Array.<number>} */
824
+ var entry = idx[i];
825
+ if (entry[0] <= pointer) {
826
+ offset = entry[0];
827
+ code_point_offset = entry[1];
828
+ }
829
+ else {
830
+ break;
831
+ }
832
+ }
833
+ // 4. Return a code point whose value is code point offset +
834
+ // pointer − offset.
835
+ return code_point_offset + pointer - offset;
836
+ }
837
+ /**
838
+ * @param {number} code_point The |code point| to locate in the gb18030 index.
839
+ * @return {number} The first pointer corresponding to |code point| in the
840
+ * gb18030 index.
841
+ */
842
+ function indexGB18030RangesPointerFor(code_point) {
843
+ // 1. If code point is U+E7C7, return pointer 7457.
844
+ if (code_point === 0xE7C7)
845
+ return 7457;
846
+ // 2. Let offset be the last code point in index gb18030 ranges
847
+ // that is equal to or less than code point and let pointer offset
848
+ // be its corresponding pointer.
849
+ var offset = 0;
850
+ var pointer_offset = 0;
851
+ var idx = index('gb18030-ranges');
852
+ var i;
853
+ for (i = 0; i < idx.length; ++i) {
854
+ /** @type {!Array.<number>} */
855
+ var entry = idx[i];
856
+ if (entry[1] <= code_point) {
857
+ offset = entry[1];
858
+ pointer_offset = entry[0];
859
+ }
860
+ else {
861
+ break;
862
+ }
863
+ }
864
+ // 3. Return a pointer whose value is pointer offset + code point
865
+ // − offset.
866
+ return pointer_offset + code_point - offset;
867
+ }
868
+ /**
869
+ * @param {number} code_point The |code_point| to search for in the Shift_JIS
870
+ * index.
871
+ * @return {?number} The code point corresponding to |pointer| in |index|,
872
+ * or null if |code point| is not in the Shift_JIS index.
873
+ */
874
+ function indexShiftJISPointerFor(code_point) {
875
+ // 1. Let index be index jis0208 excluding all entries whose
876
+ // pointer is in the range 8272 to 8835, inclusive.
877
+ shift_jis_index = shift_jis_index ||
878
+ index('jis0208').map(function (code_point, pointer) {
879
+ return inRange(pointer, 8272, 8835) ? null : code_point;
880
+ });
881
+ var index_ = shift_jis_index;
882
+ // 2. Return the index pointer for code point in index.
883
+ return index_.indexOf(code_point);
884
+ }
885
+ var shift_jis_index;
886
+ /**
887
+ * @param {number} code_point The |code_point| to search for in the big5
888
+ * index.
889
+ * @return {?number} The code point corresponding to |pointer| in |index|,
890
+ * or null if |code point| is not in the big5 index.
891
+ */
892
+ function indexBig5PointerFor(code_point) {
893
+ // 1. Let index be index Big5 excluding all entries whose pointer
894
+ big5_index_no_hkscs = big5_index_no_hkscs ||
895
+ index('big5').map(function (code_point, pointer) {
896
+ return (pointer < (0xA1 - 0x81) * 157) ? null : code_point;
897
+ });
898
+ var index_ = big5_index_no_hkscs;
899
+ // 2. If code point is U+2550, U+255E, U+2561, U+256A, U+5341, or
900
+ // U+5345, return the last pointer corresponding to code point in
901
+ // index.
902
+ if (code_point === 0x2550 || code_point === 0x255E ||
903
+ code_point === 0x2561 || code_point === 0x256A ||
904
+ code_point === 0x5341 || code_point === 0x5345) {
905
+ return index_.lastIndexOf(code_point);
906
+ }
907
+ // 3. Return the index pointer for code point in index.
908
+ return indexPointerFor(code_point, index_);
909
+ }
910
+ var big5_index_no_hkscs;
911
+ //
912
+ // 8. API
913
+ //
914
+ /** @const */ var DEFAULT_ENCODING = 'utf-8';
915
+ // 8.1 Interface TextDecoder
916
+ /**
917
+ * @constructor
918
+ * @param {string=} label The label of the encoding;
919
+ * defaults to 'utf-8'.
920
+ * @param {Object=} options
921
+ */
922
+ function TextDecoder(label, options) {
923
+ // Web IDL conventions
924
+ if (!(this instanceof TextDecoder))
925
+ throw TypeError('Called as a function. Did you forget \'new\'?');
926
+ label = label !== undefined ? String(label) : DEFAULT_ENCODING;
927
+ options = ToDictionary(options);
928
+ // A TextDecoder object has an associated encoding, decoder,
929
+ // stream, ignore BOM flag (initially unset), BOM seen flag
930
+ // (initially unset), error mode (initially replacement), and do
931
+ // not flush flag (initially unset).
932
+ /** @private */
933
+ this._encoding = null;
934
+ /** @private @type {?Decoder} */
935
+ this._decoder = null;
936
+ /** @private @type {boolean} */
937
+ this._ignoreBOM = false;
938
+ /** @private @type {boolean} */
939
+ this._BOMseen = false;
940
+ /** @private @type {string} */
941
+ this._error_mode = 'replacement';
942
+ /** @private @type {boolean} */
943
+ this._do_not_flush = false;
944
+ // 1. Let encoding be the result of getting an encoding from
945
+ // label.
946
+ var encoding = getEncoding(label);
947
+ // 2. If encoding is failure or replacement, throw a RangeError.
948
+ if (encoding === null || encoding.name === 'replacement')
949
+ throw RangeError('Unknown encoding: ' + label);
950
+ if (!decoders[encoding.name]) {
951
+ throw Error('Decoder not present.' +
952
+ ' Did you forget to include encoding-indexes.js first?');
953
+ }
954
+ // 3. Let dec be a new TextDecoder object.
955
+ var dec = this;
956
+ // 4. Set dec's encoding to encoding.
957
+ dec._encoding = encoding;
958
+ // 5. If options's fatal member is true, set dec's error mode to
959
+ // fatal.
960
+ if (Boolean(options['fatal']))
961
+ dec._error_mode = 'fatal';
962
+ // 6. If options's ignoreBOM member is true, set dec's ignore BOM
963
+ // flag.
964
+ if (Boolean(options['ignoreBOM']))
965
+ dec._ignoreBOM = true;
966
+ // For pre-ES5 runtimes:
967
+ if (!Object.defineProperty) {
968
+ this.encoding = dec._encoding.name.toLowerCase();
969
+ this.fatal = dec._error_mode === 'fatal';
970
+ this.ignoreBOM = dec._ignoreBOM;
971
+ }
972
+ // 7. Return dec.
973
+ return dec;
974
+ }
975
+ if (Object.defineProperty) {
976
+ // The encoding attribute's getter must return encoding's name.
977
+ Object.defineProperty(TextDecoder.prototype, 'encoding', {
978
+ /** @this {TextDecoder} */
979
+ get: function () { return this._encoding.name.toLowerCase(); }
980
+ });
981
+ // The fatal attribute's getter must return true if error mode
982
+ // is fatal, and false otherwise.
983
+ Object.defineProperty(TextDecoder.prototype, 'fatal', {
984
+ /** @this {TextDecoder} */
985
+ get: function () { return this._error_mode === 'fatal'; }
986
+ });
987
+ // The ignoreBOM attribute's getter must return true if ignore
988
+ // BOM flag is set, and false otherwise.
989
+ Object.defineProperty(TextDecoder.prototype, 'ignoreBOM', {
990
+ /** @this {TextDecoder} */
991
+ get: function () { return this._ignoreBOM; }
992
+ });
993
+ }
994
+ /**
995
+ * @param {BufferSource=} input The buffer of bytes to decode.
996
+ * @param {Object=} options
997
+ * @return {string} The decoded string.
998
+ */
999
+ TextDecoder.prototype.decode = function decode(input, options) {
1000
+ var bytes;
1001
+ if (typeof input === 'object' && input instanceof ArrayBuffer) {
1002
+ bytes = new Uint8Array(input);
1003
+ }
1004
+ else if (typeof input === 'object' && 'buffer' in input &&
1005
+ input.buffer instanceof ArrayBuffer) {
1006
+ bytes = new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
1007
+ }
1008
+ else {
1009
+ bytes = new Uint8Array(0);
1010
+ }
1011
+ options = ToDictionary(options);
1012
+ // 1. If the do not flush flag is unset, set decoder to a new
1013
+ // encoding's decoder, set stream to a new stream, and unset the
1014
+ // BOM seen flag.
1015
+ if (!this._do_not_flush) {
1016
+ this._decoder = decoders[this._encoding.name]({
1017
+ fatal: this._error_mode === 'fatal'
1018
+ });
1019
+ this._BOMseen = false;
1020
+ }
1021
+ // 2. If options's stream is true, set the do not flush flag, and
1022
+ // unset the do not flush flag otherwise.
1023
+ this._do_not_flush = Boolean(options['stream']);
1024
+ // 3. If input is given, push a copy of input to stream.
1025
+ // TODO: Align with spec algorithm - maintain stream on instance.
1026
+ var input_stream = new Stream(bytes);
1027
+ // 4. Let output be a new stream.
1028
+ var output = [];
1029
+ /** @type {?(number|!Array.<number>)} */
1030
+ var result;
1031
+ // 5. While true:
1032
+ while (true) {
1033
+ // 1. Let token be the result of reading from stream.
1034
+ var token = input_stream.read();
1035
+ // 2. If token is end-of-stream and the do not flush flag is
1036
+ // set, return output, serialized.
1037
+ // TODO: Align with spec algorithm.
1038
+ if (token === end_of_stream)
1039
+ break;
1040
+ // 3. Otherwise, run these subsubsteps:
1041
+ // 1. Let result be the result of processing token for decoder,
1042
+ // stream, output, and error mode.
1043
+ result = this._decoder.handler(input_stream, token);
1044
+ // 2. If result is finished, return output, serialized.
1045
+ if (result === finished)
1046
+ break;
1047
+ if (result !== null) {
1048
+ if (Array.isArray(result))
1049
+ output.push.apply(output, /**@type {!Array.<number>}*/ (result));
1050
+ else
1051
+ output.push(result);
1052
+ }
1053
+ // 3. Otherwise, if result is error, throw a TypeError.
1054
+ // (Thrown in handler)
1055
+ // 4. Otherwise, do nothing.
1056
+ }
1057
+ // TODO: Align with spec algorithm.
1058
+ if (!this._do_not_flush) {
1059
+ do {
1060
+ result = this._decoder.handler(input_stream, input_stream.read());
1061
+ if (result === finished)
1062
+ break;
1063
+ if (result === null)
1064
+ continue;
1065
+ if (Array.isArray(result))
1066
+ output.push.apply(output, /**@type {!Array.<number>}*/ (result));
1067
+ else
1068
+ output.push(result);
1069
+ } while (!input_stream.endOfStream());
1070
+ this._decoder = null;
1071
+ }
1072
+ // A TextDecoder object also has an associated serialize stream
1073
+ // algorithm...
1074
+ /**
1075
+ * @param {!Array.<number>} stream
1076
+ * @return {string}
1077
+ * @this {TextDecoder}
1078
+ */
1079
+ function serializeStream(stream) {
1080
+ // 1. Let token be the result of reading from stream.
1081
+ // (Done in-place on array, rather than as a stream)
1082
+ // 2. If encoding is UTF-8, UTF-16BE, or UTF-16LE, and ignore
1083
+ // BOM flag and BOM seen flag are unset, run these subsubsteps:
1084
+ if (includes(['UTF-8', 'UTF-16LE', 'UTF-16BE'], this._encoding.name) &&
1085
+ !this._ignoreBOM && !this._BOMseen) {
1086
+ if (stream.length > 0 && stream[0] === 0xFEFF) {
1087
+ // 1. If token is U+FEFF, set BOM seen flag.
1088
+ this._BOMseen = true;
1089
+ stream.shift();
1090
+ }
1091
+ else if (stream.length > 0) {
1092
+ // 2. Otherwise, if token is not end-of-stream, set BOM seen
1093
+ // flag and append token to stream.
1094
+ this._BOMseen = true;
1095
+ }
1096
+ else {
1097
+ // 3. Otherwise, if token is not end-of-stream, append token
1098
+ // to output.
1099
+ // (no-op)
1100
+ }
1101
+ }
1102
+ // 4. Otherwise, return output.
1103
+ return codePointsToString(stream);
1104
+ }
1105
+ return serializeStream.call(this, output);
1106
+ };
1107
+ // 8.2 Interface TextEncoder
1108
+ /**
1109
+ * @constructor
1110
+ * @param {string=} label The label of the encoding. NONSTANDARD.
1111
+ * @param {Object=} options NONSTANDARD.
1112
+ */
1113
+ function TextEncoder(label, options) {
1114
+ // Web IDL conventions
1115
+ if (!(this instanceof TextEncoder))
1116
+ throw TypeError('Called as a function. Did you forget \'new\'?');
1117
+ options = ToDictionary(options);
1118
+ // A TextEncoder object has an associated encoding and encoder.
1119
+ /** @private */
1120
+ this._encoding = null;
1121
+ /** @private @type {?Encoder} */
1122
+ this._encoder = null;
1123
+ // Non-standard
1124
+ /** @private @type {boolean} */
1125
+ this._do_not_flush = false;
1126
+ /** @private @type {string} */
1127
+ this._fatal = Boolean(options['fatal']) ? 'fatal' : 'replacement';
1128
+ // 1. Let enc be a new TextEncoder object.
1129
+ var enc = this;
1130
+ // 2. Set enc's encoding to UTF-8's encoder.
1131
+ if (Boolean(options['NONSTANDARD_allowLegacyEncoding'])) {
1132
+ // NONSTANDARD behavior.
1133
+ label = label !== undefined ? String(label) : DEFAULT_ENCODING;
1134
+ var encoding = getEncoding(label);
1135
+ if (encoding === null || encoding.name === 'replacement')
1136
+ throw RangeError('Unknown encoding: ' + label);
1137
+ if (!encoders[encoding.name]) {
1138
+ throw Error('Encoder not present.' +
1139
+ ' Did you forget to include encoding-indexes.js first?');
1140
+ }
1141
+ enc._encoding = encoding;
1142
+ }
1143
+ else {
1144
+ // Standard behavior.
1145
+ enc._encoding = getEncoding('utf-8');
1146
+ if (label !== undefined && 'console' in global) {
1147
+ console.warn('TextEncoder constructor called with encoding label, '
1148
+ + 'which is ignored.');
1149
+ }
1150
+ }
1151
+ // For pre-ES5 runtimes:
1152
+ if (!Object.defineProperty)
1153
+ this.encoding = enc._encoding.name.toLowerCase();
1154
+ // 3. Return enc.
1155
+ return enc;
1156
+ }
1157
+ if (Object.defineProperty) {
1158
+ // The encoding attribute's getter must return encoding's name.
1159
+ Object.defineProperty(TextEncoder.prototype, 'encoding', {
1160
+ /** @this {TextEncoder} */
1161
+ get: function () { return this._encoding.name.toLowerCase(); }
1162
+ });
1163
+ }
1164
+ /**
1165
+ * @param {string=} opt_string The string to encode.
1166
+ * @param {Object=} options
1167
+ * @return {!Uint8Array} Encoded bytes, as a Uint8Array.
1168
+ */
1169
+ TextEncoder.prototype.encode = function encode(opt_string, options) {
1170
+ opt_string = opt_string === undefined ? '' : String(opt_string);
1171
+ options = ToDictionary(options);
1172
+ // NOTE: This option is nonstandard. None of the encodings
1173
+ // permitted for encoding (i.e. UTF-8, UTF-16) are stateful when
1174
+ // the input is a USVString so streaming is not necessary.
1175
+ if (!this._do_not_flush)
1176
+ this._encoder = encoders[this._encoding.name]({
1177
+ fatal: this._fatal === 'fatal'
1178
+ });
1179
+ this._do_not_flush = Boolean(options['stream']);
1180
+ // 1. Convert input to a stream.
1181
+ var input = new Stream(stringToCodePoints(opt_string));
1182
+ // 2. Let output be a new stream
1183
+ var output = [];
1184
+ /** @type {?(number|!Array.<number>)} */
1185
+ var result;
1186
+ // 3. While true, run these substeps:
1187
+ while (true) {
1188
+ // 1. Let token be the result of reading from input.
1189
+ var token = input.read();
1190
+ if (token === end_of_stream)
1191
+ break;
1192
+ // 2. Let result be the result of processing token for encoder,
1193
+ // input, output.
1194
+ result = this._encoder.handler(input, token);
1195
+ if (result === finished)
1196
+ break;
1197
+ if (Array.isArray(result))
1198
+ output.push.apply(output, /**@type {!Array.<number>}*/ (result));
1199
+ else
1200
+ output.push(result);
1201
+ }
1202
+ // TODO: Align with spec algorithm.
1203
+ if (!this._do_not_flush) {
1204
+ while (true) {
1205
+ result = this._encoder.handler(input, input.read());
1206
+ if (result === finished)
1207
+ break;
1208
+ if (Array.isArray(result))
1209
+ output.push.apply(output, /**@type {!Array.<number>}*/ (result));
1210
+ else
1211
+ output.push(result);
1212
+ }
1213
+ this._encoder = null;
1214
+ }
1215
+ // 3. If result is finished, convert output into a byte sequence,
1216
+ // and then return a Uint8Array object wrapping an ArrayBuffer
1217
+ // containing output.
1218
+ return new Uint8Array(output);
1219
+ };
1220
+ //
1221
+ // 9. The encoding
1222
+ //
1223
+ // 9.1 utf-8
1224
+ // 9.1.1 utf-8 decoder
1225
+ /**
1226
+ * @constructor
1227
+ * @implements {Decoder}
1228
+ * @param {{fatal: boolean}} options
1229
+ */
1230
+ function UTF8Decoder(options) {
1231
+ var fatal = options.fatal;
1232
+ // utf-8's decoder's has an associated utf-8 code point, utf-8
1233
+ // bytes seen, and utf-8 bytes needed (all initially 0), a utf-8
1234
+ // lower boundary (initially 0x80), and a utf-8 upper boundary
1235
+ // (initially 0xBF).
1236
+ var /** @type {number} */ utf8_code_point = 0,
1237
+ /** @type {number} */ utf8_bytes_seen = 0,
1238
+ /** @type {number} */ utf8_bytes_needed = 0,
1239
+ /** @type {number} */ utf8_lower_boundary = 0x80,
1240
+ /** @type {number} */ utf8_upper_boundary = 0xBF;
1241
+ /**
1242
+ * @param {Stream} stream The stream of bytes being decoded.
1243
+ * @param {number} bite The next byte read from the stream.
1244
+ * @return {?(number|!Array.<number>)} The next code point(s)
1245
+ * decoded, or null if not enough data exists in the input
1246
+ * stream to decode a complete code point.
1247
+ */
1248
+ this.handler = function (stream, bite) {
1249
+ // 1. If byte is end-of-stream and utf-8 bytes needed is not 0,
1250
+ // set utf-8 bytes needed to 0 and return error.
1251
+ if (bite === end_of_stream && utf8_bytes_needed !== 0) {
1252
+ utf8_bytes_needed = 0;
1253
+ return decoderError(fatal);
1254
+ }
1255
+ // 2. If byte is end-of-stream, return finished.
1256
+ if (bite === end_of_stream)
1257
+ return finished;
1258
+ // 3. If utf-8 bytes needed is 0, based on byte:
1259
+ if (utf8_bytes_needed === 0) {
1260
+ // 0x00 to 0x7F
1261
+ if (inRange(bite, 0x00, 0x7F)) {
1262
+ // Return a code point whose value is byte.
1263
+ return bite;
1264
+ }
1265
+ // 0xC2 to 0xDF
1266
+ else if (inRange(bite, 0xC2, 0xDF)) {
1267
+ // 1. Set utf-8 bytes needed to 1.
1268
+ utf8_bytes_needed = 1;
1269
+ // 2. Set UTF-8 code point to byte & 0x1F.
1270
+ utf8_code_point = bite & 0x1F;
1271
+ }
1272
+ // 0xE0 to 0xEF
1273
+ else if (inRange(bite, 0xE0, 0xEF)) {
1274
+ // 1. If byte is 0xE0, set utf-8 lower boundary to 0xA0.
1275
+ if (bite === 0xE0)
1276
+ utf8_lower_boundary = 0xA0;
1277
+ // 2. If byte is 0xED, set utf-8 upper boundary to 0x9F.
1278
+ if (bite === 0xED)
1279
+ utf8_upper_boundary = 0x9F;
1280
+ // 3. Set utf-8 bytes needed to 2.
1281
+ utf8_bytes_needed = 2;
1282
+ // 4. Set UTF-8 code point to byte & 0xF.
1283
+ utf8_code_point = bite & 0xF;
1284
+ }
1285
+ // 0xF0 to 0xF4
1286
+ else if (inRange(bite, 0xF0, 0xF4)) {
1287
+ // 1. If byte is 0xF0, set utf-8 lower boundary to 0x90.
1288
+ if (bite === 0xF0)
1289
+ utf8_lower_boundary = 0x90;
1290
+ // 2. If byte is 0xF4, set utf-8 upper boundary to 0x8F.
1291
+ if (bite === 0xF4)
1292
+ utf8_upper_boundary = 0x8F;
1293
+ // 3. Set utf-8 bytes needed to 3.
1294
+ utf8_bytes_needed = 3;
1295
+ // 4. Set UTF-8 code point to byte & 0x7.
1296
+ utf8_code_point = bite & 0x7;
1297
+ }
1298
+ // Otherwise
1299
+ else {
1300
+ // Return error.
1301
+ return decoderError(fatal);
1302
+ }
1303
+ // Return continue.
1304
+ return null;
1305
+ }
1306
+ // 4. If byte is not in the range utf-8 lower boundary to utf-8
1307
+ // upper boundary, inclusive, run these substeps:
1308
+ if (!inRange(bite, utf8_lower_boundary, utf8_upper_boundary)) {
1309
+ // 1. Set utf-8 code point, utf-8 bytes needed, and utf-8
1310
+ // bytes seen to 0, set utf-8 lower boundary to 0x80, and set
1311
+ // utf-8 upper boundary to 0xBF.
1312
+ utf8_code_point = utf8_bytes_needed = utf8_bytes_seen = 0;
1313
+ utf8_lower_boundary = 0x80;
1314
+ utf8_upper_boundary = 0xBF;
1315
+ // 2. Prepend byte to stream.
1316
+ stream.prepend(bite);
1317
+ // 3. Return error.
1318
+ return decoderError(fatal);
1319
+ }
1320
+ // 5. Set utf-8 lower boundary to 0x80 and utf-8 upper boundary
1321
+ // to 0xBF.
1322
+ utf8_lower_boundary = 0x80;
1323
+ utf8_upper_boundary = 0xBF;
1324
+ // 6. Set UTF-8 code point to (UTF-8 code point << 6) | (byte &
1325
+ // 0x3F)
1326
+ utf8_code_point = (utf8_code_point << 6) | (bite & 0x3F);
1327
+ // 7. Increase utf-8 bytes seen by one.
1328
+ utf8_bytes_seen += 1;
1329
+ // 8. If utf-8 bytes seen is not equal to utf-8 bytes needed,
1330
+ // continue.
1331
+ if (utf8_bytes_seen !== utf8_bytes_needed)
1332
+ return null;
1333
+ // 9. Let code point be utf-8 code point.
1334
+ var code_point = utf8_code_point;
1335
+ // 10. Set utf-8 code point, utf-8 bytes needed, and utf-8 bytes
1336
+ // seen to 0.
1337
+ utf8_code_point = utf8_bytes_needed = utf8_bytes_seen = 0;
1338
+ // 11. Return a code point whose value is code point.
1339
+ return code_point;
1340
+ };
1341
+ }
1342
+ // 9.1.2 utf-8 encoder
1343
+ /**
1344
+ * @constructor
1345
+ * @implements {Encoder}
1346
+ * @param {{fatal: boolean}} options
1347
+ */
1348
+ function UTF8Encoder(options) {
1349
+ var fatal = options.fatal;
1350
+ /**
1351
+ * @param {Stream} stream Input stream.
1352
+ * @param {number} code_point Next code point read from the stream.
1353
+ * @return {(number|!Array.<number>)} Byte(s) to emit.
1354
+ */
1355
+ this.handler = function (stream, code_point) {
1356
+ // 1. If code point is end-of-stream, return finished.
1357
+ if (code_point === end_of_stream)
1358
+ return finished;
1359
+ // 2. If code point is an ASCII code point, return a byte whose
1360
+ // value is code point.
1361
+ if (isASCIICodePoint(code_point))
1362
+ return code_point;
1363
+ // 3. Set count and offset based on the range code point is in:
1364
+ var count, offset;
1365
+ // U+0080 to U+07FF, inclusive:
1366
+ if (inRange(code_point, 0x0080, 0x07FF)) {
1367
+ // 1 and 0xC0
1368
+ count = 1;
1369
+ offset = 0xC0;
1370
+ }
1371
+ // U+0800 to U+FFFF, inclusive:
1372
+ else if (inRange(code_point, 0x0800, 0xFFFF)) {
1373
+ // 2 and 0xE0
1374
+ count = 2;
1375
+ offset = 0xE0;
1376
+ }
1377
+ // U+10000 to U+10FFFF, inclusive:
1378
+ else if (inRange(code_point, 0x10000, 0x10FFFF)) {
1379
+ // 3 and 0xF0
1380
+ count = 3;
1381
+ offset = 0xF0;
1382
+ }
1383
+ // 4. Let bytes be a byte sequence whose first byte is (code
1384
+ // point >> (6 × count)) + offset.
1385
+ var bytes = [(code_point >> (6 * count)) + offset];
1386
+ // 5. Run these substeps while count is greater than 0:
1387
+ while (count > 0) {
1388
+ // 1. Set temp to code point >> (6 × (count − 1)).
1389
+ var temp = code_point >> (6 * (count - 1));
1390
+ // 2. Append to bytes 0x80 | (temp & 0x3F).
1391
+ bytes.push(0x80 | (temp & 0x3F));
1392
+ // 3. Decrease count by one.
1393
+ count -= 1;
1394
+ }
1395
+ // 6. Return bytes bytes, in order.
1396
+ return bytes;
1397
+ };
1398
+ }
1399
+ /** @param {{fatal: boolean}} options */
1400
+ encoders['UTF-8'] = function (options) {
1401
+ return new UTF8Encoder(options);
1402
+ };
1403
+ /** @param {{fatal: boolean}} options */
1404
+ decoders['UTF-8'] = function (options) {
1405
+ return new UTF8Decoder(options);
1406
+ };
1407
+ //
1408
+ // 10. Legacy single-byte encodings
1409
+ //
1410
+ // 10.1 single-byte decoder
1411
+ /**
1412
+ * @constructor
1413
+ * @implements {Decoder}
1414
+ * @param {!Array.<number>} index The encoding index.
1415
+ * @param {{fatal: boolean}} options
1416
+ */
1417
+ function SingleByteDecoder(index, options) {
1418
+ var fatal = options.fatal;
1419
+ /**
1420
+ * @param {Stream} stream The stream of bytes being decoded.
1421
+ * @param {number} bite The next byte read from the stream.
1422
+ * @return {?(number|!Array.<number>)} The next code point(s)
1423
+ * decoded, or null if not enough data exists in the input
1424
+ * stream to decode a complete code point.
1425
+ */
1426
+ this.handler = function (stream, bite) {
1427
+ // 1. If byte is end-of-stream, return finished.
1428
+ if (bite === end_of_stream)
1429
+ return finished;
1430
+ // 2. If byte is an ASCII byte, return a code point whose value
1431
+ // is byte.
1432
+ if (isASCIIByte(bite))
1433
+ return bite;
1434
+ // 3. Let code point be the index code point for byte − 0x80 in
1435
+ // index single-byte.
1436
+ var code_point = index[bite - 0x80];
1437
+ // 4. If code point is null, return error.
1438
+ if (code_point === null)
1439
+ return decoderError(fatal);
1440
+ // 5. Return a code point whose value is code point.
1441
+ return code_point;
1442
+ };
1443
+ }
1444
+ // 10.2 single-byte encoder
1445
+ /**
1446
+ * @constructor
1447
+ * @implements {Encoder}
1448
+ * @param {!Array.<?number>} index The encoding index.
1449
+ * @param {{fatal: boolean}} options
1450
+ */
1451
+ function SingleByteEncoder(index, options) {
1452
+ var fatal = options.fatal;
1453
+ /**
1454
+ * @param {Stream} stream Input stream.
1455
+ * @param {number} code_point Next code point read from the stream.
1456
+ * @return {(number|!Array.<number>)} Byte(s) to emit.
1457
+ */
1458
+ this.handler = function (stream, code_point) {
1459
+ // 1. If code point is end-of-stream, return finished.
1460
+ if (code_point === end_of_stream)
1461
+ return finished;
1462
+ // 2. If code point is an ASCII code point, return a byte whose
1463
+ // value is code point.
1464
+ if (isASCIICodePoint(code_point))
1465
+ return code_point;
1466
+ // 3. Let pointer be the index pointer for code point in index
1467
+ // single-byte.
1468
+ var pointer = indexPointerFor(code_point, index);
1469
+ // 4. If pointer is null, return error with code point.
1470
+ if (pointer === null)
1471
+ encoderError(code_point);
1472
+ // 5. Return a byte whose value is pointer + 0x80.
1473
+ return pointer + 0x80;
1474
+ };
1475
+ }
1476
+ (function () {
1477
+ if (!('encoding-indexes' in global))
1478
+ return;
1479
+ encodings.forEach(function (category) {
1480
+ if (category.heading !== 'Legacy single-byte encodings')
1481
+ return;
1482
+ category.encodings.forEach(function (encoding) {
1483
+ var name = encoding.name;
1484
+ var idx = index(name.toLowerCase());
1485
+ /** @param {{fatal: boolean}} options */
1486
+ decoders[name] = function (options) {
1487
+ return new SingleByteDecoder(idx, options);
1488
+ };
1489
+ /** @param {{fatal: boolean}} options */
1490
+ encoders[name] = function (options) {
1491
+ return new SingleByteEncoder(idx, options);
1492
+ };
1493
+ });
1494
+ });
1495
+ }());
1496
+ //
1497
+ // 11. Legacy multi-byte Chinese (simplified) encodings
1498
+ //
1499
+ // 11.1 gbk
1500
+ // 11.1.1 gbk decoder
1501
+ // gbk's decoder is gb18030's decoder.
1502
+ /** @param {{fatal: boolean}} options */
1503
+ decoders['GBK'] = function (options) {
1504
+ return new GB18030Decoder(options);
1505
+ };
1506
+ // 11.1.2 gbk encoder
1507
+ // gbk's encoder is gb18030's encoder with its gbk flag set.
1508
+ /** @param {{fatal: boolean}} options */
1509
+ encoders['GBK'] = function (options) {
1510
+ return new GB18030Encoder(options, true);
1511
+ };
1512
+ // 11.2 gb18030
1513
+ // 11.2.1 gb18030 decoder
1514
+ /**
1515
+ * @constructor
1516
+ * @implements {Decoder}
1517
+ * @param {{fatal: boolean}} options
1518
+ */
1519
+ function GB18030Decoder(options) {
1520
+ var fatal = options.fatal;
1521
+ // gb18030's decoder has an associated gb18030 first, gb18030
1522
+ // second, and gb18030 third (all initially 0x00).
1523
+ var /** @type {number} */ gb18030_first = 0x00,
1524
+ /** @type {number} */ gb18030_second = 0x00,
1525
+ /** @type {number} */ gb18030_third = 0x00;
1526
+ /**
1527
+ * @param {Stream} stream The stream of bytes being decoded.
1528
+ * @param {number} bite The next byte read from the stream.
1529
+ * @return {?(number|!Array.<number>)} The next code point(s)
1530
+ * decoded, or null if not enough data exists in the input
1531
+ * stream to decode a complete code point.
1532
+ */
1533
+ this.handler = function (stream, bite) {
1534
+ // 1. If byte is end-of-stream and gb18030 first, gb18030
1535
+ // second, and gb18030 third are 0x00, return finished.
1536
+ if (bite === end_of_stream && gb18030_first === 0x00 &&
1537
+ gb18030_second === 0x00 && gb18030_third === 0x00) {
1538
+ return finished;
1539
+ }
1540
+ // 2. If byte is end-of-stream, and gb18030 first, gb18030
1541
+ // second, or gb18030 third is not 0x00, set gb18030 first,
1542
+ // gb18030 second, and gb18030 third to 0x00, and return error.
1543
+ if (bite === end_of_stream &&
1544
+ (gb18030_first !== 0x00 || gb18030_second !== 0x00 ||
1545
+ gb18030_third !== 0x00)) {
1546
+ gb18030_first = 0x00;
1547
+ gb18030_second = 0x00;
1548
+ gb18030_third = 0x00;
1549
+ decoderError(fatal);
1550
+ }
1551
+ var code_point;
1552
+ // 3. If gb18030 third is not 0x00, run these substeps:
1553
+ if (gb18030_third !== 0x00) {
1554
+ // 1. Let code point be null.
1555
+ code_point = null;
1556
+ // 2. If byte is in the range 0x30 to 0x39, inclusive, set
1557
+ // code point to the index gb18030 ranges code point for
1558
+ // (((gb18030 first − 0x81) × 10 + gb18030 second − 0x30) ×
1559
+ // 126 + gb18030 third − 0x81) × 10 + byte − 0x30.
1560
+ if (inRange(bite, 0x30, 0x39)) {
1561
+ code_point = indexGB18030RangesCodePointFor((((gb18030_first - 0x81) * 10 + gb18030_second - 0x30) * 126 +
1562
+ gb18030_third - 0x81) * 10 + bite - 0x30);
1563
+ }
1564
+ // 3. Let buffer be a byte sequence consisting of gb18030
1565
+ // second, gb18030 third, and byte, in order.
1566
+ var buffer = [gb18030_second, gb18030_third, bite];
1567
+ // 4. Set gb18030 first, gb18030 second, and gb18030 third to
1568
+ // 0x00.
1569
+ gb18030_first = 0x00;
1570
+ gb18030_second = 0x00;
1571
+ gb18030_third = 0x00;
1572
+ // 5. If code point is null, prepend buffer to stream and
1573
+ // return error.
1574
+ if (code_point === null) {
1575
+ stream.prepend(buffer);
1576
+ return decoderError(fatal);
1577
+ }
1578
+ // 6. Return a code point whose value is code point.
1579
+ return code_point;
1580
+ }
1581
+ // 4. If gb18030 second is not 0x00, run these substeps:
1582
+ if (gb18030_second !== 0x00) {
1583
+ // 1. If byte is in the range 0x81 to 0xFE, inclusive, set
1584
+ // gb18030 third to byte and return continue.
1585
+ if (inRange(bite, 0x81, 0xFE)) {
1586
+ gb18030_third = bite;
1587
+ return null;
1588
+ }
1589
+ // 2. Prepend gb18030 second followed by byte to stream, set
1590
+ // gb18030 first and gb18030 second to 0x00, and return error.
1591
+ stream.prepend([gb18030_second, bite]);
1592
+ gb18030_first = 0x00;
1593
+ gb18030_second = 0x00;
1594
+ return decoderError(fatal);
1595
+ }
1596
+ // 5. If gb18030 first is not 0x00, run these substeps:
1597
+ if (gb18030_first !== 0x00) {
1598
+ // 1. If byte is in the range 0x30 to 0x39, inclusive, set
1599
+ // gb18030 second to byte and return continue.
1600
+ if (inRange(bite, 0x30, 0x39)) {
1601
+ gb18030_second = bite;
1602
+ return null;
1603
+ }
1604
+ // 2. Let lead be gb18030 first, let pointer be null, and set
1605
+ // gb18030 first to 0x00.
1606
+ var lead = gb18030_first;
1607
+ var pointer = null;
1608
+ gb18030_first = 0x00;
1609
+ // 3. Let offset be 0x40 if byte is less than 0x7F and 0x41
1610
+ // otherwise.
1611
+ var offset = bite < 0x7F ? 0x40 : 0x41;
1612
+ // 4. If byte is in the range 0x40 to 0x7E, inclusive, or 0x80
1613
+ // to 0xFE, inclusive, set pointer to (lead − 0x81) × 190 +
1614
+ // (byte − offset).
1615
+ if (inRange(bite, 0x40, 0x7E) || inRange(bite, 0x80, 0xFE))
1616
+ pointer = (lead - 0x81) * 190 + (bite - offset);
1617
+ // 5. Let code point be null if pointer is null and the index
1618
+ // code point for pointer in index gb18030 otherwise.
1619
+ code_point = pointer === null ? null :
1620
+ indexCodePointFor(pointer, index('gb18030'));
1621
+ // 6. If code point is null and byte is an ASCII byte, prepend
1622
+ // byte to stream.
1623
+ if (code_point === null && isASCIIByte(bite))
1624
+ stream.prepend(bite);
1625
+ // 7. If code point is null, return error.
1626
+ if (code_point === null)
1627
+ return decoderError(fatal);
1628
+ // 8. Return a code point whose value is code point.
1629
+ return code_point;
1630
+ }
1631
+ // 6. If byte is an ASCII byte, return a code point whose value
1632
+ // is byte.
1633
+ if (isASCIIByte(bite))
1634
+ return bite;
1635
+ // 7. If byte is 0x80, return code point U+20AC.
1636
+ if (bite === 0x80)
1637
+ return 0x20AC;
1638
+ // 8. If byte is in the range 0x81 to 0xFE, inclusive, set
1639
+ // gb18030 first to byte and return continue.
1640
+ if (inRange(bite, 0x81, 0xFE)) {
1641
+ gb18030_first = bite;
1642
+ return null;
1643
+ }
1644
+ // 9. Return error.
1645
+ return decoderError(fatal);
1646
+ };
1647
+ }
1648
+ // 11.2.2 gb18030 encoder
1649
+ /**
1650
+ * @constructor
1651
+ * @implements {Encoder}
1652
+ * @param {{fatal: boolean}} options
1653
+ * @param {boolean=} gbk_flag
1654
+ */
1655
+ function GB18030Encoder(options, gbk_flag) {
1656
+ var fatal = options.fatal;
1657
+ // gb18030's decoder has an associated gbk flag (initially unset).
1658
+ /**
1659
+ * @param {Stream} stream Input stream.
1660
+ * @param {number} code_point Next code point read from the stream.
1661
+ * @return {(number|!Array.<number>)} Byte(s) to emit.
1662
+ */
1663
+ this.handler = function (stream, code_point) {
1664
+ // 1. If code point is end-of-stream, return finished.
1665
+ if (code_point === end_of_stream)
1666
+ return finished;
1667
+ // 2. If code point is an ASCII code point, return a byte whose
1668
+ // value is code point.
1669
+ if (isASCIICodePoint(code_point))
1670
+ return code_point;
1671
+ // 3. If code point is U+E5E5, return error with code point.
1672
+ if (code_point === 0xE5E5)
1673
+ return encoderError(code_point);
1674
+ // 4. If the gbk flag is set and code point is U+20AC, return
1675
+ // byte 0x80.
1676
+ if (gbk_flag && code_point === 0x20AC)
1677
+ return 0x80;
1678
+ // 5. Let pointer be the index pointer for code point in index
1679
+ // gb18030.
1680
+ var pointer = indexPointerFor(code_point, index('gb18030'));
1681
+ // 6. If pointer is not null, run these substeps:
1682
+ if (pointer !== null) {
1683
+ // 1. Let lead be floor(pointer / 190) + 0x81.
1684
+ var lead = floor(pointer / 190) + 0x81;
1685
+ // 2. Let trail be pointer % 190.
1686
+ var trail = pointer % 190;
1687
+ // 3. Let offset be 0x40 if trail is less than 0x3F and 0x41 otherwise.
1688
+ var offset = trail < 0x3F ? 0x40 : 0x41;
1689
+ // 4. Return two bytes whose values are lead and trail + offset.
1690
+ return [lead, trail + offset];
1691
+ }
1692
+ // 7. If gbk flag is set, return error with code point.
1693
+ if (gbk_flag)
1694
+ return encoderError(code_point);
1695
+ // 8. Set pointer to the index gb18030 ranges pointer for code
1696
+ // point.
1697
+ pointer = indexGB18030RangesPointerFor(code_point);
1698
+ // 9. Let byte1 be floor(pointer / 10 / 126 / 10).
1699
+ var byte1 = floor(pointer / 10 / 126 / 10);
1700
+ // 10. Set pointer to pointer − byte1 × 10 × 126 × 10.
1701
+ pointer = pointer - byte1 * 10 * 126 * 10;
1702
+ // 11. Let byte2 be floor(pointer / 10 / 126).
1703
+ var byte2 = floor(pointer / 10 / 126);
1704
+ // 12. Set pointer to pointer − byte2 × 10 × 126.
1705
+ pointer = pointer - byte2 * 10 * 126;
1706
+ // 13. Let byte3 be floor(pointer / 10).
1707
+ var byte3 = floor(pointer / 10);
1708
+ // 14. Let byte4 be pointer − byte3 × 10.
1709
+ var byte4 = pointer - byte3 * 10;
1710
+ // 15. Return four bytes whose values are byte1 + 0x81, byte2 +
1711
+ // 0x30, byte3 + 0x81, byte4 + 0x30.
1712
+ return [byte1 + 0x81,
1713
+ byte2 + 0x30,
1714
+ byte3 + 0x81,
1715
+ byte4 + 0x30];
1716
+ };
1717
+ }
1718
+ /** @param {{fatal: boolean}} options */
1719
+ encoders['gb18030'] = function (options) {
1720
+ return new GB18030Encoder(options);
1721
+ };
1722
+ /** @param {{fatal: boolean}} options */
1723
+ decoders['gb18030'] = function (options) {
1724
+ return new GB18030Decoder(options);
1725
+ };
1726
+ //
1727
+ // 12. Legacy multi-byte Chinese (traditional) encodings
1728
+ //
1729
+ // 12.1 Big5
1730
+ // 12.1.1 Big5 decoder
1731
+ /**
1732
+ * @constructor
1733
+ * @implements {Decoder}
1734
+ * @param {{fatal: boolean}} options
1735
+ */
1736
+ function Big5Decoder(options) {
1737
+ var fatal = options.fatal;
1738
+ // Big5's decoder has an associated Big5 lead (initially 0x00).
1739
+ var /** @type {number} */ Big5_lead = 0x00;
1740
+ /**
1741
+ * @param {Stream} stream The stream of bytes being decoded.
1742
+ * @param {number} bite The next byte read from the stream.
1743
+ * @return {?(number|!Array.<number>)} The next code point(s)
1744
+ * decoded, or null if not enough data exists in the input
1745
+ * stream to decode a complete code point.
1746
+ */
1747
+ this.handler = function (stream, bite) {
1748
+ // 1. If byte is end-of-stream and Big5 lead is not 0x00, set
1749
+ // Big5 lead to 0x00 and return error.
1750
+ if (bite === end_of_stream && Big5_lead !== 0x00) {
1751
+ Big5_lead = 0x00;
1752
+ return decoderError(fatal);
1753
+ }
1754
+ // 2. If byte is end-of-stream and Big5 lead is 0x00, return
1755
+ // finished.
1756
+ if (bite === end_of_stream && Big5_lead === 0x00)
1757
+ return finished;
1758
+ // 3. If Big5 lead is not 0x00, let lead be Big5 lead, let
1759
+ // pointer be null, set Big5 lead to 0x00, and then run these
1760
+ // substeps:
1761
+ if (Big5_lead !== 0x00) {
1762
+ var lead = Big5_lead;
1763
+ var pointer = null;
1764
+ Big5_lead = 0x00;
1765
+ // 1. Let offset be 0x40 if byte is less than 0x7F and 0x62
1766
+ // otherwise.
1767
+ var offset = bite < 0x7F ? 0x40 : 0x62;
1768
+ // 2. If byte is in the range 0x40 to 0x7E, inclusive, or 0xA1
1769
+ // to 0xFE, inclusive, set pointer to (lead − 0x81) × 157 +
1770
+ // (byte − offset).
1771
+ if (inRange(bite, 0x40, 0x7E) || inRange(bite, 0xA1, 0xFE))
1772
+ pointer = (lead - 0x81) * 157 + (bite - offset);
1773
+ // 3. If there is a row in the table below whose first column
1774
+ // is pointer, return the two code points listed in its second
1775
+ // column
1776
+ // Pointer | Code points
1777
+ // --------+--------------
1778
+ // 1133 | U+00CA U+0304
1779
+ // 1135 | U+00CA U+030C
1780
+ // 1164 | U+00EA U+0304
1781
+ // 1166 | U+00EA U+030C
1782
+ switch (pointer) {
1783
+ case 1133: return [0x00CA, 0x0304];
1784
+ case 1135: return [0x00CA, 0x030C];
1785
+ case 1164: return [0x00EA, 0x0304];
1786
+ case 1166: return [0x00EA, 0x030C];
1787
+ }
1788
+ // 4. Let code point be null if pointer is null and the index
1789
+ // code point for pointer in index Big5 otherwise.
1790
+ var code_point = (pointer === null) ? null :
1791
+ indexCodePointFor(pointer, index('big5'));
1792
+ // 5. If code point is null and byte is an ASCII byte, prepend
1793
+ // byte to stream.
1794
+ if (code_point === null && isASCIIByte(bite))
1795
+ stream.prepend(bite);
1796
+ // 6. If code point is null, return error.
1797
+ if (code_point === null)
1798
+ return decoderError(fatal);
1799
+ // 7. Return a code point whose value is code point.
1800
+ return code_point;
1801
+ }
1802
+ // 4. If byte is an ASCII byte, return a code point whose value
1803
+ // is byte.
1804
+ if (isASCIIByte(bite))
1805
+ return bite;
1806
+ // 5. If byte is in the range 0x81 to 0xFE, inclusive, set Big5
1807
+ // lead to byte and return continue.
1808
+ if (inRange(bite, 0x81, 0xFE)) {
1809
+ Big5_lead = bite;
1810
+ return null;
1811
+ }
1812
+ // 6. Return error.
1813
+ return decoderError(fatal);
1814
+ };
1815
+ }
1816
+ // 12.1.2 Big5 encoder
1817
+ /**
1818
+ * @constructor
1819
+ * @implements {Encoder}
1820
+ * @param {{fatal: boolean}} options
1821
+ */
1822
+ function Big5Encoder(options) {
1823
+ var fatal = options.fatal;
1824
+ /**
1825
+ * @param {Stream} stream Input stream.
1826
+ * @param {number} code_point Next code point read from the stream.
1827
+ * @return {(number|!Array.<number>)} Byte(s) to emit.
1828
+ */
1829
+ this.handler = function (stream, code_point) {
1830
+ // 1. If code point is end-of-stream, return finished.
1831
+ if (code_point === end_of_stream)
1832
+ return finished;
1833
+ // 2. If code point is an ASCII code point, return a byte whose
1834
+ // value is code point.
1835
+ if (isASCIICodePoint(code_point))
1836
+ return code_point;
1837
+ // 3. Let pointer be the index Big5 pointer for code point.
1838
+ var pointer = indexBig5PointerFor(code_point);
1839
+ // 4. If pointer is null, return error with code point.
1840
+ if (pointer === null)
1841
+ return encoderError(code_point);
1842
+ // 5. Let lead be floor(pointer / 157) + 0x81.
1843
+ var lead = floor(pointer / 157) + 0x81;
1844
+ // 6. If lead is less than 0xA1, return error with code point.
1845
+ if (lead < 0xA1)
1846
+ return encoderError(code_point);
1847
+ // 7. Let trail be pointer % 157.
1848
+ var trail = pointer % 157;
1849
+ // 8. Let offset be 0x40 if trail is less than 0x3F and 0x62
1850
+ // otherwise.
1851
+ var offset = trail < 0x3F ? 0x40 : 0x62;
1852
+ // Return two bytes whose values are lead and trail + offset.
1853
+ return [lead, trail + offset];
1854
+ };
1855
+ }
1856
+ /** @param {{fatal: boolean}} options */
1857
+ encoders['Big5'] = function (options) {
1858
+ return new Big5Encoder(options);
1859
+ };
1860
+ /** @param {{fatal: boolean}} options */
1861
+ decoders['Big5'] = function (options) {
1862
+ return new Big5Decoder(options);
1863
+ };
1864
+ //
1865
+ // 13. Legacy multi-byte Japanese encodings
1866
+ //
1867
+ // 13.1 euc-jp
1868
+ // 13.1.1 euc-jp decoder
1869
+ /**
1870
+ * @constructor
1871
+ * @implements {Decoder}
1872
+ * @param {{fatal: boolean}} options
1873
+ */
1874
+ function EUCJPDecoder(options) {
1875
+ var fatal = options.fatal;
1876
+ // euc-jp's decoder has an associated euc-jp jis0212 flag
1877
+ // (initially unset) and euc-jp lead (initially 0x00).
1878
+ var /** @type {boolean} */ eucjp_jis0212_flag = false,
1879
+ /** @type {number} */ eucjp_lead = 0x00;
1880
+ /**
1881
+ * @param {Stream} stream The stream of bytes being decoded.
1882
+ * @param {number} bite The next byte read from the stream.
1883
+ * @return {?(number|!Array.<number>)} The next code point(s)
1884
+ * decoded, or null if not enough data exists in the input
1885
+ * stream to decode a complete code point.
1886
+ */
1887
+ this.handler = function (stream, bite) {
1888
+ // 1. If byte is end-of-stream and euc-jp lead is not 0x00, set
1889
+ // euc-jp lead to 0x00, and return error.
1890
+ if (bite === end_of_stream && eucjp_lead !== 0x00) {
1891
+ eucjp_lead = 0x00;
1892
+ return decoderError(fatal);
1893
+ }
1894
+ // 2. If byte is end-of-stream and euc-jp lead is 0x00, return
1895
+ // finished.
1896
+ if (bite === end_of_stream && eucjp_lead === 0x00)
1897
+ return finished;
1898
+ // 3. If euc-jp lead is 0x8E and byte is in the range 0xA1 to
1899
+ // 0xDF, inclusive, set euc-jp lead to 0x00 and return a code
1900
+ // point whose value is 0xFF61 − 0xA1 + byte.
1901
+ if (eucjp_lead === 0x8E && inRange(bite, 0xA1, 0xDF)) {
1902
+ eucjp_lead = 0x00;
1903
+ return 0xFF61 - 0xA1 + bite;
1904
+ }
1905
+ // 4. If euc-jp lead is 0x8F and byte is in the range 0xA1 to
1906
+ // 0xFE, inclusive, set the euc-jp jis0212 flag, set euc-jp lead
1907
+ // to byte, and return continue.
1908
+ if (eucjp_lead === 0x8F && inRange(bite, 0xA1, 0xFE)) {
1909
+ eucjp_jis0212_flag = true;
1910
+ eucjp_lead = bite;
1911
+ return null;
1912
+ }
1913
+ // 5. If euc-jp lead is not 0x00, let lead be euc-jp lead, set
1914
+ // euc-jp lead to 0x00, and run these substeps:
1915
+ if (eucjp_lead !== 0x00) {
1916
+ var lead = eucjp_lead;
1917
+ eucjp_lead = 0x00;
1918
+ // 1. Let code point be null.
1919
+ var code_point = null;
1920
+ // 2. If lead and byte are both in the range 0xA1 to 0xFE,
1921
+ // inclusive, set code point to the index code point for (lead
1922
+ // − 0xA1) × 94 + byte − 0xA1 in index jis0208 if the euc-jp
1923
+ // jis0212 flag is unset and in index jis0212 otherwise.
1924
+ if (inRange(lead, 0xA1, 0xFE) && inRange(bite, 0xA1, 0xFE)) {
1925
+ code_point = indexCodePointFor((lead - 0xA1) * 94 + (bite - 0xA1), index(!eucjp_jis0212_flag ? 'jis0208' : 'jis0212'));
1926
+ }
1927
+ // 3. Unset the euc-jp jis0212 flag.
1928
+ eucjp_jis0212_flag = false;
1929
+ // 4. If byte is not in the range 0xA1 to 0xFE, inclusive,
1930
+ // prepend byte to stream.
1931
+ if (!inRange(bite, 0xA1, 0xFE))
1932
+ stream.prepend(bite);
1933
+ // 5. If code point is null, return error.
1934
+ if (code_point === null)
1935
+ return decoderError(fatal);
1936
+ // 6. Return a code point whose value is code point.
1937
+ return code_point;
1938
+ }
1939
+ // 6. If byte is an ASCII byte, return a code point whose value
1940
+ // is byte.
1941
+ if (isASCIIByte(bite))
1942
+ return bite;
1943
+ // 7. If byte is 0x8E, 0x8F, or in the range 0xA1 to 0xFE,
1944
+ // inclusive, set euc-jp lead to byte and return continue.
1945
+ if (bite === 0x8E || bite === 0x8F || inRange(bite, 0xA1, 0xFE)) {
1946
+ eucjp_lead = bite;
1947
+ return null;
1948
+ }
1949
+ // 8. Return error.
1950
+ return decoderError(fatal);
1951
+ };
1952
+ }
1953
+ // 13.1.2 euc-jp encoder
1954
+ /**
1955
+ * @constructor
1956
+ * @implements {Encoder}
1957
+ * @param {{fatal: boolean}} options
1958
+ */
1959
+ function EUCJPEncoder(options) {
1960
+ var fatal = options.fatal;
1961
+ /**
1962
+ * @param {Stream} stream Input stream.
1963
+ * @param {number} code_point Next code point read from the stream.
1964
+ * @return {(number|!Array.<number>)} Byte(s) to emit.
1965
+ */
1966
+ this.handler = function (stream, code_point) {
1967
+ // 1. If code point is end-of-stream, return finished.
1968
+ if (code_point === end_of_stream)
1969
+ return finished;
1970
+ // 2. If code point is an ASCII code point, return a byte whose
1971
+ // value is code point.
1972
+ if (isASCIICodePoint(code_point))
1973
+ return code_point;
1974
+ // 3. If code point is U+00A5, return byte 0x5C.
1975
+ if (code_point === 0x00A5)
1976
+ return 0x5C;
1977
+ // 4. If code point is U+203E, return byte 0x7E.
1978
+ if (code_point === 0x203E)
1979
+ return 0x7E;
1980
+ // 5. If code point is in the range U+FF61 to U+FF9F, inclusive,
1981
+ // return two bytes whose values are 0x8E and code point −
1982
+ // 0xFF61 + 0xA1.
1983
+ if (inRange(code_point, 0xFF61, 0xFF9F))
1984
+ return [0x8E, code_point - 0xFF61 + 0xA1];
1985
+ // 6. If code point is U+2212, set it to U+FF0D.
1986
+ if (code_point === 0x2212)
1987
+ code_point = 0xFF0D;
1988
+ // 7. Let pointer be the index pointer for code point in index
1989
+ // jis0208.
1990
+ var pointer = indexPointerFor(code_point, index('jis0208'));
1991
+ // 8. If pointer is null, return error with code point.
1992
+ if (pointer === null)
1993
+ return encoderError(code_point);
1994
+ // 9. Let lead be floor(pointer / 94) + 0xA1.
1995
+ var lead = floor(pointer / 94) + 0xA1;
1996
+ // 10. Let trail be pointer % 94 + 0xA1.
1997
+ var trail = pointer % 94 + 0xA1;
1998
+ // 11. Return two bytes whose values are lead and trail.
1999
+ return [lead, trail];
2000
+ };
2001
+ }
2002
+ /** @param {{fatal: boolean}} options */
2003
+ encoders['EUC-JP'] = function (options) {
2004
+ return new EUCJPEncoder(options);
2005
+ };
2006
+ /** @param {{fatal: boolean}} options */
2007
+ decoders['EUC-JP'] = function (options) {
2008
+ return new EUCJPDecoder(options);
2009
+ };
2010
+ // 13.2 iso-2022-jp
2011
+ // 13.2.1 iso-2022-jp decoder
2012
+ /**
2013
+ * @constructor
2014
+ * @implements {Decoder}
2015
+ * @param {{fatal: boolean}} options
2016
+ */
2017
+ function ISO2022JPDecoder(options) {
2018
+ var fatal = options.fatal;
2019
+ /** @enum */
2020
+ var states = {
2021
+ ASCII: 0,
2022
+ Roman: 1,
2023
+ Katakana: 2,
2024
+ LeadByte: 3,
2025
+ TrailByte: 4,
2026
+ EscapeStart: 5,
2027
+ Escape: 6
2028
+ };
2029
+ // iso-2022-jp's decoder has an associated iso-2022-jp decoder
2030
+ // state (initially ASCII), iso-2022-jp decoder output state
2031
+ // (initially ASCII), iso-2022-jp lead (initially 0x00), and
2032
+ // iso-2022-jp output flag (initially unset).
2033
+ var /** @type {number} */ iso2022jp_decoder_state = states.ASCII,
2034
+ /** @type {number} */ iso2022jp_decoder_output_state = states.ASCII,
2035
+ /** @type {number} */ iso2022jp_lead = 0x00,
2036
+ /** @type {boolean} */ iso2022jp_output_flag = false;
2037
+ /**
2038
+ * @param {Stream} stream The stream of bytes being decoded.
2039
+ * @param {number} bite The next byte read from the stream.
2040
+ * @return {?(number|!Array.<number>)} The next code point(s)
2041
+ * decoded, or null if not enough data exists in the input
2042
+ * stream to decode a complete code point.
2043
+ */
2044
+ this.handler = function (stream, bite) {
2045
+ // switching on iso-2022-jp decoder state:
2046
+ switch (iso2022jp_decoder_state) {
2047
+ default:
2048
+ case states.ASCII:
2049
+ // ASCII
2050
+ // Based on byte:
2051
+ // 0x1B
2052
+ if (bite === 0x1B) {
2053
+ // Set iso-2022-jp decoder state to escape start and return
2054
+ // continue.
2055
+ iso2022jp_decoder_state = states.EscapeStart;
2056
+ return null;
2057
+ }
2058
+ // 0x00 to 0x7F, excluding 0x0E, 0x0F, and 0x1B
2059
+ if (inRange(bite, 0x00, 0x7F) && bite !== 0x0E
2060
+ && bite !== 0x0F && bite !== 0x1B) {
2061
+ // Unset the iso-2022-jp output flag and return a code point
2062
+ // whose value is byte.
2063
+ iso2022jp_output_flag = false;
2064
+ return bite;
2065
+ }
2066
+ // end-of-stream
2067
+ if (bite === end_of_stream) {
2068
+ // Return finished.
2069
+ return finished;
2070
+ }
2071
+ // Otherwise
2072
+ // Unset the iso-2022-jp output flag and return error.
2073
+ iso2022jp_output_flag = false;
2074
+ return decoderError(fatal);
2075
+ case states.Roman:
2076
+ // Roman
2077
+ // Based on byte:
2078
+ // 0x1B
2079
+ if (bite === 0x1B) {
2080
+ // Set iso-2022-jp decoder state to escape start and return
2081
+ // continue.
2082
+ iso2022jp_decoder_state = states.EscapeStart;
2083
+ return null;
2084
+ }
2085
+ // 0x5C
2086
+ if (bite === 0x5C) {
2087
+ // Unset the iso-2022-jp output flag and return code point
2088
+ // U+00A5.
2089
+ iso2022jp_output_flag = false;
2090
+ return 0x00A5;
2091
+ }
2092
+ // 0x7E
2093
+ if (bite === 0x7E) {
2094
+ // Unset the iso-2022-jp output flag and return code point
2095
+ // U+203E.
2096
+ iso2022jp_output_flag = false;
2097
+ return 0x203E;
2098
+ }
2099
+ // 0x00 to 0x7F, excluding 0x0E, 0x0F, 0x1B, 0x5C, and 0x7E
2100
+ if (inRange(bite, 0x00, 0x7F) && bite !== 0x0E && bite !== 0x0F
2101
+ && bite !== 0x1B && bite !== 0x5C && bite !== 0x7E) {
2102
+ // Unset the iso-2022-jp output flag and return a code point
2103
+ // whose value is byte.
2104
+ iso2022jp_output_flag = false;
2105
+ return bite;
2106
+ }
2107
+ // end-of-stream
2108
+ if (bite === end_of_stream) {
2109
+ // Return finished.
2110
+ return finished;
2111
+ }
2112
+ // Otherwise
2113
+ // Unset the iso-2022-jp output flag and return error.
2114
+ iso2022jp_output_flag = false;
2115
+ return decoderError(fatal);
2116
+ case states.Katakana:
2117
+ // Katakana
2118
+ // Based on byte:
2119
+ // 0x1B
2120
+ if (bite === 0x1B) {
2121
+ // Set iso-2022-jp decoder state to escape start and return
2122
+ // continue.
2123
+ iso2022jp_decoder_state = states.EscapeStart;
2124
+ return null;
2125
+ }
2126
+ // 0x21 to 0x5F
2127
+ if (inRange(bite, 0x21, 0x5F)) {
2128
+ // Unset the iso-2022-jp output flag and return a code point
2129
+ // whose value is 0xFF61 − 0x21 + byte.
2130
+ iso2022jp_output_flag = false;
2131
+ return 0xFF61 - 0x21 + bite;
2132
+ }
2133
+ // end-of-stream
2134
+ if (bite === end_of_stream) {
2135
+ // Return finished.
2136
+ return finished;
2137
+ }
2138
+ // Otherwise
2139
+ // Unset the iso-2022-jp output flag and return error.
2140
+ iso2022jp_output_flag = false;
2141
+ return decoderError(fatal);
2142
+ case states.LeadByte:
2143
+ // Lead byte
2144
+ // Based on byte:
2145
+ // 0x1B
2146
+ if (bite === 0x1B) {
2147
+ // Set iso-2022-jp decoder state to escape start and return
2148
+ // continue.
2149
+ iso2022jp_decoder_state = states.EscapeStart;
2150
+ return null;
2151
+ }
2152
+ // 0x21 to 0x7E
2153
+ if (inRange(bite, 0x21, 0x7E)) {
2154
+ // Unset the iso-2022-jp output flag, set iso-2022-jp lead
2155
+ // to byte, iso-2022-jp decoder state to trail byte, and
2156
+ // return continue.
2157
+ iso2022jp_output_flag = false;
2158
+ iso2022jp_lead = bite;
2159
+ iso2022jp_decoder_state = states.TrailByte;
2160
+ return null;
2161
+ }
2162
+ // end-of-stream
2163
+ if (bite === end_of_stream) {
2164
+ // Return finished.
2165
+ return finished;
2166
+ }
2167
+ // Otherwise
2168
+ // Unset the iso-2022-jp output flag and return error.
2169
+ iso2022jp_output_flag = false;
2170
+ return decoderError(fatal);
2171
+ case states.TrailByte:
2172
+ // Trail byte
2173
+ // Based on byte:
2174
+ // 0x1B
2175
+ if (bite === 0x1B) {
2176
+ // Set iso-2022-jp decoder state to escape start and return
2177
+ // continue.
2178
+ iso2022jp_decoder_state = states.EscapeStart;
2179
+ return decoderError(fatal);
2180
+ }
2181
+ // 0x21 to 0x7E
2182
+ if (inRange(bite, 0x21, 0x7E)) {
2183
+ // 1. Set the iso-2022-jp decoder state to lead byte.
2184
+ iso2022jp_decoder_state = states.LeadByte;
2185
+ // 2. Let pointer be (iso-2022-jp lead − 0x21) × 94 + byte − 0x21.
2186
+ var pointer = (iso2022jp_lead - 0x21) * 94 + bite - 0x21;
2187
+ // 3. Let code point be the index code point for pointer in
2188
+ // index jis0208.
2189
+ var code_point = indexCodePointFor(pointer, index('jis0208'));
2190
+ // 4. If code point is null, return error.
2191
+ if (code_point === null)
2192
+ return decoderError(fatal);
2193
+ // 5. Return a code point whose value is code point.
2194
+ return code_point;
2195
+ }
2196
+ // end-of-stream
2197
+ if (bite === end_of_stream) {
2198
+ // Set the iso-2022-jp decoder state to lead byte, prepend
2199
+ // byte to stream, and return error.
2200
+ iso2022jp_decoder_state = states.LeadByte;
2201
+ stream.prepend(bite);
2202
+ return decoderError(fatal);
2203
+ }
2204
+ // Otherwise
2205
+ // Set iso-2022-jp decoder state to lead byte and return
2206
+ // error.
2207
+ iso2022jp_decoder_state = states.LeadByte;
2208
+ return decoderError(fatal);
2209
+ case states.EscapeStart:
2210
+ // Escape start
2211
+ // 1. If byte is either 0x24 or 0x28, set iso-2022-jp lead to
2212
+ // byte, iso-2022-jp decoder state to escape, and return
2213
+ // continue.
2214
+ if (bite === 0x24 || bite === 0x28) {
2215
+ iso2022jp_lead = bite;
2216
+ iso2022jp_decoder_state = states.Escape;
2217
+ return null;
2218
+ }
2219
+ // 2. Prepend byte to stream.
2220
+ stream.prepend(bite);
2221
+ // 3. Unset the iso-2022-jp output flag, set iso-2022-jp
2222
+ // decoder state to iso-2022-jp decoder output state, and
2223
+ // return error.
2224
+ iso2022jp_output_flag = false;
2225
+ iso2022jp_decoder_state = iso2022jp_decoder_output_state;
2226
+ return decoderError(fatal);
2227
+ case states.Escape:
2228
+ // Escape
2229
+ // 1. Let lead be iso-2022-jp lead and set iso-2022-jp lead to
2230
+ // 0x00.
2231
+ var lead = iso2022jp_lead;
2232
+ iso2022jp_lead = 0x00;
2233
+ // 2. Let state be null.
2234
+ var state = null;
2235
+ // 3. If lead is 0x28 and byte is 0x42, set state to ASCII.
2236
+ if (lead === 0x28 && bite === 0x42)
2237
+ state = states.ASCII;
2238
+ // 4. If lead is 0x28 and byte is 0x4A, set state to Roman.
2239
+ if (lead === 0x28 && bite === 0x4A)
2240
+ state = states.Roman;
2241
+ // 5. If lead is 0x28 and byte is 0x49, set state to Katakana.
2242
+ if (lead === 0x28 && bite === 0x49)
2243
+ state = states.Katakana;
2244
+ // 6. If lead is 0x24 and byte is either 0x40 or 0x42, set
2245
+ // state to lead byte.
2246
+ if (lead === 0x24 && (bite === 0x40 || bite === 0x42))
2247
+ state = states.LeadByte;
2248
+ // 7. If state is non-null, run these substeps:
2249
+ if (state !== null) {
2250
+ // 1. Set iso-2022-jp decoder state and iso-2022-jp decoder
2251
+ // output state to states.
2252
+ iso2022jp_decoder_state = iso2022jp_decoder_state = state;
2253
+ // 2. Let output flag be the iso-2022-jp output flag.
2254
+ var output_flag = iso2022jp_output_flag;
2255
+ // 3. Set the iso-2022-jp output flag.
2256
+ iso2022jp_output_flag = true;
2257
+ // 4. Return continue, if output flag is unset, and error
2258
+ // otherwise.
2259
+ return !output_flag ? null : decoderError(fatal);
2260
+ }
2261
+ // 8. Prepend lead and byte to stream.
2262
+ stream.prepend([lead, bite]);
2263
+ // 9. Unset the iso-2022-jp output flag, set iso-2022-jp
2264
+ // decoder state to iso-2022-jp decoder output state and
2265
+ // return error.
2266
+ iso2022jp_output_flag = false;
2267
+ iso2022jp_decoder_state = iso2022jp_decoder_output_state;
2268
+ return decoderError(fatal);
2269
+ }
2270
+ };
2271
+ }
2272
+ // 13.2.2 iso-2022-jp encoder
2273
+ /**
2274
+ * @constructor
2275
+ * @implements {Encoder}
2276
+ * @param {{fatal: boolean}} options
2277
+ */
2278
+ function ISO2022JPEncoder(options) {
2279
+ var fatal = options.fatal;
2280
+ // iso-2022-jp's encoder has an associated iso-2022-jp encoder
2281
+ // state which is one of ASCII, Roman, and jis0208 (initially
2282
+ // ASCII).
2283
+ /** @enum */
2284
+ var states = {
2285
+ ASCII: 0,
2286
+ Roman: 1,
2287
+ jis0208: 2
2288
+ };
2289
+ var /** @type {number} */ iso2022jp_state = states.ASCII;
2290
+ /**
2291
+ * @param {Stream} stream Input stream.
2292
+ * @param {number} code_point Next code point read from the stream.
2293
+ * @return {(number|!Array.<number>)} Byte(s) to emit.
2294
+ */
2295
+ this.handler = function (stream, code_point) {
2296
+ // 1. If code point is end-of-stream and iso-2022-jp encoder
2297
+ // state is not ASCII, prepend code point to stream, set
2298
+ // iso-2022-jp encoder state to ASCII, and return three bytes
2299
+ // 0x1B 0x28 0x42.
2300
+ if (code_point === end_of_stream &&
2301
+ iso2022jp_state !== states.ASCII) {
2302
+ stream.prepend(code_point);
2303
+ iso2022jp_state = states.ASCII;
2304
+ return [0x1B, 0x28, 0x42];
2305
+ }
2306
+ // 2. If code point is end-of-stream and iso-2022-jp encoder
2307
+ // state is ASCII, return finished.
2308
+ if (code_point === end_of_stream && iso2022jp_state === states.ASCII)
2309
+ return finished;
2310
+ // 3. If ISO-2022-JP encoder state is ASCII or Roman, and code
2311
+ // point is U+000E, U+000F, or U+001B, return error with U+FFFD.
2312
+ if ((iso2022jp_state === states.ASCII ||
2313
+ iso2022jp_state === states.Roman) &&
2314
+ (code_point === 0x000E || code_point === 0x000F ||
2315
+ code_point === 0x001B)) {
2316
+ return encoderError(0xFFFD);
2317
+ }
2318
+ // 4. If iso-2022-jp encoder state is ASCII and code point is an
2319
+ // ASCII code point, return a byte whose value is code point.
2320
+ if (iso2022jp_state === states.ASCII &&
2321
+ isASCIICodePoint(code_point))
2322
+ return code_point;
2323
+ // 5. If iso-2022-jp encoder state is Roman and code point is an
2324
+ // ASCII code point, excluding U+005C and U+007E, or is U+00A5
2325
+ // or U+203E, run these substeps:
2326
+ if (iso2022jp_state === states.Roman &&
2327
+ ((isASCIICodePoint(code_point) &&
2328
+ code_point !== 0x005C && code_point !== 0x007E) ||
2329
+ (code_point == 0x00A5 || code_point == 0x203E))) {
2330
+ // 1. If code point is an ASCII code point, return a byte
2331
+ // whose value is code point.
2332
+ if (isASCIICodePoint(code_point))
2333
+ return code_point;
2334
+ // 2. If code point is U+00A5, return byte 0x5C.
2335
+ if (code_point === 0x00A5)
2336
+ return 0x5C;
2337
+ // 3. If code point is U+203E, return byte 0x7E.
2338
+ if (code_point === 0x203E)
2339
+ return 0x7E;
2340
+ }
2341
+ // 6. If code point is an ASCII code point, and iso-2022-jp
2342
+ // encoder state is not ASCII, prepend code point to stream, set
2343
+ // iso-2022-jp encoder state to ASCII, and return three bytes
2344
+ // 0x1B 0x28 0x42.
2345
+ if (isASCIICodePoint(code_point) &&
2346
+ iso2022jp_state !== states.ASCII) {
2347
+ stream.prepend(code_point);
2348
+ iso2022jp_state = states.ASCII;
2349
+ return [0x1B, 0x28, 0x42];
2350
+ }
2351
+ // 7. If code point is either U+00A5 or U+203E, and iso-2022-jp
2352
+ // encoder state is not Roman, prepend code point to stream, set
2353
+ // iso-2022-jp encoder state to Roman, and return three bytes
2354
+ // 0x1B 0x28 0x4A.
2355
+ if ((code_point === 0x00A5 || code_point === 0x203E) &&
2356
+ iso2022jp_state !== states.Roman) {
2357
+ stream.prepend(code_point);
2358
+ iso2022jp_state = states.Roman;
2359
+ return [0x1B, 0x28, 0x4A];
2360
+ }
2361
+ // 8. If code point is U+2212, set it to U+FF0D.
2362
+ if (code_point === 0x2212)
2363
+ code_point = 0xFF0D;
2364
+ // 9. Let pointer be the index pointer for code point in index
2365
+ // jis0208.
2366
+ var pointer = indexPointerFor(code_point, index('jis0208'));
2367
+ // 10. If pointer is null, return error with code point.
2368
+ if (pointer === null)
2369
+ return encoderError(code_point);
2370
+ // 11. If iso-2022-jp encoder state is not jis0208, prepend code
2371
+ // point to stream, set iso-2022-jp encoder state to jis0208,
2372
+ // and return three bytes 0x1B 0x24 0x42.
2373
+ if (iso2022jp_state !== states.jis0208) {
2374
+ stream.prepend(code_point);
2375
+ iso2022jp_state = states.jis0208;
2376
+ return [0x1B, 0x24, 0x42];
2377
+ }
2378
+ // 12. Let lead be floor(pointer / 94) + 0x21.
2379
+ var lead = floor(pointer / 94) + 0x21;
2380
+ // 13. Let trail be pointer % 94 + 0x21.
2381
+ var trail = pointer % 94 + 0x21;
2382
+ // 14. Return two bytes whose values are lead and trail.
2383
+ return [lead, trail];
2384
+ };
2385
+ }
2386
+ /** @param {{fatal: boolean}} options */
2387
+ encoders['ISO-2022-JP'] = function (options) {
2388
+ return new ISO2022JPEncoder(options);
2389
+ };
2390
+ /** @param {{fatal: boolean}} options */
2391
+ decoders['ISO-2022-JP'] = function (options) {
2392
+ return new ISO2022JPDecoder(options);
2393
+ };
2394
+ // 13.3 Shift_JIS
2395
+ // 13.3.1 Shift_JIS decoder
2396
+ /**
2397
+ * @constructor
2398
+ * @implements {Decoder}
2399
+ * @param {{fatal: boolean}} options
2400
+ */
2401
+ function ShiftJISDecoder(options) {
2402
+ var fatal = options.fatal;
2403
+ // Shift_JIS's decoder has an associated Shift_JIS lead (initially
2404
+ // 0x00).
2405
+ var /** @type {number} */ Shift_JIS_lead = 0x00;
2406
+ /**
2407
+ * @param {Stream} stream The stream of bytes being decoded.
2408
+ * @param {number} bite The next byte read from the stream.
2409
+ * @return {?(number|!Array.<number>)} The next code point(s)
2410
+ * decoded, or null if not enough data exists in the input
2411
+ * stream to decode a complete code point.
2412
+ */
2413
+ this.handler = function (stream, bite) {
2414
+ // 1. If byte is end-of-stream and Shift_JIS lead is not 0x00,
2415
+ // set Shift_JIS lead to 0x00 and return error.
2416
+ if (bite === end_of_stream && Shift_JIS_lead !== 0x00) {
2417
+ Shift_JIS_lead = 0x00;
2418
+ return decoderError(fatal);
2419
+ }
2420
+ // 2. If byte is end-of-stream and Shift_JIS lead is 0x00,
2421
+ // return finished.
2422
+ if (bite === end_of_stream && Shift_JIS_lead === 0x00)
2423
+ return finished;
2424
+ // 3. If Shift_JIS lead is not 0x00, let lead be Shift_JIS lead,
2425
+ // let pointer be null, set Shift_JIS lead to 0x00, and then run
2426
+ // these substeps:
2427
+ if (Shift_JIS_lead !== 0x00) {
2428
+ var lead = Shift_JIS_lead;
2429
+ var pointer = null;
2430
+ Shift_JIS_lead = 0x00;
2431
+ // 1. Let offset be 0x40, if byte is less than 0x7F, and 0x41
2432
+ // otherwise.
2433
+ var offset = (bite < 0x7F) ? 0x40 : 0x41;
2434
+ // 2. Let lead offset be 0x81, if lead is less than 0xA0, and
2435
+ // 0xC1 otherwise.
2436
+ var lead_offset = (lead < 0xA0) ? 0x81 : 0xC1;
2437
+ // 3. If byte is in the range 0x40 to 0x7E, inclusive, or 0x80
2438
+ // to 0xFC, inclusive, set pointer to (lead − lead offset) ×
2439
+ // 188 + byte − offset.
2440
+ if (inRange(bite, 0x40, 0x7E) || inRange(bite, 0x80, 0xFC))
2441
+ pointer = (lead - lead_offset) * 188 + bite - offset;
2442
+ // 4. If pointer is in the range 8836 to 10715, inclusive,
2443
+ // return a code point whose value is 0xE000 − 8836 + pointer.
2444
+ if (inRange(pointer, 8836, 10715))
2445
+ return 0xE000 - 8836 + pointer;
2446
+ // 5. Let code point be null, if pointer is null, and the
2447
+ // index code point for pointer in index jis0208 otherwise.
2448
+ var code_point = (pointer === null) ? null :
2449
+ indexCodePointFor(pointer, index('jis0208'));
2450
+ // 6. If code point is null and byte is an ASCII byte, prepend
2451
+ // byte to stream.
2452
+ if (code_point === null && isASCIIByte(bite))
2453
+ stream.prepend(bite);
2454
+ // 7. If code point is null, return error.
2455
+ if (code_point === null)
2456
+ return decoderError(fatal);
2457
+ // 8. Return a code point whose value is code point.
2458
+ return code_point;
2459
+ }
2460
+ // 4. If byte is an ASCII byte or 0x80, return a code point
2461
+ // whose value is byte.
2462
+ if (isASCIIByte(bite) || bite === 0x80)
2463
+ return bite;
2464
+ // 5. If byte is in the range 0xA1 to 0xDF, inclusive, return a
2465
+ // code point whose value is 0xFF61 − 0xA1 + byte.
2466
+ if (inRange(bite, 0xA1, 0xDF))
2467
+ return 0xFF61 - 0xA1 + bite;
2468
+ // 6. If byte is in the range 0x81 to 0x9F, inclusive, or 0xE0
2469
+ // to 0xFC, inclusive, set Shift_JIS lead to byte and return
2470
+ // continue.
2471
+ if (inRange(bite, 0x81, 0x9F) || inRange(bite, 0xE0, 0xFC)) {
2472
+ Shift_JIS_lead = bite;
2473
+ return null;
2474
+ }
2475
+ // 7. Return error.
2476
+ return decoderError(fatal);
2477
+ };
2478
+ }
2479
+ // 13.3.2 Shift_JIS encoder
2480
+ /**
2481
+ * @constructor
2482
+ * @implements {Encoder}
2483
+ * @param {{fatal: boolean}} options
2484
+ */
2485
+ function ShiftJISEncoder(options) {
2486
+ var fatal = options.fatal;
2487
+ /**
2488
+ * @param {Stream} stream Input stream.
2489
+ * @param {number} code_point Next code point read from the stream.
2490
+ * @return {(number|!Array.<number>)} Byte(s) to emit.
2491
+ */
2492
+ this.handler = function (stream, code_point) {
2493
+ // 1. If code point is end-of-stream, return finished.
2494
+ if (code_point === end_of_stream)
2495
+ return finished;
2496
+ // 2. If code point is an ASCII code point or U+0080, return a
2497
+ // byte whose value is code point.
2498
+ if (isASCIICodePoint(code_point) || code_point === 0x0080)
2499
+ return code_point;
2500
+ // 3. If code point is U+00A5, return byte 0x5C.
2501
+ if (code_point === 0x00A5)
2502
+ return 0x5C;
2503
+ // 4. If code point is U+203E, return byte 0x7E.
2504
+ if (code_point === 0x203E)
2505
+ return 0x7E;
2506
+ // 5. If code point is in the range U+FF61 to U+FF9F, inclusive,
2507
+ // return a byte whose value is code point − 0xFF61 + 0xA1.
2508
+ if (inRange(code_point, 0xFF61, 0xFF9F))
2509
+ return code_point - 0xFF61 + 0xA1;
2510
+ // 6. If code point is U+2212, set it to U+FF0D.
2511
+ if (code_point === 0x2212)
2512
+ code_point = 0xFF0D;
2513
+ // 7. Let pointer be the index Shift_JIS pointer for code point.
2514
+ var pointer = indexShiftJISPointerFor(code_point);
2515
+ // 8. If pointer is null, return error with code point.
2516
+ if (pointer === null)
2517
+ return encoderError(code_point);
2518
+ // 9. Let lead be floor(pointer / 188).
2519
+ var lead = floor(pointer / 188);
2520
+ // 10. Let lead offset be 0x81, if lead is less than 0x1F, and
2521
+ // 0xC1 otherwise.
2522
+ var lead_offset = (lead < 0x1F) ? 0x81 : 0xC1;
2523
+ // 11. Let trail be pointer % 188.
2524
+ var trail = pointer % 188;
2525
+ // 12. Let offset be 0x40, if trail is less than 0x3F, and 0x41
2526
+ // otherwise.
2527
+ var offset = (trail < 0x3F) ? 0x40 : 0x41;
2528
+ // 13. Return two bytes whose values are lead + lead offset and
2529
+ // trail + offset.
2530
+ return [lead + lead_offset, trail + offset];
2531
+ };
2532
+ }
2533
+ /** @param {{fatal: boolean}} options */
2534
+ encoders['Shift_JIS'] = function (options) {
2535
+ return new ShiftJISEncoder(options);
2536
+ };
2537
+ /** @param {{fatal: boolean}} options */
2538
+ decoders['Shift_JIS'] = function (options) {
2539
+ return new ShiftJISDecoder(options);
2540
+ };
2541
+ //
2542
+ // 14. Legacy multi-byte Korean encodings
2543
+ //
2544
+ // 14.1 euc-kr
2545
+ // 14.1.1 euc-kr decoder
2546
+ /**
2547
+ * @constructor
2548
+ * @implements {Decoder}
2549
+ * @param {{fatal: boolean}} options
2550
+ */
2551
+ function EUCKRDecoder(options) {
2552
+ var fatal = options.fatal;
2553
+ // euc-kr's decoder has an associated euc-kr lead (initially 0x00).
2554
+ var /** @type {number} */ euckr_lead = 0x00;
2555
+ /**
2556
+ * @param {Stream} stream The stream of bytes being decoded.
2557
+ * @param {number} bite The next byte read from the stream.
2558
+ * @return {?(number|!Array.<number>)} The next code point(s)
2559
+ * decoded, or null if not enough data exists in the input
2560
+ * stream to decode a complete code point.
2561
+ */
2562
+ this.handler = function (stream, bite) {
2563
+ // 1. If byte is end-of-stream and euc-kr lead is not 0x00, set
2564
+ // euc-kr lead to 0x00 and return error.
2565
+ if (bite === end_of_stream && euckr_lead !== 0) {
2566
+ euckr_lead = 0x00;
2567
+ return decoderError(fatal);
2568
+ }
2569
+ // 2. If byte is end-of-stream and euc-kr lead is 0x00, return
2570
+ // finished.
2571
+ if (bite === end_of_stream && euckr_lead === 0)
2572
+ return finished;
2573
+ // 3. If euc-kr lead is not 0x00, let lead be euc-kr lead, let
2574
+ // pointer be null, set euc-kr lead to 0x00, and then run these
2575
+ // substeps:
2576
+ if (euckr_lead !== 0x00) {
2577
+ var lead = euckr_lead;
2578
+ var pointer = null;
2579
+ euckr_lead = 0x00;
2580
+ // 1. If byte is in the range 0x41 to 0xFE, inclusive, set
2581
+ // pointer to (lead − 0x81) × 190 + (byte − 0x41).
2582
+ if (inRange(bite, 0x41, 0xFE))
2583
+ pointer = (lead - 0x81) * 190 + (bite - 0x41);
2584
+ // 2. Let code point be null, if pointer is null, and the
2585
+ // index code point for pointer in index euc-kr otherwise.
2586
+ var code_point = (pointer === null)
2587
+ ? null : indexCodePointFor(pointer, index('euc-kr'));
2588
+ // 3. If code point is null and byte is an ASCII byte, prepend
2589
+ // byte to stream.
2590
+ if (pointer === null && isASCIIByte(bite))
2591
+ stream.prepend(bite);
2592
+ // 4. If code point is null, return error.
2593
+ if (code_point === null)
2594
+ return decoderError(fatal);
2595
+ // 5. Return a code point whose value is code point.
2596
+ return code_point;
2597
+ }
2598
+ // 4. If byte is an ASCII byte, return a code point whose value
2599
+ // is byte.
2600
+ if (isASCIIByte(bite))
2601
+ return bite;
2602
+ // 5. If byte is in the range 0x81 to 0xFE, inclusive, set
2603
+ // euc-kr lead to byte and return continue.
2604
+ if (inRange(bite, 0x81, 0xFE)) {
2605
+ euckr_lead = bite;
2606
+ return null;
2607
+ }
2608
+ // 6. Return error.
2609
+ return decoderError(fatal);
2610
+ };
2611
+ }
2612
+ // 14.1.2 euc-kr encoder
2613
+ /**
2614
+ * @constructor
2615
+ * @implements {Encoder}
2616
+ * @param {{fatal: boolean}} options
2617
+ */
2618
+ function EUCKREncoder(options) {
2619
+ var fatal = options.fatal;
2620
+ /**
2621
+ * @param {Stream} stream Input stream.
2622
+ * @param {number} code_point Next code point read from the stream.
2623
+ * @return {(number|!Array.<number>)} Byte(s) to emit.
2624
+ */
2625
+ this.handler = function (stream, code_point) {
2626
+ // 1. If code point is end-of-stream, return finished.
2627
+ if (code_point === end_of_stream)
2628
+ return finished;
2629
+ // 2. If code point is an ASCII code point, return a byte whose
2630
+ // value is code point.
2631
+ if (isASCIICodePoint(code_point))
2632
+ return code_point;
2633
+ // 3. Let pointer be the index pointer for code point in index
2634
+ // euc-kr.
2635
+ var pointer = indexPointerFor(code_point, index('euc-kr'));
2636
+ // 4. If pointer is null, return error with code point.
2637
+ if (pointer === null)
2638
+ return encoderError(code_point);
2639
+ // 5. Let lead be floor(pointer / 190) + 0x81.
2640
+ var lead = floor(pointer / 190) + 0x81;
2641
+ // 6. Let trail be pointer % 190 + 0x41.
2642
+ var trail = (pointer % 190) + 0x41;
2643
+ // 7. Return two bytes whose values are lead and trail.
2644
+ return [lead, trail];
2645
+ };
2646
+ }
2647
+ /** @param {{fatal: boolean}} options */
2648
+ encoders['EUC-KR'] = function (options) {
2649
+ return new EUCKREncoder(options);
2650
+ };
2651
+ /** @param {{fatal: boolean}} options */
2652
+ decoders['EUC-KR'] = function (options) {
2653
+ return new EUCKRDecoder(options);
2654
+ };
2655
+ //
2656
+ // 15. Legacy miscellaneous encodings
2657
+ //
2658
+ // 15.1 replacement
2659
+ // Not needed - API throws RangeError
2660
+ // 15.2 Common infrastructure for utf-16be and utf-16le
2661
+ /**
2662
+ * @param {number} code_unit
2663
+ * @param {boolean} utf16be
2664
+ * @return {!Array.<number>} bytes
2665
+ */
2666
+ function convertCodeUnitToBytes(code_unit, utf16be) {
2667
+ // 1. Let byte1 be code unit >> 8.
2668
+ var byte1 = code_unit >> 8;
2669
+ // 2. Let byte2 be code unit & 0x00FF.
2670
+ var byte2 = code_unit & 0x00FF;
2671
+ // 3. Then return the bytes in order:
2672
+ // utf-16be flag is set: byte1, then byte2.
2673
+ if (utf16be)
2674
+ return [byte1, byte2];
2675
+ // utf-16be flag is unset: byte2, then byte1.
2676
+ return [byte2, byte1];
2677
+ }
2678
+ // 15.2.1 shared utf-16 decoder
2679
+ /**
2680
+ * @constructor
2681
+ * @implements {Decoder}
2682
+ * @param {boolean} utf16_be True if big-endian, false if little-endian.
2683
+ * @param {{fatal: boolean}} options
2684
+ */
2685
+ function UTF16Decoder(utf16_be, options) {
2686
+ var fatal = options.fatal;
2687
+ var /** @type {?number} */ utf16_lead_byte = null,
2688
+ /** @type {?number} */ utf16_lead_surrogate = null;
2689
+ /**
2690
+ * @param {Stream} stream The stream of bytes being decoded.
2691
+ * @param {number} bite The next byte read from the stream.
2692
+ * @return {?(number|!Array.<number>)} The next code point(s)
2693
+ * decoded, or null if not enough data exists in the input
2694
+ * stream to decode a complete code point.
2695
+ */
2696
+ this.handler = function (stream, bite) {
2697
+ // 1. If byte is end-of-stream and either utf-16 lead byte or
2698
+ // utf-16 lead surrogate is not null, set utf-16 lead byte and
2699
+ // utf-16 lead surrogate to null, and return error.
2700
+ if (bite === end_of_stream && (utf16_lead_byte !== null ||
2701
+ utf16_lead_surrogate !== null)) {
2702
+ return decoderError(fatal);
2703
+ }
2704
+ // 2. If byte is end-of-stream and utf-16 lead byte and utf-16
2705
+ // lead surrogate are null, return finished.
2706
+ if (bite === end_of_stream && utf16_lead_byte === null &&
2707
+ utf16_lead_surrogate === null) {
2708
+ return finished;
2709
+ }
2710
+ // 3. If utf-16 lead byte is null, set utf-16 lead byte to byte
2711
+ // and return continue.
2712
+ if (utf16_lead_byte === null) {
2713
+ utf16_lead_byte = bite;
2714
+ return null;
2715
+ }
2716
+ // 4. Let code unit be the result of:
2717
+ var code_unit;
2718
+ if (utf16_be) {
2719
+ // utf-16be decoder flag is set
2720
+ // (utf-16 lead byte << 8) + byte.
2721
+ code_unit = (utf16_lead_byte << 8) + bite;
2722
+ }
2723
+ else {
2724
+ // utf-16be decoder flag is unset
2725
+ // (byte << 8) + utf-16 lead byte.
2726
+ code_unit = (bite << 8) + utf16_lead_byte;
2727
+ }
2728
+ // Then set utf-16 lead byte to null.
2729
+ utf16_lead_byte = null;
2730
+ // 5. If utf-16 lead surrogate is not null, let lead surrogate
2731
+ // be utf-16 lead surrogate, set utf-16 lead surrogate to null,
2732
+ // and then run these substeps:
2733
+ if (utf16_lead_surrogate !== null) {
2734
+ var lead_surrogate = utf16_lead_surrogate;
2735
+ utf16_lead_surrogate = null;
2736
+ // 1. If code unit is in the range U+DC00 to U+DFFF,
2737
+ // inclusive, return a code point whose value is 0x10000 +
2738
+ // ((lead surrogate − 0xD800) << 10) + (code unit − 0xDC00).
2739
+ if (inRange(code_unit, 0xDC00, 0xDFFF)) {
2740
+ return 0x10000 + (lead_surrogate - 0xD800) * 0x400 +
2741
+ (code_unit - 0xDC00);
2742
+ }
2743
+ // 2. Prepend the sequence resulting of converting code unit
2744
+ // to bytes using utf-16be decoder flag to stream and return
2745
+ // error.
2746
+ stream.prepend(convertCodeUnitToBytes(code_unit, utf16_be));
2747
+ return decoderError(fatal);
2748
+ }
2749
+ // 6. If code unit is in the range U+D800 to U+DBFF, inclusive,
2750
+ // set utf-16 lead surrogate to code unit and return continue.
2751
+ if (inRange(code_unit, 0xD800, 0xDBFF)) {
2752
+ utf16_lead_surrogate = code_unit;
2753
+ return null;
2754
+ }
2755
+ // 7. If code unit is in the range U+DC00 to U+DFFF, inclusive,
2756
+ // return error.
2757
+ if (inRange(code_unit, 0xDC00, 0xDFFF))
2758
+ return decoderError(fatal);
2759
+ // 8. Return code point code unit.
2760
+ return code_unit;
2761
+ };
2762
+ }
2763
+ // 15.2.2 shared utf-16 encoder
2764
+ /**
2765
+ * @constructor
2766
+ * @implements {Encoder}
2767
+ * @param {boolean} utf16_be True if big-endian, false if little-endian.
2768
+ * @param {{fatal: boolean}} options
2769
+ */
2770
+ function UTF16Encoder(utf16_be, options) {
2771
+ var fatal = options.fatal;
2772
+ /**
2773
+ * @param {Stream} stream Input stream.
2774
+ * @param {number} code_point Next code point read from the stream.
2775
+ * @return {(number|!Array.<number>)} Byte(s) to emit.
2776
+ */
2777
+ this.handler = function (stream, code_point) {
2778
+ // 1. If code point is end-of-stream, return finished.
2779
+ if (code_point === end_of_stream)
2780
+ return finished;
2781
+ // 2. If code point is in the range U+0000 to U+FFFF, inclusive,
2782
+ // return the sequence resulting of converting code point to
2783
+ // bytes using utf-16be encoder flag.
2784
+ if (inRange(code_point, 0x0000, 0xFFFF))
2785
+ return convertCodeUnitToBytes(code_point, utf16_be);
2786
+ // 3. Let lead be ((code point − 0x10000) >> 10) + 0xD800,
2787
+ // converted to bytes using utf-16be encoder flag.
2788
+ var lead = convertCodeUnitToBytes(((code_point - 0x10000) >> 10) + 0xD800, utf16_be);
2789
+ // 4. Let trail be ((code point − 0x10000) & 0x3FF) + 0xDC00,
2790
+ // converted to bytes using utf-16be encoder flag.
2791
+ var trail = convertCodeUnitToBytes(((code_point - 0x10000) & 0x3FF) + 0xDC00, utf16_be);
2792
+ // 5. Return a byte sequence of lead followed by trail.
2793
+ return lead.concat(trail);
2794
+ };
2795
+ }
2796
+ // 15.3 utf-16be
2797
+ // 15.3.1 utf-16be decoder
2798
+ /** @param {{fatal: boolean}} options */
2799
+ encoders['UTF-16BE'] = function (options) {
2800
+ return new UTF16Encoder(true, options);
2801
+ };
2802
+ // 15.3.2 utf-16be encoder
2803
+ /** @param {{fatal: boolean}} options */
2804
+ decoders['UTF-16BE'] = function (options) {
2805
+ return new UTF16Decoder(true, options);
2806
+ };
2807
+ // 15.4 utf-16le
2808
+ // 15.4.1 utf-16le decoder
2809
+ /** @param {{fatal: boolean}} options */
2810
+ encoders['UTF-16LE'] = function (options) {
2811
+ return new UTF16Encoder(false, options);
2812
+ };
2813
+ // 15.4.2 utf-16le encoder
2814
+ /** @param {{fatal: boolean}} options */
2815
+ decoders['UTF-16LE'] = function (options) {
2816
+ return new UTF16Decoder(false, options);
2817
+ };
2818
+ // 15.5 x-user-defined
2819
+ // 15.5.1 x-user-defined decoder
2820
+ /**
2821
+ * @constructor
2822
+ * @implements {Decoder}
2823
+ * @param {{fatal: boolean}} options
2824
+ */
2825
+ function XUserDefinedDecoder(options) {
2826
+ var fatal = options.fatal;
2827
+ /**
2828
+ * @param {Stream} stream The stream of bytes being decoded.
2829
+ * @param {number} bite The next byte read from the stream.
2830
+ * @return {?(number|!Array.<number>)} The next code point(s)
2831
+ * decoded, or null if not enough data exists in the input
2832
+ * stream to decode a complete code point.
2833
+ */
2834
+ this.handler = function (stream, bite) {
2835
+ // 1. If byte is end-of-stream, return finished.
2836
+ if (bite === end_of_stream)
2837
+ return finished;
2838
+ // 2. If byte is an ASCII byte, return a code point whose value
2839
+ // is byte.
2840
+ if (isASCIIByte(bite))
2841
+ return bite;
2842
+ // 3. Return a code point whose value is 0xF780 + byte − 0x80.
2843
+ return 0xF780 + bite - 0x80;
2844
+ };
2845
+ }
2846
+ // 15.5.2 x-user-defined encoder
2847
+ /**
2848
+ * @constructor
2849
+ * @implements {Encoder}
2850
+ * @param {{fatal: boolean}} options
2851
+ */
2852
+ function XUserDefinedEncoder(options) {
2853
+ var fatal = options.fatal;
2854
+ /**
2855
+ * @param {Stream} stream Input stream.
2856
+ * @param {number} code_point Next code point read from the stream.
2857
+ * @return {(number|!Array.<number>)} Byte(s) to emit.
2858
+ */
2859
+ this.handler = function (stream, code_point) {
2860
+ // 1.If code point is end-of-stream, return finished.
2861
+ if (code_point === end_of_stream)
2862
+ return finished;
2863
+ // 2. If code point is an ASCII code point, return a byte whose
2864
+ // value is code point.
2865
+ if (isASCIICodePoint(code_point))
2866
+ return code_point;
2867
+ // 3. If code point is in the range U+F780 to U+F7FF, inclusive,
2868
+ // return a byte whose value is code point − 0xF780 + 0x80.
2869
+ if (inRange(code_point, 0xF780, 0xF7FF))
2870
+ return code_point - 0xF780 + 0x80;
2871
+ // 4. Return error with code point.
2872
+ return encoderError(code_point);
2873
+ };
2874
+ }
2875
+ /** @param {{fatal: boolean}} options */
2876
+ encoders['x-user-defined'] = function (options) {
2877
+ return new XUserDefinedEncoder(options);
2878
+ };
2879
+ /** @param {{fatal: boolean}} options */
2880
+ decoders['x-user-defined'] = function (options) {
2881
+ return new XUserDefinedDecoder(options);
2882
+ };
2883
+ if (!global['TextEncoder'])
2884
+ global['TextEncoder'] = TextEncoder;
2885
+ if (!global['TextDecoder'])
2886
+ global['TextDecoder'] = TextDecoder;
2887
+ if (typeof module !== "undefined" && module.exports) {
2888
+ module.exports = {
2889
+ TextEncoder: global['TextEncoder'],
2890
+ TextDecoder: global['TextDecoder'],
2891
+ EncodingIndexes: global["encoding-indexes"]
2892
+ };
2893
+ }
2894
+ // For strict environments where `this` inside the global scope
2895
+ // is `undefined`, take a pure object instead
2896
+ }(this || {}));
2897
+ //# sourceMappingURL=encoding.js.map