mirror of
https://github.com/denoland/deno.git
synced 2024-12-23 15:49:44 -05:00
perf(ext/web/encoding): avoid copy in decode (#16364)
This commit is contained in:
parent
45ac6e602d
commit
0e1167d12d
1 changed files with 6 additions and 11 deletions
|
@ -23,7 +23,6 @@
|
||||||
StringPrototypeCharCodeAt,
|
StringPrototypeCharCodeAt,
|
||||||
StringPrototypeSlice,
|
StringPrototypeSlice,
|
||||||
TypedArrayPrototypeSubarray,
|
TypedArrayPrototypeSubarray,
|
||||||
TypedArrayPrototypeSlice,
|
|
||||||
Uint8Array,
|
Uint8Array,
|
||||||
Uint32Array,
|
Uint32Array,
|
||||||
} = window.__bootstrap.primordials;
|
} = window.__bootstrap.primordials;
|
||||||
|
@ -404,27 +403,23 @@
|
||||||
*/
|
*/
|
||||||
function decode(bytes, encoding) {
|
function decode(bytes, encoding) {
|
||||||
const BOMEncoding = BOMSniff(bytes);
|
const BOMEncoding = BOMSniff(bytes);
|
||||||
let start = 0;
|
|
||||||
if (BOMEncoding !== null) {
|
if (BOMEncoding !== null) {
|
||||||
encoding = BOMEncoding;
|
encoding = BOMEncoding;
|
||||||
if (BOMEncoding === "UTF-8") start = 3;
|
const start = BOMEncoding === "UTF-8" ? 3 : 2;
|
||||||
else start = 2;
|
bytes = TypedArrayPrototypeSubarray(bytes, start);
|
||||||
}
|
}
|
||||||
return new TextDecoder(encoding).decode(
|
return new TextDecoder(encoding).decode(bytes);
|
||||||
TypedArrayPrototypeSlice(bytes, start),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Uint8Array} bytes
|
* @param {Uint8Array} bytes
|
||||||
*/
|
*/
|
||||||
function BOMSniff(bytes) {
|
function BOMSniff(bytes) {
|
||||||
const BOM = TypedArrayPrototypeSubarray(bytes, 0, 3);
|
if (bytes[0] === 0xEF && bytes[1] === 0xBB && bytes[2] === 0xBF) {
|
||||||
if (BOM[0] === 0xEF && BOM[1] === 0xBB && BOM[2] === 0xBF) {
|
|
||||||
return "UTF-8";
|
return "UTF-8";
|
||||||
}
|
}
|
||||||
if (BOM[0] === 0xFE && BOM[1] === 0xFF) return "UTF-16BE";
|
if (bytes[0] === 0xFE && bytes[1] === 0xFF) return "UTF-16BE";
|
||||||
if (BOM[0] === 0xFF && BOM[1] === 0xFE) return "UTF-16LE";
|
if (bytes[0] === 0xFF && bytes[1] === 0xFE) return "UTF-16LE";
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue