Repository navigation
Expand file tree
/
Copy pathutf8.js
More file actions
247 lines (213 loc) · 7.97 KB
/
Copy pathutf8.js
File metadata and controls
247 lines (213 loc) · 7.97 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// utf8.js - fast UTF-8 encoding/decoding in JS
(function () {
var utf8 = {};
var root = this;
// If there's no Promise, try to import plite.
var PromiseImpl = root.Promise;
if (PromiseImpl === undefined) {
// Determine if we're in a CommonJS-compatible environment
// i.e. Node/Webpack/Browserify.
if (typeof module !== 'undefined' && module.exports) {
PromiseImpl = require('plite');
} else {
console.error('Promise implementation unavailable.');
}
}
// NativeCodec implements UTF-8 encoding/decoding using native APIs,
// namely those of TextEncoder/TextDecoder. These APIs are the most
// efficient.
function NativeCodec () {}
NativeCodec.prototype.encode = function (str) {
return PromiseImpl.resolve((new root.TextEncoder()).encode(str));
};
NativeCodec.prototype.decode = function (buf) {
return PromiseImpl.resolve((new root.TextDecoder()).decode(buf));
};
utf8['NativeCodec'] = NativeCodec;
// NodeJSCodec implements UTF-8 encoding/decoding using NodeJS APIs.
function NodeJSCodec () {}
NodeJSCodec.prototype.encode = function (str) {
return PromiseImpl.resolve(new Uint8Array(new Buffer(str, 'utf8')).buffer);
};
NodeJSCodec.prototype.decode = function (buf) {
return PromiseImpl.resolve((new Buffer(new Uint8Array(buf))).toString('utf8'));
};
utf8['NodeJSCodec'] = NodeJSCodec;
// BlobCodec implements UTF-8 encoding/decoding on top of the Blob and
// FileReader APIs. Performance and memory usage are better than
// using pure JS, but worse than TextEncoder/TextDecoder.
function BlobCodec () {}
BlobCodec.prototype.encode = function (str) {
var blob = new root.Blob([str], {type: 'text/plain; charset=utf-8'});
var reader = new root.FileReader();
return new PromiseImpl(function (resolve, reject) {
reader.onload = function (e) { resolve(reader.result); };
reader.onerror = function (e) { resolve(reader.error); };
reader.readAsArrayBuffer(blob);
});
};
BlobCodec.prototype.decode = function (buf) {
var blob = new root.Blob([buf], {type: 'text/plain; charset=utf-8'});
var reader = new root.FileReader();
return new PromiseImpl(function (resolve, reject) {
reader.onload = function (e) { resolve(reader.result); };
reader.onerror = function (e) { resolve(reader.error); };
reader.readAsText(blob);
});
};
utf8['BlobCodec'] = BlobCodec;
// JSCodec implements UTF-8 encoding/decoding on top of DOMString
// directly. Unfortunately, due to horrible characteristics of strings,
// it seems impossible to efficiently build strings in JS. JSCodec is a
// last resort and can take a long time for large arrays (>50 MiB.)
// --
// TODO: Performance can likely be improved. There's a lot of branching
// that could be removed.
function JSCodec () {}
JSCodec.prototype.encode = function (str) {
var h, l, i, j;
// Step 1. Preflight
var len = 0;
for (i = 0; i < str.length;) {
h = str.charCodeAt(i++);
if (h >= 0xD800 && h <= 0xDBFF) {
if (i + 1 < str.length) {
l = str.charCodeAt(i++);
if (l >= 0xDC00 && l <= 0xDFFF) {
len += 4;
} else {
len += 3;
}
} else {
len += 3;
}
} else {
if (h < 0x80) len += 1; else
if (h < 0x800) len += 2; else
if (h < 0x10000) len += 3;
}
}
// Step 2. Rewrite :)
var buf = new Uint8Array(len);
for (i = 0, j = 0; i < str.length;) {
h = str.charCodeAt(i++);
// Handle surrogate pairs.
if (h >= 0xD800 && h <= 0xDBFF) {
if (i + 1 < str.length) {
l = str.charCodeAt(i++);
if (l >= 0xDC00 && l <= 0xDFFF) {
var cp = ((h - 0xD800) << 10) + (l + 0x2400);
buf[j++] = 0xF0 | (cp >> 0x12);
buf[j++] = 0x80 | (cp >> 0x0C) & 0x3F;
buf[j++] = 0x80 | (cp >> 0x06) & 0x3F;
buf[j++] = 0x80 | cp & 0x3F;
} else {
buf[j++] = 0xEF;
buf[j++] = 0xBF;
buf[j++] = 0xBD;
continue;
}
} else {
buf[j++] = 0xEF;
buf[j++] = 0xBF;
buf[j++] = 0xBD;
continue;
}
} else {
if (h < 0x80) {
buf[j++] = h;
} else if (h < 0x800) {
buf[j++] = 0xC0 | h >> 6;
buf[j++] = 0x80 | h & 0x3F;
len += 2;
} else if (h < 0x10000) {
buf[j++] = 0xE0 | (h >> 0xC);
buf[j++] = 0x80 | (h >> 0x6) & 0x3F;
buf[j++] = 0x80 | (h >> 0x0) & 0x3F;
len += 3;
}
}
}
return PromiseImpl.resolve(buf.buffer);
};
// This decoder is based on "Flexible and Economical UTF-8 Decoder"
// Copyright (c) 2008-2010 Bjoern Hoehrmann <bjoern@hoehrmann.de>
// See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details
var utf8d = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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= (state !== 0) ? (byte & 0x3f) | (codepoint << 6) : (0xff >> type) & (byte);
state = utf8d[256 + state * 16 + type];
return state;
}
for (var i = 0; i < buf.length; ++i) {
// Force V8 to flatten the string.
if (i % 1000000 === 0) {
str[0];
}
switch (decode1(buf[i])) {
case 0:
break;
case 1:
str += String.fromCharCode(0xFFFD);
while (i + 1 < buf.length && (buf[i + 1] & 0xC0) === 0x80) ++i;
state = codepoint = 0;
continue;
default:
continue;
}
if (codepoint <= 0xFFFF) {
str += String.fromCharCode(codepoint);
} else {
str += String.fromCharCode(
0xD7C0 + (codepoint >> 10),
0xDC00 + (codepoint & 0x3FF)
);
}
codepoint = 0;
}
return PromiseImpl.resolve(str);
};
utf8['JSCodec'] = JSCodec;
var defaultCodec = null;
// TODO: detect polyfill
// We can handle data much more efficiently than the polyfill.
if (root.TextDecoder && root.TextEncoder) {
defaultCodec = new NativeCodec();
} else if (root.Blob && root.FileReader) {
defaultCodec = new BlobCodec();
} else if (root.Buffer && root.Buffer.from) {
defaultCodec = new NodeJSCodec();
} else {
defaultCodec = new JSCodec();
}
// API
utf8['encode'] = function (str) { return defaultCodec.encode(str); };
utf8['decode'] = function (buf) { return defaultCodec.decode(buf); };
if (typeof module !== 'undefined' && module.exports) {
// CommonJS
module.exports = utf8;
} else {
// Global
root.utf8 = utf8;
}
})();