4
4
* SPDX-License-Identifier: MIT
5
5
*/
6
6
7
+ #if TEXTDECODER != 1 && TEXTDECODER != 2
8
+ #error "TEXTDECODER must be either 1 or 2"
9
+ #endif
10
+
7
11
addToLibrary ( {
8
12
// TextDecoder constructor defaults to UTF-8
9
13
#if TEXTDECODER == 2
10
14
$UTF8Decoder : "new TextDecoder()" ,
11
- #elif TEXTDECODER == 1
15
+ #else
12
16
$UTF8Decoder : "typeof TextDecoder != 'undefined' ? new TextDecoder() : undefined" ,
13
17
#endif
14
18
@@ -35,49 +39,28 @@ addToLibrary({
35
39
* @param {boolean=} ignoreNul - If true, the function will not stop on a NUL character.
36
40
* @return {string}
37
41
*/` ,
38
- #if TEXTDECODER
39
42
$UTF8ArrayToString__deps : [ '$UTF8Decoder' , '$findStringEnd' ] ,
40
- #endif
41
43
$UTF8ArrayToString : ( heapOrArray , idx = 0 , maxBytesToRead , ignoreNul ) => {
42
44
#if CAN_ADDRESS_2GB
43
45
idx >>>= 0 ;
44
46
#endif
45
47
46
- #if TEXTDECODER
47
48
var endPtr = findStringEnd ( heapOrArray , idx , maxBytesToRead , ignoreNul ) ;
48
- #else
49
- var endIdx = idx + maxBytesToRead ;
50
- #endif
51
49
52
50
#if TEXTDECODER == 2
53
51
return UTF8Decoder . decode ( heapOrArray . buffer ? { { { getUnsharedTextDecoderView ( 'heapOrArray' , 'idx' , 'endPtr' ) } } } : new Uint8Array ( heapOrArray . slice ( idx , endPtr ) ) ) ;
54
52
#else // TEXTDECODER == 2
55
- #if TEXTDECODER
56
53
// When using conditional TextDecoder, skip it for short strings as the overhead of the native call is not worth it.
57
54
if ( endPtr - idx > 16 && heapOrArray . buffer && UTF8Decoder ) {
58
55
return UTF8Decoder . decode ( { { { getUnsharedTextDecoderView ( 'heapOrArray' , 'idx' , 'endPtr' ) } } } ) ;
59
56
}
60
- #endif // TEXTDECODER
61
57
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
65
58
while ( idx < endPtr ) {
66
- #else
67
- while ( ! ( idx >= endIdx ) ) {
68
- #endif
69
59
// For UTF8 byte structure, see:
70
60
// http://en.wikipedia.org/wiki/UTF-8#Description
71
61
// https://www.ietf.org/rfc/rfc2279.txt
72
62
// https://tools.ietf.org/html/rfc3629
73
63
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
81
64
if ( ! ( u0 & 0x80 ) ) { str += String . fromCharCode ( u0 ) ; continue ; }
82
65
var u1 = heapOrArray [ idx ++ ] & 63 ;
83
66
if ( ( u0 & 0xE0 ) == 0xC0 ) { str += String . fromCharCode ( ( ( u0 & 31 ) << 6 ) | u1 ) ; continue ; }
@@ -310,32 +293,26 @@ addToLibrary({
310
293
311
294
#if TEXTDECODER == 2
312
295
$UTF16Decoder: "new TextDecoder('utf-16le');" ,
313
- #elif TEXTDECODER == 1
296
+ #else
314
297
$UTF16Decoder : "typeof TextDecoder != 'undefined' ? new TextDecoder('utf-16le') : undefined;" ,
315
298
#endif
316
299
317
300
// Given a pointer 'ptr' to a null-terminated UTF16LE-encoded string in the
318
301
// emscripten HEAP, returns a copy of that string as a Javascript String
319
302
// object.
320
- #if TEXTDECODER
321
303
$UTF16ToString__deps : [ '$UTF16Decoder' , '$findStringEnd' ] ,
322
- #endif
323
304
$UTF16ToString : ( ptr , maxBytesToRead , ignoreNul ) = > {
324
305
#if ASSERTIONS
325
306
assert ( ptr % 2 == 0 , 'Pointer passed to UTF16ToString must be aligned to two bytes!' ) ;
326
307
#endif
327
308
var idx = { { { getHeapOffset ( 'ptr' , 'u16' ) } } } ;
328
- #if TEXTDECODER
329
309
var endIdx = findStringEnd ( HEAPU16 , idx , maxBytesToRead / 2 , ignoreNul ) ;
330
310
331
311
#if TEXTDECODER != 2
332
312
// When using conditional TextDecoder, skip it for short strings as the overhead of the native call is not worth it.
333
313
if ( endIdx - idx > 16 && UTF16Decoder )
334
314
#endif // TEXTDECODER != 2
335
315
return UTF16Decoder . decode ( { { { getUnsharedTextDecoderView ( 'HEAPU16' , 'idx' , 'endIdx' ) } } } ) ;
336
- #else
337
- var maxIdx = idx + maxBytesToRead / 2;
338
- #endif // TEXTDECODER
339
316
340
317
#if TEXTDECODER != 2
341
318
// Fallback: decode without UTF16Decoder
@@ -344,25 +321,8 @@ addToLibrary({
344
321
// If maxBytesToRead is not passed explicitly, it will be undefined, and the
345
322
// for-loop's condition will always evaluate to true. The loop is then
346
323
// 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 ) {
358
325
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
366
326
// fromCharCode constructs a character from a UTF-16 code unit, so we can
367
327
// pass the UTF16 string right through.
368
328
str += String . fromCharCode ( codeUnit ) ;
0 commit comments