@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.
- package/dist/bundle.js +2 -2
- package/dist/bundle.js.map +1 -0
- package/dist/index.js +49 -84
- package/dist/index.js.map +1 -0
- package/dist/lib/encoding-indexes.js +29 -34
- package/dist/lib/encoding-indexes.js.map +1 -0
- package/dist/lib/encoding.js +1281 -2610
- package/dist/lib/encoding.js.map +1 -0
- package/dist/libs/encoding-indexes-asian.js +8 -9
- package/dist/node/buffer/btoa.node.js +5 -12
- package/dist/node/buffer/btoa.node.js.map +1 -0
- package/dist/node/buffer/to-array-buffer.node.js +8 -11
- package/dist/node/buffer/to-array-buffer.node.js.map +1 -0
- package/dist/node/fetch/fetch.node.js +112 -103
- package/dist/node/fetch/fetch.node.js.map +1 -0
- package/dist/node/fetch/headers.node.js +114 -94
- package/dist/node/fetch/headers.node.js.map +1 -0
- package/dist/node/fetch/response.node.js +84 -73
- package/dist/node/fetch/response.node.js.map +1 -0
- package/dist/node/fetch/utils/decode-data-uri.node.js +53 -63
- package/dist/node/fetch/utils/decode-data-uri.node.js.map +1 -0
- package/dist/node/fetch/utils/stream-utils.node.js +87 -105
- package/dist/node/fetch/utils/stream-utils.node.js.map +1 -0
- package/dist/node/file/blob-stream-controller.js +52 -59
- package/dist/node/file/blob-stream-controller.js.map +1 -0
- package/dist/node/file/blob-stream.js +25 -36
- package/dist/node/file/blob-stream.js.map +1 -0
- package/dist/node/file/blob.js +131 -151
- package/dist/node/file/blob.js.map +1 -0
- package/dist/node/file/file-reader.js +77 -28
- package/dist/node/file/file-reader.js.map +1 -0
- package/dist/node/file/file.js +25 -36
- package/dist/node/file/file.js.map +1 -0
- package/dist/node/file/install-file-polyfills.js +21 -26
- package/dist/node/file/install-file-polyfills.js.map +1 -0
- package/dist/node/file/readable-stream.js +3 -10
- package/dist/node/file/readable-stream.js.map +1 -0
- package/dist/node/images/encode-image.node.js +17 -38
- package/dist/node/images/encode-image.node.js.map +1 -0
- package/dist/node/images/parse-image.node.js +19 -28
- package/dist/node/images/parse-image.node.js.map +1 -0
- package/dist/promise/all-settled.js +17 -22
- package/dist/promise/all-settled.js.map +1 -0
- package/dist/utils/assert.js +5 -8
- package/dist/utils/assert.js.map +1 -0
- package/dist/utils/globals.js +7 -34
- package/dist/utils/globals.js.map +1 -0
- package/package.json +4 -4
- package/dist/es5/bundle.js +0 -7
- package/dist/es5/bundle.js.map +0 -1
- package/dist/es5/index.js +0 -122
- package/dist/es5/index.js.map +0 -1
- package/dist/es5/lib/encoding-indexes.js +0 -37
- package/dist/es5/lib/encoding-indexes.js.map +0 -1
- package/dist/es5/lib/encoding.js +0 -1465
- package/dist/es5/lib/encoding.js.map +0 -1
- package/dist/es5/libs/encoding-indexes-asian.js +0 -13
- package/dist/es5/node/buffer/btoa.node.js +0 -16
- package/dist/es5/node/buffer/btoa.node.js.map +0 -1
- package/dist/es5/node/buffer/to-array-buffer.node.js +0 -16
- package/dist/es5/node/buffer/to-array-buffer.node.js.map +0 -1
- package/dist/es5/node/fetch/fetch.node.js +0 -183
- package/dist/es5/node/fetch/fetch.node.js.map +0 -1
- package/dist/es5/node/fetch/headers.node.js +0 -175
- package/dist/es5/node/fetch/headers.node.js.map +0 -1
- package/dist/es5/node/fetch/response.node.js +0 -231
- package/dist/es5/node/fetch/response.node.js.map +0 -1
- package/dist/es5/node/fetch/utils/decode-data-uri.node.js +0 -72
- package/dist/es5/node/fetch/utils/decode-data-uri.node.js.map +0 -1
- package/dist/es5/node/fetch/utils/stream-utils.node.js +0 -213
- package/dist/es5/node/fetch/utils/stream-utils.node.js.map +0 -1
- package/dist/es5/node/file/blob-stream-controller.js +0 -110
- package/dist/es5/node/file/blob-stream-controller.js.map +0 -1
- package/dist/es5/node/file/blob-stream.js +0 -93
- package/dist/es5/node/file/blob-stream.js.map +0 -1
- package/dist/es5/node/file/blob.js +0 -259
- package/dist/es5/node/file/blob.js.map +0 -1
- package/dist/es5/node/file/file-reader.js +0 -189
- package/dist/es5/node/file/file-reader.js.map +0 -1
- package/dist/es5/node/file/file.js +0 -63
- package/dist/es5/node/file/file.js.map +0 -1
- package/dist/es5/node/file/install-file-polyfills.js +0 -33
- package/dist/es5/node/file/install-file-polyfills.js.map +0 -1
- package/dist/es5/node/file/readable-stream.js +0 -40
- package/dist/es5/node/file/readable-stream.js.map +0 -1
- package/dist/es5/node/images/encode-image.node.js +0 -36
- package/dist/es5/node/images/encode-image.node.js.map +0 -1
- package/dist/es5/node/images/parse-image.node.js +0 -61
- package/dist/es5/node/images/parse-image.node.js.map +0 -1
- package/dist/es5/promise/all-settled.js +0 -29
- package/dist/es5/promise/all-settled.js.map +0 -1
- package/dist/es5/utils/assert.js +0 -13
- package/dist/es5/utils/assert.js.map +0 -1
- package/dist/es5/utils/globals.js +0 -21
- package/dist/es5/utils/globals.js.map +0 -1
- package/dist/esm/bundle.js +0 -5
- package/dist/esm/bundle.js.map +0 -1
- package/dist/esm/index.js +0 -63
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/lib/encoding-indexes.js +0 -30
- package/dist/esm/lib/encoding-indexes.js.map +0 -1
- package/dist/esm/lib/encoding.js +0 -1450
- package/dist/esm/lib/encoding.js.map +0 -1
- package/dist/esm/libs/encoding-indexes-asian.js +0 -13
- package/dist/esm/node/buffer/btoa.node.js +0 -7
- package/dist/esm/node/buffer/btoa.node.js.map +0 -1
- package/dist/esm/node/buffer/to-array-buffer.node.js +0 -9
- package/dist/esm/node/buffer/to-array-buffer.node.js.map +0 -1
- package/dist/esm/node/fetch/fetch.node.js +0 -126
- package/dist/esm/node/fetch/fetch.node.js.map +0 -1
- package/dist/esm/node/fetch/headers.node.js +0 -125
- package/dist/esm/node/fetch/headers.node.js.map +0 -1
- package/dist/esm/node/fetch/response.node.js +0 -88
- package/dist/esm/node/fetch/response.node.js.map +0 -1
- package/dist/esm/node/fetch/utils/decode-data-uri.node.js +0 -55
- package/dist/esm/node/fetch/utils/decode-data-uri.node.js.map +0 -1
- package/dist/esm/node/fetch/utils/stream-utils.node.js +0 -98
- package/dist/esm/node/fetch/utils/stream-utils.node.js.map +0 -1
- package/dist/esm/node/file/blob-stream-controller.js +0 -56
- package/dist/esm/node/file/blob-stream-controller.js.map +0 -1
- package/dist/esm/node/file/blob-stream.js +0 -26
- package/dist/esm/node/file/blob-stream.js.map +0 -1
- package/dist/esm/node/file/blob.js +0 -140
- package/dist/esm/node/file/blob.js.map +0 -1
- package/dist/esm/node/file/file-reader.js +0 -84
- package/dist/esm/node/file/file-reader.js.map +0 -1
- package/dist/esm/node/file/file.js +0 -26
- package/dist/esm/node/file/file.js.map +0 -1
- package/dist/esm/node/file/install-file-polyfills.js +0 -22
- package/dist/esm/node/file/install-file-polyfills.js.map +0 -1
- package/dist/esm/node/file/readable-stream.js +0 -4
- package/dist/esm/node/file/readable-stream.js.map +0 -1
- package/dist/esm/node/images/encode-image.node.js +0 -20
- package/dist/esm/node/images/encode-image.node.js.map +0 -1
- package/dist/esm/node/images/parse-image.node.js +0 -20
- package/dist/esm/node/images/parse-image.node.js.map +0 -1
- package/dist/esm/promise/all-settled.js +0 -19
- package/dist/esm/promise/all-settled.js.map +0 -1
- package/dist/esm/utils/assert.js +0 -6
- package/dist/esm/utils/assert.js.map +0 -1
- package/dist/esm/utils/globals.js +0 -9
- package/dist/esm/utils/globals.js.map +0 -1
package/dist/lib/encoding.js
CHANGED
|
@@ -1,2779 +1,1450 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
var
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
u.push(
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
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
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
165
|
-
|
|
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
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
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
|
-
|
|
239
|
-
|
|
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
|
-
|
|
120
|
+
throw TypeError('The code point ' + code_point + ' could not be encoded.');
|
|
248
121
|
}
|
|
249
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
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
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
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
|
-
|
|
647
|
-
|
|
648
|
-
|
|
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
|
-
|
|
668
|
-
|
|
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
|
-
|
|
679
|
-
|
|
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
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
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
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
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
|
-
|
|
723
|
-
|
|
724
|
-
|
|
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
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
var
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
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
|
-
|
|
754
|
-
|
|
755
|
-
|
|
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
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
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
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
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
|
-
|
|
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
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
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
|
-
|
|
415
|
+
|
|
869
416
|
if (Object.defineProperty) {
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
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
|
-
|
|
910
|
-
|
|
421
|
+
});
|
|
422
|
+
Object.defineProperty(TextDecoder.prototype, 'fatal', {
|
|
423
|
+
get: function () {
|
|
424
|
+
return this._error_mode === 'fatal';
|
|
911
425
|
}
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
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
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
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
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
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
|
-
|
|
490
|
+
|
|
491
|
+
return codePointsToString(stream);
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
return serializeStream.call(this, output);
|
|
1008
495
|
};
|
|
1009
|
-
|
|
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
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
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
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
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
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
return enc;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
if (!Object.defineProperty) this.encoding = enc._encoding.name.toLowerCase();
|
|
525
|
+
return enc;
|
|
1056
526
|
}
|
|
1057
|
-
|
|
527
|
+
|
|
1058
528
|
if (Object.defineProperty) {
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
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
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
var
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
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
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
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
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
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
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
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
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
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
|
-
|
|
1246
|
-
/**
|
|
1247
|
-
* @constructor
|
|
1248
|
-
* @implements {Encoder}
|
|
1249
|
-
* @param {{fatal: boolean}} options
|
|
1250
|
-
*/
|
|
625
|
+
|
|
1251
626
|
function UTF8Encoder(options) {
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
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
|
-
|
|
656
|
+
|
|
1303
657
|
encoders['UTF-8'] = function (options) {
|
|
1304
|
-
|
|
658
|
+
return new UTF8Encoder(options);
|
|
1305
659
|
};
|
|
1306
|
-
|
|
660
|
+
|
|
1307
661
|
decoders['UTF-8'] = function (options) {
|
|
1308
|
-
|
|
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
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
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
|
-
|
|
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
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
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
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
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
|
-
|
|
709
|
+
return new GB18030Decoder(options);
|
|
1408
710
|
};
|
|
1409
|
-
|
|
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
|
-
|
|
713
|
+
return new GB18030Encoder(options, true);
|
|
1414
714
|
};
|
|
1415
|
-
|
|
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
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
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
|
-
|
|
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
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
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
|
-
|
|
825
|
+
|
|
1621
826
|
encoders['gb18030'] = function (options) {
|
|
1622
|
-
|
|
827
|
+
return new GB18030Encoder(options);
|
|
1623
828
|
};
|
|
1624
|
-
|
|
829
|
+
|
|
1625
830
|
decoders['gb18030'] = function (options) {
|
|
1626
|
-
|
|
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
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
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
|
-
|
|
1722
|
-
/**
|
|
1723
|
-
* @constructor
|
|
1724
|
-
* @implements {Encoder}
|
|
1725
|
-
* @param {{fatal: boolean}} options
|
|
1726
|
-
*/
|
|
883
|
+
|
|
1727
884
|
function Big5Encoder(options) {
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
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
|
-
|
|
899
|
+
|
|
1762
900
|
encoders['Big5'] = function (options) {
|
|
1763
|
-
|
|
901
|
+
return new Big5Encoder(options);
|
|
1764
902
|
};
|
|
1765
|
-
|
|
903
|
+
|
|
1766
904
|
decoders['Big5'] = function (options) {
|
|
1767
|
-
|
|
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
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
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
|
-
|
|
1859
|
-
/**
|
|
1860
|
-
* @constructor
|
|
1861
|
-
* @implements {Encoder}
|
|
1862
|
-
* @param {{fatal: boolean}} options
|
|
1863
|
-
*/
|
|
957
|
+
|
|
1864
958
|
function EUCJPEncoder(options) {
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
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
|
-
|
|
975
|
+
|
|
1908
976
|
encoders['EUC-JP'] = function (options) {
|
|
1909
|
-
|
|
977
|
+
return new EUCJPEncoder(options);
|
|
1910
978
|
};
|
|
1911
|
-
|
|
979
|
+
|
|
1912
980
|
decoders['EUC-JP'] = function (options) {
|
|
1913
|
-
|
|
981
|
+
return new EUCJPDecoder(options);
|
|
1914
982
|
};
|
|
1915
|
-
|
|
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
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
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
|
-
|
|
2181
|
-
/**
|
|
2182
|
-
* @constructor
|
|
2183
|
-
* @implements {Encoder}
|
|
2184
|
-
* @param {{fatal: boolean}} options
|
|
2185
|
-
*/
|
|
1145
|
+
|
|
2186
1146
|
function ISO2022JPEncoder(options) {
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
|
|
2194
|
-
|
|
2195
|
-
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
|
|
2202
|
-
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
|
|
2207
|
-
|
|
2208
|
-
|
|
2209
|
-
|
|
2210
|
-
|
|
2211
|
-
|
|
2212
|
-
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
|
|
2220
|
-
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
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
|
-
|
|
1203
|
+
|
|
2289
1204
|
encoders['ISO-2022-JP'] = function (options) {
|
|
2290
|
-
|
|
1205
|
+
return new ISO2022JPEncoder(options);
|
|
2291
1206
|
};
|
|
2292
|
-
|
|
1207
|
+
|
|
2293
1208
|
decoders['ISO-2022-JP'] = function (options) {
|
|
2294
|
-
|
|
1209
|
+
return new ISO2022JPDecoder(options);
|
|
2295
1210
|
};
|
|
2296
|
-
|
|
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
|
-
|
|
2305
|
-
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
2318
|
-
|
|
2319
|
-
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
|
|
2326
|
-
|
|
2327
|
-
|
|
2328
|
-
|
|
2329
|
-
|
|
2330
|
-
|
|
2331
|
-
|
|
2332
|
-
|
|
2333
|
-
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
|
|
2337
|
-
|
|
2338
|
-
|
|
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
|
-
|
|
2381
|
-
/**
|
|
2382
|
-
* @constructor
|
|
2383
|
-
* @implements {Encoder}
|
|
2384
|
-
* @param {{fatal: boolean}} options
|
|
2385
|
-
*/
|
|
1249
|
+
|
|
2386
1250
|
function ShiftJISEncoder(options) {
|
|
2387
|
-
|
|
2388
|
-
|
|
2389
|
-
|
|
2390
|
-
|
|
2391
|
-
|
|
2392
|
-
|
|
2393
|
-
|
|
2394
|
-
|
|
2395
|
-
|
|
2396
|
-
|
|
2397
|
-
|
|
2398
|
-
|
|
2399
|
-
|
|
2400
|
-
|
|
2401
|
-
|
|
2402
|
-
|
|
2403
|
-
|
|
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
|
-
|
|
1269
|
+
|
|
2435
1270
|
encoders['Shift_JIS'] = function (options) {
|
|
2436
|
-
|
|
1271
|
+
return new ShiftJISEncoder(options);
|
|
2437
1272
|
};
|
|
2438
|
-
|
|
1273
|
+
|
|
2439
1274
|
decoders['Shift_JIS'] = function (options) {
|
|
2440
|
-
|
|
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
|
-
|
|
2454
|
-
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
|
|
2476
|
-
|
|
2477
|
-
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
|
|
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
|
-
|
|
2513
|
-
/**
|
|
2514
|
-
* @constructor
|
|
2515
|
-
* @implements {Encoder}
|
|
2516
|
-
* @param {{fatal: boolean}} options
|
|
2517
|
-
*/
|
|
1311
|
+
|
|
2518
1312
|
function EUCKREncoder(options) {
|
|
2519
|
-
|
|
2520
|
-
|
|
2521
|
-
|
|
2522
|
-
|
|
2523
|
-
|
|
2524
|
-
|
|
2525
|
-
|
|
2526
|
-
|
|
2527
|
-
|
|
2528
|
-
|
|
2529
|
-
|
|
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
|
-
|
|
1325
|
+
|
|
2548
1326
|
encoders['EUC-KR'] = function (options) {
|
|
2549
|
-
|
|
1327
|
+
return new EUCKREncoder(options);
|
|
2550
1328
|
};
|
|
2551
|
-
|
|
1329
|
+
|
|
2552
1330
|
decoders['EUC-KR'] = function (options) {
|
|
2553
|
-
|
|
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
|
-
|
|
2568
|
-
|
|
2569
|
-
|
|
2570
|
-
|
|
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
|
-
|
|
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
|
-
|
|
2587
|
-
|
|
2588
|
-
|
|
2589
|
-
|
|
2590
|
-
|
|
2591
|
-
|
|
2592
|
-
|
|
2593
|
-
|
|
2594
|
-
|
|
2595
|
-
|
|
2596
|
-
|
|
2597
|
-
|
|
2598
|
-
|
|
2599
|
-
|
|
2600
|
-
|
|
2601
|
-
|
|
2602
|
-
|
|
2603
|
-
|
|
2604
|
-
|
|
2605
|
-
|
|
2606
|
-
|
|
2607
|
-
|
|
2608
|
-
|
|
2609
|
-
|
|
2610
|
-
|
|
2611
|
-
|
|
2612
|
-
|
|
2613
|
-
|
|
2614
|
-
|
|
2615
|
-
|
|
2616
|
-
|
|
2617
|
-
|
|
2618
|
-
|
|
2619
|
-
|
|
2620
|
-
|
|
2621
|
-
|
|
2622
|
-
|
|
2623
|
-
|
|
2624
|
-
|
|
2625
|
-
|
|
2626
|
-
|
|
2627
|
-
|
|
2628
|
-
|
|
2629
|
-
|
|
2630
|
-
|
|
2631
|
-
|
|
2632
|
-
|
|
2633
|
-
|
|
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
|
-
|
|
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
|
-
|
|
2669
|
-
|
|
2670
|
-
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
|
|
2676
|
-
|
|
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
|
-
|
|
2694
|
-
// 15.3.1 utf-16be decoder
|
|
2695
|
-
/** @param {{fatal: boolean}} options */
|
|
1403
|
+
|
|
2696
1404
|
encoders['UTF-16BE'] = function (options) {
|
|
2697
|
-
|
|
1405
|
+
return new UTF16Encoder(true, options);
|
|
2698
1406
|
};
|
|
2699
|
-
|
|
2700
|
-
/** @param {{fatal: boolean}} options */
|
|
1407
|
+
|
|
2701
1408
|
decoders['UTF-16BE'] = function (options) {
|
|
2702
|
-
|
|
1409
|
+
return new UTF16Decoder(true, options);
|
|
2703
1410
|
};
|
|
2704
|
-
|
|
2705
|
-
// 15.4.1 utf-16le decoder
|
|
2706
|
-
/** @param {{fatal: boolean}} options */
|
|
1411
|
+
|
|
2707
1412
|
encoders['UTF-16LE'] = function (options) {
|
|
2708
|
-
|
|
1413
|
+
return new UTF16Encoder(false, options);
|
|
2709
1414
|
};
|
|
2710
|
-
|
|
2711
|
-
/** @param {{fatal: boolean}} options */
|
|
1415
|
+
|
|
2712
1416
|
decoders['UTF-16LE'] = function (options) {
|
|
2713
|
-
|
|
1417
|
+
return new UTF16Decoder(false, options);
|
|
2714
1418
|
};
|
|
2715
|
-
|
|
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
|
-
|
|
2724
|
-
|
|
2725
|
-
|
|
2726
|
-
|
|
2727
|
-
|
|
2728
|
-
|
|
2729
|
-
|
|
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
|
-
|
|
2744
|
-
/**
|
|
2745
|
-
* @constructor
|
|
2746
|
-
* @implements {Encoder}
|
|
2747
|
-
* @param {{fatal: boolean}} options
|
|
2748
|
-
*/
|
|
1429
|
+
|
|
2749
1430
|
function XUserDefinedEncoder(options) {
|
|
2750
|
-
|
|
2751
|
-
|
|
2752
|
-
|
|
2753
|
-
|
|
2754
|
-
|
|
2755
|
-
|
|
2756
|
-
|
|
2757
|
-
|
|
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
|
-
|
|
1440
|
+
|
|
2773
1441
|
encoders['x-user-defined'] = function (options) {
|
|
2774
|
-
|
|
1442
|
+
return new XUserDefinedEncoder(options);
|
|
2775
1443
|
};
|
|
2776
|
-
|
|
1444
|
+
|
|
2777
1445
|
decoders['x-user-defined'] = function (options) {
|
|
2778
|
-
|
|
1446
|
+
return new XUserDefinedDecoder(options);
|
|
2779
1447
|
};
|
|
1448
|
+
|
|
1449
|
+
export { TextEncoder, TextDecoder };
|
|
1450
|
+
//# sourceMappingURL=encoding.js.map
|