@@ -13,10 +13,11 @@ export const assertEncoding = (encoding) => {
1313function getEncoding ( encoding ) {
1414 assertEncoding ( encoding )
1515 if ( encoding === xUserDefined ) {
16- return Array . from ( { length : 128 } , ( _ , i ) => String . fromCharCode ( 0xf7_80 + i ) ) . join ( '' )
16+ return Array . from ( { length : 128 } , ( _ , i ) => 0xf7_80 + i )
1717 }
1818
19- return encodings [ encoding ]
19+ let prev = 0
20+ return encodings [ encoding ] . map ( ( x ) => ( prev += x ) ) // eslint-disable-line no-return-assign
2021}
2122
2223const mappers = new Map ( )
@@ -28,13 +29,13 @@ export function encodingMapper(encoding) {
2829 const cached = mappers . get ( encoding )
2930 if ( cached ) return cached
3031
31- const incomplete = getEncoding ( encoding ) . includes ( '\uFFFD' )
32+ const codes = getEncoding ( encoding )
33+ const incomplete = codes . includes ( 0xff_fd )
3234 let map
3335 const mapper = ( arr , start = 0 ) => {
3436 if ( ! map ) {
3537 map = Uint16Array . from ( { length : 256 } , ( _ , i ) => i ) // Unicode subset
36- const strings = getEncoding ( encoding ) . split ( '' )
37- map . set ( Uint16Array . from ( strings . map ( ( x ) => x . charCodeAt ( 0 ) ) ) , 128 )
38+ map . set ( Uint16Array . from ( codes ) , 128 )
3839 }
3940
4041 const o = Uint16Array . from ( start === 0 ? arr : arr . subarray ( start ) ) // copy to modify in-place, also those are 16-bit now
@@ -63,12 +64,13 @@ export function encodingDecoder(encoding) {
6364 if ( cached ) return cached
6465
6566 let strings
66- const incomplete = getEncoding ( encoding ) . includes ( '\uFFFD' )
67+ const codes = getEncoding ( encoding )
68+ const incomplete = codes . includes ( 0xff_fd )
6769 const decoder = ( arr , loose = false ) => {
6870 if ( ! strings ) {
69- const part = getEncoding ( encoding ) . split ( '' )
70- strings = Array . from ( { length : 128 } , ( _ , i ) => String . fromCharCode ( i ) ) . concat ( part )
71- while ( strings . length < 256 ) strings . push ( String . fromCharCode ( strings . length ) )
71+ const allCodes = Array . from ( { length : 128 } , ( _ , i ) => i ) . concat ( codes )
72+ while ( allCodes . length < 256 ) allCodes . push ( allCodes . length )
73+ strings = allCodes . map ( ( c ) => String . fromCharCode ( c ) )
7274 }
7375
7476 const prefix = decodeLatin1 ( arr , 0 , asciiPrefix ( arr ) )
0 commit comments