|
1 | | -import { asciiPrefix, decodeLatin1 } from './latin1.js' |
| 1 | +import { asciiPrefix, decodeLatin1, decodeUCS2 } from './latin1.js' |
2 | 2 | import { getTable } from './multi-byte.table.js' |
3 | 3 |
|
4 | 4 | export const E_STRICT = 'Input is not well-formed for this encoding' |
@@ -281,45 +281,56 @@ const mappers = { |
281 | 281 | shift_jis: (err) => { |
282 | 282 | const jis0208 = getTable('jis0208') |
283 | 283 | let lead = 0 |
| 284 | + let oi = 0 |
| 285 | + let out |
284 | 286 |
|
285 | 287 | const decodeLead = (b) => { |
286 | 288 | const l = lead |
287 | 289 | lead = 0 |
288 | 290 | if (b >= 0x40 && b <= 0xfc && b !== 0x7f) { |
289 | 291 | const p = (l - (l < 0xa0 ? 0x81 : 0xc1)) * 188 + b - (b < 0x7f ? 0x40 : 0x41) |
290 | | - if (p >= 8836 && p <= 10_715) return String.fromCharCode(0xe0_00 - 8836 + p) |
| 292 | + if (p >= 8836 && p <= 10_715) { |
| 293 | + out[oi++] = 0xe0_00 - 8836 + p |
| 294 | + return |
| 295 | + } |
| 296 | + |
291 | 297 | const cp = jis0208[p] |
292 | | - if (cp !== undefined && cp !== REP) return String.fromCharCode(cp) |
| 298 | + if (cp !== undefined && cp !== REP) { |
| 299 | + out[oi++] = cp |
| 300 | + return |
| 301 | + } |
293 | 302 | } |
294 | 303 |
|
295 | | - return b < 128 ? String.fromCharCode(err(), b) : String.fromCharCode(err()) |
| 304 | + out[oi++] = err() |
| 305 | + if (b < 128) out[oi++] = b |
296 | 306 | } |
297 | 307 |
|
298 | 308 | const decode = (arr, start, end, stream) => { |
299 | | - let res = '' |
| 309 | + out = new Uint16Array(end - start) |
| 310 | + oi = 0 |
300 | 311 | let i = start |
301 | 312 |
|
302 | | - if (lead && i < end) res += decodeLead(arr[i++]) |
| 313 | + if (lead && i < end) decodeLead(arr[i++]) |
303 | 314 | while (i < end) { |
304 | 315 | const b = arr[i++] |
305 | 316 | if (b <= 0x80) { |
306 | | - res += String.fromCharCode(b) // 0x80 is allowed |
| 317 | + out[oi++] = b // 0x80 is allowed |
307 | 318 | } else if (b >= 0xa1 && b <= 0xdf) { |
308 | | - res += String.fromCharCode(0xfe_c0 + b) |
| 319 | + out[oi++] = 0xfe_c0 + b |
309 | 320 | } else if (b === 0xa0 || b > 0xfc) { |
310 | | - res += String.fromCharCode(err()) |
| 321 | + out[oi++] = err() |
311 | 322 | } else { |
312 | 323 | lead = b |
313 | | - if (i < end) res += decodeLead(arr[i++]) |
| 324 | + if (i < end) decodeLead(arr[i++]) |
314 | 325 | } |
315 | 326 | } |
316 | 327 |
|
317 | 328 | if (lead && !stream) { |
318 | 329 | lead = 0 |
319 | | - res += String.fromCharCode(err()) |
| 330 | + out[oi++] = err() |
320 | 331 | } |
321 | 332 |
|
322 | | - return res |
| 333 | + return decodeUCS2(out, oi) |
323 | 334 | } |
324 | 335 |
|
325 | 336 | return { decode, isAscii: () => lead === 0 } |
|
0 commit comments