@loaders.gl/polyfills 3.1.3 → 4.0.0-alpha.5

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