123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- (function(scope) {
- 'use strict';
- if (scope['TextEncoder'] && scope['TextDecoder']) {
- return false;
- }
- const validUtfLabels = ['utf-8', 'utf8', 'unicode-1-1-utf-8'];
- function FastTextEncoder() {
-
-
- }
- Object.defineProperty(FastTextEncoder.prototype, 'encoding', {value: 'utf-8'});
- FastTextEncoder.prototype['encode'] = function(string, options={stream: false}) {
- if (options.stream) {
- throw new Error(`Failed to encode: the 'stream' option is unsupported.`);
- }
- let pos = 0;
- const len = string.length;
- let at = 0;
- let tlen = Math.max(32, len + (len >>> 1) + 7);
- let target = new Uint8Array((tlen >>> 3) << 3);
- while (pos < len) {
- let value = string.charCodeAt(pos++);
- if (value >= 0xd800 && value <= 0xdbff) {
-
- if (pos < len) {
- const extra = string.charCodeAt(pos);
- if ((extra & 0xfc00) === 0xdc00) {
- ++pos;
- value = ((value & 0x3ff) << 10) + (extra & 0x3ff) + 0x10000;
- }
- }
- if (value >= 0xd800 && value <= 0xdbff) {
- continue;
- }
- }
-
- if (at + 4 > target.length) {
- tlen += 8;
- tlen *= (1.0 + (pos / string.length) * 2);
- tlen = (tlen >>> 3) << 3;
- const update = new Uint8Array(tlen);
- update.set(target);
- target = update;
- }
- if ((value & 0xffffff80) === 0) {
- target[at++] = value;
- continue;
- } else if ((value & 0xfffff800) === 0) {
- target[at++] = ((value >>> 6) & 0x1f) | 0xc0;
- } else if ((value & 0xffff0000) === 0) {
- target[at++] = ((value >>> 12) & 0x0f) | 0xe0;
- target[at++] = ((value >>> 6) & 0x3f) | 0x80;
- } else if ((value & 0xffe00000) === 0) {
- target[at++] = ((value >>> 18) & 0x07) | 0xf0;
- target[at++] = ((value >>> 12) & 0x3f) | 0x80;
- target[at++] = ((value >>> 6) & 0x3f) | 0x80;
- } else {
- continue;
- }
- target[at++] = (value & 0x3f) | 0x80;
- }
-
-
- return target.slice ? target.slice(0, at) : target.subarray(0, at);
- }
- function FastTextDecoder(utfLabel='utf-8', options={fatal: false}) {
- if (validUtfLabels.indexOf(utfLabel.toLowerCase()) === -1) {
- throw new RangeError(
- `Failed to construct 'TextDecoder': The encoding label provided ('${utfLabel}') is invalid.`);
- }
- if (options.fatal) {
- throw new Error(`Failed to construct 'TextDecoder': the 'fatal' option is unsupported.`);
- }
- }
- Object.defineProperty(FastTextDecoder.prototype, 'encoding', {value: 'utf-8'});
- Object.defineProperty(FastTextDecoder.prototype, 'fatal', {value: false});
- Object.defineProperty(FastTextDecoder.prototype, 'ignoreBOM', {value: false});
- function decodeBuffer(bytes) {
- return Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength).toString('utf-8');
- }
- function decodeSyncXHR(bytes) {
- const b = new Blob([bytes], {type: 'text/plain;charset=UTF-8'});
- const u = URL.createObjectURL(b);
-
-
- try {
- const x = new XMLHttpRequest();
- x.open('GET', u, false);
- x.send();
- return x.responseText;
- } catch (e) {
- return decodeFallback(bytes);
- } finally {
- URL.revokeObjectURL(u);
- }
- }
- function decodeFallback(bytes) {
- let inputIndex = 0;
-
-
-
-
-
- const pendingSize = Math.min(256 * 256, bytes.length + 1);
- const pending = new Uint16Array(pendingSize);
- const chunks = [];
- let pendingIndex = 0;
- for (;;) {
- const more = inputIndex < bytes.length;
-
-
-
- if (!more || (pendingIndex >= pendingSize - 1)) {
-
-
-
- chunks.push(String.fromCharCode.apply(null, pending.subarray(0, pendingIndex)));
- if (!more) {
- return chunks.join('');
- }
-
- bytes = bytes.subarray(inputIndex);
- inputIndex = 0;
- pendingIndex = 0;
- }
-
-
-
- const byte1 = bytes[inputIndex++];
- if ((byte1 & 0x80) === 0) {
- pending[pendingIndex++] = byte1;
- } else if ((byte1 & 0xe0) === 0xc0) {
- const byte2 = bytes[inputIndex++] & 0x3f;
- pending[pendingIndex++] = ((byte1 & 0x1f) << 6) | byte2;
- } else if ((byte1 & 0xf0) === 0xe0) {
- const byte2 = bytes[inputIndex++] & 0x3f;
- const byte3 = bytes[inputIndex++] & 0x3f;
- pending[pendingIndex++] = ((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3;
- } else if ((byte1 & 0xf8) === 0xf0) {
- const byte2 = bytes[inputIndex++] & 0x3f;
- const byte3 = bytes[inputIndex++] & 0x3f;
- const byte4 = bytes[inputIndex++] & 0x3f;
-
- let codepoint = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
- if (codepoint > 0xffff) {
-
- codepoint -= 0x10000;
- pending[pendingIndex++] = (codepoint >>> 10) & 0x3ff | 0xd800;
- codepoint = 0xdc00 | codepoint & 0x3ff;
- }
- pending[pendingIndex++] = codepoint;
- } else {
-
- }
- }
- }
- let decodeImpl = decodeFallback;
- if (typeof Buffer === 'function' && Buffer.from) {
-
- decodeImpl = decodeBuffer;
- } else if (typeof Blob === 'function' && typeof URL === 'function' && typeof URL.createObjectURL === 'function') {
-
-
- decodeImpl = decodeSyncXHR;
- }
- FastTextDecoder.prototype['decode'] = function(buffer, options={stream: false}) {
- if (options['stream']) {
- throw new Error(`Failed to decode: the 'stream' option is unsupported.`);
- }
- let bytes;
- if (buffer instanceof Uint8Array) {
-
- bytes = buffer;
- } else if (buffer.buffer instanceof ArrayBuffer) {
-
-
-
- bytes = new Uint8Array(buffer.buffer);
- } else {
-
-
-
- bytes = new Uint8Array(buffer);
- }
- return decodeImpl( (bytes));
- }
- scope['TextEncoder'] = FastTextEncoder;
- scope['TextDecoder'] = FastTextDecoder;
- }(typeof window !== 'undefined' ? window : (typeof global !== 'undefined' ? global : this)));
|