@exodus/bytes 1.0.0-rc.8 → 1.0.0-rc.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +128 -4
- package/encoding.js +234 -0
- package/fallback/_utils.js +88 -10
- package/fallback/encoding.labels.js +46 -0
- package/fallback/encoding.util.js +34 -0
- package/fallback/hex.js +2 -70
- package/fallback/latin1.js +2 -1
- package/fallback/multi-byte.encodings.cjs +1 -0
- package/fallback/multi-byte.encodings.json +545 -0
- package/fallback/multi-byte.js +449 -0
- package/fallback/multi-byte.table.js +114 -0
- package/fallback/single-byte.encodings.js +45 -0
- package/fallback/single-byte.js +83 -0
- package/fallback/utf16.js +180 -0
- package/hex.node.js +2 -0
- package/multi-byte.js +13 -0
- package/multi-byte.node.js +25 -0
- package/package.json +39 -8
- package/single-byte.js +55 -0
- package/single-byte.node.js +62 -0
- package/utf16.js +73 -0
- package/utf16.node.js +79 -0
- package/utf8.js +7 -9
- package/utf8.node.js +8 -5
package/utf8.node.js
CHANGED
|
@@ -5,16 +5,17 @@ import { isAscii } from 'node:buffer'
|
|
|
5
5
|
|
|
6
6
|
if (Buffer.TYPED_ARRAY_SUPPORT) throw new Error('Unexpected Buffer polyfill')
|
|
7
7
|
|
|
8
|
-
const decoderFatal = new TextDecoder('
|
|
9
|
-
const decoderLoose = new TextDecoder('
|
|
8
|
+
const decoderFatal = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true })
|
|
9
|
+
const decoderLoose = new TextDecoder('utf-8', { ignoreBOM: true })
|
|
10
10
|
const { isWellFormed } = String.prototype
|
|
11
|
+
const isDeno = Boolean(globalThis.Deno)
|
|
11
12
|
|
|
12
13
|
function encode(str, loose = false) {
|
|
13
14
|
if (typeof str !== 'string') throw new TypeError('Input is not a string')
|
|
14
15
|
const strLength = str.length
|
|
15
16
|
if (strLength === 0) return new Uint8Array() // faster than Uint8Array.of
|
|
16
17
|
let res
|
|
17
|
-
if (strLength > 0x4_00) {
|
|
18
|
+
if (strLength > 0x4_00 && !isDeno) {
|
|
18
19
|
// Faster for large strings
|
|
19
20
|
const byteLength = Buffer.byteLength(str)
|
|
20
21
|
res = Buffer.allocUnsafe(byteLength)
|
|
@@ -35,11 +36,13 @@ function decode(arr, loose = false) {
|
|
|
35
36
|
assertUint8(arr)
|
|
36
37
|
const byteLength = arr.byteLength
|
|
37
38
|
if (byteLength === 0) return ''
|
|
38
|
-
if (byteLength > 0x6_00 && isAscii(arr)) {
|
|
39
|
+
if (byteLength > 0x6_00 && !(isDeno && loose) && isAscii(arr)) {
|
|
39
40
|
// On non-ascii strings, this loses ~10% * [relative position of the first non-ascii byte] (up to 10% total)
|
|
40
41
|
// On ascii strings, this wins 1.5x on loose = false and 1.3x on loose = true
|
|
41
42
|
// Only makes sense for large enough strings
|
|
42
|
-
|
|
43
|
+
const buf = Buffer.from(arr.buffer, arr.byteOffset, arr.byteLength)
|
|
44
|
+
if (isDeno) return buf.toString() // Deno suffers from .latin1Slice
|
|
45
|
+
return buf.latin1Slice(0, arr.byteLength) // .latin1Slice is faster than .asciiSlice
|
|
43
46
|
}
|
|
44
47
|
|
|
45
48
|
return loose ? decoderLoose.decode(arr) : decoderFatal.decode(arr)
|