UNPKG

4.6 kBJavaScriptView Raw
1var _a, _b;
2const Object_prototype_toString = {}.toString;
3const ArrayBufferString = Object_prototype_toString.call(ArrayBuffer.prototype);
4function decoderReplacer(encoded) {
5 let codePoint = encoded.charCodeAt(0) << 24;
6 const leadingOnes = Math.clz32(~codePoint) | 0;
7 let endPos = 0;
8 const stringLen = encoded.length | 0;
9 let result = '';
10 if (leadingOnes < 5 && stringLen >= leadingOnes) {
11 codePoint = (codePoint << leadingOnes) >>> (24 + leadingOnes);
12 for (endPos = 1; endPos < leadingOnes; endPos = (endPos + 1) | 0) {
13 codePoint = (codePoint << 6) | (encoded.charCodeAt(endPos) & 0x3f) /*0b00111111*/;
14 }
15 if (codePoint <= 0xffff) {
16 // BMP code point
17 result += String.fromCharCode(codePoint);
18 }
19 else if (codePoint <= 0x10ffff) {
20 // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
21 codePoint = (codePoint - 0x10000) | 0;
22 result += String.fromCharCode(((codePoint >> 10) + 0xd800) | 0, // highSurrogate
23 ((codePoint & 0x3ff) + 0xdc00) | 0 // lowSurrogate
24 );
25 }
26 else {
27 endPos = 0;
28 } // to fill it in with INVALIDs
29 }
30 for (; endPos < stringLen; endPos = (endPos + 1) | 0) {
31 result += '\ufffd';
32 }
33 return result;
34}
35function encoderReplacer(nonAsciiChars) {
36 // make the UTF string into a binary UTF-8 encoded string
37 let point = nonAsciiChars.charCodeAt(0) | 0;
38 if (point >= 0xd800 && point <= 0xdbff) {
39 const nextcode = nonAsciiChars.charCodeAt(1) | 0;
40 if (nextcode !== nextcode) {
41 // NaN because string is 1 code point long
42 return String.fromCharCode(0xef /*11101111*/, 0xbf /*10111111*/, 0xbd /*10111101*/);
43 }
44 // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
45 if (nextcode >= 0xdc00 && nextcode <= 0xdfff) {
46 point = (((point - 0xd800) << 10) + nextcode - 0xdc00 + 0x10000) | 0;
47 if (point > 0xffff) {
48 return String.fromCharCode((0x1e /*0b11110*/ << 3) | (point >>> 18), (0x2 /*0b10*/ << 6) | ((point >>> 12) & 0x3f) /*0b00111111*/, (0x2 /*0b10*/ << 6) | ((point >>> 6) & 0x3f) /*0b00111111*/, (0x2 /*0b10*/ << 6) | (point & 0x3f) /*0b00111111*/);
49 }
50 }
51 else {
52 return String.fromCharCode(0xef, 0xbf, 0xbd);
53 }
54 }
55 if (point <= 0x007f) {
56 return nonAsciiChars;
57 }
58 else if (point <= 0x07ff) {
59 return String.fromCharCode((0x6 << 5) | (point >>> 6), (0x2 << 6) | (point & 0x3f));
60 }
61 else {
62 return String.fromCharCode((0xe /*0b1110*/ << 4) | (point >>> 12), (0x2 /*0b10*/ << 6) | ((point >>> 6) & 0x3f) /*0b00111111*/, (0x2 /*0b10*/ << 6) | (point & 0x3f) /*0b00111111*/);
63 }
64}
65export class TextDecoder {
66 constructor() {
67 this[_a] = 'TextDecoder';
68 }
69 get encoding() {
70 return 'utf-8';
71 }
72 decode(input) {
73 const buffer = ArrayBuffer.isView(input) ? input.buffer : input;
74 if (Object_prototype_toString.call(buffer) !== ArrayBufferString) {
75 throw Error("Failed to execute 'decode' on 'TextDecoder': The provided value is not of type '(ArrayBuffer or ArrayBufferView)'");
76 }
77 const inputAs8 = new Uint8Array(buffer);
78 let resultingString = '';
79 for (let index = 0, len = inputAs8.length | 0; index < len; index = (index + 32768) | 0) {
80 resultingString += String.fromCharCode.apply(0, inputAs8.slice(index, (index + 32768) | 0));
81 }
82 return resultingString.replace(/[\xc0-\xff][\x80-\xbf]*/g, decoderReplacer);
83 }
84 toString() {
85 return '[object TextDecoder]';
86 }
87}
88_a = Symbol.toStringTag;
89export class TextEncoder {
90 constructor() {
91 this[_b] = 'TextEncoder';
92 }
93 get encoding() {
94 return 'utf-8';
95 }
96 encode(input = '') {
97 // 0xc0 => 0b11000000; 0xff => 0b11111111; 0xc0-0xff => 0b11xxxxxx
98 // 0x80 => 0b10000000; 0xbf => 0b10111111; 0x80-0xbf => 0b10xxxxxx
99 const encodedString = input === undefined ? '' : ('' + input).replace(/[\x80-\uD7ff\uDC00-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]?/g, encoderReplacer);
100 const len = encodedString.length | 0, result = new Uint8Array(len);
101 for (let i = 0; i < len; i = (i + 1) | 0) {
102 result[i] = encodedString.charCodeAt(i);
103 }
104 return result;
105 }
106 toString() {
107 return '[object TextEncoder]';
108 }
109}
110_b = Symbol.toStringTag;
111//# sourceMappingURL=text-common.js.map
\No newline at end of file