Skip to content

Commit bc7cad0

Browse files
committed
perf: use utf16 slice for euc-jp
1 parent 8ae26ea commit bc7cad0

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

fallback/multi-byte.js

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function bigDecoder(err, pair) {
3030

3131
const decode = (arr, start, end, stream) => {
3232
let i = start
33-
o16 = new Uint16Array(end - start)
33+
o16 = new Uint16Array(end - start + (lead ? 1 : 0)) // there are pairs but they consume more than one byte
3434
oi = 0
3535

3636
if (lead && i < end) decodeLead(arr[i++])
@@ -78,55 +78,61 @@ const mappers = {
7878
const jis0212 = getTable('jis0212')
7979
let j12 = false
8080
let lead = 0
81+
let oi = 0
82+
let o16
8183

8284
const decodeLead = (b) => {
8385
if (lead === 0x8e && b >= 0xa1 && b <= 0xdf) {
8486
lead = 0
85-
return String.fromCharCode(0xfe_c0 + b)
86-
}
87-
88-
if (lead === 0x8f && b >= 0xa1 && b <= 0xfe) {
87+
o16[oi++] = 0xfe_c0 + b
88+
} else if (lead === 0x8f && b >= 0xa1 && b <= 0xfe) {
8989
j12 = true
9090
lead = b
91-
return ''
92-
}
91+
} else {
92+
let cp
93+
if (lead >= 0xa1 && lead <= 0xfe && b >= 0xa1 && b <= 0xfe) {
94+
cp = (j12 ? jis0212 : jis0208)[(lead - 0xa1) * 94 + b - 0xa1]
95+
}
9396

94-
let cp
95-
if (lead >= 0xa1 && lead <= 0xfe && b >= 0xa1 && b <= 0xfe) {
96-
cp = (j12 ? jis0212 : jis0208)[(lead - 0xa1) * 94 + b - 0xa1]
97+
lead = 0
98+
j12 = false
99+
if (cp !== undefined && cp !== REP) {
100+
o16[oi++] = cp
101+
} else {
102+
o16[oi++] = err()
103+
if (b < 128) o16[oi++] = b
104+
}
97105
}
98-
99-
lead = 0
100-
j12 = false
101-
if (cp !== undefined && cp !== REP) return String.fromCharCode(cp)
102-
return b < 128 ? String.fromCharCode(err(), b) : String.fromCharCode(err())
103106
}
104107

105108
const decode = (arr, start, end, stream) => {
106-
let res = ''
107109
let i = start
110+
o16 = new Uint16Array(end - start + (lead ? 1 : 0))
111+
oi = 0
108112

109-
if (lead && i < end) res += decodeLead(arr[i++])
110-
if (lead && i < end) res += decodeLead(arr[i++]) // could be two leads, but no more
113+
if (lead && i < end) decodeLead(arr[i++])
114+
if (lead && i < end) decodeLead(arr[i++]) // could be two leads, but no more
111115
while (i < end) {
112116
const b = arr[i++]
113117
if (b < 128) {
114-
res += String.fromCharCode(b)
118+
o16[oi++] = b
115119
} else if ((b < 0xa1 && b !== 0x8e && b !== 0x8f) || b === 0xff) {
116-
res += String.fromCharCode(err())
120+
o16[oi++] = err()
117121
} else {
118122
lead = b
119-
if (i < end) res += decodeLead(arr[i++])
120-
if (lead && i < end) res += decodeLead(arr[i++]) // could be two leads
123+
if (i < end) decodeLead(arr[i++])
124+
if (lead && i < end) decodeLead(arr[i++]) // could be two leads
121125
}
122126
}
123127

124128
if (lead && !stream) {
125129
lead = 0
126130
j12 = false // can be true only when lead is non-zero
127-
res += String.fromCharCode(err())
131+
o16[oi++] = err()
128132
}
129133

134+
const res = decodeUCS2(o16, oi)
135+
o16 = null
130136
return res
131137
}
132138

@@ -318,7 +324,7 @@ const mappers = {
318324
}
319325

320326
const decode = (arr, start, end, stream) => {
321-
o16 = new Uint16Array(end - start)
327+
o16 = new Uint16Array(end - start + (lead ? 1 : 0))
322328
oi = 0
323329
let i = start
324330

0 commit comments

Comments
 (0)