This repository was archived by the owner on Sep 13, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathTextEncoder.js
More file actions
100 lines (97 loc) · 3.34 KB
/
Copy pathTextEncoder.js
File metadata and controls
100 lines (97 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// TextEncoder/TextDecoder polyfills for utf-8 - an implementation of TextEncoder/TextDecoder APIs
// Written in 2013 by Viktor Mukhachev <vic99999@yandex.ru>
// To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
// You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
// Some important notes about the polyfill below:
// Native TextEncoder/TextDecoder implementation is overwritten
// String.prototype.codePointAt polyfill not included, as well as String.fromCodePoint
// TextEncoder.prototype.encode returns a regular array instead of Uint8Array
// No options (fatal of the TextDecoder constructor and stream of the TextDecoder.prototype.decode method) are supported.
// TextDecoder.prototype.decode does not valid byte sequences
// This is a demonstrative implementation not intended to have the best performance
// http://encoding.spec.whatwg.org/#textencoder
(function (window) {
"use strict";
function TextEncoder() {}
TextEncoder.prototype.encode = function (string) {
var octets = [];
var length = string.length;
var i = 0;
while (i < length) {
var codePoint = string.codePointAt(i);
var c = 0;
var bits = 0;
if (codePoint <= 0x0000007f) {
c = 0;
bits = 0x00;
} else if (codePoint <= 0x000007ff) {
c = 6;
bits = 0xc0;
} else if (codePoint <= 0x0000ffff) {
c = 12;
bits = 0xe0;
} else if (codePoint <= 0x001fffff) {
c = 18;
bits = 0xf0;
}
octets.push(bits | (codePoint >> c));
c -= 6;
while (c >= 0) {
octets.push(0x80 | ((codePoint >> c) & 0x3f));
c -= 6;
}
i += codePoint >= 0x10000 ? 2 : 1;
}
return octets;
};
globalThis.TextEncoder = TextEncoder;
if (!window["TextEncoder"]) window["TextEncoder"] = TextEncoder;
function TextDecoder() {}
TextDecoder.prototype.decode = function (octets) {
if (!octets) return "";
var string = "";
var i = 0;
while (i < octets.length) {
var octet = octets[i];
var bytesNeeded = 0;
var codePoint = 0;
if (octet <= 0x7f) {
bytesNeeded = 0;
codePoint = octet & 0xff;
} else if (octet <= 0xdf) {
bytesNeeded = 1;
codePoint = octet & 0x1f;
} else if (octet <= 0xef) {
bytesNeeded = 2;
codePoint = octet & 0x0f;
} else if (octet <= 0xf4) {
bytesNeeded = 3;
codePoint = octet & 0x07;
}
if (octets.length - i - bytesNeeded > 0) {
var k = 0;
while (k < bytesNeeded) {
octet = octets[i + k + 1];
codePoint = (codePoint << 6) | (octet & 0x3f);
k += 1;
}
} else {
codePoint = 0xfffd;
bytesNeeded = octets.length - i;
}
string += String.fromCodePoint(codePoint);
i += bytesNeeded + 1;
}
return string;
};
globalThis.TextDecoder = TextDecoder;
if (!window["TextDecoder"]) window["TextDecoder"] = TextDecoder;
})(
typeof globalThis == "" + void 0
? typeof global == "" + void 0
? typeof self == "" + void 0
? this
: self
: global
: globalThis
);