• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

visgl / loaders.gl / 25798238260

13 May 2026 12:10PM UTC coverage: 60.607% (+0.3%) from 60.27%
25798238260

push

github

web-flow
feat(json) GeoJSON -> geoarrow, schema, logging  (#3399)

13466 of 24516 branches covered (54.93%)

Branch coverage included in aggregate %.

448 of 541 new or added lines in 12 files covered. (82.81%)

1264 existing lines in 117 files now uncovered.

27516 of 43103 relevant lines covered (63.84%)

15056.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

4.66
/modules/polyfills/src/text-encoder/text-encoder.ts
1
// @ts-nocheck
2
/* eslint-disable */
3

4
// Copied from https://github.com/inexorabletash/text-encoding/blob/b4e5bc26e26e51f56e3daa9f13138c79f49d3c34/lib/encoding.js
5
// This is free and unencumbered software released into the public domain.
6
// See LICENSE.md for more information.
7

8
import indexes from './encoding-indexes';
9
// Note: Aaian character indices add half a megabyte to bundle. Ignore, since we really only want the built-in UTF8...
10
// import indexes from './encoding-indexes-asian.js';
UNCOV
11
globalThis['encoding-indexes'] = indexes || {};
379!
12

13
//
14
// Utilities
15
//
16

17
/**
18
 * @param {number} a The number to test.
19
 * @param {number} min The minimum value in the range, inclusive.
20
 * @param {number} max The maximum value in the range, inclusive.
21
 * @return {boolean} True if a >= min and a <= max.
22
 */
23
function inRange(a, min, max) {
24
  return min <= a && a <= max;
×
25
}
26

27
/**
28
 * @param {!Array.<*>} array The array to check.
29
 * @param {*} item The item to look for in the array.
30
 * @return {boolean} True if the item appears in the array.
31
 */
32
function includes(array, item) {
33
  return array.indexOf(item) !== -1;
×
34
}
35

UNCOV
36
var floor = Math.floor;
379✔
37

38
/**
39
 * @param {*} o
40
 * @return {Object}
41
 */
42
function ToDictionary(o) {
43
  if (o === undefined) return {};
×
44
  if (o === Object(o)) return o;
×
45
  throw TypeError('Could not convert argument to dictionary');
×
46
}
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

55
  // 1. Let S be the DOMString value.
56
  var s = String(string);
×
57

58
  // 2. Let n be the length of S.
59
  var n = s.length;
×
60

61
  // 3. Initialize i to 0.
62
  var i = 0;
×
63

64
  // 4. Initialize U to be an empty sequence of Unicode characters.
65
  var u = [];
×
66

67
  // 5. While i < n:
68
  while (i < n) {
×
69
    // 1. Let c be the code unit in S at index i.
70
    var c = s.charCodeAt(i);
×
71

72
    // 2. Depending on the value of c:
73

74
    // c < 0xD800 or c > 0xDFFF
75
    if (c < 0xd800 || c > 0xdfff) {
×
76
      // Append to U the Unicode character with code point c.
77
      u.push(c);
×
78
    }
79

80
    // 0xDC00 ≤ c ≤ 0xDFFF
81
    else if (0xdc00 <= c && c <= 0xdfff) {
×
82
      // Append to U a U+FFFD REPLACEMENT CHARACTER.
83
      u.push(0xfffd);
×
84
    }
85

86
    // 0xD800 ≤ c ≤ 0xDBFF
87
    else if (0xd800 <= c && c <= 0xdbff) {
×
88
      // 1. If i = n−1, then append to U a U+FFFD REPLACEMENT
89
      // CHARACTER.
90
      if (i === n - 1) {
×
91
        u.push(0xfffd);
×
92
      }
93
      // 2. Otherwise, i < n−1:
94
      else {
95
        // 1. Let d be the code unit in S at index i+1.
96
        var d = s.charCodeAt(i + 1);
×
97

98
        // 2. If 0xDC00 ≤ d ≤ 0xDFFF, then:
99
        if (0xdc00 <= d && d <= 0xdfff) {
×
100
          // 1. Let a be c & 0x3FF.
101
          var a = c & 0x3ff;
×
102

103
          // 2. Let b be d & 0x3FF.
104
          var b = d & 0x3ff;
×
105

106
          // 3. Append to U the Unicode character with code point
107
          // 2^16+2^10*a+b.
108
          u.push(0x10000 + (a << 10) + b);
×
109

110
          // 4. Set i to i+1.
111
          i += 1;
×
112
        }
113

114
        // 3. Otherwise, d < 0xDC00 or d > 0xDFFF. Append to U a
115
        // U+FFFD REPLACEMENT CHARACTER.
116
        else {
117
          u.push(0xfffd);
×
118
        }
119
      }
120
    }
121

122
    // 3. Set i to i+1.
123
    i += 1;
×
124
  }
125

126
  // 6. Return U.
127
  return u;
×
128
}
129

130
/**
131
 * @param {!Array.<number>} code_points Array of code points.
132
 * @return {string} string String of UTF-16 code units.
133
 */
134
function codePointsToString(code_points) {
135
  var s = '';
×
136
  for (var i = 0; i < code_points.length; ++i) {
×
137
    var cp = code_points[i];
×
138
    if (cp <= 0xffff) {
×
139
      s += String.fromCharCode(cp);
×
140
    } else {
141
      cp -= 0x10000;
×
142
      s += String.fromCharCode((cp >> 10) + 0xd800, (cp & 0x3ff) + 0xdc00);
×
143
    }
144
  }
145
  return s;
×
146
}
147

148
//
149
// Implementation of Encoding specification
150
// https://encoding.spec.whatwg.org/
151
//
152

153
//
154
// 4. Terminology
155
//
156

157
/**
158
 * An ASCII byte is a byte in the range 0x00 to 0x7F, inclusive.
159
 * @param {number} a The number to test.
160
 * @return {boolean} True if a is in the range 0x00 to 0x7F, inclusive.
161
 */
162
function isASCIIByte(a) {
163
  return 0x00 <= a && a <= 0x7f;
×
164
}
165

166
/**
167
 * An ASCII code point is a code point in the range U+0000 to
168
 * U+007F, inclusive.
169
 */
UNCOV
170
var isASCIICodePoint = isASCIIByte;
379✔
171

172
/**
173
 * End-of-stream is a special token that signifies no more tokens
174
 * are in the stream.
175
 * @const
UNCOV
176
 */ var end_of_stream = -1;
379✔
177

178
/**
179
 * A stream represents an ordered sequence of tokens.
180
 *
181
 * @constructor
182
 * @param {!(Array.<number>|Uint8Array)} tokens Array of tokens that provide
183
 * the stream.
184
 */
185
function Stream(tokens) {
186
  /** @type {!Array.<number>} */
187
  this.tokens = [].slice.call(tokens);
×
188
  // Reversed as push/pop is more efficient than shift/unshift.
189
  this.tokens.reverse();
×
190
}
191

UNCOV
192
Stream.prototype = {
379✔
193
  /**
194
   * @return {boolean} True if end-of-stream has been hit.
195
   */
196
  endOfStream: function () {
197
    return !this.tokens.length;
×
198
  },
199

200
  /**
201
   * When a token is read from a stream, the first token in the
202
   * stream must be returned and subsequently removed, and
203
   * end-of-stream must be returned otherwise.
204
   *
205
   * @return {number} Get the next token from the stream, or
206
   * end_of_stream.
207
   */
208
  read: function () {
209
    if (!this.tokens.length) return end_of_stream;
×
210
    return this.tokens.pop();
×
211
  },
212

213
  /**
214
   * When one or more tokens are prepended to a stream, those tokens
215
   * must be inserted, in given order, before the first token in the
216
   * stream.
217
   *
218
   * @param {(number|!Array.<number>)} token The token(s) to prepend to the
219
   * stream.
220
   */
221
  prepend: function (token) {
222
    if (Array.isArray(token)) {
×
223
      var tokens = /**@type {!Array.<number>}*/ token;
×
224
      while (tokens.length) this.tokens.push(tokens.pop());
×
225
    } else {
226
      this.tokens.push(token);
×
227
    }
228
  },
229

230
  /**
231
   * When one or more tokens are pushed to a stream, those tokens
232
   * must be inserted, in given order, after the last token in the
233
   * stream.
234
   *
235
   * @param {(number|!Array.<number>)} token The tokens(s) to push to the
236
   * stream.
237
   */
238
  push: function (token) {
239
    if (Array.isArray(token)) {
×
240
      var tokens = /**@type {!Array.<number>}*/ token;
×
241
      while (tokens.length) this.tokens.unshift(tokens.shift());
×
242
    } else {
243
      this.tokens.unshift(token);
×
244
    }
245
  }
246
};
247

248
//
249
// 5. Encodings
250
//
251

252
// 5.1 Encoders and decoders
253

254
/** @const */
UNCOV
255
var finished = -1;
379✔
256

257
/**
258
 * @param {boolean} fatal If true, decoding errors raise an exception.
259
 * @param {number=} opt_code_point Override the standard fallback code point.
260
 * @return {number} The code point to insert on a decoding error.
261
 */
262
function decoderError(fatal, opt_code_point) {
263
  if (fatal) throw TypeError('Decoder error');
×
264
  return opt_code_point || 0xfffd;
×
265
}
266

267
/**
268
 * @param {number} code_point The code point that could not be encoded.
269
 * @return {number} Always throws, no value is actually returned.
270
 */
271
function encoderError(code_point) {
272
  throw TypeError('The code point ' + code_point + ' could not be encoded.');
×
273
}
274

275
/** @interface */
276
function Decoder() {}
UNCOV
277
Decoder.prototype = {
379✔
278
  /**
279
   * @param {Stream} stream The stream of bytes being decoded.
280
   * @param {number} bite The next byte read from the stream.
281
   * @return {?(number|!Array.<number>)} The next code point(s)
282
   *     decoded, or null if not enough data exists in the input
283
   *     stream to decode a complete code point, or |finished|.
284
   */
285
  handler: function (stream, bite) {}
286
};
287

288
/** @interface */
289
function Encoder() {}
UNCOV
290
Encoder.prototype = {
379✔
291
  /**
292
   * @param {Stream} stream The stream of code points being encoded.
293
   * @param {number} code_point Next code point read from the stream.
294
   * @return {(number|!Array.<number>)} Byte(s) to emit, or |finished|.
295
   */
296
  handler: function (stream, code_point) {}
297
};
298

299
// 5.2 Names and labels
300

301
// TODO: Define @typedef for Encoding: {name:string,labels:Array.<string>}
302
// https://github.com/google/closure-compiler/issues/247
303

304
/**
305
 * @param {string} label The encoding label.
306
 * @return {?{name:string,labels:Array.<string>}}
307
 */
308
function getEncoding(label) {
309
  // 1. Remove any leading and trailing ASCII whitespace from label.
310
  label = String(label).trim().toLowerCase();
×
311

312
  // 2. If label is an ASCII case-insensitive match for any of the
313
  // labels listed in the table below, return the corresponding
314
  // encoding, and failure otherwise.
315
  if (Object.prototype.hasOwnProperty.call(label_to_encoding, label)) {
×
316
    return label_to_encoding[label];
×
317
  }
318
  return null;
×
319
}
320

321
/**
322
 * Encodings table: https://encoding.spec.whatwg.org/encodings.json
323
 * @const
324
 * @type {!Array.<{
325
 *          heading: string,
326
 *          encodings: Array.<{name:string,labels:Array.<string>}>
327
 *        }>}
328
 */
UNCOV
329
var encodings = [
379✔
330
  {
331
    encodings: [
332
      {
333
        labels: ['unicode-1-1-utf-8', 'utf-8', 'utf8'],
334
        name: 'UTF-8'
335
      }
336
    ],
337
    heading: 'The Encoding'
338
  },
339
  {
340
    encodings: [
341
      {
342
        labels: ['866', 'cp866', 'csibm866', 'ibm866'],
343
        name: 'IBM866'
344
      },
345
      {
346
        labels: [
347
          'csisolatin2',
348
          'iso-8859-2',
349
          'iso-ir-101',
350
          'iso8859-2',
351
          'iso88592',
352
          'iso_8859-2',
353
          'iso_8859-2:1987',
354
          'l2',
355
          'latin2'
356
        ],
357
        name: 'ISO-8859-2'
358
      },
359
      {
360
        labels: [
361
          'csisolatin3',
362
          'iso-8859-3',
363
          'iso-ir-109',
364
          'iso8859-3',
365
          'iso88593',
366
          'iso_8859-3',
367
          'iso_8859-3:1988',
368
          'l3',
369
          'latin3'
370
        ],
371
        name: 'ISO-8859-3'
372
      },
373
      {
374
        labels: [
375
          'csisolatin4',
376
          'iso-8859-4',
377
          'iso-ir-110',
378
          'iso8859-4',
379
          'iso88594',
380
          'iso_8859-4',
381
          'iso_8859-4:1988',
382
          'l4',
383
          'latin4'
384
        ],
385
        name: 'ISO-8859-4'
386
      },
387
      {
388
        labels: [
389
          'csisolatincyrillic',
390
          'cyrillic',
391
          'iso-8859-5',
392
          'iso-ir-144',
393
          'iso8859-5',
394
          'iso88595',
395
          'iso_8859-5',
396
          'iso_8859-5:1988'
397
        ],
398
        name: 'ISO-8859-5'
399
      },
400
      {
401
        labels: [
402
          'arabic',
403
          'asmo-708',
404
          'csiso88596e',
405
          'csiso88596i',
406
          'csisolatinarabic',
407
          'ecma-114',
408
          'iso-8859-6',
409
          'iso-8859-6-e',
410
          'iso-8859-6-i',
411
          'iso-ir-127',
412
          'iso8859-6',
413
          'iso88596',
414
          'iso_8859-6',
415
          'iso_8859-6:1987'
416
        ],
417
        name: 'ISO-8859-6'
418
      },
419
      {
420
        labels: [
421
          'csisolatingreek',
422
          'ecma-118',
423
          'elot_928',
424
          'greek',
425
          'greek8',
426
          'iso-8859-7',
427
          'iso-ir-126',
428
          'iso8859-7',
429
          'iso88597',
430
          'iso_8859-7',
431
          'iso_8859-7:1987',
432
          'sun_eu_greek'
433
        ],
434
        name: 'ISO-8859-7'
435
      },
436
      {
437
        labels: [
438
          'csiso88598e',
439
          'csisolatinhebrew',
440
          'hebrew',
441
          'iso-8859-8',
442
          'iso-8859-8-e',
443
          'iso-ir-138',
444
          'iso8859-8',
445
          'iso88598',
446
          'iso_8859-8',
447
          'iso_8859-8:1988',
448
          'visual'
449
        ],
450
        name: 'ISO-8859-8'
451
      },
452
      {
453
        labels: ['csiso88598i', 'iso-8859-8-i', 'logical'],
454
        name: 'ISO-8859-8-I'
455
      },
456
      {
457
        labels: [
458
          'csisolatin6',
459
          'iso-8859-10',
460
          'iso-ir-157',
461
          'iso8859-10',
462
          'iso885910',
463
          'l6',
464
          'latin6'
465
        ],
466
        name: 'ISO-8859-10'
467
      },
468
      {
469
        labels: ['iso-8859-13', 'iso8859-13', 'iso885913'],
470
        name: 'ISO-8859-13'
471
      },
472
      {
473
        labels: ['iso-8859-14', 'iso8859-14', 'iso885914'],
474
        name: 'ISO-8859-14'
475
      },
476
      {
477
        labels: ['csisolatin9', 'iso-8859-15', 'iso8859-15', 'iso885915', 'iso_8859-15', 'l9'],
478
        name: 'ISO-8859-15'
479
      },
480
      {
481
        labels: ['iso-8859-16'],
482
        name: 'ISO-8859-16'
483
      },
484
      {
485
        labels: ['cskoi8r', 'koi', 'koi8', 'koi8-r', 'koi8_r'],
486
        name: 'KOI8-R'
487
      },
488
      {
489
        labels: ['koi8-ru', 'koi8-u'],
490
        name: 'KOI8-U'
491
      },
492
      {
493
        labels: ['csmacintosh', 'mac', 'macintosh', 'x-mac-roman'],
494
        name: 'macintosh'
495
      },
496
      {
497
        labels: ['dos-874', 'iso-8859-11', 'iso8859-11', 'iso885911', 'tis-620', 'windows-874'],
498
        name: 'windows-874'
499
      },
500
      {
501
        labels: ['cp1250', 'windows-1250', 'x-cp1250'],
502
        name: 'windows-1250'
503
      },
504
      {
505
        labels: ['cp1251', 'windows-1251', 'x-cp1251'],
506
        name: 'windows-1251'
507
      },
508
      {
509
        labels: [
510
          'ansi_x3.4-1968',
511
          'ascii',
512
          'cp1252',
513
          'cp819',
514
          'csisolatin1',
515
          'ibm819',
516
          'iso-8859-1',
517
          'iso-ir-100',
518
          'iso8859-1',
519
          'iso88591',
520
          'iso_8859-1',
521
          'iso_8859-1:1987',
522
          'l1',
523
          'latin1',
524
          'us-ascii',
525
          'windows-1252',
526
          'x-cp1252'
527
        ],
528
        name: 'windows-1252'
529
      },
530
      {
531
        labels: ['cp1253', 'windows-1253', 'x-cp1253'],
532
        name: 'windows-1253'
533
      },
534
      {
535
        labels: [
536
          'cp1254',
537
          'csisolatin5',
538
          'iso-8859-9',
539
          'iso-ir-148',
540
          'iso8859-9',
541
          'iso88599',
542
          'iso_8859-9',
543
          'iso_8859-9:1989',
544
          'l5',
545
          'latin5',
546
          'windows-1254',
547
          'x-cp1254'
548
        ],
549
        name: 'windows-1254'
550
      },
551
      {
552
        labels: ['cp1255', 'windows-1255', 'x-cp1255'],
553
        name: 'windows-1255'
554
      },
555
      {
556
        labels: ['cp1256', 'windows-1256', 'x-cp1256'],
557
        name: 'windows-1256'
558
      },
559
      {
560
        labels: ['cp1257', 'windows-1257', 'x-cp1257'],
561
        name: 'windows-1257'
562
      },
563
      {
564
        labels: ['cp1258', 'windows-1258', 'x-cp1258'],
565
        name: 'windows-1258'
566
      },
567
      {
568
        labels: ['x-mac-cyrillic', 'x-mac-ukrainian'],
569
        name: 'x-mac-cyrillic'
570
      }
571
    ],
572
    heading: 'Legacy single-byte encodings'
573
  },
574
  {
575
    encodings: [
576
      {
577
        labels: [
578
          'chinese',
579
          'csgb2312',
580
          'csiso58gb231280',
581
          'gb2312',
582
          'gb_2312',
583
          'gb_2312-80',
584
          'gbk',
585
          'iso-ir-58',
586
          'x-gbk'
587
        ],
588
        name: 'GBK'
589
      },
590
      {
591
        labels: ['gb18030'],
592
        name: 'gb18030'
593
      }
594
    ],
595
    heading: 'Legacy multi-byte Chinese (simplified) encodings'
596
  },
597
  {
598
    encodings: [
599
      {
600
        labels: ['big5', 'big5-hkscs', 'cn-big5', 'csbig5', 'x-x-big5'],
601
        name: 'Big5'
602
      }
603
    ],
604
    heading: 'Legacy multi-byte Chinese (traditional) encodings'
605
  },
606
  {
607
    encodings: [
608
      {
609
        labels: ['cseucpkdfmtjapanese', 'euc-jp', 'x-euc-jp'],
610
        name: 'EUC-JP'
611
      },
612
      {
613
        labels: ['csiso2022jp', 'iso-2022-jp'],
614
        name: 'ISO-2022-JP'
615
      },
616
      {
617
        labels: [
618
          'csshiftjis',
619
          'ms932',
620
          'ms_kanji',
621
          'shift-jis',
622
          'shift_jis',
623
          'sjis',
624
          'windows-31j',
625
          'x-sjis'
626
        ],
627
        name: 'Shift_JIS'
628
      }
629
    ],
630
    heading: 'Legacy multi-byte Japanese encodings'
631
  },
632
  {
633
    encodings: [
634
      {
635
        labels: [
636
          'cseuckr',
637
          'csksc56011987',
638
          'euc-kr',
639
          'iso-ir-149',
640
          'korean',
641
          'ks_c_5601-1987',
642
          'ks_c_5601-1989',
643
          'ksc5601',
644
          'ksc_5601',
645
          'windows-949'
646
        ],
647
        name: 'EUC-KR'
648
      }
649
    ],
650
    heading: 'Legacy multi-byte Korean encodings'
651
  },
652
  {
653
    encodings: [
654
      {
655
        labels: ['csiso2022kr', 'hz-gb-2312', 'iso-2022-cn', 'iso-2022-cn-ext', 'iso-2022-kr'],
656
        name: 'replacement'
657
      },
658
      {
659
        labels: ['utf-16be'],
660
        name: 'UTF-16BE'
661
      },
662
      {
663
        labels: ['utf-16', 'utf-16le'],
664
        name: 'UTF-16LE'
665
      },
666
      {
667
        labels: ['x-user-defined'],
668
        name: 'x-user-defined'
669
      }
670
    ],
671
    heading: 'Legacy miscellaneous encodings'
672
  }
673
];
674

675
// Label to encoding registry.
676
/** @type {Object.<string,{name:string,labels:Array.<string>}>} */
UNCOV
677
var label_to_encoding = {};
379✔
UNCOV
678
encodings.forEach(function (category) {
379✔
UNCOV
679
  category.encodings.forEach(function (encoding) {
2,653✔
UNCOV
680
    encoding.labels.forEach(function (label) {
15,160✔
UNCOV
681
      label_to_encoding[label] = encoding;
82,622✔
682
    });
683
  });
684
});
685

686
// Registry of of encoder/decoder factories, by encoding name.
687
/** @type {Object.<string, function({fatal:boolean}): Encoder>} */
UNCOV
688
var encoders = {};
379✔
689
/** @type {Object.<string, function({fatal:boolean}): Decoder>} */
UNCOV
690
var decoders = {};
379✔
691

692
//
693
// 6. Indexes
694
//
695

696
/**
697
 * @param {number} pointer The |pointer| to search for.
698
 * @param {(!Array.<?number>|undefined)} index The |index| to search within.
699
 * @return {?number} The code point corresponding to |pointer| in |index|,
700
 *     or null if |code point| is not in |index|.
701
 */
702
function indexCodePointFor(pointer, index) {
703
  if (!index) return null;
×
704
  return index[pointer] || null;
×
705
}
706

707
/**
708
 * @param {number} code_point The |code point| to search for.
709
 * @param {!Array.<?number>} index The |index| to search within.
710
 * @return {?number} The first pointer corresponding to |code point| in
711
 *     |index|, or null if |code point| is not in |index|.
712
 */
713
function indexPointerFor(code_point, index) {
714
  var pointer = index.indexOf(code_point);
×
715
  return pointer === -1 ? null : pointer;
×
716
}
717

718
/**
719
 * @param {string} name Name of the index.
720
 * @return {(!Array.<number>|!Array.<Array.<number>>)}
721
 *  */
722
function index(name) {
UNCOV
723
  if (!('encoding-indexes' in globalThis)) {
10,612!
724
    throw Error('Indexes missing.' + ' Did you forget to include encoding-indexes.js first?');
×
725
  }
UNCOV
726
  return globalThis['encoding-indexes'][name];
10,612✔
727
}
728

729
/**
730
 * @param {number} pointer The |pointer| to search for in the gb18030 index.
731
 * @return {?number} The code point corresponding to |pointer| in |index|,
732
 *     or null if |code point| is not in the gb18030 index.
733
 */
734
function indexGB18030RangesCodePointFor(pointer) {
735
  // 1. If pointer is greater than 39419 and less than 189000, or
736
  // pointer is greater than 1237575, return null.
737
  if ((pointer > 39419 && pointer < 189000) || pointer > 1237575) return null;
×
738

739
  // 2. If pointer is 7457, return code point U+E7C7.
740
  if (pointer === 7457) return 0xe7c7;
×
741

742
  // 3. Let offset be the last pointer in index gb18030 ranges that
743
  // is equal to or less than pointer and let code point offset be
744
  // its corresponding code point.
745
  var offset = 0;
×
746
  var code_point_offset = 0;
×
747
  var idx = index('gb18030-ranges');
×
748
  var i;
749
  for (i = 0; i < idx.length; ++i) {
×
750
    /** @type {!Array.<number>} */
751
    var entry = idx[i];
×
752
    if (entry[0] <= pointer) {
×
753
      offset = entry[0];
×
754
      code_point_offset = entry[1];
×
755
    } else {
756
      break;
×
757
    }
758
  }
759

760
  // 4. Return a code point whose value is code point offset +
761
  // pointer − offset.
762
  return code_point_offset + pointer - offset;
×
763
}
764

765
/**
766
 * @param {number} code_point The |code point| to locate in the gb18030 index.
767
 * @return {number} The first pointer corresponding to |code point| in the
768
 *     gb18030 index.
769
 */
770
function indexGB18030RangesPointerFor(code_point) {
771
  // 1. If code point is U+E7C7, return pointer 7457.
772
  if (code_point === 0xe7c7) return 7457;
×
773

774
  // 2. Let offset be the last code point in index gb18030 ranges
775
  // that is equal to or less than code point and let pointer offset
776
  // be its corresponding pointer.
777
  var offset = 0;
×
778
  var pointer_offset = 0;
×
779
  var idx = index('gb18030-ranges');
×
780
  var i;
781
  for (i = 0; i < idx.length; ++i) {
×
782
    /** @type {!Array.<number>} */
783
    var entry = idx[i];
×
784
    if (entry[1] <= code_point) {
×
785
      offset = entry[1];
×
786
      pointer_offset = entry[0];
×
787
    } else {
788
      break;
×
789
    }
790
  }
791

792
  // 3. Return a pointer whose value is pointer offset + code point
793
  // − offset.
794
  return pointer_offset + code_point - offset;
×
795
}
796

797
/**
798
 * @param {number} code_point The |code_point| to search for in the Shift_JIS
799
 *     index.
800
 * @return {?number} The code point corresponding to |pointer| in |index|,
801
 *     or null if |code point| is not in the Shift_JIS index.
802
 */
803
function indexShiftJISPointerFor(code_point) {
804
  // 1. Let index be index jis0208 excluding all entries whose
805
  // pointer is in the range 8272 to 8835, inclusive.
806
  shift_jis_index =
×
807
    shift_jis_index ||
×
808
    index('jis0208').map(function (code_point, pointer) {
809
      return inRange(pointer, 8272, 8835) ? null : code_point;
×
810
    });
811
  var index_ = shift_jis_index;
×
812

813
  // 2. Return the index pointer for code point in index.
814
  return index_.indexOf(code_point);
×
815
}
816
var shift_jis_index;
817

818
/**
819
 * @param {number} code_point The |code_point| to search for in the big5
820
 *     index.
821
 * @return {?number} The code point corresponding to |pointer| in |index|,
822
 *     or null if |code point| is not in the big5 index.
823
 */
824
function indexBig5PointerFor(code_point) {
825
  // 1. Let index be index Big5 excluding all entries whose pointer
826
  big5_index_no_hkscs =
×
827
    big5_index_no_hkscs ||
×
828
    index('big5').map(function (code_point, pointer) {
829
      return pointer < (0xa1 - 0x81) * 157 ? null : code_point;
×
830
    });
831
  var index_ = big5_index_no_hkscs;
×
832

833
  // 2. If code point is U+2550, U+255E, U+2561, U+256A, U+5341, or
834
  // U+5345, return the last pointer corresponding to code point in
835
  // index.
836
  if (
×
837
    code_point === 0x2550 ||
×
838
    code_point === 0x255e ||
839
    code_point === 0x2561 ||
840
    code_point === 0x256a ||
841
    code_point === 0x5341 ||
842
    code_point === 0x5345
843
  ) {
844
    return index_.lastIndexOf(code_point);
×
845
  }
846

847
  // 3. Return the index pointer for code point in index.
848
  return indexPointerFor(code_point, index_);
×
849
}
850
var big5_index_no_hkscs;
851

852
//
853
// 8. API
854
//
855

UNCOV
856
/** @const */ var DEFAULT_ENCODING = 'utf-8';
379✔
857

858
// 8.1 Interface TextDecoder
859

860
/**
861
 * @constructor
862
 * @param {string=} label The label of the encoding;
863
 *     defaults to 'utf-8'.
864
 * @param {Object=} options
865
 */
866
function TextDecoder(label, options) {
867
  // Web IDL conventions
868
  if (!(this instanceof TextDecoder))
×
869
    throw TypeError("Called as a function. Did you forget 'new'?");
×
870
  label = label !== undefined ? String(label) : DEFAULT_ENCODING;
×
871
  options = ToDictionary(options);
×
872

873
  // A TextDecoder object has an associated encoding, decoder,
874
  // stream, ignore BOM flag (initially unset), BOM seen flag
875
  // (initially unset), error mode (initially replacement), and do
876
  // not flush flag (initially unset).
877

878
  /** @private */
879
  this._encoding = null;
×
880
  /** @private @type {?Decoder} */
881
  this._decoder = null;
×
882
  /** @private @type {boolean} */
883
  this._ignoreBOM = false;
×
884
  /** @private @type {boolean} */
885
  this._BOMseen = false;
×
886
  /** @private @type {string} */
887
  this._error_mode = 'replacement';
×
888
  /** @private @type {boolean} */
889
  this._do_not_flush = false;
×
890

891
  // 1. Let encoding be the result of getting an encoding from
892
  // label.
893
  var encoding = getEncoding(label);
×
894

895
  // 2. If encoding is failure or replacement, throw a RangeError.
896
  if (encoding === null || encoding.name === 'replacement')
×
897
    throw RangeError('Unknown encoding: ' + label);
×
898
  if (!decoders[encoding.name]) {
×
899
    throw Error('Decoder not present.' + ' Did you forget to include encoding-indexes.js first?');
×
900
  }
901

902
  // 3. Let dec be a new TextDecoder object.
903
  var dec = this;
×
904

905
  // 4. Set dec's encoding to encoding.
906
  dec._encoding = encoding;
×
907

908
  // 5. If options's fatal member is true, set dec's error mode to
909
  // fatal.
910
  if (Boolean(options['fatal'])) dec._error_mode = 'fatal';
×
911

912
  // 6. If options's ignoreBOM member is true, set dec's ignore BOM
913
  // flag.
914
  if (Boolean(options['ignoreBOM'])) dec._ignoreBOM = true;
×
915

916
  // For pre-ES5 runtimes:
917
  if (!Object.defineProperty) {
×
918
    this.encoding = dec._encoding.name.toLowerCase();
×
919
    this.fatal = dec._error_mode === 'fatal';
×
920
    this.ignoreBOM = dec._ignoreBOM;
×
921
  }
922

923
  // 7. Return dec.
924
  return dec;
×
925
}
926

UNCOV
927
if (Object.defineProperty) {
379!
928
  // The encoding attribute's getter must return encoding's name.
UNCOV
929
  Object.defineProperty(TextDecoder.prototype, 'encoding', {
379✔
930
    /** @this {TextDecoder} */
931
    get: function () {
932
      return this._encoding.name.toLowerCase();
×
933
    }
934
  });
935

936
  // The fatal attribute's getter must return true if error mode
937
  // is fatal, and false otherwise.
UNCOV
938
  Object.defineProperty(TextDecoder.prototype, 'fatal', {
379✔
939
    /** @this {TextDecoder} */
940
    get: function () {
941
      return this._error_mode === 'fatal';
×
942
    }
943
  });
944

945
  // The ignoreBOM attribute's getter must return true if ignore
946
  // BOM flag is set, and false otherwise.
UNCOV
947
  Object.defineProperty(TextDecoder.prototype, 'ignoreBOM', {
379✔
948
    /** @this {TextDecoder} */
949
    get: function () {
950
      return this._ignoreBOM;
×
951
    }
952
  });
953
}
954

955
/**
956
 * @param {BufferSource=} input The buffer of bytes to decode.
957
 * @param {Object=} options
958
 * @return {string} The decoded string.
959
 */
UNCOV
960
TextDecoder.prototype.decode = function decode(input, options) {
379✔
961
  var bytes;
962
  if (typeof input === 'object' && input instanceof ArrayBuffer) {
×
963
    bytes = new Uint8Array(input);
×
964
  } else if (
965
    typeof input === 'object' &&
×
966
    'buffer' in input &&
967
    input.buffer instanceof ArrayBuffer
968
  ) {
969
    bytes = new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
×
970
  } else {
971
    bytes = new Uint8Array(0);
×
972
  }
973

974
  options = ToDictionary(options);
×
975

976
  // 1. If the do not flush flag is unset, set decoder to a new
977
  // encoding's decoder, set stream to a new stream, and unset the
978
  // BOM seen flag.
979
  if (!this._do_not_flush) {
×
980
    this._decoder = decoders[this._encoding.name]({
×
981
      fatal: this._error_mode === 'fatal'
982
    });
983
    this._BOMseen = false;
×
984
  }
985

986
  // 2. If options's stream is true, set the do not flush flag, and
987
  // unset the do not flush flag otherwise.
988
  this._do_not_flush = Boolean(options['stream']);
×
989

990
  // 3. If input is given, push a copy of input to stream.
991
  // TODO: Align with spec algorithm - maintain stream on instance.
992
  var input_stream = new Stream(bytes);
×
993

994
  // 4. Let output be a new stream.
995
  var output = [];
×
996

997
  /** @type {?(number|!Array.<number>)} */
998
  var result;
999

1000
  // 5. While true:
1001
  while (true) {
×
1002
    // 1. Let token be the result of reading from stream.
1003
    var token = input_stream.read();
×
1004

1005
    // 2. If token is end-of-stream and the do not flush flag is
1006
    // set, return output, serialized.
1007
    // TODO: Align with spec algorithm.
1008
    if (token === end_of_stream) break;
×
1009

1010
    // 3. Otherwise, run these subsubsteps:
1011

1012
    // 1. Let result be the result of processing token for decoder,
1013
    // stream, output, and error mode.
1014
    result = this._decoder.handler(input_stream, token);
×
1015

1016
    // 2. If result is finished, return output, serialized.
1017
    if (result === finished) break;
×
1018

1019
    if (result !== null) {
×
1020
      if (Array.isArray(result)) output.push.apply(output, /**@type {!Array.<number>}*/ result);
×
1021
      else output.push(result);
×
1022
    }
1023

1024
    // 3. Otherwise, if result is error, throw a TypeError.
1025
    // (Thrown in handler)
1026

1027
    // 4. Otherwise, do nothing.
1028
  }
1029
  // TODO: Align with spec algorithm.
1030
  if (!this._do_not_flush) {
×
1031
    do {
×
1032
      result = this._decoder.handler(input_stream, input_stream.read());
×
1033
      if (result === finished) break;
×
1034
      if (result === null) continue;
×
1035
      if (Array.isArray(result)) output.push.apply(output, /**@type {!Array.<number>}*/ result);
×
1036
      else output.push(result);
×
1037
    } while (!input_stream.endOfStream());
1038
    this._decoder = null;
×
1039
  }
1040

1041
  // A TextDecoder object also has an associated serialize stream
1042
  // algorithm...
1043
  /**
1044
   * @param {!Array.<number>} stream
1045
   * @return {string}
1046
   * @this {TextDecoder}
1047
   */
1048
  function serializeStream(stream) {
1049
    // 1. Let token be the result of reading from stream.
1050
    // (Done in-place on array, rather than as a stream)
1051

1052
    // 2. If encoding is UTF-8, UTF-16BE, or UTF-16LE, and ignore
1053
    // BOM flag and BOM seen flag are unset, run these subsubsteps:
1054
    if (
×
1055
      includes(['UTF-8', 'UTF-16LE', 'UTF-16BE'], this._encoding.name) &&
×
1056
      !this._ignoreBOM &&
1057
      !this._BOMseen
1058
    ) {
1059
      if (stream.length > 0 && stream[0] === 0xfeff) {
×
1060
        // 1. If token is U+FEFF, set BOM seen flag.
1061
        this._BOMseen = true;
×
1062
        stream.shift();
×
1063
      } else if (stream.length > 0) {
×
1064
        // 2. Otherwise, if token is not end-of-stream, set BOM seen
1065
        // flag and append token to stream.
1066
        this._BOMseen = true;
×
1067
      } else {
1068
        // 3. Otherwise, if token is not end-of-stream, append token
1069
        // to output.
1070
        // (no-op)
1071
      }
1072
    }
1073
    // 4. Otherwise, return output.
1074
    return codePointsToString(stream);
×
1075
  }
1076

1077
  return serializeStream.call(this, output);
×
1078
};
1079

1080
// 8.2 Interface TextEncoder
1081

1082
/**
1083
 * @constructor
1084
 * @param {string=} label The label of the encoding. NONSTANDARD.
1085
 * @param {Object=} options NONSTANDARD.
1086
 */
1087
function TextEncoder(label, options) {
1088
  // Web IDL conventions
1089
  if (!(this instanceof TextEncoder))
×
1090
    throw TypeError("Called as a function. Did you forget 'new'?");
×
1091
  options = ToDictionary(options);
×
1092

1093
  // A TextEncoder object has an associated encoding and encoder.
1094

1095
  /** @private */
1096
  this._encoding = null;
×
1097
  /** @private @type {?Encoder} */
1098
  this._encoder = null;
×
1099

1100
  // Non-standard
1101
  /** @private @type {boolean} */
1102
  this._do_not_flush = false;
×
1103
  /** @private @type {string} */
1104
  this._fatal = Boolean(options['fatal']) ? 'fatal' : 'replacement';
×
1105

1106
  // 1. Let enc be a new TextEncoder object.
1107
  var enc = this;
×
1108

1109
  // 2. Set enc's encoding to UTF-8's encoder.
1110
  if (Boolean(options['NONSTANDARD_allowLegacyEncoding'])) {
×
1111
    // NONSTANDARD behavior.
1112
    label = label !== undefined ? String(label) : DEFAULT_ENCODING;
×
1113
    var encoding = getEncoding(label);
×
1114
    if (encoding === null || encoding.name === 'replacement')
×
1115
      throw RangeError('Unknown encoding: ' + label);
×
1116
    if (!encoders[encoding.name]) {
×
1117
      throw Error('Encoder not present.' + ' Did you forget to include encoding-indexes.js first?');
×
1118
    }
1119
    enc._encoding = encoding;
×
1120
  } else {
1121
    // Standard behavior.
1122
    enc._encoding = getEncoding('utf-8');
×
1123

1124
    if (label !== undefined && 'console' in globalThis) {
×
1125
    }
1126
  }
1127

1128
  // For pre-ES5 runtimes:
1129
  if (!Object.defineProperty) this.encoding = enc._encoding.name.toLowerCase();
×
1130

1131
  // 3. Return enc.
1132
  return enc;
×
1133
}
1134

UNCOV
1135
if (Object.defineProperty) {
379!
1136
  // The encoding attribute's getter must return encoding's name.
UNCOV
1137
  Object.defineProperty(TextEncoder.prototype, 'encoding', {
379✔
1138
    /** @this {TextEncoder} */
1139
    get: function () {
1140
      return this._encoding.name.toLowerCase();
×
1141
    }
1142
  });
1143
}
1144

1145
/**
1146
 * @param {string=} opt_string The string to encode.
1147
 * @param {Object=} options
1148
 * @return {!Uint8Array} Encoded bytes, as a Uint8Array.
1149
 */
UNCOV
1150
TextEncoder.prototype.encode = function encode(opt_string, options) {
379✔
1151
  opt_string = opt_string === undefined ? '' : String(opt_string);
×
1152
  options = ToDictionary(options);
×
1153

1154
  // NOTE: This option is nonstandard. None of the encodings
1155
  // permitted for encoding (i.e. UTF-8, UTF-16) are stateful when
1156
  // the input is a USVString so streaming is not necessary.
1157
  if (!this._do_not_flush)
×
1158
    this._encoder = encoders[this._encoding.name]({
×
1159
      fatal: this._fatal === 'fatal'
1160
    });
1161
  this._do_not_flush = Boolean(options['stream']);
×
1162

1163
  // 1. Convert input to a stream.
1164
  var input = new Stream(stringToCodePoints(opt_string));
×
1165

1166
  // 2. Let output be a new stream
1167
  var output = [];
×
1168

1169
  /** @type {?(number|!Array.<number>)} */
1170
  var result;
1171
  // 3. While true, run these substeps:
1172
  while (true) {
×
1173
    // 1. Let token be the result of reading from input.
1174
    var token = input.read();
×
1175
    if (token === end_of_stream) break;
×
1176
    // 2. Let result be the result of processing token for encoder,
1177
    // input, output.
1178
    result = this._encoder.handler(input, token);
×
1179
    if (result === finished) break;
×
1180
    if (Array.isArray(result)) output.push.apply(output, /**@type {!Array.<number>}*/ result);
×
1181
    else output.push(result);
×
1182
  }
1183
  // TODO: Align with spec algorithm.
1184
  if (!this._do_not_flush) {
×
1185
    while (true) {
×
1186
      result = this._encoder.handler(input, input.read());
×
1187
      if (result === finished) break;
×
1188
      if (Array.isArray(result)) output.push.apply(output, /**@type {!Array.<number>}*/ result);
×
1189
      else output.push(result);
×
1190
    }
1191
    this._encoder = null;
×
1192
  }
1193
  // 3. If result is finished, convert output into a byte sequence,
1194
  // and then return a Uint8Array object wrapping an ArrayBuffer
1195
  // containing output.
1196
  return new Uint8Array(output);
×
1197
};
1198

1199
//
1200
// 9. The encoding
1201
//
1202

1203
// 9.1 utf-8
1204

1205
// 9.1.1 utf-8 decoder
1206
/**
1207
 * @constructor
1208
 * @implements {Decoder}
1209
 * @param {{fatal: boolean}} options
1210
 */
1211
function UTF8Decoder(options) {
1212
  var fatal = options.fatal;
×
1213

1214
  // utf-8's decoder's has an associated utf-8 code point, utf-8
1215
  // bytes seen, and utf-8 bytes needed (all initially 0), a utf-8
1216
  // lower boundary (initially 0x80), and a utf-8 upper boundary
1217
  // (initially 0xBF).
1218
  var /** @type {number} */ utf8_code_point = 0,
×
1219
    /** @type {number} */ utf8_bytes_seen = 0,
×
1220
    /** @type {number} */ utf8_bytes_needed = 0,
×
1221
    /** @type {number} */ utf8_lower_boundary = 0x80,
×
1222
    /** @type {number} */ utf8_upper_boundary = 0xbf;
×
1223

1224
  /**
1225
   * @param {Stream} stream The stream of bytes being decoded.
1226
   * @param {number} bite The next byte read from the stream.
1227
   * @return {?(number|!Array.<number>)} The next code point(s)
1228
   *     decoded, or null if not enough data exists in the input
1229
   *     stream to decode a complete code point.
1230
   */
1231
  this.handler = function (stream, bite) {
×
1232
    // 1. If byte is end-of-stream and utf-8 bytes needed is not 0,
1233
    // set utf-8 bytes needed to 0 and return error.
1234
    if (bite === end_of_stream && utf8_bytes_needed !== 0) {
×
1235
      utf8_bytes_needed = 0;
×
1236
      return decoderError(fatal);
×
1237
    }
1238

1239
    // 2. If byte is end-of-stream, return finished.
1240
    if (bite === end_of_stream) return finished;
×
1241

1242
    // 3. If utf-8 bytes needed is 0, based on byte:
1243
    if (utf8_bytes_needed === 0) {
×
1244
      // 0x00 to 0x7F
1245
      if (inRange(bite, 0x00, 0x7f)) {
×
1246
        // Return a code point whose value is byte.
1247
        return bite;
×
1248
      }
1249

1250
      // 0xC2 to 0xDF
1251
      else if (inRange(bite, 0xc2, 0xdf)) {
×
1252
        // 1. Set utf-8 bytes needed to 1.
1253
        utf8_bytes_needed = 1;
×
1254

1255
        // 2. Set UTF-8 code point to byte & 0x1F.
1256
        utf8_code_point = bite & 0x1f;
×
1257
      }
1258

1259
      // 0xE0 to 0xEF
1260
      else if (inRange(bite, 0xe0, 0xef)) {
×
1261
        // 1. If byte is 0xE0, set utf-8 lower boundary to 0xA0.
1262
        if (bite === 0xe0) utf8_lower_boundary = 0xa0;
×
1263
        // 2. If byte is 0xED, set utf-8 upper boundary to 0x9F.
1264
        if (bite === 0xed) utf8_upper_boundary = 0x9f;
×
1265
        // 3. Set utf-8 bytes needed to 2.
1266
        utf8_bytes_needed = 2;
×
1267
        // 4. Set UTF-8 code point to byte & 0xF.
1268
        utf8_code_point = bite & 0xf;
×
1269
      }
1270

1271
      // 0xF0 to 0xF4
1272
      else if (inRange(bite, 0xf0, 0xf4)) {
×
1273
        // 1. If byte is 0xF0, set utf-8 lower boundary to 0x90.
1274
        if (bite === 0xf0) utf8_lower_boundary = 0x90;
×
1275
        // 2. If byte is 0xF4, set utf-8 upper boundary to 0x8F.
1276
        if (bite === 0xf4) utf8_upper_boundary = 0x8f;
×
1277
        // 3. Set utf-8 bytes needed to 3.
1278
        utf8_bytes_needed = 3;
×
1279
        // 4. Set UTF-8 code point to byte & 0x7.
1280
        utf8_code_point = bite & 0x7;
×
1281
      }
1282

1283
      // Otherwise
1284
      else {
1285
        // Return error.
1286
        return decoderError(fatal);
×
1287
      }
1288

1289
      // Return continue.
1290
      return null;
×
1291
    }
1292

1293
    // 4. If byte is not in the range utf-8 lower boundary to utf-8
1294
    // upper boundary, inclusive, run these substeps:
1295
    if (!inRange(bite, utf8_lower_boundary, utf8_upper_boundary)) {
×
1296
      // 1. Set utf-8 code point, utf-8 bytes needed, and utf-8
1297
      // bytes seen to 0, set utf-8 lower boundary to 0x80, and set
1298
      // utf-8 upper boundary to 0xBF.
1299
      utf8_code_point = utf8_bytes_needed = utf8_bytes_seen = 0;
×
1300
      utf8_lower_boundary = 0x80;
×
1301
      utf8_upper_boundary = 0xbf;
×
1302

1303
      // 2. Prepend byte to stream.
1304
      stream.prepend(bite);
×
1305

1306
      // 3. Return error.
1307
      return decoderError(fatal);
×
1308
    }
1309

1310
    // 5. Set utf-8 lower boundary to 0x80 and utf-8 upper boundary
1311
    // to 0xBF.
1312
    utf8_lower_boundary = 0x80;
×
1313
    utf8_upper_boundary = 0xbf;
×
1314

1315
    // 6. Set UTF-8 code point to (UTF-8 code point << 6) | (byte &
1316
    // 0x3F)
1317
    utf8_code_point = (utf8_code_point << 6) | (bite & 0x3f);
×
1318

1319
    // 7. Increase utf-8 bytes seen by one.
1320
    utf8_bytes_seen += 1;
×
1321

1322
    // 8. If utf-8 bytes seen is not equal to utf-8 bytes needed,
1323
    // continue.
1324
    if (utf8_bytes_seen !== utf8_bytes_needed) return null;
×
1325

1326
    // 9. Let code point be utf-8 code point.
1327
    var code_point = utf8_code_point;
×
1328

1329
    // 10. Set utf-8 code point, utf-8 bytes needed, and utf-8 bytes
1330
    // seen to 0.
1331
    utf8_code_point = utf8_bytes_needed = utf8_bytes_seen = 0;
×
1332

1333
    // 11. Return a code point whose value is code point.
1334
    return code_point;
×
1335
  };
1336
}
1337

1338
// 9.1.2 utf-8 encoder
1339
/**
1340
 * @constructor
1341
 * @implements {Encoder}
1342
 * @param {{fatal: boolean}} options
1343
 */
1344
function UTF8Encoder(options) {
1345
  var _fatal = options.fatal;
×
1346
  /**
1347
   * @param {Stream} stream Input stream.
1348
   * @param {number} code_point Next code point read from the stream.
1349
   * @return {(number|!Array.<number>)} Byte(s) to emit.
1350
   */
1351
  this.handler = function (stream, code_point) {
×
1352
    // 1. If code point is end-of-stream, return finished.
1353
    if (code_point === end_of_stream) return finished;
×
1354

1355
    // 2. If code point is an ASCII code point, return a byte whose
1356
    // value is code point.
1357
    if (isASCIICodePoint(code_point)) return code_point;
×
1358

1359
    // 3. Set count and offset based on the range code point is in:
1360
    var count, offset;
1361
    // U+0080 to U+07FF, inclusive:
1362
    if (inRange(code_point, 0x0080, 0x07ff)) {
×
1363
      // 1 and 0xC0
1364
      count = 1;
×
1365
      offset = 0xc0;
×
1366
    }
1367
    // U+0800 to U+FFFF, inclusive:
1368
    else if (inRange(code_point, 0x0800, 0xffff)) {
×
1369
      // 2 and 0xE0
1370
      count = 2;
×
1371
      offset = 0xe0;
×
1372
    }
1373
    // U+10000 to U+10FFFF, inclusive:
1374
    else if (inRange(code_point, 0x10000, 0x10ffff)) {
×
1375
      // 3 and 0xF0
1376
      count = 3;
×
1377
      offset = 0xf0;
×
1378
    }
1379

1380
    // 4. Let bytes be a byte sequence whose first byte is (code
1381
    // point >> (6 × count)) + offset.
1382
    var bytes = [(code_point >> (6 * count)) + offset];
×
1383

1384
    // 5. Run these substeps while count is greater than 0:
1385
    while (count > 0) {
×
1386
      // 1. Set temp to code point >> (6 × (count − 1)).
1387
      var temp = code_point >> (6 * (count - 1));
×
1388

1389
      // 2. Append to bytes 0x80 | (temp & 0x3F).
1390
      bytes.push(0x80 | (temp & 0x3f));
×
1391

1392
      // 3. Decrease count by one.
1393
      count -= 1;
×
1394
    }
1395

1396
    // 6. Return bytes bytes, in order.
1397
    return bytes;
×
1398
  };
1399
}
1400

1401
/** @param {{fatal: boolean}} options */
UNCOV
1402
encoders['UTF-8'] = function (options) {
379✔
1403
  return new UTF8Encoder(options);
×
1404
};
1405
/** @param {{fatal: boolean}} options */
UNCOV
1406
decoders['UTF-8'] = function (options) {
379✔
1407
  return new UTF8Decoder(options);
×
1408
};
1409

1410
//
1411
// 10. Legacy single-byte encodings
1412
//
1413

1414
// 10.1 single-byte decoder
1415
/**
1416
 * @constructor
1417
 * @implements {Decoder}
1418
 * @param {!Array.<number>} index The encoding index.
1419
 * @param {{fatal: boolean}} options
1420
 */
1421
function SingleByteDecoder(index, options) {
1422
  var fatal = options.fatal;
×
1423
  /**
1424
   * @param {Stream} stream The stream of bytes being decoded.
1425
   * @param {number} bite The next byte read from the stream.
1426
   * @return {?(number|!Array.<number>)} The next code point(s)
1427
   *     decoded, or null if not enough data exists in the input
1428
   *     stream to decode a complete code point.
1429
   */
1430
  this.handler = function (stream, bite) {
×
1431
    // 1. If byte is end-of-stream, return finished.
1432
    if (bite === end_of_stream) return finished;
×
1433

1434
    // 2. If byte is an ASCII byte, return a code point whose value
1435
    // is byte.
1436
    if (isASCIIByte(bite)) return bite;
×
1437

1438
    // 3. Let code point be the index code point for byte − 0x80 in
1439
    // index single-byte.
1440
    var code_point = index[bite - 0x80];
×
1441

1442
    // 4. If code point is null, return error.
1443
    if (code_point === null) return decoderError(fatal);
×
1444

1445
    // 5. Return a code point whose value is code point.
1446
    return code_point;
×
1447
  };
1448
}
1449

1450
// 10.2 single-byte encoder
1451
/**
1452
 * @constructor
1453
 * @implements {Encoder}
1454
 * @param {!Array.<?number>} index The encoding index.
1455
 * @param {{fatal: boolean}} options
1456
 */
1457
function SingleByteEncoder(index, options) {
1458
  var _fatal = options.fatal;
×
1459
  /**
1460
   * @param {Stream} stream Input stream.
1461
   * @param {number} code_point Next code point read from the stream.
1462
   * @return {(number|!Array.<number>)} Byte(s) to emit.
1463
   */
1464
  this.handler = function (stream, code_point) {
×
1465
    // 1. If code point is end-of-stream, return finished.
1466
    if (code_point === end_of_stream) return finished;
×
1467

1468
    // 2. If code point is an ASCII code point, return a byte whose
1469
    // value is code point.
1470
    if (isASCIICodePoint(code_point)) return code_point;
×
1471

1472
    // 3. Let pointer be the index pointer for code point in index
1473
    // single-byte.
1474
    var pointer = indexPointerFor(code_point, index);
×
1475

1476
    // 4. If pointer is null, return error with code point.
1477
    if (pointer === null) encoderError(code_point);
×
1478

1479
    // 5. Return a byte whose value is pointer + 0x80.
1480
    return pointer + 0x80;
×
1481
  };
1482
}
1483

UNCOV
1484
(function () {
379✔
UNCOV
1485
  if (!('encoding-indexes' in globalThis)) return;
379!
UNCOV
1486
  encodings.forEach(function (category) {
379✔
UNCOV
1487
    if (category.heading !== 'Legacy single-byte encodings') return;
2,653✔
UNCOV
1488
    category.encodings.forEach(function (encoding) {
379✔
UNCOV
1489
      var name = encoding.name;
10,612✔
UNCOV
1490
      var idx = index(name.toLowerCase());
10,612✔
1491
      /** @param {{fatal: boolean}} options */
UNCOV
1492
      decoders[name] = function (options) {
10,612✔
1493
        return new SingleByteDecoder(idx, options);
×
1494
      };
1495
      /** @param {{fatal: boolean}} options */
UNCOV
1496
      encoders[name] = function (options) {
10,612✔
1497
        return new SingleByteEncoder(idx, options);
×
1498
      };
1499
    });
1500
  });
1501
})();
1502

1503
//
1504
// 11. Legacy multi-byte Chinese (simplified) encodings
1505
//
1506

1507
// 11.1 gbk
1508

1509
// 11.1.1 gbk decoder
1510
// gbk's decoder is gb18030's decoder.
1511
/** @param {{fatal: boolean}} options */
UNCOV
1512
decoders['GBK'] = function (options) {
379✔
1513
  return new GB18030Decoder(options);
×
1514
};
1515

1516
// 11.1.2 gbk encoder
1517
// gbk's encoder is gb18030's encoder with its gbk flag set.
1518
/** @param {{fatal: boolean}} options */
UNCOV
1519
encoders['GBK'] = function (options) {
379✔
1520
  return new GB18030Encoder(options, true);
×
1521
};
1522

1523
// 11.2 gb18030
1524

1525
// 11.2.1 gb18030 decoder
1526
/**
1527
 * @constructor
1528
 * @implements {Decoder}
1529
 * @param {{fatal: boolean}} options
1530
 */
1531
function GB18030Decoder(options) {
1532
  var fatal = options.fatal;
×
1533
  // gb18030's decoder has an associated gb18030 first, gb18030
1534
  // second, and gb18030 third (all initially 0x00).
1535
  var /** @type {number} */ gb18030_first = 0x00,
×
1536
    /** @type {number} */ gb18030_second = 0x00,
×
1537
    /** @type {number} */ gb18030_third = 0x00;
×
1538
  /**
1539
   * @param {Stream} stream The stream of bytes being decoded.
1540
   * @param {number} bite The next byte read from the stream.
1541
   * @return {?(number|!Array.<number>)} The next code point(s)
1542
   *     decoded, or null if not enough data exists in the input
1543
   *     stream to decode a complete code point.
1544
   */
1545
  this.handler = function (stream, bite) {
×
1546
    // 1. If byte is end-of-stream and gb18030 first, gb18030
1547
    // second, and gb18030 third are 0x00, return finished.
1548
    if (
×
1549
      bite === end_of_stream &&
×
1550
      gb18030_first === 0x00 &&
1551
      gb18030_second === 0x00 &&
1552
      gb18030_third === 0x00
1553
    ) {
1554
      return finished;
×
1555
    }
1556
    // 2. If byte is end-of-stream, and gb18030 first, gb18030
1557
    // second, or gb18030 third is not 0x00, set gb18030 first,
1558
    // gb18030 second, and gb18030 third to 0x00, and return error.
1559
    if (
×
1560
      bite === end_of_stream &&
×
1561
      (gb18030_first !== 0x00 || gb18030_second !== 0x00 || gb18030_third !== 0x00)
1562
    ) {
1563
      gb18030_first = 0x00;
×
1564
      gb18030_second = 0x00;
×
1565
      gb18030_third = 0x00;
×
1566
      decoderError(fatal);
×
1567
    }
1568
    var code_point;
1569
    // 3. If gb18030 third is not 0x00, run these substeps:
1570
    if (gb18030_third !== 0x00) {
×
1571
      // 1. Let code point be null.
1572
      code_point = null;
×
1573
      // 2. If byte is in the range 0x30 to 0x39, inclusive, set
1574
      // code point to the index gb18030 ranges code point for
1575
      // (((gb18030 first − 0x81) × 10 + gb18030 second − 0x30) ×
1576
      // 126 + gb18030 third − 0x81) × 10 + byte − 0x30.
1577
      if (inRange(bite, 0x30, 0x39)) {
×
1578
        code_point = indexGB18030RangesCodePointFor(
×
1579
          (((gb18030_first - 0x81) * 10 + gb18030_second - 0x30) * 126 + gb18030_third - 0x81) *
1580
            10 +
1581
            bite -
1582
            0x30
1583
        );
1584
      }
1585

1586
      // 3. Let buffer be a byte sequence consisting of gb18030
1587
      // second, gb18030 third, and byte, in order.
1588
      var buffer = [gb18030_second, gb18030_third, bite];
×
1589

1590
      // 4. Set gb18030 first, gb18030 second, and gb18030 third to
1591
      // 0x00.
1592
      gb18030_first = 0x00;
×
1593
      gb18030_second = 0x00;
×
1594
      gb18030_third = 0x00;
×
1595

1596
      // 5. If code point is null, prepend buffer to stream and
1597
      // return error.
1598
      if (code_point === null) {
×
1599
        stream.prepend(buffer);
×
1600
        return decoderError(fatal);
×
1601
      }
1602

1603
      // 6. Return a code point whose value is code point.
1604
      return code_point;
×
1605
    }
1606

1607
    // 4. If gb18030 second is not 0x00, run these substeps:
1608
    if (gb18030_second !== 0x00) {
×
1609
      // 1. If byte is in the range 0x81 to 0xFE, inclusive, set
1610
      // gb18030 third to byte and return continue.
1611
      if (inRange(bite, 0x81, 0xfe)) {
×
1612
        gb18030_third = bite;
×
1613
        return null;
×
1614
      }
1615

1616
      // 2. Prepend gb18030 second followed by byte to stream, set
1617
      // gb18030 first and gb18030 second to 0x00, and return error.
1618
      stream.prepend([gb18030_second, bite]);
×
1619
      gb18030_first = 0x00;
×
1620
      gb18030_second = 0x00;
×
1621
      return decoderError(fatal);
×
1622
    }
1623

1624
    // 5. If gb18030 first is not 0x00, run these substeps:
1625
    if (gb18030_first !== 0x00) {
×
1626
      // 1. If byte is in the range 0x30 to 0x39, inclusive, set
1627
      // gb18030 second to byte and return continue.
1628
      if (inRange(bite, 0x30, 0x39)) {
×
1629
        gb18030_second = bite;
×
1630
        return null;
×
1631
      }
1632

1633
      // 2. Let lead be gb18030 first, let pointer be null, and set
1634
      // gb18030 first to 0x00.
1635
      var lead = gb18030_first;
×
1636
      var pointer = null;
×
1637
      gb18030_first = 0x00;
×
1638

1639
      // 3. Let offset be 0x40 if byte is less than 0x7F and 0x41
1640
      // otherwise.
1641
      var offset = bite < 0x7f ? 0x40 : 0x41;
×
1642

1643
      // 4. If byte is in the range 0x40 to 0x7E, inclusive, or 0x80
1644
      // to 0xFE, inclusive, set pointer to (lead − 0x81) × 190 +
1645
      // (byte − offset).
1646
      if (inRange(bite, 0x40, 0x7e) || inRange(bite, 0x80, 0xfe))
×
1647
        pointer = (lead - 0x81) * 190 + (bite - offset);
×
1648

1649
      // 5. Let code point be null if pointer is null and the index
1650
      // code point for pointer in index gb18030 otherwise.
1651
      code_point = pointer === null ? null : indexCodePointFor(pointer, index('gb18030'));
×
1652

1653
      // 6. If code point is null and byte is an ASCII byte, prepend
1654
      // byte to stream.
1655
      if (code_point === null && isASCIIByte(bite)) stream.prepend(bite);
×
1656

1657
      // 7. If code point is null, return error.
1658
      if (code_point === null) return decoderError(fatal);
×
1659

1660
      // 8. Return a code point whose value is code point.
1661
      return code_point;
×
1662
    }
1663

1664
    // 6. If byte is an ASCII byte, return a code point whose value
1665
    // is byte.
1666
    if (isASCIIByte(bite)) return bite;
×
1667

1668
    // 7. If byte is 0x80, return code point U+20AC.
1669
    if (bite === 0x80) return 0x20ac;
×
1670

1671
    // 8. If byte is in the range 0x81 to 0xFE, inclusive, set
1672
    // gb18030 first to byte and return continue.
1673
    if (inRange(bite, 0x81, 0xfe)) {
×
1674
      gb18030_first = bite;
×
1675
      return null;
×
1676
    }
1677

1678
    // 9. Return error.
1679
    return decoderError(fatal);
×
1680
  };
1681
}
1682

1683
// 11.2.2 gb18030 encoder
1684
/**
1685
 * @constructor
1686
 * @implements {Encoder}
1687
 * @param {{fatal: boolean}} options
1688
 * @param {boolean=} gbk_flag
1689
 */
1690
function GB18030Encoder(options, gbk_flag) {
1691
  var _fatal = options.fatal;
×
1692
  // gb18030's decoder has an associated gbk flag (initially unset).
1693
  /**
1694
   * @param {Stream} stream Input stream.
1695
   * @param {number} code_point Next code point read from the stream.
1696
   * @return {(number|!Array.<number>)} Byte(s) to emit.
1697
   */
1698
  this.handler = function (stream, code_point) {
×
1699
    // 1. If code point is end-of-stream, return finished.
1700
    if (code_point === end_of_stream) return finished;
×
1701

1702
    // 2. If code point is an ASCII code point, return a byte whose
1703
    // value is code point.
1704
    if (isASCIICodePoint(code_point)) return code_point;
×
1705

1706
    // 3. If code point is U+E5E5, return error with code point.
1707
    if (code_point === 0xe5e5) return encoderError(code_point);
×
1708

1709
    // 4. If the gbk flag is set and code point is U+20AC, return
1710
    // byte 0x80.
1711
    if (gbk_flag && code_point === 0x20ac) return 0x80;
×
1712

1713
    // 5. Let pointer be the index pointer for code point in index
1714
    // gb18030.
1715
    var pointer = indexPointerFor(code_point, index('gb18030'));
×
1716

1717
    // 6. If pointer is not null, run these substeps:
1718
    if (pointer !== null) {
×
1719
      // 1. Let lead be floor(pointer / 190) + 0x81.
1720
      var lead = floor(pointer / 190) + 0x81;
×
1721

1722
      // 2. Let trail be pointer % 190.
1723
      var trail = pointer % 190;
×
1724

1725
      // 3. Let offset be 0x40 if trail is less than 0x3F and 0x41 otherwise.
1726
      var offset = trail < 0x3f ? 0x40 : 0x41;
×
1727

1728
      // 4. Return two bytes whose values are lead and trail + offset.
1729
      return [lead, trail + offset];
×
1730
    }
1731

1732
    // 7. If gbk flag is set, return error with code point.
1733
    if (gbk_flag) return encoderError(code_point);
×
1734

1735
    // 8. Set pointer to the index gb18030 ranges pointer for code
1736
    // point.
1737
    pointer = indexGB18030RangesPointerFor(code_point);
×
1738

1739
    // 9. Let byte1 be floor(pointer / 10 / 126 / 10).
1740
    var byte1 = floor(pointer / 10 / 126 / 10);
×
1741

1742
    // 10. Set pointer to pointer − byte1 × 10 × 126 × 10.
1743
    pointer = pointer - byte1 * 10 * 126 * 10;
×
1744

1745
    // 11. Let byte2 be floor(pointer / 10 / 126).
1746
    var byte2 = floor(pointer / 10 / 126);
×
1747

1748
    // 12. Set pointer to pointer − byte2 × 10 × 126.
1749
    pointer = pointer - byte2 * 10 * 126;
×
1750

1751
    // 13. Let byte3 be floor(pointer / 10).
1752
    var byte3 = floor(pointer / 10);
×
1753

1754
    // 14. Let byte4 be pointer − byte3 × 10.
1755
    var byte4 = pointer - byte3 * 10;
×
1756

1757
    // 15. Return four bytes whose values are byte1 + 0x81, byte2 +
1758
    // 0x30, byte3 + 0x81, byte4 + 0x30.
1759
    return [byte1 + 0x81, byte2 + 0x30, byte3 + 0x81, byte4 + 0x30];
×
1760
  };
1761
}
1762

1763
/** @param {{fatal: boolean}} options */
UNCOV
1764
encoders['gb18030'] = function (options) {
379✔
1765
  return new GB18030Encoder(options);
×
1766
};
1767
/** @param {{fatal: boolean}} options */
UNCOV
1768
decoders['gb18030'] = function (options) {
379✔
1769
  return new GB18030Decoder(options);
×
1770
};
1771

1772
//
1773
// 12. Legacy multi-byte Chinese (traditional) encodings
1774
//
1775

1776
// 12.1 Big5
1777

1778
// 12.1.1 Big5 decoder
1779
/**
1780
 * @constructor
1781
 * @implements {Decoder}
1782
 * @param {{fatal: boolean}} options
1783
 */
1784
function Big5Decoder(options) {
1785
  var fatal = options.fatal;
×
1786
  // Big5's decoder has an associated Big5 lead (initially 0x00).
1787
  var /** @type {number} */ Big5_lead = 0x00;
×
1788

1789
  /**
1790
   * @param {Stream} stream The stream of bytes being decoded.
1791
   * @param {number} bite The next byte read from the stream.
1792
   * @return {?(number|!Array.<number>)} The next code point(s)
1793
   *     decoded, or null if not enough data exists in the input
1794
   *     stream to decode a complete code point.
1795
   */
1796
  this.handler = function (stream, bite) {
×
1797
    // 1. If byte is end-of-stream and Big5 lead is not 0x00, set
1798
    // Big5 lead to 0x00 and return error.
1799
    if (bite === end_of_stream && Big5_lead !== 0x00) {
×
1800
      Big5_lead = 0x00;
×
1801
      return decoderError(fatal);
×
1802
    }
1803

1804
    // 2. If byte is end-of-stream and Big5 lead is 0x00, return
1805
    // finished.
1806
    if (bite === end_of_stream && Big5_lead === 0x00) return finished;
×
1807

1808
    // 3. If Big5 lead is not 0x00, let lead be Big5 lead, let
1809
    // pointer be null, set Big5 lead to 0x00, and then run these
1810
    // substeps:
1811
    if (Big5_lead !== 0x00) {
×
1812
      var lead = Big5_lead;
×
1813
      var pointer = null;
×
1814
      Big5_lead = 0x00;
×
1815

1816
      // 1. Let offset be 0x40 if byte is less than 0x7F and 0x62
1817
      // otherwise.
1818
      var offset = bite < 0x7f ? 0x40 : 0x62;
×
1819

1820
      // 2. If byte is in the range 0x40 to 0x7E, inclusive, or 0xA1
1821
      // to 0xFE, inclusive, set pointer to (lead − 0x81) × 157 +
1822
      // (byte − offset).
1823
      if (inRange(bite, 0x40, 0x7e) || inRange(bite, 0xa1, 0xfe))
×
1824
        pointer = (lead - 0x81) * 157 + (bite - offset);
×
1825

1826
      // 3. If there is a row in the table below whose first column
1827
      // is pointer, return the two code points listed in its second
1828
      // column
1829
      // Pointer | Code points
1830
      // --------+--------------
1831
      // 1133    | U+00CA U+0304
1832
      // 1135    | U+00CA U+030C
1833
      // 1164    | U+00EA U+0304
1834
      // 1166    | U+00EA U+030C
1835
      switch (pointer) {
×
1836
        case 1133:
1837
          return [0x00ca, 0x0304];
×
1838
        case 1135:
1839
          return [0x00ca, 0x030c];
×
1840
        case 1164:
1841
          return [0x00ea, 0x0304];
×
1842
        case 1166:
1843
          return [0x00ea, 0x030c];
×
1844
      }
1845

1846
      // 4. Let code point be null if pointer is null and the index
1847
      // code point for pointer in index Big5 otherwise.
1848
      var code_point = pointer === null ? null : indexCodePointFor(pointer, index('big5'));
×
1849

1850
      // 5. If code point is null and byte is an ASCII byte, prepend
1851
      // byte to stream.
1852
      if (code_point === null && isASCIIByte(bite)) stream.prepend(bite);
×
1853

1854
      // 6. If code point is null, return error.
1855
      if (code_point === null) return decoderError(fatal);
×
1856

1857
      // 7. Return a code point whose value is code point.
1858
      return code_point;
×
1859
    }
1860

1861
    // 4. If byte is an ASCII byte, return a code point whose value
1862
    // is byte.
1863
    if (isASCIIByte(bite)) return bite;
×
1864

1865
    // 5. If byte is in the range 0x81 to 0xFE, inclusive, set Big5
1866
    // lead to byte and return continue.
1867
    if (inRange(bite, 0x81, 0xfe)) {
×
1868
      Big5_lead = bite;
×
1869
      return null;
×
1870
    }
1871

1872
    // 6. Return error.
1873
    return decoderError(fatal);
×
1874
  };
1875
}
1876

1877
// 12.1.2 Big5 encoder
1878
/**
1879
 * @constructor
1880
 * @implements {Encoder}
1881
 * @param {{fatal: boolean}} options
1882
 */
1883
function Big5Encoder(options) {
1884
  var _fatal = options.fatal;
×
1885
  /**
1886
   * @param {Stream} stream Input stream.
1887
   * @param {number} code_point Next code point read from the stream.
1888
   * @return {(number|!Array.<number>)} Byte(s) to emit.
1889
   */
1890
  this.handler = function (stream, code_point) {
×
1891
    // 1. If code point is end-of-stream, return finished.
1892
    if (code_point === end_of_stream) return finished;
×
1893

1894
    // 2. If code point is an ASCII code point, return a byte whose
1895
    // value is code point.
1896
    if (isASCIICodePoint(code_point)) return code_point;
×
1897

1898
    // 3. Let pointer be the index Big5 pointer for code point.
1899
    var pointer = indexBig5PointerFor(code_point);
×
1900

1901
    // 4. If pointer is null, return error with code point.
1902
    if (pointer === null) return encoderError(code_point);
×
1903

1904
    // 5. Let lead be floor(pointer / 157) + 0x81.
1905
    var lead = floor(pointer / 157) + 0x81;
×
1906

1907
    // 6. If lead is less than 0xA1, return error with code point.
1908
    if (lead < 0xa1) return encoderError(code_point);
×
1909

1910
    // 7. Let trail be pointer % 157.
1911
    var trail = pointer % 157;
×
1912

1913
    // 8. Let offset be 0x40 if trail is less than 0x3F and 0x62
1914
    // otherwise.
1915
    var offset = trail < 0x3f ? 0x40 : 0x62;
×
1916

1917
    // Return two bytes whose values are lead and trail + offset.
1918
    return [lead, trail + offset];
×
1919
  };
1920
}
1921

1922
/** @param {{fatal: boolean}} options */
UNCOV
1923
encoders['Big5'] = function (options) {
379✔
1924
  return new Big5Encoder(options);
×
1925
};
1926
/** @param {{fatal: boolean}} options */
UNCOV
1927
decoders['Big5'] = function (options) {
379✔
1928
  return new Big5Decoder(options);
×
1929
};
1930

1931
//
1932
// 13. Legacy multi-byte Japanese encodings
1933
//
1934

1935
// 13.1 euc-jp
1936

1937
// 13.1.1 euc-jp decoder
1938
/**
1939
 * @constructor
1940
 * @implements {Decoder}
1941
 * @param {{fatal: boolean}} options
1942
 */
1943
function EUCJPDecoder(options) {
1944
  var fatal = options.fatal;
×
1945

1946
  // euc-jp's decoder has an associated euc-jp jis0212 flag
1947
  // (initially unset) and euc-jp lead (initially 0x00).
1948
  var /** @type {boolean} */ eucjp_jis0212_flag = false,
×
1949
    /** @type {number} */ eucjp_lead = 0x00;
×
1950

1951
  /**
1952
   * @param {Stream} stream The stream of bytes being decoded.
1953
   * @param {number} bite The next byte read from the stream.
1954
   * @return {?(number|!Array.<number>)} The next code point(s)
1955
   *     decoded, or null if not enough data exists in the input
1956
   *     stream to decode a complete code point.
1957
   */
1958
  this.handler = function (stream, bite) {
×
1959
    // 1. If byte is end-of-stream and euc-jp lead is not 0x00, set
1960
    // euc-jp lead to 0x00, and return error.
1961
    if (bite === end_of_stream && eucjp_lead !== 0x00) {
×
1962
      eucjp_lead = 0x00;
×
1963
      return decoderError(fatal);
×
1964
    }
1965

1966
    // 2. If byte is end-of-stream and euc-jp lead is 0x00, return
1967
    // finished.
1968
    if (bite === end_of_stream && eucjp_lead === 0x00) return finished;
×
1969

1970
    // 3. If euc-jp lead is 0x8E and byte is in the range 0xA1 to
1971
    // 0xDF, inclusive, set euc-jp lead to 0x00 and return a code
1972
    // point whose value is 0xFF61 − 0xA1 + byte.
1973
    if (eucjp_lead === 0x8e && inRange(bite, 0xa1, 0xdf)) {
×
1974
      eucjp_lead = 0x00;
×
1975
      return 0xff61 - 0xa1 + bite;
×
1976
    }
1977

1978
    // 4. If euc-jp lead is 0x8F and byte is in the range 0xA1 to
1979
    // 0xFE, inclusive, set the euc-jp jis0212 flag, set euc-jp lead
1980
    // to byte, and return continue.
1981
    if (eucjp_lead === 0x8f && inRange(bite, 0xa1, 0xfe)) {
×
1982
      eucjp_jis0212_flag = true;
×
1983
      eucjp_lead = bite;
×
1984
      return null;
×
1985
    }
1986

1987
    // 5. If euc-jp lead is not 0x00, let lead be euc-jp lead, set
1988
    // euc-jp lead to 0x00, and run these substeps:
1989
    if (eucjp_lead !== 0x00) {
×
1990
      var lead = eucjp_lead;
×
1991
      eucjp_lead = 0x00;
×
1992

1993
      // 1. Let code point be null.
1994
      var code_point = null;
×
1995

1996
      // 2. If lead and byte are both in the range 0xA1 to 0xFE,
1997
      // inclusive, set code point to the index code point for (lead
1998
      // − 0xA1) × 94 + byte − 0xA1 in index jis0208 if the euc-jp
1999
      // jis0212 flag is unset and in index jis0212 otherwise.
2000
      if (inRange(lead, 0xa1, 0xfe) && inRange(bite, 0xa1, 0xfe)) {
×
2001
        code_point = indexCodePointFor(
×
2002
          (lead - 0xa1) * 94 + (bite - 0xa1),
2003
          index(!eucjp_jis0212_flag ? 'jis0208' : 'jis0212')
×
2004
        );
2005
      }
2006

2007
      // 3. Unset the euc-jp jis0212 flag.
2008
      eucjp_jis0212_flag = false;
×
2009

2010
      // 4. If byte is not in the range 0xA1 to 0xFE, inclusive,
2011
      // prepend byte to stream.
2012
      if (!inRange(bite, 0xa1, 0xfe)) stream.prepend(bite);
×
2013

2014
      // 5. If code point is null, return error.
2015
      if (code_point === null) return decoderError(fatal);
×
2016

2017
      // 6. Return a code point whose value is code point.
2018
      return code_point;
×
2019
    }
2020

2021
    // 6. If byte is an ASCII byte, return a code point whose value
2022
    // is byte.
2023
    if (isASCIIByte(bite)) return bite;
×
2024

2025
    // 7. If byte is 0x8E, 0x8F, or in the range 0xA1 to 0xFE,
2026
    // inclusive, set euc-jp lead to byte and return continue.
2027
    if (bite === 0x8e || bite === 0x8f || inRange(bite, 0xa1, 0xfe)) {
×
2028
      eucjp_lead = bite;
×
2029
      return null;
×
2030
    }
2031

2032
    // 8. Return error.
2033
    return decoderError(fatal);
×
2034
  };
2035
}
2036

2037
// 13.1.2 euc-jp encoder
2038
/**
2039
 * @constructor
2040
 * @implements {Encoder}
2041
 * @param {{fatal: boolean}} options
2042
 */
2043
function EUCJPEncoder(options) {
2044
  var _fatal = options.fatal;
×
2045
  /**
2046
   * @param {Stream} stream Input stream.
2047
   * @param {number} code_point Next code point read from the stream.
2048
   * @return {(number|!Array.<number>)} Byte(s) to emit.
2049
   */
2050
  this.handler = function (stream, code_point) {
×
2051
    // 1. If code point is end-of-stream, return finished.
2052
    if (code_point === end_of_stream) return finished;
×
2053

2054
    // 2. If code point is an ASCII code point, return a byte whose
2055
    // value is code point.
2056
    if (isASCIICodePoint(code_point)) return code_point;
×
2057

2058
    // 3. If code point is U+00A5, return byte 0x5C.
2059
    if (code_point === 0x00a5) return 0x5c;
×
2060

2061
    // 4. If code point is U+203E, return byte 0x7E.
2062
    if (code_point === 0x203e) return 0x7e;
×
2063

2064
    // 5. If code point is in the range U+FF61 to U+FF9F, inclusive,
2065
    // return two bytes whose values are 0x8E and code point −
2066
    // 0xFF61 + 0xA1.
2067
    if (inRange(code_point, 0xff61, 0xff9f)) return [0x8e, code_point - 0xff61 + 0xa1];
×
2068

2069
    // 6. If code point is U+2212, set it to U+FF0D.
2070
    if (code_point === 0x2212) code_point = 0xff0d;
×
2071

2072
    // 7. Let pointer be the index pointer for code point in index
2073
    // jis0208.
2074
    var pointer = indexPointerFor(code_point, index('jis0208'));
×
2075

2076
    // 8. If pointer is null, return error with code point.
2077
    if (pointer === null) return encoderError(code_point);
×
2078

2079
    // 9. Let lead be floor(pointer / 94) + 0xA1.
2080
    var lead = floor(pointer / 94) + 0xa1;
×
2081

2082
    // 10. Let trail be pointer % 94 + 0xA1.
2083
    var trail = (pointer % 94) + 0xa1;
×
2084

2085
    // 11. Return two bytes whose values are lead and trail.
2086
    return [lead, trail];
×
2087
  };
2088
}
2089

2090
/** @param {{fatal: boolean}} options */
UNCOV
2091
encoders['EUC-JP'] = function (options) {
379✔
2092
  return new EUCJPEncoder(options);
×
2093
};
2094
/** @param {{fatal: boolean}} options */
UNCOV
2095
decoders['EUC-JP'] = function (options) {
379✔
2096
  return new EUCJPDecoder(options);
×
2097
};
2098

2099
// 13.2 iso-2022-jp
2100

2101
// 13.2.1 iso-2022-jp decoder
2102
/**
2103
 * @constructor
2104
 * @implements {Decoder}
2105
 * @param {{fatal: boolean}} options
2106
 */
2107
function ISO2022JPDecoder(options) {
2108
  var fatal = options.fatal;
×
2109
  /** @enum */
2110
  var states = {
×
2111
    ASCII: 0,
2112
    Roman: 1,
2113
    Katakana: 2,
2114
    LeadByte: 3,
2115
    TrailByte: 4,
2116
    EscapeStart: 5,
2117
    Escape: 6
2118
  };
2119
  // iso-2022-jp's decoder has an associated iso-2022-jp decoder
2120
  // state (initially ASCII), iso-2022-jp decoder output state
2121
  // (initially ASCII), iso-2022-jp lead (initially 0x00), and
2122
  // iso-2022-jp output flag (initially unset).
2123
  var /** @type {number} */ iso2022jp_decoder_state = states.ASCII,
×
2124
    /** @type {number} */ iso2022jp_decoder_output_state = states.ASCII,
×
2125
    /** @type {number} */ iso2022jp_lead = 0x00,
×
2126
    /** @type {boolean} */ iso2022jp_output_flag = false;
×
2127
  /**
2128
   * @param {Stream} stream The stream of bytes being decoded.
2129
   * @param {number} bite The next byte read from the stream.
2130
   * @return {?(number|!Array.<number>)} The next code point(s)
2131
   *     decoded, or null if not enough data exists in the input
2132
   *     stream to decode a complete code point.
2133
   */
2134
  this.handler = function (stream, bite) {
×
2135
    // switching on iso-2022-jp decoder state:
2136
    switch (iso2022jp_decoder_state) {
×
2137
      default:
2138
      case states.ASCII:
2139
        // ASCII
2140
        // Based on byte:
2141

2142
        // 0x1B
2143
        if (bite === 0x1b) {
×
2144
          // Set iso-2022-jp decoder state to escape start and return
2145
          // continue.
2146
          iso2022jp_decoder_state = states.EscapeStart;
×
2147
          return null;
×
2148
        }
2149

2150
        // 0x00 to 0x7F, excluding 0x0E, 0x0F, and 0x1B
2151
        if (inRange(bite, 0x00, 0x7f) && bite !== 0x0e && bite !== 0x0f && bite !== 0x1b) {
×
2152
          // Unset the iso-2022-jp output flag and return a code point
2153
          // whose value is byte.
2154
          iso2022jp_output_flag = false;
×
2155
          return bite;
×
2156
        }
2157

2158
        // end-of-stream
2159
        if (bite === end_of_stream) {
×
2160
          // Return finished.
2161
          return finished;
×
2162
        }
2163

2164
        // Otherwise
2165
        // Unset the iso-2022-jp output flag and return error.
2166
        iso2022jp_output_flag = false;
×
2167
        return decoderError(fatal);
×
2168

2169
      case states.Roman:
2170
        // Roman
2171
        // Based on byte:
2172

2173
        // 0x1B
2174
        if (bite === 0x1b) {
×
2175
          // Set iso-2022-jp decoder state to escape start and return
2176
          // continue.
2177
          iso2022jp_decoder_state = states.EscapeStart;
×
2178
          return null;
×
2179
        }
2180

2181
        // 0x5C
2182
        if (bite === 0x5c) {
×
2183
          // Unset the iso-2022-jp output flag and return code point
2184
          // U+00A5.
2185
          iso2022jp_output_flag = false;
×
2186
          return 0x00a5;
×
2187
        }
2188

2189
        // 0x7E
2190
        if (bite === 0x7e) {
×
2191
          // Unset the iso-2022-jp output flag and return code point
2192
          // U+203E.
2193
          iso2022jp_output_flag = false;
×
2194
          return 0x203e;
×
2195
        }
2196

2197
        // 0x00 to 0x7F, excluding 0x0E, 0x0F, 0x1B, 0x5C, and 0x7E
2198
        if (
×
2199
          inRange(bite, 0x00, 0x7f) &&
×
2200
          bite !== 0x0e &&
2201
          bite !== 0x0f &&
2202
          bite !== 0x1b &&
2203
          bite !== 0x5c &&
2204
          bite !== 0x7e
2205
        ) {
2206
          // Unset the iso-2022-jp output flag and return a code point
2207
          // whose value is byte.
2208
          iso2022jp_output_flag = false;
×
2209
          return bite;
×
2210
        }
2211

2212
        // end-of-stream
2213
        if (bite === end_of_stream) {
×
2214
          // Return finished.
2215
          return finished;
×
2216
        }
2217

2218
        // Otherwise
2219
        // Unset the iso-2022-jp output flag and return error.
2220
        iso2022jp_output_flag = false;
×
2221
        return decoderError(fatal);
×
2222

2223
      case states.Katakana:
2224
        // Katakana
2225
        // Based on byte:
2226

2227
        // 0x1B
2228
        if (bite === 0x1b) {
×
2229
          // Set iso-2022-jp decoder state to escape start and return
2230
          // continue.
2231
          iso2022jp_decoder_state = states.EscapeStart;
×
2232
          return null;
×
2233
        }
2234

2235
        // 0x21 to 0x5F
2236
        if (inRange(bite, 0x21, 0x5f)) {
×
2237
          // Unset the iso-2022-jp output flag and return a code point
2238
          // whose value is 0xFF61 − 0x21 + byte.
2239
          iso2022jp_output_flag = false;
×
2240
          return 0xff61 - 0x21 + bite;
×
2241
        }
2242

2243
        // end-of-stream
2244
        if (bite === end_of_stream) {
×
2245
          // Return finished.
2246
          return finished;
×
2247
        }
2248

2249
        // Otherwise
2250
        // Unset the iso-2022-jp output flag and return error.
2251
        iso2022jp_output_flag = false;
×
2252
        return decoderError(fatal);
×
2253

2254
      case states.LeadByte:
2255
        // Lead byte
2256
        // Based on byte:
2257

2258
        // 0x1B
2259
        if (bite === 0x1b) {
×
2260
          // Set iso-2022-jp decoder state to escape start and return
2261
          // continue.
2262
          iso2022jp_decoder_state = states.EscapeStart;
×
2263
          return null;
×
2264
        }
2265

2266
        // 0x21 to 0x7E
2267
        if (inRange(bite, 0x21, 0x7e)) {
×
2268
          // Unset the iso-2022-jp output flag, set iso-2022-jp lead
2269
          // to byte, iso-2022-jp decoder state to trail byte, and
2270
          // return continue.
2271
          iso2022jp_output_flag = false;
×
2272
          iso2022jp_lead = bite;
×
2273
          iso2022jp_decoder_state = states.TrailByte;
×
2274
          return null;
×
2275
        }
2276

2277
        // end-of-stream
2278
        if (bite === end_of_stream) {
×
2279
          // Return finished.
2280
          return finished;
×
2281
        }
2282

2283
        // Otherwise
2284
        // Unset the iso-2022-jp output flag and return error.
2285
        iso2022jp_output_flag = false;
×
2286
        return decoderError(fatal);
×
2287

2288
      case states.TrailByte:
2289
        // Trail byte
2290
        // Based on byte:
2291

2292
        // 0x1B
2293
        if (bite === 0x1b) {
×
2294
          // Set iso-2022-jp decoder state to escape start and return
2295
          // continue.
2296
          iso2022jp_decoder_state = states.EscapeStart;
×
2297
          return decoderError(fatal);
×
2298
        }
2299

2300
        // 0x21 to 0x7E
2301
        if (inRange(bite, 0x21, 0x7e)) {
×
2302
          // 1. Set the iso-2022-jp decoder state to lead byte.
2303
          iso2022jp_decoder_state = states.LeadByte;
×
2304

2305
          // 2. Let pointer be (iso-2022-jp lead − 0x21) × 94 + byte − 0x21.
2306
          var pointer = (iso2022jp_lead - 0x21) * 94 + bite - 0x21;
×
2307

2308
          // 3. Let code point be the index code point for pointer in
2309
          // index jis0208.
2310
          var code_point = indexCodePointFor(pointer, index('jis0208'));
×
2311

2312
          // 4. If code point is null, return error.
2313
          if (code_point === null) return decoderError(fatal);
×
2314

2315
          // 5. Return a code point whose value is code point.
2316
          return code_point;
×
2317
        }
2318

2319
        // end-of-stream
2320
        if (bite === end_of_stream) {
×
2321
          // Set the iso-2022-jp decoder state to lead byte, prepend
2322
          // byte to stream, and return error.
2323
          iso2022jp_decoder_state = states.LeadByte;
×
2324
          stream.prepend(bite);
×
2325
          return decoderError(fatal);
×
2326
        }
2327

2328
        // Otherwise
2329
        // Set iso-2022-jp decoder state to lead byte and return
2330
        // error.
2331
        iso2022jp_decoder_state = states.LeadByte;
×
2332
        return decoderError(fatal);
×
2333

2334
      case states.EscapeStart:
2335
        // Escape start
2336

2337
        // 1. If byte is either 0x24 or 0x28, set iso-2022-jp lead to
2338
        // byte, iso-2022-jp decoder state to escape, and return
2339
        // continue.
2340
        if (bite === 0x24 || bite === 0x28) {
×
2341
          iso2022jp_lead = bite;
×
2342
          iso2022jp_decoder_state = states.Escape;
×
2343
          return null;
×
2344
        }
2345

2346
        // 2. Prepend byte to stream.
2347
        stream.prepend(bite);
×
2348

2349
        // 3. Unset the iso-2022-jp output flag, set iso-2022-jp
2350
        // decoder state to iso-2022-jp decoder output state, and
2351
        // return error.
2352
        iso2022jp_output_flag = false;
×
2353
        iso2022jp_decoder_state = iso2022jp_decoder_output_state;
×
2354
        return decoderError(fatal);
×
2355

2356
      case states.Escape:
2357
        // Escape
2358

2359
        // 1. Let lead be iso-2022-jp lead and set iso-2022-jp lead to
2360
        // 0x00.
2361
        var lead = iso2022jp_lead;
×
2362
        iso2022jp_lead = 0x00;
×
2363

2364
        // 2. Let state be null.
2365
        var state = null;
×
2366

2367
        // 3. If lead is 0x28 and byte is 0x42, set state to ASCII.
2368
        if (lead === 0x28 && bite === 0x42) state = states.ASCII;
×
2369

2370
        // 4. If lead is 0x28 and byte is 0x4A, set state to Roman.
2371
        if (lead === 0x28 && bite === 0x4a) state = states.Roman;
×
2372

2373
        // 5. If lead is 0x28 and byte is 0x49, set state to Katakana.
2374
        if (lead === 0x28 && bite === 0x49) state = states.Katakana;
×
2375

2376
        // 6. If lead is 0x24 and byte is either 0x40 or 0x42, set
2377
        // state to lead byte.
2378
        if (lead === 0x24 && (bite === 0x40 || bite === 0x42)) state = states.LeadByte;
×
2379

2380
        // 7. If state is non-null, run these substeps:
2381
        if (state !== null) {
×
2382
          // 1. Set iso-2022-jp decoder state and iso-2022-jp decoder
2383
          // output state to states.
2384
          iso2022jp_decoder_state = iso2022jp_decoder_state = state;
×
2385

2386
          // 2. Let output flag be the iso-2022-jp output flag.
2387
          var output_flag = iso2022jp_output_flag;
×
2388

2389
          // 3. Set the iso-2022-jp output flag.
2390
          iso2022jp_output_flag = true;
×
2391

2392
          // 4. Return continue, if output flag is unset, and error
2393
          // otherwise.
2394
          return !output_flag ? null : decoderError(fatal);
×
2395
        }
2396

2397
        // 8. Prepend lead and byte to stream.
2398
        stream.prepend([lead, bite]);
×
2399

2400
        // 9. Unset the iso-2022-jp output flag, set iso-2022-jp
2401
        // decoder state to iso-2022-jp decoder output state and
2402
        // return error.
2403
        iso2022jp_output_flag = false;
×
2404
        iso2022jp_decoder_state = iso2022jp_decoder_output_state;
×
2405
        return decoderError(fatal);
×
2406
    }
2407
  };
2408
}
2409

2410
// 13.2.2 iso-2022-jp encoder
2411
/**
2412
 * @constructor
2413
 * @implements {Encoder}
2414
 * @param {{fatal: boolean}} options
2415
 */
2416
function ISO2022JPEncoder(options) {
2417
  var _fatal = options.fatal;
×
2418
  // iso-2022-jp's encoder has an associated iso-2022-jp encoder
2419
  // state which is one of ASCII, Roman, and jis0208 (initially
2420
  // ASCII).
2421
  /** @enum */
2422
  var states = {
×
2423
    ASCII: 0,
2424
    Roman: 1,
2425
    jis0208: 2
2426
  };
2427
  var /** @type {number} */ iso2022jp_state = states.ASCII;
×
2428
  /**
2429
   * @param {Stream} stream Input stream.
2430
   * @param {number} code_point Next code point read from the stream.
2431
   * @return {(number|!Array.<number>)} Byte(s) to emit.
2432
   */
2433
  this.handler = function (stream, code_point) {
×
2434
    // 1. If code point is end-of-stream and iso-2022-jp encoder
2435
    // state is not ASCII, prepend code point to stream, set
2436
    // iso-2022-jp encoder state to ASCII, and return three bytes
2437
    // 0x1B 0x28 0x42.
2438
    if (code_point === end_of_stream && iso2022jp_state !== states.ASCII) {
×
2439
      stream.prepend(code_point);
×
2440
      iso2022jp_state = states.ASCII;
×
2441
      return [0x1b, 0x28, 0x42];
×
2442
    }
2443

2444
    // 2. If code point is end-of-stream and iso-2022-jp encoder
2445
    // state is ASCII, return finished.
2446
    if (code_point === end_of_stream && iso2022jp_state === states.ASCII) return finished;
×
2447

2448
    // 3. If ISO-2022-JP encoder state is ASCII or Roman, and code
2449
    // point is U+000E, U+000F, or U+001B, return error with U+FFFD.
2450
    if (
×
2451
      (iso2022jp_state === states.ASCII || iso2022jp_state === states.Roman) &&
2452
      (code_point === 0x000e || code_point === 0x000f || code_point === 0x001b)
2453
    ) {
2454
      return encoderError(0xfffd);
×
2455
    }
2456

2457
    // 4. If iso-2022-jp encoder state is ASCII and code point is an
2458
    // ASCII code point, return a byte whose value is code point.
2459
    if (iso2022jp_state === states.ASCII && isASCIICodePoint(code_point)) return code_point;
×
2460

2461
    // 5. If iso-2022-jp encoder state is Roman and code point is an
2462
    // ASCII code point, excluding U+005C and U+007E, or is U+00A5
2463
    // or U+203E, run these substeps:
2464
    if (
×
2465
      iso2022jp_state === states.Roman &&
×
2466
      ((isASCIICodePoint(code_point) && code_point !== 0x005c && code_point !== 0x007e) ||
2467
        code_point === 0x00a5 ||
2468
        code_point === 0x203e)
2469
    ) {
2470
      // 1. If code point is an ASCII code point, return a byte
2471
      // whose value is code point.
2472
      if (isASCIICodePoint(code_point)) return code_point;
×
2473

2474
      // 2. If code point is U+00A5, return byte 0x5C.
2475
      if (code_point === 0x00a5) return 0x5c;
×
2476

2477
      // 3. If code point is U+203E, return byte 0x7E.
2478
      if (code_point === 0x203e) return 0x7e;
×
2479
    }
2480

2481
    // 6. If code point is an ASCII code point, and iso-2022-jp
2482
    // encoder state is not ASCII, prepend code point to stream, set
2483
    // iso-2022-jp encoder state to ASCII, and return three bytes
2484
    // 0x1B 0x28 0x42.
2485
    if (isASCIICodePoint(code_point) && iso2022jp_state !== states.ASCII) {
×
2486
      stream.prepend(code_point);
×
2487
      iso2022jp_state = states.ASCII;
×
2488
      return [0x1b, 0x28, 0x42];
×
2489
    }
2490

2491
    // 7. If code point is either U+00A5 or U+203E, and iso-2022-jp
2492
    // encoder state is not Roman, prepend code point to stream, set
2493
    // iso-2022-jp encoder state to Roman, and return three bytes
2494
    // 0x1B 0x28 0x4A.
2495
    if ((code_point === 0x00a5 || code_point === 0x203e) && iso2022jp_state !== states.Roman) {
×
2496
      stream.prepend(code_point);
×
2497
      iso2022jp_state = states.Roman;
×
2498
      return [0x1b, 0x28, 0x4a];
×
2499
    }
2500

2501
    // 8. If code point is U+2212, set it to U+FF0D.
2502
    if (code_point === 0x2212) code_point = 0xff0d;
×
2503

2504
    // 9. Let pointer be the index pointer for code point in index
2505
    // jis0208.
2506
    var pointer = indexPointerFor(code_point, index('jis0208'));
×
2507

2508
    // 10. If pointer is null, return error with code point.
2509
    if (pointer === null) return encoderError(code_point);
×
2510

2511
    // 11. If iso-2022-jp encoder state is not jis0208, prepend code
2512
    // point to stream, set iso-2022-jp encoder state to jis0208,
2513
    // and return three bytes 0x1B 0x24 0x42.
2514
    if (iso2022jp_state !== states.jis0208) {
×
2515
      stream.prepend(code_point);
×
2516
      iso2022jp_state = states.jis0208;
×
2517
      return [0x1b, 0x24, 0x42];
×
2518
    }
2519

2520
    // 12. Let lead be floor(pointer / 94) + 0x21.
2521
    var lead = floor(pointer / 94) + 0x21;
×
2522

2523
    // 13. Let trail be pointer % 94 + 0x21.
2524
    var trail = (pointer % 94) + 0x21;
×
2525

2526
    // 14. Return two bytes whose values are lead and trail.
2527
    return [lead, trail];
×
2528
  };
2529
}
2530

2531
/** @param {{fatal: boolean}} options */
UNCOV
2532
encoders['ISO-2022-JP'] = function (options) {
379✔
2533
  return new ISO2022JPEncoder(options);
×
2534
};
2535
/** @param {{fatal: boolean}} options */
UNCOV
2536
decoders['ISO-2022-JP'] = function (options) {
379✔
2537
  return new ISO2022JPDecoder(options);
×
2538
};
2539

2540
// 13.3 Shift_JIS
2541

2542
// 13.3.1 Shift_JIS decoder
2543
/**
2544
 * @constructor
2545
 * @implements {Decoder}
2546
 * @param {{fatal: boolean}} options
2547
 */
2548
function ShiftJISDecoder(options) {
2549
  var fatal = options.fatal;
×
2550
  // Shift_JIS's decoder has an associated Shift_JIS lead (initially
2551
  // 0x00).
2552
  var /** @type {number} */ Shift_JIS_lead = 0x00;
×
2553
  /**
2554
   * @param {Stream} stream The stream of bytes being decoded.
2555
   * @param {number} bite The next byte read from the stream.
2556
   * @return {?(number|!Array.<number>)} The next code point(s)
2557
   *     decoded, or null if not enough data exists in the input
2558
   *     stream to decode a complete code point.
2559
   */
2560
  this.handler = function (stream, bite) {
×
2561
    // 1. If byte is end-of-stream and Shift_JIS lead is not 0x00,
2562
    // set Shift_JIS lead to 0x00 and return error.
2563
    if (bite === end_of_stream && Shift_JIS_lead !== 0x00) {
×
2564
      Shift_JIS_lead = 0x00;
×
2565
      return decoderError(fatal);
×
2566
    }
2567

2568
    // 2. If byte is end-of-stream and Shift_JIS lead is 0x00,
2569
    // return finished.
2570
    if (bite === end_of_stream && Shift_JIS_lead === 0x00) return finished;
×
2571

2572
    // 3. If Shift_JIS lead is not 0x00, let lead be Shift_JIS lead,
2573
    // let pointer be null, set Shift_JIS lead to 0x00, and then run
2574
    // these substeps:
2575
    if (Shift_JIS_lead !== 0x00) {
×
2576
      var lead = Shift_JIS_lead;
×
2577
      var pointer = null;
×
2578
      Shift_JIS_lead = 0x00;
×
2579

2580
      // 1. Let offset be 0x40, if byte is less than 0x7F, and 0x41
2581
      // otherwise.
2582
      var offset = bite < 0x7f ? 0x40 : 0x41;
×
2583

2584
      // 2. Let lead offset be 0x81, if lead is less than 0xA0, and
2585
      // 0xC1 otherwise.
2586
      var lead_offset = lead < 0xa0 ? 0x81 : 0xc1;
×
2587

2588
      // 3. If byte is in the range 0x40 to 0x7E, inclusive, or 0x80
2589
      // to 0xFC, inclusive, set pointer to (lead − lead offset) ×
2590
      // 188 + byte − offset.
2591
      if (inRange(bite, 0x40, 0x7e) || inRange(bite, 0x80, 0xfc))
×
2592
        pointer = (lead - lead_offset) * 188 + bite - offset;
×
2593

2594
      // 4. If pointer is in the range 8836 to 10715, inclusive,
2595
      // return a code point whose value is 0xE000 − 8836 + pointer.
2596
      if (inRange(pointer, 8836, 10715)) return 0xe000 - 8836 + pointer;
×
2597

2598
      // 5. Let code point be null, if pointer is null, and the
2599
      // index code point for pointer in index jis0208 otherwise.
2600
      var code_point = pointer === null ? null : indexCodePointFor(pointer, index('jis0208'));
×
2601

2602
      // 6. If code point is null and byte is an ASCII byte, prepend
2603
      // byte to stream.
2604
      if (code_point === null && isASCIIByte(bite)) stream.prepend(bite);
×
2605

2606
      // 7. If code point is null, return error.
2607
      if (code_point === null) return decoderError(fatal);
×
2608

2609
      // 8. Return a code point whose value is code point.
2610
      return code_point;
×
2611
    }
2612

2613
    // 4. If byte is an ASCII byte or 0x80, return a code point
2614
    // whose value is byte.
2615
    if (isASCIIByte(bite) || bite === 0x80) return bite;
×
2616

2617
    // 5. If byte is in the range 0xA1 to 0xDF, inclusive, return a
2618
    // code point whose value is 0xFF61 − 0xA1 + byte.
2619
    if (inRange(bite, 0xa1, 0xdf)) return 0xff61 - 0xa1 + bite;
×
2620

2621
    // 6. If byte is in the range 0x81 to 0x9F, inclusive, or 0xE0
2622
    // to 0xFC, inclusive, set Shift_JIS lead to byte and return
2623
    // continue.
2624
    if (inRange(bite, 0x81, 0x9f) || inRange(bite, 0xe0, 0xfc)) {
×
2625
      Shift_JIS_lead = bite;
×
2626
      return null;
×
2627
    }
2628

2629
    // 7. Return error.
2630
    return decoderError(fatal);
×
2631
  };
2632
}
2633

2634
// 13.3.2 Shift_JIS encoder
2635
/**
2636
 * @constructor
2637
 * @implements {Encoder}
2638
 * @param {{fatal: boolean}} options
2639
 */
2640
function ShiftJISEncoder(options) {
2641
  var _fatal = options.fatal;
×
2642
  /**
2643
   * @param {Stream} stream Input stream.
2644
   * @param {number} code_point Next code point read from the stream.
2645
   * @return {(number|!Array.<number>)} Byte(s) to emit.
2646
   */
2647
  this.handler = function (stream, code_point) {
×
2648
    // 1. If code point is end-of-stream, return finished.
2649
    if (code_point === end_of_stream) return finished;
×
2650

2651
    // 2. If code point is an ASCII code point or U+0080, return a
2652
    // byte whose value is code point.
2653
    if (isASCIICodePoint(code_point) || code_point === 0x0080) return code_point;
×
2654

2655
    // 3. If code point is U+00A5, return byte 0x5C.
2656
    if (code_point === 0x00a5) return 0x5c;
×
2657

2658
    // 4. If code point is U+203E, return byte 0x7E.
2659
    if (code_point === 0x203e) return 0x7e;
×
2660

2661
    // 5. If code point is in the range U+FF61 to U+FF9F, inclusive,
2662
    // return a byte whose value is code point − 0xFF61 + 0xA1.
2663
    if (inRange(code_point, 0xff61, 0xff9f)) return code_point - 0xff61 + 0xa1;
×
2664

2665
    // 6. If code point is U+2212, set it to U+FF0D.
2666
    if (code_point === 0x2212) code_point = 0xff0d;
×
2667

2668
    // 7. Let pointer be the index Shift_JIS pointer for code point.
2669
    var pointer = indexShiftJISPointerFor(code_point);
×
2670

2671
    // 8. If pointer is null, return error with code point.
2672
    if (pointer === null) return encoderError(code_point);
×
2673

2674
    // 9. Let lead be floor(pointer / 188).
2675
    var lead = floor(pointer / 188);
×
2676

2677
    // 10. Let lead offset be 0x81, if lead is less than 0x1F, and
2678
    // 0xC1 otherwise.
2679
    var lead_offset = lead < 0x1f ? 0x81 : 0xc1;
×
2680

2681
    // 11. Let trail be pointer % 188.
2682
    var trail = pointer % 188;
×
2683

2684
    // 12. Let offset be 0x40, if trail is less than 0x3F, and 0x41
2685
    // otherwise.
2686
    var offset = trail < 0x3f ? 0x40 : 0x41;
×
2687

2688
    // 13. Return two bytes whose values are lead + lead offset and
2689
    // trail + offset.
2690
    return [lead + lead_offset, trail + offset];
×
2691
  };
2692
}
2693

2694
/** @param {{fatal: boolean}} options */
UNCOV
2695
encoders['Shift_JIS'] = function (options) {
379✔
2696
  return new ShiftJISEncoder(options);
×
2697
};
2698
/** @param {{fatal: boolean}} options */
UNCOV
2699
decoders['Shift_JIS'] = function (options) {
379✔
2700
  return new ShiftJISDecoder(options);
×
2701
};
2702

2703
//
2704
// 14. Legacy multi-byte Korean encodings
2705
//
2706

2707
// 14.1 euc-kr
2708

2709
// 14.1.1 euc-kr decoder
2710
/**
2711
 * @constructor
2712
 * @implements {Decoder}
2713
 * @param {{fatal: boolean}} options
2714
 */
2715
function EUCKRDecoder(options) {
2716
  var fatal = options.fatal;
×
2717

2718
  // euc-kr's decoder has an associated euc-kr lead (initially 0x00).
2719
  var /** @type {number} */ euckr_lead = 0x00;
×
2720
  /**
2721
   * @param {Stream} stream The stream of bytes being decoded.
2722
   * @param {number} bite The next byte read from the stream.
2723
   * @return {?(number|!Array.<number>)} The next code point(s)
2724
   *     decoded, or null if not enough data exists in the input
2725
   *     stream to decode a complete code point.
2726
   */
2727
  this.handler = function (stream, bite) {
×
2728
    // 1. If byte is end-of-stream and euc-kr lead is not 0x00, set
2729
    // euc-kr lead to 0x00 and return error.
2730
    if (bite === end_of_stream && euckr_lead !== 0) {
×
2731
      euckr_lead = 0x00;
×
2732
      return decoderError(fatal);
×
2733
    }
2734

2735
    // 2. If byte is end-of-stream and euc-kr lead is 0x00, return
2736
    // finished.
2737
    if (bite === end_of_stream && euckr_lead === 0) return finished;
×
2738

2739
    // 3. If euc-kr lead is not 0x00, let lead be euc-kr lead, let
2740
    // pointer be null, set euc-kr lead to 0x00, and then run these
2741
    // substeps:
2742
    if (euckr_lead !== 0x00) {
×
2743
      var lead = euckr_lead;
×
2744
      var pointer = null;
×
2745
      euckr_lead = 0x00;
×
2746

2747
      // 1. If byte is in the range 0x41 to 0xFE, inclusive, set
2748
      // pointer to (lead − 0x81) × 190 + (byte − 0x41).
2749
      if (inRange(bite, 0x41, 0xfe)) pointer = (lead - 0x81) * 190 + (bite - 0x41);
×
2750

2751
      // 2. Let code point be null, if pointer is null, and the
2752
      // index code point for pointer in index euc-kr otherwise.
2753
      var code_point = pointer === null ? null : indexCodePointFor(pointer, index('euc-kr'));
×
2754

2755
      // 3. If code point is null and byte is an ASCII byte, prepend
2756
      // byte to stream.
2757
      if (pointer === null && isASCIIByte(bite)) stream.prepend(bite);
×
2758

2759
      // 4. If code point is null, return error.
2760
      if (code_point === null) return decoderError(fatal);
×
2761

2762
      // 5. Return a code point whose value is code point.
2763
      return code_point;
×
2764
    }
2765

2766
    // 4. If byte is an ASCII byte, return a code point whose value
2767
    // is byte.
2768
    if (isASCIIByte(bite)) return bite;
×
2769

2770
    // 5. If byte is in the range 0x81 to 0xFE, inclusive, set
2771
    // euc-kr lead to byte and return continue.
2772
    if (inRange(bite, 0x81, 0xfe)) {
×
2773
      euckr_lead = bite;
×
2774
      return null;
×
2775
    }
2776

2777
    // 6. Return error.
2778
    return decoderError(fatal);
×
2779
  };
2780
}
2781

2782
// 14.1.2 euc-kr encoder
2783
/**
2784
 * @constructor
2785
 * @implements {Encoder}
2786
 * @param {{fatal: boolean}} options
2787
 */
2788
function EUCKREncoder(options) {
2789
  var _fatal = options.fatal;
×
2790
  /**
2791
   * @param {Stream} stream Input stream.
2792
   * @param {number} code_point Next code point read from the stream.
2793
   * @return {(number|!Array.<number>)} Byte(s) to emit.
2794
   */
2795
  this.handler = function (stream, code_point) {
×
2796
    // 1. If code point is end-of-stream, return finished.
2797
    if (code_point === end_of_stream) return finished;
×
2798

2799
    // 2. If code point is an ASCII code point, return a byte whose
2800
    // value is code point.
2801
    if (isASCIICodePoint(code_point)) return code_point;
×
2802

2803
    // 3. Let pointer be the index pointer for code point in index
2804
    // euc-kr.
2805
    var pointer = indexPointerFor(code_point, index('euc-kr'));
×
2806

2807
    // 4. If pointer is null, return error with code point.
2808
    if (pointer === null) return encoderError(code_point);
×
2809

2810
    // 5. Let lead be floor(pointer / 190) + 0x81.
2811
    var lead = floor(pointer / 190) + 0x81;
×
2812

2813
    // 6. Let trail be pointer % 190 + 0x41.
2814
    var trail = (pointer % 190) + 0x41;
×
2815

2816
    // 7. Return two bytes whose values are lead and trail.
2817
    return [lead, trail];
×
2818
  };
2819
}
2820

2821
/** @param {{fatal: boolean}} options */
UNCOV
2822
encoders['EUC-KR'] = function (options) {
379✔
2823
  return new EUCKREncoder(options);
×
2824
};
2825
/** @param {{fatal: boolean}} options */
UNCOV
2826
decoders['EUC-KR'] = function (options) {
379✔
2827
  return new EUCKRDecoder(options);
×
2828
};
2829

2830
//
2831
// 15. Legacy miscellaneous encodings
2832
//
2833

2834
// 15.1 replacement
2835

2836
// Not needed - API throws RangeError
2837

2838
// 15.2 Common infrastructure for utf-16be and utf-16le
2839

2840
/**
2841
 * @param {number} code_unit
2842
 * @param {boolean} utf16be
2843
 * @return {!Array.<number>} bytes
2844
 */
2845
function convertCodeUnitToBytes(code_unit, utf16be) {
2846
  // 1. Let byte1 be code unit >> 8.
2847
  var byte1 = code_unit >> 8;
×
2848

2849
  // 2. Let byte2 be code unit & 0x00FF.
2850
  var byte2 = code_unit & 0x00ff;
×
2851

2852
  // 3. Then return the bytes in order:
2853
  // utf-16be flag is set: byte1, then byte2.
2854
  if (utf16be) return [byte1, byte2];
×
2855
  // utf-16be flag is unset: byte2, then byte1.
2856
  return [byte2, byte1];
×
2857
}
2858

2859
// 15.2.1 shared utf-16 decoder
2860
/**
2861
 * @constructor
2862
 * @implements {Decoder}
2863
 * @param {boolean} utf16_be True if big-endian, false if little-endian.
2864
 * @param {{fatal: boolean}} options
2865
 */
2866
function UTF16Decoder(utf16_be, options) {
2867
  var fatal = options.fatal;
×
2868
  var /** @type {?number} */ utf16_lead_byte = null,
×
2869
    /** @type {?number} */ utf16_lead_surrogate = null;
×
2870
  /**
2871
   * @param {Stream} stream The stream of bytes being decoded.
2872
   * @param {number} bite The next byte read from the stream.
2873
   * @return {?(number|!Array.<number>)} The next code point(s)
2874
   *     decoded, or null if not enough data exists in the input
2875
   *     stream to decode a complete code point.
2876
   */
2877
  this.handler = function (stream, bite) {
×
2878
    // 1. If byte is end-of-stream and either utf-16 lead byte or
2879
    // utf-16 lead surrogate is not null, set utf-16 lead byte and
2880
    // utf-16 lead surrogate to null, and return error.
2881
    if (bite === end_of_stream && (utf16_lead_byte !== null || utf16_lead_surrogate !== null)) {
×
2882
      return decoderError(fatal);
×
2883
    }
2884

2885
    // 2. If byte is end-of-stream and utf-16 lead byte and utf-16
2886
    // lead surrogate are null, return finished.
2887
    if (bite === end_of_stream && utf16_lead_byte === null && utf16_lead_surrogate === null) {
×
2888
      return finished;
×
2889
    }
2890

2891
    // 3. If utf-16 lead byte is null, set utf-16 lead byte to byte
2892
    // and return continue.
2893
    if (utf16_lead_byte === null) {
×
2894
      utf16_lead_byte = bite;
×
2895
      return null;
×
2896
    }
2897

2898
    // 4. Let code unit be the result of:
2899
    var code_unit;
2900
    if (utf16_be) {
×
2901
      // utf-16be decoder flag is set
2902
      //   (utf-16 lead byte << 8) + byte.
2903
      code_unit = (utf16_lead_byte << 8) + bite;
×
2904
    } else {
2905
      // utf-16be decoder flag is unset
2906
      //   (byte << 8) + utf-16 lead byte.
2907
      code_unit = (bite << 8) + utf16_lead_byte;
×
2908
    }
2909
    // Then set utf-16 lead byte to null.
2910
    utf16_lead_byte = null;
×
2911

2912
    // 5. If utf-16 lead surrogate is not null, let lead surrogate
2913
    // be utf-16 lead surrogate, set utf-16 lead surrogate to null,
2914
    // and then run these substeps:
2915
    if (utf16_lead_surrogate !== null) {
×
2916
      var lead_surrogate = utf16_lead_surrogate;
×
2917
      utf16_lead_surrogate = null;
×
2918

2919
      // 1. If code unit is in the range U+DC00 to U+DFFF,
2920
      // inclusive, return a code point whose value is 0x10000 +
2921
      // ((lead surrogate − 0xD800) << 10) + (code unit − 0xDC00).
2922
      if (inRange(code_unit, 0xdc00, 0xdfff)) {
×
2923
        return 0x10000 + (lead_surrogate - 0xd800) * 0x400 + (code_unit - 0xdc00);
×
2924
      }
2925

2926
      // 2. Prepend the sequence resulting of converting code unit
2927
      // to bytes using utf-16be decoder flag to stream and return
2928
      // error.
2929
      stream.prepend(convertCodeUnitToBytes(code_unit, utf16_be));
×
2930
      return decoderError(fatal);
×
2931
    }
2932

2933
    // 6. If code unit is in the range U+D800 to U+DBFF, inclusive,
2934
    // set utf-16 lead surrogate to code unit and return continue.
2935
    if (inRange(code_unit, 0xd800, 0xdbff)) {
×
2936
      utf16_lead_surrogate = code_unit;
×
2937
      return null;
×
2938
    }
2939

2940
    // 7. If code unit is in the range U+DC00 to U+DFFF, inclusive,
2941
    // return error.
2942
    if (inRange(code_unit, 0xdc00, 0xdfff)) return decoderError(fatal);
×
2943

2944
    // 8. Return code point code unit.
2945
    return code_unit;
×
2946
  };
2947
}
2948

2949
// 15.2.2 shared utf-16 encoder
2950
/**
2951
 * @constructor
2952
 * @implements {Encoder}
2953
 * @param {boolean} utf16_be True if big-endian, false if little-endian.
2954
 * @param {{fatal: boolean}} options
2955
 */
2956
function UTF16Encoder(utf16_be, options) {
2957
  var _fatal = options.fatal;
×
2958
  /**
2959
   * @param {Stream} stream Input stream.
2960
   * @param {number} code_point Next code point read from the stream.
2961
   * @return {(number|!Array.<number>)} Byte(s) to emit.
2962
   */
2963
  this.handler = function (stream, code_point) {
×
2964
    // 1. If code point is end-of-stream, return finished.
2965
    if (code_point === end_of_stream) return finished;
×
2966

2967
    // 2. If code point is in the range U+0000 to U+FFFF, inclusive,
2968
    // return the sequence resulting of converting code point to
2969
    // bytes using utf-16be encoder flag.
2970
    if (inRange(code_point, 0x0000, 0xffff)) return convertCodeUnitToBytes(code_point, utf16_be);
×
2971

2972
    // 3. Let lead be ((code point − 0x10000) >> 10) + 0xD800,
2973
    // converted to bytes using utf-16be encoder flag.
2974
    var lead = convertCodeUnitToBytes(((code_point - 0x10000) >> 10) + 0xd800, utf16_be);
×
2975

2976
    // 4. Let trail be ((code point − 0x10000) & 0x3FF) + 0xDC00,
2977
    // converted to bytes using utf-16be encoder flag.
2978
    var trail = convertCodeUnitToBytes(((code_point - 0x10000) & 0x3ff) + 0xdc00, utf16_be);
×
2979

2980
    // 5. Return a byte sequence of lead followed by trail.
2981
    return lead.concat(trail);
×
2982
  };
2983
}
2984

2985
// 15.3 utf-16be
2986
// 15.3.1 utf-16be decoder
2987
/** @param {{fatal: boolean}} options */
UNCOV
2988
encoders['UTF-16BE'] = function (options) {
379✔
2989
  return new UTF16Encoder(true, options);
×
2990
};
2991
// 15.3.2 utf-16be encoder
2992
/** @param {{fatal: boolean}} options */
UNCOV
2993
decoders['UTF-16BE'] = function (options) {
379✔
2994
  return new UTF16Decoder(true, options);
×
2995
};
2996

2997
// 15.4 utf-16le
2998
// 15.4.1 utf-16le decoder
2999
/** @param {{fatal: boolean}} options */
UNCOV
3000
encoders['UTF-16LE'] = function (options) {
379✔
3001
  return new UTF16Encoder(false, options);
×
3002
};
3003
// 15.4.2 utf-16le encoder
3004
/** @param {{fatal: boolean}} options */
UNCOV
3005
decoders['UTF-16LE'] = function (options) {
379✔
3006
  return new UTF16Decoder(false, options);
×
3007
};
3008

3009
// 15.5 x-user-defined
3010

3011
// 15.5.1 x-user-defined decoder
3012
/**
3013
 * @constructor
3014
 * @implements {Decoder}
3015
 * @param {{fatal: boolean}} options
3016
 */
3017
function XUserDefinedDecoder(options) {
3018
  var _fatal = options.fatal;
×
3019
  /**
3020
   * @param {Stream} stream The stream of bytes being decoded.
3021
   * @param {number} bite The next byte read from the stream.
3022
   * @return {?(number|!Array.<number>)} The next code point(s)
3023
   *     decoded, or null if not enough data exists in the input
3024
   *     stream to decode a complete code point.
3025
   */
3026
  this.handler = function (stream, bite) {
×
3027
    // 1. If byte is end-of-stream, return finished.
3028
    if (bite === end_of_stream) return finished;
×
3029

3030
    // 2. If byte is an ASCII byte, return a code point whose value
3031
    // is byte.
3032
    if (isASCIIByte(bite)) return bite;
×
3033

3034
    // 3. Return a code point whose value is 0xF780 + byte − 0x80.
3035
    return 0xf780 + bite - 0x80;
×
3036
  };
3037
}
3038

3039
// 15.5.2 x-user-defined encoder
3040
/**
3041
 * @constructor
3042
 * @implements {Encoder}
3043
 * @param {{fatal: boolean}} options
3044
 */
3045
function XUserDefinedEncoder(options) {
3046
  var _fatal = options.fatal;
×
3047
  /**
3048
   * @param {Stream} stream Input stream.
3049
   * @param {number} code_point Next code point read from the stream.
3050
   * @return {(number|!Array.<number>)} Byte(s) to emit.
3051
   */
3052
  this.handler = function (stream, code_point) {
×
3053
    // 1.If code point is end-of-stream, return finished.
3054
    if (code_point === end_of_stream) return finished;
×
3055

3056
    // 2. If code point is an ASCII code point, return a byte whose
3057
    // value is code point.
3058
    if (isASCIICodePoint(code_point)) return code_point;
×
3059

3060
    // 3. If code point is in the range U+F780 to U+F7FF, inclusive,
3061
    // return a byte whose value is code point − 0xF780 + 0x80.
3062
    if (inRange(code_point, 0xf780, 0xf7ff)) return code_point - 0xf780 + 0x80;
×
3063

3064
    // 4. Return error with code point.
3065
    return encoderError(code_point);
×
3066
  };
3067
}
3068

3069
/** @param {{fatal: boolean}} options */
UNCOV
3070
encoders['x-user-defined'] = function (options) {
379✔
3071
  return new XUserDefinedEncoder(options);
×
3072
};
3073
/** @param {{fatal: boolean}} options */
UNCOV
3074
decoders['x-user-defined'] = function (options) {
379✔
3075
  return new XUserDefinedDecoder(options);
×
3076
};
3077

3078
// FORK
3079
// if (!globalThis['TextEncoder']) globalThis['TextEncoder'] = TextEncoder;
3080
// if (!globalThis['TextDecoder']) globalThis['TextDecoder'] = TextDecoder;
3081
// babel.config.js skip transpiling files in `libs/`
3082
// module.exports = {TextEncoder, TextDecoder};
3083
export {TextEncoder, TextDecoder};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc