@exodus/bytes 1.7.0 → 1.9.0

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/fallback/hex.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { assertUint8 } from '../assert.js'
2
- import { nativeDecoder, nativeEncoder, decode2string } from './_utils.js'
2
+ import { nativeDecoder, nativeEncoder, decode2string, E_STRING } from './_utils.js'
3
3
  import { encodeAscii, decodeAscii } from './latin1.js'
4
4
 
5
5
  let hexArray // array of 256 bytes converted to two-char hex strings
@@ -52,7 +52,7 @@ export function toHex(arr) {
52
52
  }
53
53
 
54
54
  export function fromHex(str) {
55
- if (typeof str !== 'string') throw new TypeError('Input is not a string')
55
+ if (typeof str !== 'string') throw new TypeError(E_STRING)
56
56
  if (str.length % 2 !== 0) throw new SyntaxError(E_HEX)
57
57
 
58
58
  const length = str.length / 2 // this helps Hermes in loops
@@ -6,12 +6,19 @@ import {
6
6
  isHermes,
7
7
  isDeno,
8
8
  isLE,
9
+ skipWeb,
9
10
  } from './_utils.js'
10
11
 
12
+ const { atob } = globalThis
13
+ const { toBase64: web64 } = Uint8Array.prototype
14
+
11
15
  // See http://stackoverflow.com/a/22747272/680742, which says that lowest limit is in Chrome, with 0xffff args
12
16
  // On Hermes, actual max is 0x20_000 minus current stack depth, 1/16 of that should be safe
13
17
  const maxFunctionArgs = 0x20_00
14
18
 
19
+ // toBase64+atob path is faster on everything where fromBase64 is fast
20
+ const useLatin1atob = web64 && atob && !skipWeb
21
+
15
22
  export function asciiPrefix(arr) {
16
23
  let p = 0 // verified ascii bytes
17
24
  const length = arr.length
@@ -30,6 +37,7 @@ export function asciiPrefix(arr) {
30
37
  const b = u32[i + 1]
31
38
  const c = u32[i + 2]
32
39
  const d = u32[i + 3]
40
+ // "(a | b | c | d) & mask" is slower on Hermes though faster on v8
33
41
  if (a & 0x80_80_80_80 || b & 0x80_80_80_80 || c & 0x80_80_80_80 || d & 0x80_80_80_80) break
34
42
  }
35
43
 
@@ -46,6 +54,18 @@ export function decodeLatin1(arr, start = 0, stop = arr.length) {
46
54
  stop |= 0
47
55
  const total = stop - start
48
56
  if (total === 0) return ''
57
+
58
+ if (
59
+ useLatin1atob &&
60
+ total >= 256 &&
61
+ total < 1e8 &&
62
+ arr.toBase64 === web64 &&
63
+ arr.BYTES_PER_ELEMENT === 1
64
+ ) {
65
+ const sliced = start === 0 && stop === arr.length ? arr : arr.subarray(start, stop)
66
+ return atob(sliced.toBase64())
67
+ }
68
+
49
69
  if (total > maxFunctionArgs) {
50
70
  let prefix = ''
51
71
  for (let i = start; i < stop; ) {
@@ -107,6 +127,20 @@ export const encodeCharcodes = isHermes
107
127
  return arr
108
128
  }
109
129
 
130
+ export function encodeAsciiPrefix(x, s) {
131
+ let i = 0
132
+ for (const len3 = s.length - 3; i < len3; i += 4) {
133
+ const x0 = s.charCodeAt(i), x1 = s.charCodeAt(i + 1), x2 = s.charCodeAt(i + 2), x3 = s.charCodeAt(i + 3) // prettier-ignore
134
+ if ((x0 | x1 | x2 | x3) >= 128) break
135
+ x[i] = x0
136
+ x[i + 1] = x1
137
+ x[i + 2] = x2
138
+ x[i + 3] = x3
139
+ }
140
+
141
+ return i
142
+ }
143
+
110
144
  /* eslint-enable @exodus/mutable/no-param-reassign-prop-only */
111
145
 
112
146
  // Warning: can be used only on checked strings, converts strings to 8-bit