forked from ExodusOSS/bytes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti-byte.node.js
More file actions
25 lines (22 loc) · 872 Bytes
/
multi-byte.node.js
File metadata and controls
25 lines (22 loc) · 872 Bytes
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
import { assertUint8 } from './assert.js'
import { isDeno } from './fallback/_utils.js'
import { isAsciiSuperset, multibyteDecoder } from './fallback/multi-byte.js'
import { isAscii } from 'node:buffer'
const toBuf = (x) => Buffer.from(x.buffer, x.byteOffset, x.byteLength)
export function createMultibyteDecoder(encoding, loose = false) {
const jsDecoder = multibyteDecoder(encoding, loose) // asserts
let streaming = false
const asciiSuperset = isAsciiSuperset(encoding)
return (arr, stream = false) => {
assertUint8(arr)
if (!streaming) {
if (arr.byteLength === 0) return ''
if (asciiSuperset && isAscii(arr)) {
if (isDeno) return toBuf(arr).toString()
return toBuf(arr).latin1Slice(0, arr.byteLength) // .latin1Slice is faster than .asciiSlice
}
}
streaming = stream
return jsDecoder(arr, stream)
}
}