Skip to content

Commit d2e80a6

Browse files
authored
Simplify UTF8ArrayToString. NFC (#17449)
Simplify code by avoiding "else after return".
1 parent 0fe3e31 commit d2e80a6

4 files changed

+31
-34
lines changed

src/runtime_strings.js

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -39,48 +39,45 @@ function UTF8ArrayToString(heapOrArray, idx, maxBytesToRead) {
3939
#if TEXTDECODER
4040
if (endPtr - idx > 16 && heapOrArray.buffer && UTF8Decoder) {
4141
return UTF8Decoder.decode({{{ getUnsharedTextDecoderView('heapOrArray', 'idx', 'endPtr') }}});
42-
} else {
42+
}
4343
#endif // TEXTDECODER
44-
var str = '';
44+
var str = '';
4545
#if TEXTDECODER
46-
// If building with TextDecoder, we have already computed the string length above, so test loop end condition against that
47-
while (idx < endPtr) {
46+
// If building with TextDecoder, we have already computed the string length above, so test loop end condition against that
47+
while (idx < endPtr) {
4848
#else
49-
while (!(idx >= endIdx)) {
49+
while (!(idx >= endIdx)) {
5050
#endif
51-
// For UTF8 byte structure, see:
52-
// http://en.wikipedia.org/wiki/UTF-8#Description
53-
// https://www.ietf.org/rfc/rfc2279.txt
54-
// https://tools.ietf.org/html/rfc3629
55-
var u0 = heapOrArray[idx++];
51+
// For UTF8 byte structure, see:
52+
// http://en.wikipedia.org/wiki/UTF-8#Description
53+
// https://www.ietf.org/rfc/rfc2279.txt
54+
// https://tools.ietf.org/html/rfc3629
55+
var u0 = heapOrArray[idx++];
5656
#if !TEXTDECODER
57-
// If not building with TextDecoder enabled, we don't know the string length, so scan for \0 byte.
58-
// If building with TextDecoder, we know exactly at what byte index the string ends, so checking for nulls here would be redundant.
59-
if (!u0) return str;
57+
// If not building with TextDecoder enabled, we don't know the string length, so scan for \0 byte.
58+
// If building with TextDecoder, we know exactly at what byte index the string ends, so checking for nulls here would be redundant.
59+
if (!u0) return str;
6060
#endif
61-
if (!(u0 & 0x80)) { str += String.fromCharCode(u0); continue; }
62-
var u1 = heapOrArray[idx++] & 63;
63-
if ((u0 & 0xE0) == 0xC0) { str += String.fromCharCode(((u0 & 31) << 6) | u1); continue; }
64-
var u2 = heapOrArray[idx++] & 63;
65-
if ((u0 & 0xF0) == 0xE0) {
66-
u0 = ((u0 & 15) << 12) | (u1 << 6) | u2;
67-
} else {
61+
if (!(u0 & 0x80)) { str += String.fromCharCode(u0); continue; }
62+
var u1 = heapOrArray[idx++] & 63;
63+
if ((u0 & 0xE0) == 0xC0) { str += String.fromCharCode(((u0 & 31) << 6) | u1); continue; }
64+
var u2 = heapOrArray[idx++] & 63;
65+
if ((u0 & 0xF0) == 0xE0) {
66+
u0 = ((u0 & 15) << 12) | (u1 << 6) | u2;
67+
} else {
6868
#if ASSERTIONS
69-
if ((u0 & 0xF8) != 0xF0) warnOnce('Invalid UTF-8 leading byte 0x' + u0.toString(16) + ' encountered when deserializing a UTF-8 string in wasm memory to a JS string!');
69+
if ((u0 & 0xF8) != 0xF0) warnOnce('Invalid UTF-8 leading byte 0x' + u0.toString(16) + ' encountered when deserializing a UTF-8 string in wasm memory to a JS string!');
7070
#endif
71-
u0 = ((u0 & 7) << 18) | (u1 << 12) | (u2 << 6) | (heapOrArray[idx++] & 63);
72-
}
71+
u0 = ((u0 & 7) << 18) | (u1 << 12) | (u2 << 6) | (heapOrArray[idx++] & 63);
72+
}
7373

74-
if (u0 < 0x10000) {
75-
str += String.fromCharCode(u0);
76-
} else {
77-
var ch = u0 - 0x10000;
78-
str += String.fromCharCode(0xD800 | (ch >> 10), 0xDC00 | (ch & 0x3FF));
79-
}
74+
if (u0 < 0x10000) {
75+
str += String.fromCharCode(u0);
76+
} else {
77+
var ch = u0 - 0x10000;
78+
str += String.fromCharCode(0xD800 | (ch >> 10), 0xDC00 | (ch & 0x3FF));
8079
}
81-
#if TEXTDECODER
8280
}
83-
#endif // TEXTDECODER
8481
return str;
8582
#endif // TEXTDECODER == 2
8683
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
95246
1+
95185
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
56695
1+
56636
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
76534
1+
76473

0 commit comments

Comments
 (0)