Skip to content

Commit d1cced2

Browse files
committed
size: separate latin1fromString in single-byte
1 parent c9df4da commit d1cced2

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

single-byte.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -90,30 +90,32 @@ function encode(s, m) {
9090
// fromBase64+btoa path is faster on everything where fromBase64 is fast
9191
const useLatin1btoa = Uint8Array.fromBase64 && btoa
9292

93+
export function latin1fromString(s) {
94+
if (typeof s !== 'string') throw new TypeError(E_STRING)
95+
// max limit is to not produce base64 strings that are too long
96+
if (useLatin1btoa && s.length >= 1024 && s.length < 1e8) {
97+
try {
98+
return Uint8Array.fromBase64(btoa(s)) // fails on non-latin1
99+
} catch {
100+
throw new TypeError(E_STRICT)
101+
}
102+
}
103+
104+
if (NON_LATIN.test(s)) throw new TypeError(E_STRICT)
105+
return encodeLatin1(s)
106+
}
107+
93108
export function createSinglebyteEncoder(encoding, { mode = 'fatal' } = {}) {
94109
// TODO: replacement, truncate (replacement will need varying length)
95110
if (mode !== 'fatal') throw new Error('Unsupported mode')
111+
if (encoding === 'iso-8859-1') return latin1fromString
96112
const m = encodeMap(encoding) // asserts
97-
const isLatin1 = encoding === 'iso-8859-1'
98113

99114
// No single-byte encoder produces surrogate pairs, so any surrogate is invalid
100115
// This needs special treatment only to decide how many replacement chars to output, one or two
101116
// Not much use in running isWellFormed, most likely cause of error is unmapped chars, not surrogate pairs
102117
return (s) => {
103118
if (typeof s !== 'string') throw new TypeError(E_STRING)
104-
if (isLatin1) {
105-
// max limit is to not produce base64 strings that are too long
106-
if (useLatin1btoa && s.length >= 1024 && s.length < 1e8) {
107-
try {
108-
return Uint8Array.fromBase64(btoa(s)) // fails on non-latin1
109-
} catch {
110-
throw new TypeError(E_STRICT)
111-
}
112-
}
113-
114-
if (NON_LATIN.test(s)) throw new TypeError(E_STRICT)
115-
return encodeLatin1(s)
116-
}
117119

118120
// Instead of an ASCII regex check, encode optimistically - this is faster
119121
// Check for 8-bit string with a regex though, this is instant on 8-bit strings so doesn't hurt the ASCII fast path
@@ -130,6 +132,5 @@ export function createSinglebyteEncoder(encoding, { mode = 'fatal' } = {}) {
130132
}
131133

132134
export const latin1toString = /* @__PURE__ */ createSinglebyteDecoder('iso-8859-1')
133-
export const latin1fromString = /* @__PURE__ */ createSinglebyteEncoder('iso-8859-1')
134135
export const windows1252toString = /* @__PURE__ */ createSinglebyteDecoder('windows-1252')
135136
export const windows1252fromString = /* @__PURE__ */ createSinglebyteEncoder('windows-1252')

0 commit comments

Comments
 (0)