1
|
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
|
2
|
|
3
|
function _toArray(arr) { return Array.isArray(arr) ? arr : Array.from(arr); }
|
4
|
|
5
|
function con(b) {
|
6
|
if ((b & 0xc0) === 0x80) {
|
7
|
return b & 0x3f;
|
8
|
} else {
|
9
|
throw new Error("invalid UTF-8 encoding");
|
10
|
}
|
11
|
}
|
12
|
|
13
|
function code(min, n) {
|
14
|
if (n < min || 0xd800 <= n && n < 0xe000 || n >= 0x10000) {
|
15
|
throw new Error("invalid UTF-8 encoding");
|
16
|
} else {
|
17
|
return n;
|
18
|
}
|
19
|
}
|
20
|
|
21
|
export function decode(bytes) {
|
22
|
return _decode(bytes).map(function (x) {
|
23
|
return String.fromCharCode(x);
|
24
|
}).join("");
|
25
|
}
|
26
|
|
27
|
function _decode(bytes) {
|
28
|
if (bytes.length === 0) {
|
29
|
return [];
|
30
|
}
|
31
|
/**
|
32
|
* 1 byte
|
33
|
*/
|
34
|
|
35
|
|
36
|
{
|
37
|
var _bytes = _toArray(bytes),
|
38
|
b1 = _bytes[0],
|
39
|
bs = _bytes.slice(1);
|
40
|
|
41
|
if (b1 < 0x80) {
|
42
|
return [code(0x0, b1)].concat(_toConsumableArray(_decode(bs)));
|
43
|
}
|
44
|
|
45
|
if (b1 < 0xc0) {
|
46
|
throw new Error("invalid UTF-8 encoding");
|
47
|
}
|
48
|
}
|
49
|
/**
|
50
|
* 2 bytes
|
51
|
*/
|
52
|
|
53
|
{
|
54
|
var _bytes2 = _toArray(bytes),
|
55
|
_b = _bytes2[0],
|
56
|
b2 = _bytes2[1],
|
57
|
_bs = _bytes2.slice(2);
|
58
|
|
59
|
if (_b < 0xe0) {
|
60
|
return [code(0x80, ((_b & 0x1f) << 6) + con(b2))].concat(_toConsumableArray(_decode(_bs)));
|
61
|
}
|
62
|
}
|
63
|
/**
|
64
|
* 3 bytes
|
65
|
*/
|
66
|
|
67
|
{
|
68
|
var _bytes3 = _toArray(bytes),
|
69
|
_b2 = _bytes3[0],
|
70
|
_b3 = _bytes3[1],
|
71
|
b3 = _bytes3[2],
|
72
|
_bs2 = _bytes3.slice(3);
|
73
|
|
74
|
if (_b2 < 0xf0) {
|
75
|
return [code(0x800, ((_b2 & 0x0f) << 12) + (con(_b3) << 6) + con(b3))].concat(_toConsumableArray(_decode(_bs2)));
|
76
|
}
|
77
|
}
|
78
|
/**
|
79
|
* 4 bytes
|
80
|
*/
|
81
|
|
82
|
{
|
83
|
var _bytes4 = _toArray(bytes),
|
84
|
_b4 = _bytes4[0],
|
85
|
_b5 = _bytes4[1],
|
86
|
_b6 = _bytes4[2],
|
87
|
b4 = _bytes4[3],
|
88
|
_bs3 = _bytes4.slice(4);
|
89
|
|
90
|
if (_b4 < 0xf8) {
|
91
|
return [code(0x10000, (((_b4 & 0x07) << 18) + con(_b5) << 12) + (con(_b6) << 6) + con(b4))].concat(_toConsumableArray(_decode(_bs3)));
|
92
|
}
|
93
|
}
|
94
|
throw new Error("invalid UTF-8 encoding");
|
95
|
}
|