1- import * as js from './fallback/utf16.js'
2- import { nativeDecoder , isLE } from './fallback/_utils.js'
1+ import { encodeApi , decodeApiDecoders , decodeApiJS } from './fallback/utf16.js'
2+ import { nativeDecoder } from './fallback/_utils.js'
33
44function checkDecoders ( ) {
55 // Not all barebone engines with TextDecoder support something except utf-8
@@ -14,52 +14,9 @@ function checkDecoders() {
1414 return false
1515}
1616
17- const canDecoders = checkDecoders ( )
18- const ignoreBOM = true
19- const decoderFatalLE = canDecoders ? new TextDecoder ( 'utf-16le' , { ignoreBOM, fatal : true } ) : null
20- const decoderLooseLE = canDecoders ? new TextDecoder ( 'utf-16le' , { ignoreBOM } ) : null
21- const decoderFatalBE = canDecoders ? new TextDecoder ( 'utf-16be' , { ignoreBOM, fatal : true } ) : null
22- const decoderLooseBE = canDecoders ? new TextDecoder ( 'utf-16be' , { ignoreBOM } ) : null
23- const decoderFatal16 = isLE ? decoderFatalLE : decoderFatalBE
24- const decoderLoose16 = isLE ? decoderLooseLE : decoderLooseBE
25- const { isWellFormed, toWellFormed } = String . prototype
17+ const decode = checkDecoders ( ) ? decodeApiDecoders : decodeApiJS
2618
27- const { E_STRICT } = js
28-
29- // Unlike utf8, operates on Uint16Arrays by default
30-
31- function decode ( input , loose = false , format = 'uint16' ) {
32- let u16
33- switch ( format ) {
34- case 'uint16' :
35- if ( ! ( input instanceof Uint16Array ) ) throw new TypeError ( 'Expected an Uint16Array' )
36- if ( canDecoders ) return loose ? decoderLoose16 . decode ( input ) : decoderFatal16 . decode ( input )
37- u16 = input
38- break
39- case 'uint8-le' :
40- if ( ! ( input instanceof Uint8Array ) ) throw new TypeError ( 'Expected an Uint8Array' )
41- if ( input . byteLength % 2 !== 0 ) throw new TypeError ( 'Expected even number of bytes' )
42- if ( canDecoders ) return loose ? decoderLooseLE . decode ( input ) : decoderFatalLE . decode ( input )
43- u16 = js . to16input ( input , true )
44- break
45- case 'uint8-be' :
46- if ( ! ( input instanceof Uint8Array ) ) throw new TypeError ( 'Expected an Uint8Array' )
47- if ( input . byteLength % 2 !== 0 ) throw new TypeError ( 'Expected even number of bytes' )
48- if ( canDecoders ) return loose ? decoderLooseBE . decode ( input ) : decoderFatalBE . decode ( input )
49- u16 = js . to16input ( input , false )
50- break
51- default :
52- throw new TypeError ( 'Unknown format' )
53- }
54-
55- const str = js . decode ( u16 , loose , ( ! loose && isWellFormed ) || ( loose && toWellFormed ) )
56- if ( ! loose && isWellFormed && ! isWellFormed . call ( str ) ) throw new TypeError ( E_STRICT )
57- if ( loose && toWellFormed ) return toWellFormed . call ( str )
58-
59- return str
60- }
61-
62- export const utf16fromString = ( str , format = 'uint16' ) => js . encodeApi ( str , false , format )
63- export const utf16fromStringLoose = ( str , format = 'uint16' ) => js . encodeApi ( str , true , format )
19+ export const utf16fromString = ( str , format = 'uint16' ) => encodeApi ( str , false , format )
20+ export const utf16fromStringLoose = ( str , format = 'uint16' ) => encodeApi ( str , true , format )
6421export const utf16toString = ( arr , format = 'uint16' ) => decode ( arr , false , format )
6522export const utf16toStringLoose = ( arr , format = 'uint16' ) => decode ( arr , true , format )
0 commit comments