diff options
Diffstat (limited to 'op_crates/web/08_text_encoding.js')
-rw-r--r-- | op_crates/web/08_text_encoding.js | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/op_crates/web/08_text_encoding.js b/op_crates/web/08_text_encoding.js index 73cb38311..1fda1a816 100644 --- a/op_crates/web/08_text_encoding.js +++ b/op_crates/web/08_text_encoding.js @@ -4545,10 +4545,38 @@ fromByteArray, }; + /** + * @param {Uint8Array} bytes + */ + function decode(bytes, encoding) { + const BOMEncoding = BOMSniff(bytes); + let start = 0; + if (BOMEncoding !== null) { + encoding = BOMEncoding; + if (BOMEncoding === "UTF-8") start = 3; + else start = 2; + } + return new TextDecoder(encoding).decode(bytes.slice(start)); + } + + /** + * @param {Uint8Array} bytes + */ + function BOMSniff(bytes) { + const BOM = bytes.subarray(0, 3); + if (BOM[0] === 0xEF && BOM[1] === 0xBB && BOM[2] === 0xBF) { + return "UTF-8"; + } + if (BOM[0] === 0xFE && BOM[1] === 0xFF) return "UTF-16BE"; + if (BOM[0] === 0xFF && BOM[1] === 0xFE) return "UTF-16LE"; + return null; + } + window.TextEncoder = TextEncoder; window.TextDecoder = TextDecoder; window.atob = atob; window.btoa = btoa; window.__bootstrap = window.__bootstrap || {}; + window.__bootstrap.encoding = { decode }; window.__bootstrap.base64 = base64; })(this); |