1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.toUtf8 = exports.fromUtf8 = void 0;
|
4 | const fromUtf8 = (input) => {
|
5 | const bytes = [];
|
6 | for (let i = 0, len = input.length; i < len; i++) {
|
7 | const value = input.charCodeAt(i);
|
8 | if (value < 0x80) {
|
9 | bytes.push(value);
|
10 | }
|
11 | else if (value < 0x800) {
|
12 | bytes.push((value >> 6) | 0b11000000, (value & 0b111111) | 0b10000000);
|
13 | }
|
14 | else if (i + 1 < input.length && (value & 0xfc00) === 0xd800 && (input.charCodeAt(i + 1) & 0xfc00) === 0xdc00) {
|
15 | const surrogatePair = 0x10000 + ((value & 0b1111111111) << 10) + (input.charCodeAt(++i) & 0b1111111111);
|
16 | bytes.push((surrogatePair >> 18) | 0b11110000, ((surrogatePair >> 12) & 0b111111) | 0b10000000, ((surrogatePair >> 6) & 0b111111) | 0b10000000, (surrogatePair & 0b111111) | 0b10000000);
|
17 | }
|
18 | else {
|
19 | bytes.push((value >> 12) | 0b11100000, ((value >> 6) & 0b111111) | 0b10000000, (value & 0b111111) | 0b10000000);
|
20 | }
|
21 | }
|
22 | return Uint8Array.from(bytes);
|
23 | };
|
24 | exports.fromUtf8 = fromUtf8;
|
25 | const toUtf8 = (input) => {
|
26 | let decoded = "";
|
27 | for (let i = 0, len = input.length; i < len; i++) {
|
28 | const byte = input[i];
|
29 | if (byte < 0x80) {
|
30 | decoded += String.fromCharCode(byte);
|
31 | }
|
32 | else if (0b11000000 <= byte && byte < 0b11100000) {
|
33 | const nextByte = input[++i];
|
34 | decoded += String.fromCharCode(((byte & 0b11111) << 6) | (nextByte & 0b111111));
|
35 | }
|
36 | else if (0b11110000 <= byte && byte < 0b101101101) {
|
37 | const surrogatePair = [byte, input[++i], input[++i], input[++i]];
|
38 | const encoded = "%" + surrogatePair.map((byteValue) => byteValue.toString(16)).join("%");
|
39 | decoded += decodeURIComponent(encoded);
|
40 | }
|
41 | else {
|
42 | decoded += String.fromCharCode(((byte & 0b1111) << 12) | ((input[++i] & 0b111111) << 6) | (input[++i] & 0b111111));
|
43 | }
|
44 | }
|
45 | return decoded;
|
46 | };
|
47 | exports.toUtf8 = toUtf8;
|