@loaders.gl/polyfills 3.3.0-alpha.5 → 3.3.0-alpha.7
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/dist.min.js +22 -10
- package/dist/es5/bundle.js +0 -1
- package/dist/es5/bundle.js.map +1 -1
- package/dist/es5/index.js +10 -37
- package/dist/es5/index.js.map +1 -1
- package/dist/es5/lib/encoding-indexes.js.map +1 -1
- package/dist/es5/lib/encoding.js +255 -135
- package/dist/es5/lib/encoding.js.map +1 -1
- package/dist/es5/node/buffer/btoa.node.js +0 -1
- package/dist/es5/node/buffer/btoa.node.js.map +1 -1
- package/dist/es5/node/buffer/to-array-buffer.node.js +0 -2
- package/dist/es5/node/buffer/to-array-buffer.node.js.map +1 -1
- package/dist/es5/node/fetch/fetch.node.js +2 -36
- package/dist/es5/node/fetch/fetch.node.js.map +1 -1
- package/dist/es5/node/fetch/headers.node.js +0 -21
- package/dist/es5/node/fetch/headers.node.js.map +1 -1
- package/dist/es5/node/fetch/response.node.js +10 -45
- package/dist/es5/node/fetch/response.node.js.map +1 -1
- package/dist/es5/node/fetch/utils/decode-data-uri.node.js +0 -8
- package/dist/es5/node/fetch/utils/decode-data-uri.node.js.map +1 -1
- package/dist/es5/node/fetch/utils/stream-utils.node.js +10 -40
- package/dist/es5/node/fetch/utils/stream-utils.node.js.map +1 -1
- package/dist/es5/node/file/blob-stream-controller.js +5 -19
- package/dist/es5/node/file/blob-stream-controller.js.map +1 -1
- package/dist/es5/node/file/blob-stream.js +5 -30
- package/dist/es5/node/file/blob-stream.js.map +1 -1
- package/dist/es5/node/file/blob.js +17 -50
- package/dist/es5/node/file/blob.js.map +1 -1
- package/dist/es5/node/file/file-reader.js +1 -28
- package/dist/es5/node/file/file-reader.js.map +1 -1
- package/dist/es5/node/file/file.js +0 -18
- package/dist/es5/node/file/file.js.map +1 -1
- package/dist/es5/node/file/install-file-polyfills.js +0 -7
- package/dist/es5/node/file/install-file-polyfills.js.map +1 -1
- package/dist/es5/node/file/readable-stream.js +2 -14
- package/dist/es5/node/file/readable-stream.js.map +1 -1
- package/dist/es5/node/images/encode-image.node.js +2 -6
- package/dist/es5/node/images/encode-image.node.js.map +1 -1
- package/dist/es5/node/images/parse-image.node.js +1 -11
- package/dist/es5/node/images/parse-image.node.js.map +1 -1
- package/dist/es5/promise/all-settled.js +2 -2
- package/dist/es5/promise/all-settled.js.map +1 -1
- package/dist/es5/utils/assert.js +0 -1
- package/dist/es5/utils/assert.js.map +1 -1
- package/dist/es5/utils/globals.js +3 -4
- package/dist/es5/utils/globals.js.map +1 -1
- package/dist/esm/bundle.js +1 -1
- package/dist/esm/bundle.js.map +1 -1
- package/dist/esm/index.js +3 -8
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/lib/encoding-indexes.js +2 -0
- package/dist/esm/lib/encoding-indexes.js.map +1 -1
- package/dist/esm/lib/encoding.js +256 -131
- package/dist/esm/lib/encoding.js.map +1 -1
- package/dist/esm/node/buffer/btoa.node.js +2 -0
- package/dist/esm/node/buffer/btoa.node.js.map +1 -1
- package/dist/esm/node/buffer/to-array-buffer.node.js +0 -1
- package/dist/esm/node/buffer/to-array-buffer.node.js.map +1 -1
- package/dist/esm/node/fetch/fetch.node.js +5 -17
- package/dist/esm/node/fetch/fetch.node.js.map +1 -1
- package/dist/esm/node/fetch/headers.node.js +2 -24
- package/dist/esm/node/fetch/headers.node.js.map +1 -1
- package/dist/esm/node/fetch/response.node.js +0 -17
- package/dist/esm/node/fetch/response.node.js.map +1 -1
- package/dist/esm/node/fetch/utils/decode-data-uri.node.js +3 -6
- package/dist/esm/node/fetch/utils/decode-data-uri.node.js.map +1 -1
- package/dist/esm/node/fetch/utils/stream-utils.node.js +4 -7
- package/dist/esm/node/fetch/utils/stream-utils.node.js.map +1 -1
- package/dist/esm/node/file/blob-stream-controller.js +0 -8
- package/dist/esm/node/file/blob-stream-controller.js.map +1 -1
- package/dist/esm/node/file/blob-stream.js +1 -5
- package/dist/esm/node/file/blob-stream.js.map +1 -1
- package/dist/esm/node/file/blob.js +12 -21
- package/dist/esm/node/file/blob.js.map +1 -1
- package/dist/esm/node/file/file-reader.js +1 -24
- package/dist/esm/node/file/file-reader.js.map +1 -1
- package/dist/esm/node/file/file.js +4 -9
- package/dist/esm/node/file/file.js.map +1 -1
- package/dist/esm/node/file/install-file-polyfills.js +0 -2
- package/dist/esm/node/file/install-file-polyfills.js.map +1 -1
- package/dist/esm/node/file/readable-stream.js +2 -0
- package/dist/esm/node/file/readable-stream.js.map +1 -1
- package/dist/esm/node/images/encode-image.node.js +4 -0
- package/dist/esm/node/images/encode-image.node.js.map +1 -1
- package/dist/esm/node/images/parse-image.node.js +3 -0
- package/dist/esm/node/images/parse-image.node.js.map +1 -1
- package/dist/esm/promise/all-settled.js +1 -0
- package/dist/esm/promise/all-settled.js.map +1 -1
- package/dist/esm/utils/assert.js.map +1 -1
- package/dist/esm/utils/globals.js +4 -1
- package/dist/esm/utils/globals.js.map +1 -1
- package/dist/index.js +5 -1
- package/dist/libs/encoding-indexes-asian.d.ts +1 -1
- package/package.json +2 -2
package/dist/esm/lib/encoding.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
1
3
|
import indexes from './encoding-indexes';
|
|
2
4
|
global['encoding-indexes'] = indexes || {};
|
|
3
5
|
|
|
@@ -8,7 +10,6 @@ function inRange(a, min, max) {
|
|
|
8
10
|
function includes(array, item) {
|
|
9
11
|
return array.indexOf(item) !== -1;
|
|
10
12
|
}
|
|
11
|
-
|
|
12
13
|
var floor = Math.floor;
|
|
13
14
|
|
|
14
15
|
function ToDictionary(o) {
|
|
@@ -18,9 +19,13 @@ function ToDictionary(o) {
|
|
|
18
19
|
}
|
|
19
20
|
|
|
20
21
|
function stringToCodePoints(string) {
|
|
22
|
+
|
|
21
23
|
var s = String(string);
|
|
24
|
+
|
|
22
25
|
var n = s.length;
|
|
26
|
+
|
|
23
27
|
var i = 0;
|
|
28
|
+
|
|
24
29
|
var u = [];
|
|
25
30
|
|
|
26
31
|
while (i < n) {
|
|
@@ -28,24 +33,34 @@ function stringToCodePoints(string) {
|
|
|
28
33
|
|
|
29
34
|
if (c < 0xd800 || c > 0xdfff) {
|
|
30
35
|
u.push(c);
|
|
31
|
-
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
else if (0xdc00 <= c && c <= 0xdfff) {
|
|
39
|
+
u.push(0xfffd);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
else if (0xd800 <= c && c <= 0xdbff) {
|
|
43
|
+
if (i === n - 1) {
|
|
32
44
|
u.push(0xfffd);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
var d = s.charCodeAt(i + 1);
|
|
48
|
+
|
|
49
|
+
if (0xdc00 <= d && d <= 0xdfff) {
|
|
50
|
+
var a = c & 0x3ff;
|
|
51
|
+
|
|
52
|
+
var b = d & 0x3ff;
|
|
53
|
+
|
|
54
|
+
u.push(0x10000 + (a << 10) + b);
|
|
55
|
+
|
|
56
|
+
i += 1;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
else {
|
|
60
|
+
u.push(0xfffd);
|
|
48
61
|
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
49
64
|
|
|
50
65
|
i += 1;
|
|
51
66
|
}
|
|
@@ -55,10 +70,8 @@ function stringToCodePoints(string) {
|
|
|
55
70
|
|
|
56
71
|
function codePointsToString(code_points) {
|
|
57
72
|
var s = '';
|
|
58
|
-
|
|
59
73
|
for (var i = 0; i < code_points.length; ++i) {
|
|
60
74
|
var cp = code_points[i];
|
|
61
|
-
|
|
62
75
|
if (cp <= 0xffff) {
|
|
63
76
|
s += String.fromCharCode(cp);
|
|
64
77
|
} else {
|
|
@@ -66,7 +79,6 @@ function codePointsToString(code_points) {
|
|
|
66
79
|
s += String.fromCharCode((cp >> 10) + 0xd800, (cp & 0x3ff) + 0xdc00);
|
|
67
80
|
}
|
|
68
81
|
}
|
|
69
|
-
|
|
70
82
|
return s;
|
|
71
83
|
}
|
|
72
84
|
|
|
@@ -75,13 +87,13 @@ function isASCIIByte(a) {
|
|
|
75
87
|
}
|
|
76
88
|
|
|
77
89
|
var isASCIICodePoint = isASCIIByte;
|
|
90
|
+
|
|
78
91
|
var end_of_stream = -1;
|
|
79
92
|
|
|
80
93
|
function Stream(tokens) {
|
|
81
94
|
this.tokens = [].slice.call(tokens);
|
|
82
95
|
this.tokens.reverse();
|
|
83
96
|
}
|
|
84
|
-
|
|
85
97
|
Stream.prototype = {
|
|
86
98
|
endOfStream: function () {
|
|
87
99
|
return !this.tokens.length;
|
|
@@ -93,7 +105,6 @@ Stream.prototype = {
|
|
|
93
105
|
prepend: function (token) {
|
|
94
106
|
if (Array.isArray(token)) {
|
|
95
107
|
var tokens = token;
|
|
96
|
-
|
|
97
108
|
while (tokens.length) this.tokens.push(tokens.pop());
|
|
98
109
|
} else {
|
|
99
110
|
this.tokens.push(token);
|
|
@@ -102,13 +113,13 @@ Stream.prototype = {
|
|
|
102
113
|
push: function (token) {
|
|
103
114
|
if (Array.isArray(token)) {
|
|
104
115
|
var tokens = token;
|
|
105
|
-
|
|
106
116
|
while (tokens.length) this.tokens.unshift(tokens.shift());
|
|
107
117
|
} else {
|
|
108
118
|
this.tokens.unshift(token);
|
|
109
119
|
}
|
|
110
120
|
}
|
|
111
121
|
};
|
|
122
|
+
|
|
112
123
|
var finished = -1;
|
|
113
124
|
|
|
114
125
|
function decoderError(fatal, opt_code_point) {
|
|
@@ -121,13 +132,11 @@ function encoderError(code_point) {
|
|
|
121
132
|
}
|
|
122
133
|
|
|
123
134
|
function Decoder() {}
|
|
124
|
-
|
|
125
135
|
Decoder.prototype = {
|
|
126
136
|
handler: function (stream, bite) {}
|
|
127
137
|
};
|
|
128
138
|
|
|
129
139
|
function Encoder() {}
|
|
130
|
-
|
|
131
140
|
Encoder.prototype = {
|
|
132
141
|
handler: function (stream, code_point) {}
|
|
133
142
|
};
|
|
@@ -138,7 +147,6 @@ function getEncoding(label) {
|
|
|
138
147
|
if (Object.prototype.hasOwnProperty.call(label_to_encoding, label)) {
|
|
139
148
|
return label_to_encoding[label];
|
|
140
149
|
}
|
|
141
|
-
|
|
142
150
|
return null;
|
|
143
151
|
}
|
|
144
152
|
|
|
@@ -284,6 +292,7 @@ var encodings = [{
|
|
|
284
292
|
}],
|
|
285
293
|
heading: 'Legacy miscellaneous encodings'
|
|
286
294
|
}];
|
|
295
|
+
|
|
287
296
|
var label_to_encoding = {};
|
|
288
297
|
encodings.forEach(function (category) {
|
|
289
298
|
category.encodings.forEach(function (encoding) {
|
|
@@ -292,6 +301,7 @@ encodings.forEach(function (category) {
|
|
|
292
301
|
});
|
|
293
302
|
});
|
|
294
303
|
});
|
|
304
|
+
|
|
295
305
|
var encoders = {};
|
|
296
306
|
var decoders = {};
|
|
297
307
|
|
|
@@ -309,21 +319,20 @@ function index(name) {
|
|
|
309
319
|
if (!('encoding-indexes' in global)) {
|
|
310
320
|
throw Error('Indexes missing.' + ' Did you forget to include encoding-indexes.js first?');
|
|
311
321
|
}
|
|
312
|
-
|
|
313
322
|
return global['encoding-indexes'][name];
|
|
314
323
|
}
|
|
315
324
|
|
|
316
325
|
function indexGB18030RangesCodePointFor(pointer) {
|
|
317
326
|
if (pointer > 39419 && pointer < 189000 || pointer > 1237575) return null;
|
|
327
|
+
|
|
318
328
|
if (pointer === 7457) return 0xe7c7;
|
|
329
|
+
|
|
319
330
|
var offset = 0;
|
|
320
331
|
var code_point_offset = 0;
|
|
321
332
|
var idx = index('gb18030-ranges');
|
|
322
333
|
var i;
|
|
323
|
-
|
|
324
334
|
for (i = 0; i < idx.length; ++i) {
|
|
325
335
|
var entry = idx[i];
|
|
326
|
-
|
|
327
336
|
if (entry[0] <= pointer) {
|
|
328
337
|
offset = entry[0];
|
|
329
338
|
code_point_offset = entry[1];
|
|
@@ -337,14 +346,13 @@ function indexGB18030RangesCodePointFor(pointer) {
|
|
|
337
346
|
|
|
338
347
|
function indexGB18030RangesPointerFor(code_point) {
|
|
339
348
|
if (code_point === 0xe7c7) return 7457;
|
|
349
|
+
|
|
340
350
|
var offset = 0;
|
|
341
351
|
var pointer_offset = 0;
|
|
342
352
|
var idx = index('gb18030-ranges');
|
|
343
353
|
var i;
|
|
344
|
-
|
|
345
354
|
for (i = 0; i < idx.length; ++i) {
|
|
346
355
|
var entry = idx[i];
|
|
347
|
-
|
|
348
356
|
if (entry[1] <= code_point) {
|
|
349
357
|
offset = entry[1];
|
|
350
358
|
pointer_offset = entry[0];
|
|
@@ -361,9 +369,9 @@ function indexShiftJISPointerFor(code_point) {
|
|
|
361
369
|
return inRange(pointer, 8272, 8835) ? null : code_point;
|
|
362
370
|
});
|
|
363
371
|
var index_ = shift_jis_index;
|
|
372
|
+
|
|
364
373
|
return index_.indexOf(code_point);
|
|
365
374
|
}
|
|
366
|
-
|
|
367
375
|
var shift_jis_index;
|
|
368
376
|
|
|
369
377
|
function indexBig5PointerFor(code_point) {
|
|
@@ -378,30 +386,35 @@ function indexBig5PointerFor(code_point) {
|
|
|
378
386
|
|
|
379
387
|
return indexPointerFor(code_point, index_);
|
|
380
388
|
}
|
|
381
|
-
|
|
382
389
|
var big5_index_no_hkscs;
|
|
390
|
+
|
|
383
391
|
var DEFAULT_ENCODING = 'utf-8';
|
|
384
392
|
|
|
385
393
|
function TextDecoder(label, options) {
|
|
386
394
|
if (!(this instanceof TextDecoder)) throw TypeError("Called as a function. Did you forget 'new'?");
|
|
387
395
|
label = label !== undefined ? String(label) : DEFAULT_ENCODING;
|
|
388
396
|
options = ToDictionary(options);
|
|
397
|
+
|
|
389
398
|
this._encoding = null;
|
|
390
399
|
this._decoder = null;
|
|
391
400
|
this._ignoreBOM = false;
|
|
392
401
|
this._BOMseen = false;
|
|
393
402
|
this._error_mode = 'replacement';
|
|
394
403
|
this._do_not_flush = false;
|
|
404
|
+
|
|
395
405
|
var encoding = getEncoding(label);
|
|
396
|
-
if (encoding === null || encoding.name === 'replacement') throw RangeError('Unknown encoding: ' + label);
|
|
397
406
|
|
|
407
|
+
if (encoding === null || encoding.name === 'replacement') throw RangeError('Unknown encoding: ' + label);
|
|
398
408
|
if (!decoders[encoding.name]) {
|
|
399
409
|
throw Error('Decoder not present.' + ' Did you forget to include encoding-indexes.js first?');
|
|
400
410
|
}
|
|
401
411
|
|
|
402
412
|
var dec = this;
|
|
413
|
+
|
|
403
414
|
dec._encoding = encoding;
|
|
415
|
+
|
|
404
416
|
if (Boolean(options['fatal'])) dec._error_mode = 'fatal';
|
|
417
|
+
|
|
405
418
|
if (Boolean(options['ignoreBOM'])) dec._ignoreBOM = true;
|
|
406
419
|
|
|
407
420
|
if (!Object.defineProperty) {
|
|
@@ -412,18 +425,19 @@ function TextDecoder(label, options) {
|
|
|
412
425
|
|
|
413
426
|
return dec;
|
|
414
427
|
}
|
|
415
|
-
|
|
416
428
|
if (Object.defineProperty) {
|
|
417
429
|
Object.defineProperty(TextDecoder.prototype, 'encoding', {
|
|
418
430
|
get: function () {
|
|
419
431
|
return this._encoding.name.toLowerCase();
|
|
420
432
|
}
|
|
421
433
|
});
|
|
434
|
+
|
|
422
435
|
Object.defineProperty(TextDecoder.prototype, 'fatal', {
|
|
423
436
|
get: function () {
|
|
424
437
|
return this._error_mode === 'fatal';
|
|
425
438
|
}
|
|
426
439
|
});
|
|
440
|
+
|
|
427
441
|
Object.defineProperty(TextDecoder.prototype, 'ignoreBOM', {
|
|
428
442
|
get: function () {
|
|
429
443
|
return this._ignoreBOM;
|
|
@@ -433,7 +447,6 @@ if (Object.defineProperty) {
|
|
|
433
447
|
|
|
434
448
|
TextDecoder.prototype.decode = function decode(input, options) {
|
|
435
449
|
var bytes;
|
|
436
|
-
|
|
437
450
|
if (typeof input === 'object' && input instanceof ArrayBuffer) {
|
|
438
451
|
bytes = new Uint8Array(input);
|
|
439
452
|
} else if (typeof input === 'object' && 'buffer' in input && input.buffer instanceof ArrayBuffer) {
|
|
@@ -441,7 +454,6 @@ TextDecoder.prototype.decode = function decode(input, options) {
|
|
|
441
454
|
} else {
|
|
442
455
|
bytes = new Uint8Array(0);
|
|
443
456
|
}
|
|
444
|
-
|
|
445
457
|
options = ToDictionary(options);
|
|
446
458
|
|
|
447
459
|
if (!this._do_not_flush) {
|
|
@@ -452,21 +464,26 @@ TextDecoder.prototype.decode = function decode(input, options) {
|
|
|
452
464
|
}
|
|
453
465
|
|
|
454
466
|
this._do_not_flush = Boolean(options['stream']);
|
|
467
|
+
|
|
455
468
|
var input_stream = new Stream(bytes);
|
|
469
|
+
|
|
456
470
|
var output = [];
|
|
471
|
+
|
|
457
472
|
var result;
|
|
458
473
|
|
|
459
474
|
while (true) {
|
|
460
475
|
var token = input_stream.read();
|
|
476
|
+
|
|
461
477
|
if (token === end_of_stream) break;
|
|
478
|
+
|
|
462
479
|
result = this._decoder.handler(input_stream, token);
|
|
463
|
-
if (result === finished) break;
|
|
464
480
|
|
|
481
|
+
if (result === finished) break;
|
|
465
482
|
if (result !== null) {
|
|
466
483
|
if (Array.isArray(result)) output.push.apply(output, result);else output.push(result);
|
|
467
484
|
}
|
|
468
|
-
}
|
|
469
485
|
|
|
486
|
+
}
|
|
470
487
|
if (!this._do_not_flush) {
|
|
471
488
|
do {
|
|
472
489
|
result = this._decoder.handler(input_stream, input_stream.read());
|
|
@@ -474,57 +491,56 @@ TextDecoder.prototype.decode = function decode(input, options) {
|
|
|
474
491
|
if (result === null) continue;
|
|
475
492
|
if (Array.isArray(result)) output.push.apply(output, result);else output.push(result);
|
|
476
493
|
} while (!input_stream.endOfStream());
|
|
477
|
-
|
|
478
494
|
this._decoder = null;
|
|
479
495
|
}
|
|
480
496
|
|
|
481
497
|
function serializeStream(stream) {
|
|
498
|
+
|
|
482
499
|
if (includes(['UTF-8', 'UTF-16LE', 'UTF-16BE'], this._encoding.name) && !this._ignoreBOM && !this._BOMseen) {
|
|
483
500
|
if (stream.length > 0 && stream[0] === 0xfeff) {
|
|
484
501
|
this._BOMseen = true;
|
|
485
502
|
stream.shift();
|
|
486
503
|
} else if (stream.length > 0) {
|
|
487
504
|
this._BOMseen = true;
|
|
488
|
-
} else {
|
|
505
|
+
} else {
|
|
506
|
+
}
|
|
489
507
|
}
|
|
490
|
-
|
|
491
508
|
return codePointsToString(stream);
|
|
492
509
|
}
|
|
493
|
-
|
|
494
510
|
return serializeStream.call(this, output);
|
|
495
511
|
};
|
|
496
512
|
|
|
497
513
|
function TextEncoder(label, options) {
|
|
498
514
|
if (!(this instanceof TextEncoder)) throw TypeError("Called as a function. Did you forget 'new'?");
|
|
499
515
|
options = ToDictionary(options);
|
|
516
|
+
|
|
500
517
|
this._encoding = null;
|
|
501
518
|
this._encoder = null;
|
|
519
|
+
|
|
502
520
|
this._do_not_flush = false;
|
|
503
521
|
this._fatal = Boolean(options['fatal']) ? 'fatal' : 'replacement';
|
|
522
|
+
|
|
504
523
|
var enc = this;
|
|
505
524
|
|
|
506
525
|
if (Boolean(options['NONSTANDARD_allowLegacyEncoding'])) {
|
|
507
526
|
label = label !== undefined ? String(label) : DEFAULT_ENCODING;
|
|
508
527
|
var encoding = getEncoding(label);
|
|
509
528
|
if (encoding === null || encoding.name === 'replacement') throw RangeError('Unknown encoding: ' + label);
|
|
510
|
-
|
|
511
529
|
if (!encoders[encoding.name]) {
|
|
512
530
|
throw Error('Encoder not present.' + ' Did you forget to include encoding-indexes.js first?');
|
|
513
531
|
}
|
|
514
|
-
|
|
515
532
|
enc._encoding = encoding;
|
|
516
533
|
} else {
|
|
517
534
|
enc._encoding = getEncoding('utf-8');
|
|
518
|
-
|
|
519
535
|
if (label !== undefined && 'console' in global) {
|
|
520
536
|
console.warn('TextEncoder constructor called with encoding label, ' + 'which is ignored.');
|
|
521
537
|
}
|
|
522
538
|
}
|
|
523
539
|
|
|
524
540
|
if (!Object.defineProperty) this.encoding = enc._encoding.name.toLowerCase();
|
|
541
|
+
|
|
525
542
|
return enc;
|
|
526
543
|
}
|
|
527
|
-
|
|
528
544
|
if (Object.defineProperty) {
|
|
529
545
|
Object.defineProperty(TextEncoder.prototype, 'encoding', {
|
|
530
546
|
get: function () {
|
|
@@ -536,14 +552,17 @@ if (Object.defineProperty) {
|
|
|
536
552
|
TextEncoder.prototype.encode = function encode(opt_string, options) {
|
|
537
553
|
opt_string = opt_string === undefined ? '' : String(opt_string);
|
|
538
554
|
options = ToDictionary(options);
|
|
555
|
+
|
|
539
556
|
if (!this._do_not_flush) this._encoder = encoders[this._encoding.name]({
|
|
540
557
|
fatal: this._fatal === 'fatal'
|
|
541
558
|
});
|
|
542
559
|
this._do_not_flush = Boolean(options['stream']);
|
|
560
|
+
|
|
543
561
|
var input = new Stream(stringToCodePoints(opt_string));
|
|
562
|
+
|
|
544
563
|
var output = [];
|
|
545
|
-
var result;
|
|
546
564
|
|
|
565
|
+
var result;
|
|
547
566
|
while (true) {
|
|
548
567
|
var token = input.read();
|
|
549
568
|
if (token === end_of_stream) break;
|
|
@@ -551,27 +570,25 @@ TextEncoder.prototype.encode = function encode(opt_string, options) {
|
|
|
551
570
|
if (result === finished) break;
|
|
552
571
|
if (Array.isArray(result)) output.push.apply(output, result);else output.push(result);
|
|
553
572
|
}
|
|
554
|
-
|
|
555
573
|
if (!this._do_not_flush) {
|
|
556
574
|
while (true) {
|
|
557
575
|
result = this._encoder.handler(input, input.read());
|
|
558
576
|
if (result === finished) break;
|
|
559
577
|
if (Array.isArray(result)) output.push.apply(output, result);else output.push(result);
|
|
560
578
|
}
|
|
561
|
-
|
|
562
579
|
this._encoder = null;
|
|
563
580
|
}
|
|
564
|
-
|
|
565
581
|
return new Uint8Array(output);
|
|
566
582
|
};
|
|
567
583
|
|
|
568
584
|
function UTF8Decoder(options) {
|
|
569
585
|
var fatal = options.fatal;
|
|
586
|
+
|
|
570
587
|
var utf8_code_point = 0,
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
588
|
+
utf8_bytes_seen = 0,
|
|
589
|
+
utf8_bytes_needed = 0,
|
|
590
|
+
utf8_lower_boundary = 0x80,
|
|
591
|
+
utf8_upper_boundary = 0xbf;
|
|
575
592
|
|
|
576
593
|
this.handler = function (stream, bite) {
|
|
577
594
|
if (bite === end_of_stream && utf8_bytes_needed !== 0) {
|
|
@@ -584,22 +601,31 @@ function UTF8Decoder(options) {
|
|
|
584
601
|
if (utf8_bytes_needed === 0) {
|
|
585
602
|
if (inRange(bite, 0x00, 0x7f)) {
|
|
586
603
|
return bite;
|
|
587
|
-
}
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
else if (inRange(bite, 0xc2, 0xdf)) {
|
|
607
|
+
utf8_bytes_needed = 1;
|
|
608
|
+
|
|
609
|
+
utf8_code_point = bite & 0x1f;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
else if (inRange(bite, 0xe0, 0xef)) {
|
|
613
|
+
if (bite === 0xe0) utf8_lower_boundary = 0xa0;
|
|
614
|
+
if (bite === 0xed) utf8_upper_boundary = 0x9f;
|
|
615
|
+
utf8_bytes_needed = 2;
|
|
616
|
+
utf8_code_point = bite & 0xf;
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
else if (inRange(bite, 0xf0, 0xf4)) {
|
|
620
|
+
if (bite === 0xf0) utf8_lower_boundary = 0x90;
|
|
621
|
+
if (bite === 0xf4) utf8_upper_boundary = 0x8f;
|
|
622
|
+
utf8_bytes_needed = 3;
|
|
623
|
+
utf8_code_point = bite & 0x7;
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
else {
|
|
627
|
+
return decoderError(fatal);
|
|
628
|
+
}
|
|
603
629
|
|
|
604
630
|
return null;
|
|
605
631
|
}
|
|
@@ -608,45 +634,57 @@ function UTF8Decoder(options) {
|
|
|
608
634
|
utf8_code_point = utf8_bytes_needed = utf8_bytes_seen = 0;
|
|
609
635
|
utf8_lower_boundary = 0x80;
|
|
610
636
|
utf8_upper_boundary = 0xbf;
|
|
637
|
+
|
|
611
638
|
stream.prepend(bite);
|
|
639
|
+
|
|
612
640
|
return decoderError(fatal);
|
|
613
641
|
}
|
|
614
642
|
|
|
615
643
|
utf8_lower_boundary = 0x80;
|
|
616
644
|
utf8_upper_boundary = 0xbf;
|
|
645
|
+
|
|
617
646
|
utf8_code_point = utf8_code_point << 6 | bite & 0x3f;
|
|
647
|
+
|
|
618
648
|
utf8_bytes_seen += 1;
|
|
649
|
+
|
|
619
650
|
if (utf8_bytes_seen !== utf8_bytes_needed) return null;
|
|
651
|
+
|
|
620
652
|
var code_point = utf8_code_point;
|
|
653
|
+
|
|
621
654
|
utf8_code_point = utf8_bytes_needed = utf8_bytes_seen = 0;
|
|
655
|
+
|
|
622
656
|
return code_point;
|
|
623
657
|
};
|
|
624
658
|
}
|
|
625
659
|
|
|
626
660
|
function UTF8Encoder(options) {
|
|
627
661
|
var fatal = options.fatal;
|
|
628
|
-
|
|
629
662
|
this.handler = function (stream, code_point) {
|
|
630
663
|
if (code_point === end_of_stream) return finished;
|
|
664
|
+
|
|
631
665
|
if (isASCIICodePoint(code_point)) return code_point;
|
|
632
|
-
var count, offset;
|
|
633
666
|
|
|
667
|
+
var count, offset;
|
|
634
668
|
if (inRange(code_point, 0x0080, 0x07ff)) {
|
|
635
669
|
count = 1;
|
|
636
670
|
offset = 0xc0;
|
|
637
|
-
}
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
671
|
+
}
|
|
672
|
+
else if (inRange(code_point, 0x0800, 0xffff)) {
|
|
673
|
+
count = 2;
|
|
674
|
+
offset = 0xe0;
|
|
675
|
+
}
|
|
676
|
+
else if (inRange(code_point, 0x10000, 0x10ffff)) {
|
|
677
|
+
count = 3;
|
|
678
|
+
offset = 0xf0;
|
|
679
|
+
}
|
|
644
680
|
|
|
645
681
|
var bytes = [(code_point >> 6 * count) + offset];
|
|
646
682
|
|
|
647
683
|
while (count > 0) {
|
|
648
684
|
var temp = code_point >> 6 * (count - 1);
|
|
685
|
+
|
|
649
686
|
bytes.push(0x80 | temp & 0x3f);
|
|
687
|
+
|
|
650
688
|
count -= 1;
|
|
651
689
|
}
|
|
652
690
|
|
|
@@ -657,35 +695,39 @@ function UTF8Encoder(options) {
|
|
|
657
695
|
encoders['UTF-8'] = function (options) {
|
|
658
696
|
return new UTF8Encoder(options);
|
|
659
697
|
};
|
|
660
|
-
|
|
661
698
|
decoders['UTF-8'] = function (options) {
|
|
662
699
|
return new UTF8Decoder(options);
|
|
663
700
|
};
|
|
664
701
|
|
|
665
702
|
function SingleByteDecoder(index, options) {
|
|
666
703
|
var fatal = options.fatal;
|
|
667
|
-
|
|
668
704
|
this.handler = function (stream, bite) {
|
|
669
705
|
if (bite === end_of_stream) return finished;
|
|
706
|
+
|
|
670
707
|
if (isASCIIByte(bite)) return bite;
|
|
708
|
+
|
|
671
709
|
var code_point = index[bite - 0x80];
|
|
710
|
+
|
|
672
711
|
if (code_point === null) return decoderError(fatal);
|
|
712
|
+
|
|
673
713
|
return code_point;
|
|
674
714
|
};
|
|
675
715
|
}
|
|
676
716
|
|
|
677
717
|
function SingleByteEncoder(index, options) {
|
|
678
718
|
var fatal = options.fatal;
|
|
679
|
-
|
|
680
719
|
this.handler = function (stream, code_point) {
|
|
681
720
|
if (code_point === end_of_stream) return finished;
|
|
721
|
+
|
|
682
722
|
if (isASCIICodePoint(code_point)) return code_point;
|
|
723
|
+
|
|
683
724
|
var pointer = indexPointerFor(code_point, index);
|
|
725
|
+
|
|
684
726
|
if (pointer === null) encoderError(code_point);
|
|
727
|
+
|
|
685
728
|
return pointer + 0x80;
|
|
686
729
|
};
|
|
687
730
|
}
|
|
688
|
-
|
|
689
731
|
(function () {
|
|
690
732
|
if (!('encoding-indexes' in global)) return;
|
|
691
733
|
encodings.forEach(function (category) {
|
|
@@ -693,11 +735,9 @@ function SingleByteEncoder(index, options) {
|
|
|
693
735
|
category.encodings.forEach(function (encoding) {
|
|
694
736
|
var name = encoding.name;
|
|
695
737
|
var idx = index(name.toLowerCase());
|
|
696
|
-
|
|
697
738
|
decoders[name] = function (options) {
|
|
698
739
|
return new SingleByteDecoder(idx, options);
|
|
699
740
|
};
|
|
700
|
-
|
|
701
741
|
encoders[name] = function (options) {
|
|
702
742
|
return new SingleByteEncoder(idx, options);
|
|
703
743
|
};
|
|
@@ -716,31 +756,27 @@ encoders['GBK'] = function (options) {
|
|
|
716
756
|
function GB18030Decoder(options) {
|
|
717
757
|
var fatal = options.fatal;
|
|
718
758
|
var gb18030_first = 0x00,
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
759
|
+
gb18030_second = 0x00,
|
|
760
|
+
gb18030_third = 0x00;
|
|
722
761
|
this.handler = function (stream, bite) {
|
|
723
762
|
if (bite === end_of_stream && gb18030_first === 0x00 && gb18030_second === 0x00 && gb18030_third === 0x00) {
|
|
724
763
|
return finished;
|
|
725
764
|
}
|
|
726
|
-
|
|
727
765
|
if (bite === end_of_stream && (gb18030_first !== 0x00 || gb18030_second !== 0x00 || gb18030_third !== 0x00)) {
|
|
728
766
|
gb18030_first = 0x00;
|
|
729
767
|
gb18030_second = 0x00;
|
|
730
768
|
gb18030_third = 0x00;
|
|
731
769
|
decoderError(fatal);
|
|
732
770
|
}
|
|
733
|
-
|
|
734
771
|
var code_point;
|
|
735
|
-
|
|
736
772
|
if (gb18030_third !== 0x00) {
|
|
737
773
|
code_point = null;
|
|
738
|
-
|
|
739
774
|
if (inRange(bite, 0x30, 0x39)) {
|
|
740
775
|
code_point = indexGB18030RangesCodePointFor((((gb18030_first - 0x81) * 10 + gb18030_second - 0x30) * 126 + gb18030_third - 0x81) * 10 + bite - 0x30);
|
|
741
776
|
}
|
|
742
777
|
|
|
743
778
|
var buffer = [gb18030_second, gb18030_third, bite];
|
|
779
|
+
|
|
744
780
|
gb18030_first = 0x00;
|
|
745
781
|
gb18030_second = 0x00;
|
|
746
782
|
gb18030_third = 0x00;
|
|
@@ -774,15 +810,22 @@ function GB18030Decoder(options) {
|
|
|
774
810
|
var lead = gb18030_first;
|
|
775
811
|
var pointer = null;
|
|
776
812
|
gb18030_first = 0x00;
|
|
813
|
+
|
|
777
814
|
var offset = bite < 0x7f ? 0x40 : 0x41;
|
|
815
|
+
|
|
778
816
|
if (inRange(bite, 0x40, 0x7e) || inRange(bite, 0x80, 0xfe)) pointer = (lead - 0x81) * 190 + (bite - offset);
|
|
817
|
+
|
|
779
818
|
code_point = pointer === null ? null : indexCodePointFor(pointer, index('gb18030'));
|
|
819
|
+
|
|
780
820
|
if (code_point === null && isASCIIByte(bite)) stream.prepend(bite);
|
|
821
|
+
|
|
781
822
|
if (code_point === null) return decoderError(fatal);
|
|
823
|
+
|
|
782
824
|
return code_point;
|
|
783
825
|
}
|
|
784
826
|
|
|
785
827
|
if (isASCIIByte(bite)) return bite;
|
|
828
|
+
|
|
786
829
|
if (bite === 0x80) return 0x20ac;
|
|
787
830
|
|
|
788
831
|
if (inRange(bite, 0x81, 0xfe)) {
|
|
@@ -796,29 +839,43 @@ function GB18030Decoder(options) {
|
|
|
796
839
|
|
|
797
840
|
function GB18030Encoder(options, gbk_flag) {
|
|
798
841
|
var fatal = options.fatal;
|
|
799
|
-
|
|
800
842
|
this.handler = function (stream, code_point) {
|
|
801
843
|
if (code_point === end_of_stream) return finished;
|
|
844
|
+
|
|
802
845
|
if (isASCIICodePoint(code_point)) return code_point;
|
|
846
|
+
|
|
803
847
|
if (code_point === 0xe5e5) return encoderError(code_point);
|
|
848
|
+
|
|
804
849
|
if (gbk_flag && code_point === 0x20ac) return 0x80;
|
|
850
|
+
|
|
805
851
|
var pointer = indexPointerFor(code_point, index('gb18030'));
|
|
806
852
|
|
|
807
853
|
if (pointer !== null) {
|
|
808
854
|
var lead = floor(pointer / 190) + 0x81;
|
|
855
|
+
|
|
809
856
|
var trail = pointer % 190;
|
|
857
|
+
|
|
810
858
|
var offset = trail < 0x3f ? 0x40 : 0x41;
|
|
859
|
+
|
|
811
860
|
return [lead, trail + offset];
|
|
812
861
|
}
|
|
813
862
|
|
|
814
863
|
if (gbk_flag) return encoderError(code_point);
|
|
864
|
+
|
|
815
865
|
pointer = indexGB18030RangesPointerFor(code_point);
|
|
866
|
+
|
|
816
867
|
var byte1 = floor(pointer / 10 / 126 / 10);
|
|
868
|
+
|
|
817
869
|
pointer = pointer - byte1 * 10 * 126 * 10;
|
|
870
|
+
|
|
818
871
|
var byte2 = floor(pointer / 10 / 126);
|
|
872
|
+
|
|
819
873
|
pointer = pointer - byte2 * 10 * 126;
|
|
874
|
+
|
|
820
875
|
var byte3 = floor(pointer / 10);
|
|
876
|
+
|
|
821
877
|
var byte4 = pointer - byte3 * 10;
|
|
878
|
+
|
|
822
879
|
return [byte1 + 0x81, byte2 + 0x30, byte3 + 0x81, byte4 + 0x30];
|
|
823
880
|
};
|
|
824
881
|
}
|
|
@@ -826,7 +883,6 @@ function GB18030Encoder(options, gbk_flag) {
|
|
|
826
883
|
encoders['gb18030'] = function (options) {
|
|
827
884
|
return new GB18030Encoder(options);
|
|
828
885
|
};
|
|
829
|
-
|
|
830
886
|
decoders['gb18030'] = function (options) {
|
|
831
887
|
return new GB18030Decoder(options);
|
|
832
888
|
};
|
|
@@ -847,26 +903,28 @@ function Big5Decoder(options) {
|
|
|
847
903
|
var lead = Big5_lead;
|
|
848
904
|
var pointer = null;
|
|
849
905
|
Big5_lead = 0x00;
|
|
906
|
+
|
|
850
907
|
var offset = bite < 0x7f ? 0x40 : 0x62;
|
|
908
|
+
|
|
851
909
|
if (inRange(bite, 0x40, 0x7e) || inRange(bite, 0xa1, 0xfe)) pointer = (lead - 0x81) * 157 + (bite - offset);
|
|
852
910
|
|
|
853
911
|
switch (pointer) {
|
|
854
912
|
case 1133:
|
|
855
913
|
return [0x00ca, 0x0304];
|
|
856
|
-
|
|
857
914
|
case 1135:
|
|
858
915
|
return [0x00ca, 0x030c];
|
|
859
|
-
|
|
860
916
|
case 1164:
|
|
861
917
|
return [0x00ea, 0x0304];
|
|
862
|
-
|
|
863
918
|
case 1166:
|
|
864
919
|
return [0x00ea, 0x030c];
|
|
865
920
|
}
|
|
866
921
|
|
|
867
922
|
var code_point = pointer === null ? null : indexCodePointFor(pointer, index('big5'));
|
|
923
|
+
|
|
868
924
|
if (code_point === null && isASCIIByte(bite)) stream.prepend(bite);
|
|
925
|
+
|
|
869
926
|
if (code_point === null) return decoderError(fatal);
|
|
927
|
+
|
|
870
928
|
return code_point;
|
|
871
929
|
}
|
|
872
930
|
|
|
@@ -883,16 +941,23 @@ function Big5Decoder(options) {
|
|
|
883
941
|
|
|
884
942
|
function Big5Encoder(options) {
|
|
885
943
|
var fatal = options.fatal;
|
|
886
|
-
|
|
887
944
|
this.handler = function (stream, code_point) {
|
|
888
945
|
if (code_point === end_of_stream) return finished;
|
|
946
|
+
|
|
889
947
|
if (isASCIICodePoint(code_point)) return code_point;
|
|
948
|
+
|
|
890
949
|
var pointer = indexBig5PointerFor(code_point);
|
|
950
|
+
|
|
891
951
|
if (pointer === null) return encoderError(code_point);
|
|
952
|
+
|
|
892
953
|
var lead = floor(pointer / 157) + 0x81;
|
|
954
|
+
|
|
893
955
|
if (lead < 0xa1) return encoderError(code_point);
|
|
956
|
+
|
|
894
957
|
var trail = pointer % 157;
|
|
958
|
+
|
|
895
959
|
var offset = trail < 0x3f ? 0x40 : 0x62;
|
|
960
|
+
|
|
896
961
|
return [lead, trail + offset];
|
|
897
962
|
};
|
|
898
963
|
}
|
|
@@ -900,15 +965,15 @@ function Big5Encoder(options) {
|
|
|
900
965
|
encoders['Big5'] = function (options) {
|
|
901
966
|
return new Big5Encoder(options);
|
|
902
967
|
};
|
|
903
|
-
|
|
904
968
|
decoders['Big5'] = function (options) {
|
|
905
969
|
return new Big5Decoder(options);
|
|
906
970
|
};
|
|
907
971
|
|
|
908
972
|
function EUCJPDecoder(options) {
|
|
909
973
|
var fatal = options.fatal;
|
|
974
|
+
|
|
910
975
|
var eucjp_jis0212_flag = false,
|
|
911
|
-
|
|
976
|
+
eucjp_lead = 0x00;
|
|
912
977
|
|
|
913
978
|
this.handler = function (stream, bite) {
|
|
914
979
|
if (bite === end_of_stream && eucjp_lead !== 0x00) {
|
|
@@ -932,6 +997,7 @@ function EUCJPDecoder(options) {
|
|
|
932
997
|
if (eucjp_lead !== 0x00) {
|
|
933
998
|
var lead = eucjp_lead;
|
|
934
999
|
eucjp_lead = 0x00;
|
|
1000
|
+
|
|
935
1001
|
var code_point = null;
|
|
936
1002
|
|
|
937
1003
|
if (inRange(lead, 0xa1, 0xfe) && inRange(bite, 0xa1, 0xfe)) {
|
|
@@ -939,8 +1005,11 @@ function EUCJPDecoder(options) {
|
|
|
939
1005
|
}
|
|
940
1006
|
|
|
941
1007
|
eucjp_jis0212_flag = false;
|
|
1008
|
+
|
|
942
1009
|
if (!inRange(bite, 0xa1, 0xfe)) stream.prepend(bite);
|
|
1010
|
+
|
|
943
1011
|
if (code_point === null) return decoderError(fatal);
|
|
1012
|
+
|
|
944
1013
|
return code_point;
|
|
945
1014
|
}
|
|
946
1015
|
|
|
@@ -957,18 +1026,27 @@ function EUCJPDecoder(options) {
|
|
|
957
1026
|
|
|
958
1027
|
function EUCJPEncoder(options) {
|
|
959
1028
|
var fatal = options.fatal;
|
|
960
|
-
|
|
961
1029
|
this.handler = function (stream, code_point) {
|
|
962
1030
|
if (code_point === end_of_stream) return finished;
|
|
1031
|
+
|
|
963
1032
|
if (isASCIICodePoint(code_point)) return code_point;
|
|
1033
|
+
|
|
964
1034
|
if (code_point === 0x00a5) return 0x5c;
|
|
1035
|
+
|
|
965
1036
|
if (code_point === 0x203e) return 0x7e;
|
|
1037
|
+
|
|
966
1038
|
if (inRange(code_point, 0xff61, 0xff9f)) return [0x8e, code_point - 0xff61 + 0xa1];
|
|
1039
|
+
|
|
967
1040
|
if (code_point === 0x2212) code_point = 0xff0d;
|
|
1041
|
+
|
|
968
1042
|
var pointer = indexPointerFor(code_point, index('jis0208'));
|
|
1043
|
+
|
|
969
1044
|
if (pointer === null) return encoderError(code_point);
|
|
1045
|
+
|
|
970
1046
|
var lead = floor(pointer / 94) + 0xa1;
|
|
1047
|
+
|
|
971
1048
|
var trail = pointer % 94 + 0xa1;
|
|
1049
|
+
|
|
972
1050
|
return [lead, trail];
|
|
973
1051
|
};
|
|
974
1052
|
}
|
|
@@ -976,7 +1054,6 @@ function EUCJPEncoder(options) {
|
|
|
976
1054
|
encoders['EUC-JP'] = function (options) {
|
|
977
1055
|
return new EUCJPEncoder(options);
|
|
978
1056
|
};
|
|
979
|
-
|
|
980
1057
|
decoders['EUC-JP'] = function (options) {
|
|
981
1058
|
return new EUCJPDecoder(options);
|
|
982
1059
|
};
|
|
@@ -993,14 +1070,14 @@ function ISO2022JPDecoder(options) {
|
|
|
993
1070
|
Escape: 6
|
|
994
1071
|
};
|
|
995
1072
|
var iso2022jp_decoder_state = states.ASCII,
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1073
|
+
iso2022jp_decoder_output_state = states.ASCII,
|
|
1074
|
+
iso2022jp_lead = 0x00,
|
|
1075
|
+
iso2022jp_output_flag = false;
|
|
1000
1076
|
this.handler = function (stream, bite) {
|
|
1001
1077
|
switch (iso2022jp_decoder_state) {
|
|
1002
1078
|
default:
|
|
1003
1079
|
case states.ASCII:
|
|
1080
|
+
|
|
1004
1081
|
if (bite === 0x1b) {
|
|
1005
1082
|
iso2022jp_decoder_state = states.EscapeStart;
|
|
1006
1083
|
return null;
|
|
@@ -1017,8 +1094,8 @@ function ISO2022JPDecoder(options) {
|
|
|
1017
1094
|
|
|
1018
1095
|
iso2022jp_output_flag = false;
|
|
1019
1096
|
return decoderError(fatal);
|
|
1020
|
-
|
|
1021
1097
|
case states.Roman:
|
|
1098
|
+
|
|
1022
1099
|
if (bite === 0x1b) {
|
|
1023
1100
|
iso2022jp_decoder_state = states.EscapeStart;
|
|
1024
1101
|
return null;
|
|
@@ -1045,8 +1122,8 @@ function ISO2022JPDecoder(options) {
|
|
|
1045
1122
|
|
|
1046
1123
|
iso2022jp_output_flag = false;
|
|
1047
1124
|
return decoderError(fatal);
|
|
1048
|
-
|
|
1049
1125
|
case states.Katakana:
|
|
1126
|
+
|
|
1050
1127
|
if (bite === 0x1b) {
|
|
1051
1128
|
iso2022jp_decoder_state = states.EscapeStart;
|
|
1052
1129
|
return null;
|
|
@@ -1063,8 +1140,8 @@ function ISO2022JPDecoder(options) {
|
|
|
1063
1140
|
|
|
1064
1141
|
iso2022jp_output_flag = false;
|
|
1065
1142
|
return decoderError(fatal);
|
|
1066
|
-
|
|
1067
1143
|
case states.LeadByte:
|
|
1144
|
+
|
|
1068
1145
|
if (bite === 0x1b) {
|
|
1069
1146
|
iso2022jp_decoder_state = states.EscapeStart;
|
|
1070
1147
|
return null;
|
|
@@ -1083,8 +1160,8 @@ function ISO2022JPDecoder(options) {
|
|
|
1083
1160
|
|
|
1084
1161
|
iso2022jp_output_flag = false;
|
|
1085
1162
|
return decoderError(fatal);
|
|
1086
|
-
|
|
1087
1163
|
case states.TrailByte:
|
|
1164
|
+
|
|
1088
1165
|
if (bite === 0x1b) {
|
|
1089
1166
|
iso2022jp_decoder_state = states.EscapeStart;
|
|
1090
1167
|
return decoderError(fatal);
|
|
@@ -1092,9 +1169,13 @@ function ISO2022JPDecoder(options) {
|
|
|
1092
1169
|
|
|
1093
1170
|
if (inRange(bite, 0x21, 0x7e)) {
|
|
1094
1171
|
iso2022jp_decoder_state = states.LeadByte;
|
|
1172
|
+
|
|
1095
1173
|
var pointer = (iso2022jp_lead - 0x21) * 94 + bite - 0x21;
|
|
1174
|
+
|
|
1096
1175
|
var code_point = indexCodePointFor(pointer, index('jis0208'));
|
|
1176
|
+
|
|
1097
1177
|
if (code_point === null) return decoderError(fatal);
|
|
1178
|
+
|
|
1098
1179
|
return code_point;
|
|
1099
1180
|
}
|
|
1100
1181
|
|
|
@@ -1106,8 +1187,8 @@ function ISO2022JPDecoder(options) {
|
|
|
1106
1187
|
|
|
1107
1188
|
iso2022jp_decoder_state = states.LeadByte;
|
|
1108
1189
|
return decoderError(fatal);
|
|
1109
|
-
|
|
1110
1190
|
case states.EscapeStart:
|
|
1191
|
+
|
|
1111
1192
|
if (bite === 0x24 || bite === 0x28) {
|
|
1112
1193
|
iso2022jp_lead = bite;
|
|
1113
1194
|
iso2022jp_decoder_state = states.Escape;
|
|
@@ -1115,27 +1196,37 @@ function ISO2022JPDecoder(options) {
|
|
|
1115
1196
|
}
|
|
1116
1197
|
|
|
1117
1198
|
stream.prepend(bite);
|
|
1199
|
+
|
|
1118
1200
|
iso2022jp_output_flag = false;
|
|
1119
1201
|
iso2022jp_decoder_state = iso2022jp_decoder_output_state;
|
|
1120
1202
|
return decoderError(fatal);
|
|
1121
|
-
|
|
1122
1203
|
case states.Escape:
|
|
1204
|
+
|
|
1123
1205
|
var lead = iso2022jp_lead;
|
|
1124
1206
|
iso2022jp_lead = 0x00;
|
|
1207
|
+
|
|
1125
1208
|
var state = null;
|
|
1209
|
+
|
|
1126
1210
|
if (lead === 0x28 && bite === 0x42) state = states.ASCII;
|
|
1211
|
+
|
|
1127
1212
|
if (lead === 0x28 && bite === 0x4a) state = states.Roman;
|
|
1213
|
+
|
|
1128
1214
|
if (lead === 0x28 && bite === 0x49) state = states.Katakana;
|
|
1215
|
+
|
|
1129
1216
|
if (lead === 0x24 && (bite === 0x40 || bite === 0x42)) state = states.LeadByte;
|
|
1130
1217
|
|
|
1131
1218
|
if (state !== null) {
|
|
1132
1219
|
iso2022jp_decoder_state = iso2022jp_decoder_state = state;
|
|
1220
|
+
|
|
1133
1221
|
var output_flag = iso2022jp_output_flag;
|
|
1222
|
+
|
|
1134
1223
|
iso2022jp_output_flag = true;
|
|
1224
|
+
|
|
1135
1225
|
return !output_flag ? null : decoderError(fatal);
|
|
1136
1226
|
}
|
|
1137
1227
|
|
|
1138
1228
|
stream.prepend([lead, bite]);
|
|
1229
|
+
|
|
1139
1230
|
iso2022jp_output_flag = false;
|
|
1140
1231
|
iso2022jp_decoder_state = iso2022jp_decoder_output_state;
|
|
1141
1232
|
return decoderError(fatal);
|
|
@@ -1151,7 +1242,6 @@ function ISO2022JPEncoder(options) {
|
|
|
1151
1242
|
jis0208: 2
|
|
1152
1243
|
};
|
|
1153
1244
|
var iso2022jp_state = states.ASCII;
|
|
1154
|
-
|
|
1155
1245
|
this.handler = function (stream, code_point) {
|
|
1156
1246
|
if (code_point === end_of_stream && iso2022jp_state !== states.ASCII) {
|
|
1157
1247
|
stream.prepend(code_point);
|
|
@@ -1169,7 +1259,9 @@ function ISO2022JPEncoder(options) {
|
|
|
1169
1259
|
|
|
1170
1260
|
if (iso2022jp_state === states.Roman && (isASCIICodePoint(code_point) && code_point !== 0x005c && code_point !== 0x007e || code_point == 0x00a5 || code_point == 0x203e)) {
|
|
1171
1261
|
if (isASCIICodePoint(code_point)) return code_point;
|
|
1262
|
+
|
|
1172
1263
|
if (code_point === 0x00a5) return 0x5c;
|
|
1264
|
+
|
|
1173
1265
|
if (code_point === 0x203e) return 0x7e;
|
|
1174
1266
|
}
|
|
1175
1267
|
|
|
@@ -1186,7 +1278,9 @@ function ISO2022JPEncoder(options) {
|
|
|
1186
1278
|
}
|
|
1187
1279
|
|
|
1188
1280
|
if (code_point === 0x2212) code_point = 0xff0d;
|
|
1281
|
+
|
|
1189
1282
|
var pointer = indexPointerFor(code_point, index('jis0208'));
|
|
1283
|
+
|
|
1190
1284
|
if (pointer === null) return encoderError(code_point);
|
|
1191
1285
|
|
|
1192
1286
|
if (iso2022jp_state !== states.jis0208) {
|
|
@@ -1196,7 +1290,9 @@ function ISO2022JPEncoder(options) {
|
|
|
1196
1290
|
}
|
|
1197
1291
|
|
|
1198
1292
|
var lead = floor(pointer / 94) + 0x21;
|
|
1293
|
+
|
|
1199
1294
|
var trail = pointer % 94 + 0x21;
|
|
1295
|
+
|
|
1200
1296
|
return [lead, trail];
|
|
1201
1297
|
};
|
|
1202
1298
|
}
|
|
@@ -1204,7 +1300,6 @@ function ISO2022JPEncoder(options) {
|
|
|
1204
1300
|
encoders['ISO-2022-JP'] = function (options) {
|
|
1205
1301
|
return new ISO2022JPEncoder(options);
|
|
1206
1302
|
};
|
|
1207
|
-
|
|
1208
1303
|
decoders['ISO-2022-JP'] = function (options) {
|
|
1209
1304
|
return new ISO2022JPDecoder(options);
|
|
1210
1305
|
};
|
|
@@ -1212,7 +1307,6 @@ decoders['ISO-2022-JP'] = function (options) {
|
|
|
1212
1307
|
function ShiftJISDecoder(options) {
|
|
1213
1308
|
var fatal = options.fatal;
|
|
1214
1309
|
var Shift_JIS_lead = 0x00;
|
|
1215
|
-
|
|
1216
1310
|
this.handler = function (stream, bite) {
|
|
1217
1311
|
if (bite === end_of_stream && Shift_JIS_lead !== 0x00) {
|
|
1218
1312
|
Shift_JIS_lead = 0x00;
|
|
@@ -1225,17 +1319,26 @@ function ShiftJISDecoder(options) {
|
|
|
1225
1319
|
var lead = Shift_JIS_lead;
|
|
1226
1320
|
var pointer = null;
|
|
1227
1321
|
Shift_JIS_lead = 0x00;
|
|
1322
|
+
|
|
1228
1323
|
var offset = bite < 0x7f ? 0x40 : 0x41;
|
|
1324
|
+
|
|
1229
1325
|
var lead_offset = lead < 0xa0 ? 0x81 : 0xc1;
|
|
1326
|
+
|
|
1230
1327
|
if (inRange(bite, 0x40, 0x7e) || inRange(bite, 0x80, 0xfc)) pointer = (lead - lead_offset) * 188 + bite - offset;
|
|
1328
|
+
|
|
1231
1329
|
if (inRange(pointer, 8836, 10715)) return 0xe000 - 8836 + pointer;
|
|
1330
|
+
|
|
1232
1331
|
var code_point = pointer === null ? null : indexCodePointFor(pointer, index('jis0208'));
|
|
1332
|
+
|
|
1233
1333
|
if (code_point === null && isASCIIByte(bite)) stream.prepend(bite);
|
|
1334
|
+
|
|
1234
1335
|
if (code_point === null) return decoderError(fatal);
|
|
1336
|
+
|
|
1235
1337
|
return code_point;
|
|
1236
1338
|
}
|
|
1237
1339
|
|
|
1238
1340
|
if (isASCIIByte(bite) || bite === 0x80) return bite;
|
|
1341
|
+
|
|
1239
1342
|
if (inRange(bite, 0xa1, 0xdf)) return 0xff61 - 0xa1 + bite;
|
|
1240
1343
|
|
|
1241
1344
|
if (inRange(bite, 0x81, 0x9f) || inRange(bite, 0xe0, 0xfc)) {
|
|
@@ -1249,20 +1352,31 @@ function ShiftJISDecoder(options) {
|
|
|
1249
1352
|
|
|
1250
1353
|
function ShiftJISEncoder(options) {
|
|
1251
1354
|
var fatal = options.fatal;
|
|
1252
|
-
|
|
1253
1355
|
this.handler = function (stream, code_point) {
|
|
1254
1356
|
if (code_point === end_of_stream) return finished;
|
|
1357
|
+
|
|
1255
1358
|
if (isASCIICodePoint(code_point) || code_point === 0x0080) return code_point;
|
|
1359
|
+
|
|
1256
1360
|
if (code_point === 0x00a5) return 0x5c;
|
|
1361
|
+
|
|
1257
1362
|
if (code_point === 0x203e) return 0x7e;
|
|
1363
|
+
|
|
1258
1364
|
if (inRange(code_point, 0xff61, 0xff9f)) return code_point - 0xff61 + 0xa1;
|
|
1365
|
+
|
|
1259
1366
|
if (code_point === 0x2212) code_point = 0xff0d;
|
|
1367
|
+
|
|
1260
1368
|
var pointer = indexShiftJISPointerFor(code_point);
|
|
1369
|
+
|
|
1261
1370
|
if (pointer === null) return encoderError(code_point);
|
|
1371
|
+
|
|
1262
1372
|
var lead = floor(pointer / 188);
|
|
1373
|
+
|
|
1263
1374
|
var lead_offset = lead < 0x1f ? 0x81 : 0xc1;
|
|
1375
|
+
|
|
1264
1376
|
var trail = pointer % 188;
|
|
1377
|
+
|
|
1265
1378
|
var offset = trail < 0x3f ? 0x40 : 0x41;
|
|
1379
|
+
|
|
1266
1380
|
return [lead + lead_offset, trail + offset];
|
|
1267
1381
|
};
|
|
1268
1382
|
}
|
|
@@ -1270,15 +1384,14 @@ function ShiftJISEncoder(options) {
|
|
|
1270
1384
|
encoders['Shift_JIS'] = function (options) {
|
|
1271
1385
|
return new ShiftJISEncoder(options);
|
|
1272
1386
|
};
|
|
1273
|
-
|
|
1274
1387
|
decoders['Shift_JIS'] = function (options) {
|
|
1275
1388
|
return new ShiftJISDecoder(options);
|
|
1276
1389
|
};
|
|
1277
1390
|
|
|
1278
1391
|
function EUCKRDecoder(options) {
|
|
1279
1392
|
var fatal = options.fatal;
|
|
1280
|
-
var euckr_lead = 0x00;
|
|
1281
1393
|
|
|
1394
|
+
var euckr_lead = 0x00;
|
|
1282
1395
|
this.handler = function (stream, bite) {
|
|
1283
1396
|
if (bite === end_of_stream && euckr_lead !== 0) {
|
|
1284
1397
|
euckr_lead = 0x00;
|
|
@@ -1291,10 +1404,15 @@ function EUCKRDecoder(options) {
|
|
|
1291
1404
|
var lead = euckr_lead;
|
|
1292
1405
|
var pointer = null;
|
|
1293
1406
|
euckr_lead = 0x00;
|
|
1407
|
+
|
|
1294
1408
|
if (inRange(bite, 0x41, 0xfe)) pointer = (lead - 0x81) * 190 + (bite - 0x41);
|
|
1409
|
+
|
|
1295
1410
|
var code_point = pointer === null ? null : indexCodePointFor(pointer, index('euc-kr'));
|
|
1411
|
+
|
|
1296
1412
|
if (pointer === null && isASCIIByte(bite)) stream.prepend(bite);
|
|
1413
|
+
|
|
1297
1414
|
if (code_point === null) return decoderError(fatal);
|
|
1415
|
+
|
|
1298
1416
|
return code_point;
|
|
1299
1417
|
}
|
|
1300
1418
|
|
|
@@ -1311,14 +1429,19 @@ function EUCKRDecoder(options) {
|
|
|
1311
1429
|
|
|
1312
1430
|
function EUCKREncoder(options) {
|
|
1313
1431
|
var fatal = options.fatal;
|
|
1314
|
-
|
|
1315
1432
|
this.handler = function (stream, code_point) {
|
|
1316
1433
|
if (code_point === end_of_stream) return finished;
|
|
1434
|
+
|
|
1317
1435
|
if (isASCIICodePoint(code_point)) return code_point;
|
|
1436
|
+
|
|
1318
1437
|
var pointer = indexPointerFor(code_point, index('euc-kr'));
|
|
1438
|
+
|
|
1319
1439
|
if (pointer === null) return encoderError(code_point);
|
|
1440
|
+
|
|
1320
1441
|
var lead = floor(pointer / 190) + 0x81;
|
|
1442
|
+
|
|
1321
1443
|
var trail = pointer % 190 + 0x41;
|
|
1444
|
+
|
|
1322
1445
|
return [lead, trail];
|
|
1323
1446
|
};
|
|
1324
1447
|
}
|
|
@@ -1326,14 +1449,15 @@ function EUCKREncoder(options) {
|
|
|
1326
1449
|
encoders['EUC-KR'] = function (options) {
|
|
1327
1450
|
return new EUCKREncoder(options);
|
|
1328
1451
|
};
|
|
1329
|
-
|
|
1330
1452
|
decoders['EUC-KR'] = function (options) {
|
|
1331
1453
|
return new EUCKRDecoder(options);
|
|
1332
1454
|
};
|
|
1333
1455
|
|
|
1334
1456
|
function convertCodeUnitToBytes(code_unit, utf16be) {
|
|
1335
1457
|
var byte1 = code_unit >> 8;
|
|
1458
|
+
|
|
1336
1459
|
var byte2 = code_unit & 0x00ff;
|
|
1460
|
+
|
|
1337
1461
|
if (utf16be) return [byte1, byte2];
|
|
1338
1462
|
return [byte2, byte1];
|
|
1339
1463
|
}
|
|
@@ -1341,8 +1465,7 @@ function convertCodeUnitToBytes(code_unit, utf16be) {
|
|
|
1341
1465
|
function UTF16Decoder(utf16_be, options) {
|
|
1342
1466
|
var fatal = options.fatal;
|
|
1343
1467
|
var utf16_lead_byte = null,
|
|
1344
|
-
|
|
1345
|
-
|
|
1468
|
+
utf16_lead_surrogate = null;
|
|
1346
1469
|
this.handler = function (stream, bite) {
|
|
1347
1470
|
if (bite === end_of_stream && (utf16_lead_byte !== null || utf16_lead_surrogate !== null)) {
|
|
1348
1471
|
return decoderError(fatal);
|
|
@@ -1358,13 +1481,11 @@ function UTF16Decoder(utf16_be, options) {
|
|
|
1358
1481
|
}
|
|
1359
1482
|
|
|
1360
1483
|
var code_unit;
|
|
1361
|
-
|
|
1362
1484
|
if (utf16_be) {
|
|
1363
1485
|
code_unit = (utf16_lead_byte << 8) + bite;
|
|
1364
1486
|
} else {
|
|
1365
1487
|
code_unit = (bite << 8) + utf16_lead_byte;
|
|
1366
1488
|
}
|
|
1367
|
-
|
|
1368
1489
|
utf16_lead_byte = null;
|
|
1369
1490
|
|
|
1370
1491
|
if (utf16_lead_surrogate !== null) {
|
|
@@ -1385,18 +1506,22 @@ function UTF16Decoder(utf16_be, options) {
|
|
|
1385
1506
|
}
|
|
1386
1507
|
|
|
1387
1508
|
if (inRange(code_unit, 0xdc00, 0xdfff)) return decoderError(fatal);
|
|
1509
|
+
|
|
1388
1510
|
return code_unit;
|
|
1389
1511
|
};
|
|
1390
1512
|
}
|
|
1391
1513
|
|
|
1392
1514
|
function UTF16Encoder(utf16_be, options) {
|
|
1393
1515
|
var fatal = options.fatal;
|
|
1394
|
-
|
|
1395
1516
|
this.handler = function (stream, code_point) {
|
|
1396
1517
|
if (code_point === end_of_stream) return finished;
|
|
1518
|
+
|
|
1397
1519
|
if (inRange(code_point, 0x0000, 0xffff)) return convertCodeUnitToBytes(code_point, utf16_be);
|
|
1520
|
+
|
|
1398
1521
|
var lead = convertCodeUnitToBytes((code_point - 0x10000 >> 10) + 0xd800, utf16_be);
|
|
1522
|
+
|
|
1399
1523
|
var trail = convertCodeUnitToBytes((code_point - 0x10000 & 0x3ff) + 0xdc00, utf16_be);
|
|
1524
|
+
|
|
1400
1525
|
return lead.concat(trail);
|
|
1401
1526
|
};
|
|
1402
1527
|
}
|
|
@@ -1404,7 +1529,6 @@ function UTF16Encoder(utf16_be, options) {
|
|
|
1404
1529
|
encoders['UTF-16BE'] = function (options) {
|
|
1405
1530
|
return new UTF16Encoder(true, options);
|
|
1406
1531
|
};
|
|
1407
|
-
|
|
1408
1532
|
decoders['UTF-16BE'] = function (options) {
|
|
1409
1533
|
return new UTF16Decoder(true, options);
|
|
1410
1534
|
};
|
|
@@ -1412,28 +1536,30 @@ decoders['UTF-16BE'] = function (options) {
|
|
|
1412
1536
|
encoders['UTF-16LE'] = function (options) {
|
|
1413
1537
|
return new UTF16Encoder(false, options);
|
|
1414
1538
|
};
|
|
1415
|
-
|
|
1416
1539
|
decoders['UTF-16LE'] = function (options) {
|
|
1417
1540
|
return new UTF16Decoder(false, options);
|
|
1418
1541
|
};
|
|
1419
1542
|
|
|
1420
1543
|
function XUserDefinedDecoder(options) {
|
|
1421
1544
|
var fatal = options.fatal;
|
|
1422
|
-
|
|
1423
1545
|
this.handler = function (stream, bite) {
|
|
1424
1546
|
if (bite === end_of_stream) return finished;
|
|
1547
|
+
|
|
1425
1548
|
if (isASCIIByte(bite)) return bite;
|
|
1549
|
+
|
|
1426
1550
|
return 0xf780 + bite - 0x80;
|
|
1427
1551
|
};
|
|
1428
1552
|
}
|
|
1429
1553
|
|
|
1430
1554
|
function XUserDefinedEncoder(options) {
|
|
1431
1555
|
var fatal = options.fatal;
|
|
1432
|
-
|
|
1433
1556
|
this.handler = function (stream, code_point) {
|
|
1434
1557
|
if (code_point === end_of_stream) return finished;
|
|
1558
|
+
|
|
1435
1559
|
if (isASCIICodePoint(code_point)) return code_point;
|
|
1560
|
+
|
|
1436
1561
|
if (inRange(code_point, 0xf780, 0xf7ff)) return code_point - 0xf780 + 0x80;
|
|
1562
|
+
|
|
1437
1563
|
return encoderError(code_point);
|
|
1438
1564
|
};
|
|
1439
1565
|
}
|
|
@@ -1441,7 +1567,6 @@ function XUserDefinedEncoder(options) {
|
|
|
1441
1567
|
encoders['x-user-defined'] = function (options) {
|
|
1442
1568
|
return new XUserDefinedEncoder(options);
|
|
1443
1569
|
};
|
|
1444
|
-
|
|
1445
1570
|
decoders['x-user-defined'] = function (options) {
|
|
1446
1571
|
return new XUserDefinedDecoder(options);
|
|
1447
1572
|
};
|