44 * SPDX-License-Identifier: MIT
55 */
66
7+ #if TEXTDECODER != 1 && TEXTDECODER != 2
8+ #error "TEXTDECODER must be either 1 or 2"
9+ #endif
10+
711addToLibrary ( {
812 // TextDecoder constructor defaults to UTF-8
913#if TEXTDECODER == 2
1014 $UTF8Decoder : "new TextDecoder()" ,
11- #elif TEXTDECODER == 1
15+ #else
1216 $UTF8Decoder : "typeof TextDecoder != 'undefined' ? new TextDecoder() : undefined" ,
1317#endif
1418
@@ -35,49 +39,28 @@ addToLibrary({
3539 * @param {boolean=} ignoreNul - If true, the function will not stop on a NUL character.
3640 * @return {string}
3741 */` ,
38- #if TEXTDECODER
3942 $UTF8ArrayToString__deps : [ '$UTF8Decoder' , '$findStringEnd' ] ,
40- #endif
4143 $UTF8ArrayToString : ( heapOrArray , idx = 0 , maxBytesToRead , ignoreNul ) => {
4244#if CAN_ADDRESS_2GB
4345 idx >>>= 0 ;
4446#endif
4547
46- #if TEXTDECODER
4748 var endPtr = findStringEnd ( heapOrArray , idx , maxBytesToRead , ignoreNul ) ;
48- #else
49- var endIdx = idx + maxBytesToRead ;
50- #endif
5149
5250#if TEXTDECODER == 2
5351 return UTF8Decoder . decode ( heapOrArray . buffer ? { { { getUnsharedTextDecoderView ( 'heapOrArray' , 'idx' , 'endPtr' ) } } } : new Uint8Array ( heapOrArray . slice ( idx , endPtr ) ) ) ;
5452#else // TEXTDECODER == 2
55- #if TEXTDECODER
5653 // When using conditional TextDecoder, skip it for short strings as the overhead of the native call is not worth it.
5754 if ( endPtr - idx > 16 && heapOrArray . buffer && UTF8Decoder ) {
5855 return UTF8Decoder . decode ( { { { getUnsharedTextDecoderView ( 'heapOrArray' , 'idx' , 'endPtr' ) } } } ) ;
5956 }
60- #endif // TEXTDECODER
6157 var str = '' ;
62- #if TEXTDECODER
63- // If building with TextDecoder, we have already computed the string length
64- // above, so test loop end condition against that
6558 while ( idx < endPtr ) {
66- #else
67- while ( ! ( idx >= endIdx ) ) {
68- #endif
6959 // For UTF8 byte structure, see:
7060 // http://en.wikipedia.org/wiki/UTF-8#Description
7161 // https://www.ietf.org/rfc/rfc2279.txt
7262 // https://tools.ietf.org/html/rfc3629
7363 var u0 = heapOrArray [ idx ++ ] ;
74- #if ! TEXTDECODER
75- // If not building with TextDecoder enabled, we don't know the string
76- // length, so scan for \0 byte.
77- // If building with TextDecoder, we know exactly at what byte index the
78- // string ends, so checking for nulls here would be redundant.
79- if ( ! u0 && ! ignoreNul ) return str ;
80- #endif
8164 if ( ! ( u0 & 0x80 ) ) { str += String . fromCharCode ( u0 ) ; continue ; }
8265 var u1 = heapOrArray [ idx ++ ] & 63 ;
8366 if ( ( u0 & 0xE0 ) == 0xC0 ) { str += String . fromCharCode ( ( ( u0 & 31 ) << 6 ) | u1 ) ; continue ; }
@@ -310,32 +293,26 @@ addToLibrary({
310293
311294#if TEXTDECODER == 2
312295 $UTF16Decoder: "new TextDecoder('utf-16le');" ,
313- #elif TEXTDECODER == 1
296+ #else
314297 $UTF16Decoder : "typeof TextDecoder != 'undefined' ? new TextDecoder('utf-16le') : undefined;" ,
315298#endif
316299
317300 // Given a pointer 'ptr' to a null-terminated UTF16LE-encoded string in the
318301 // emscripten HEAP, returns a copy of that string as a Javascript String
319302 // object.
320- #if TEXTDECODER
321303 $UTF16ToString__deps : [ '$UTF16Decoder' , '$findStringEnd' ] ,
322- #endif
323304 $UTF16ToString : ( ptr , maxBytesToRead , ignoreNul ) = > {
324305#if ASSERTIONS
325306 assert ( ptr % 2 == 0 , 'Pointer passed to UTF16ToString must be aligned to two bytes!' ) ;
326307#endif
327308 var idx = { { { getHeapOffset ( 'ptr' , 'u16' ) } } } ;
328- #if TEXTDECODER
329309 var endIdx = findStringEnd ( HEAPU16 , idx , maxBytesToRead / 2 , ignoreNul ) ;
330310
331311#if TEXTDECODER != 2
332312 // When using conditional TextDecoder, skip it for short strings as the overhead of the native call is not worth it.
333313 if ( endIdx - idx > 16 && UTF16Decoder )
334314#endif // TEXTDECODER != 2
335315 return UTF16Decoder . decode ( { { { getUnsharedTextDecoderView ( 'HEAPU16' , 'idx' , 'endIdx' ) } } } ) ;
336- #else
337- var maxIdx = idx + maxBytesToRead / 2;
338- #endif // TEXTDECODER
339316
340317#if TEXTDECODER != 2
341318 // Fallback: decode without UTF16Decoder
@@ -344,25 +321,8 @@ addToLibrary({
344321 // If maxBytesToRead is not passed explicitly, it will be undefined, and the
345322 // for-loop's condition will always evaluate to true. The loop is then
346323 // terminated on the first null char.
347- for (
348- var i = idx;
349- #if TEXTDECODER
350- // If building with TextDecoder, we have already computed the string length
351- // above, so test loop end condition against that
352- i < endIdx;
353- #else
354- !(i >= maxIdx);
355- #endif
356- ++i
357- ) {
324+ for ( var i = idx ; i < endIdx ; ++ i ) {
358325 var codeUnit = HEAPU16 [ i ] ;
359- #if !TEXTDECODER
360- // If not building with TextDecoder enabled, we don't know the string
361- // length, so scan for \0 character .
362- // If building with TextDecoder, we know exactly at what index the
363- // string ends, so checking for nulls here would be redundant.
364- if ( ! codeUnit && ! ignoreNul ) break ;
365- #endif
366326 // fromCharCode constructs a character from a UTF-16 code unit, so we can
367327 // pass the UTF16 string right through.
368328 str += String . fromCharCode ( codeUnit ) ;
0 commit comments