Skip to content

Commit bd5fd33

Browse files
authored
Cleanup comments in src/runtime_strings.js. NFC (#18039)
1 parent 03ccf3a commit bd5fd33

File tree

1 file changed

+77
-43
lines changed

1 file changed

+77
-43
lines changed

src/runtime_strings.js

Lines changed: 77 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@
44
* SPDX-License-Identifier: MIT
55
*/
66

7-
// runtime_strings.js: Strings related runtime functions that are part of both MINIMAL_RUNTIME and regular runtime.
7+
// runtime_strings.js: String related runtime functions that are part of both
8+
// MINIMAL_RUNTIME and regular runtime.
89

910
#if TEXTDECODER == 2
1011
var UTF8Decoder = new TextDecoder('utf8');
1112
#elif TEXTDECODER == 1
1213
var UTF8Decoder = typeof TextDecoder != 'undefined' ? new TextDecoder('utf8') : undefined;
1314
#endif
1415

15-
// Given a pointer 'ptr' to a null-terminated UTF8-encoded string in the given array that contains uint8 values, returns
16-
// a copy of that string as a Javascript String object.
1716
/**
17+
* Given a pointer 'idx' to a null-terminated UTF8-encoded string in the given
18+
* array that contains uint8 values, returns a copy of that string as a
19+
* Javascript String object.
1820
* heapOrArray is either a regular array, or a JavaScript typed array view.
1921
* @param {number} idx
2022
* @param {number=} maxBytesToRead
@@ -27,9 +29,11 @@ function UTF8ArrayToString(heapOrArray, idx, maxBytesToRead) {
2729
var endIdx = idx + maxBytesToRead;
2830
#if TEXTDECODER
2931
var endPtr = idx;
30-
// TextDecoder needs to know the byte length in advance, it doesn't stop on null terminator by itself.
31-
// Also, use the length info to avoid running tiny strings through TextDecoder, since .subarray() allocates garbage.
32-
// (As a tiny code save trick, compare endPtr against endIdx using a negation, so that undefined means Infinity)
32+
// TextDecoder needs to know the byte length in advance, it doesn't stop on
33+
// null terminator by itself. Also, use the length info to avoid running tiny
34+
// strings through TextDecoder, since .subarray() allocates garbage.
35+
// (As a tiny code save trick, compare endPtr against endIdx using a negation,
36+
// so that undefined means Infinity)
3337
while (heapOrArray[endPtr] && !(endPtr >= endIdx)) ++endPtr;
3438
#endif // TEXTDECODER
3539

@@ -43,7 +47,8 @@ function UTF8ArrayToString(heapOrArray, idx, maxBytesToRead) {
4347
#endif // TEXTDECODER
4448
var str = '';
4549
#if TEXTDECODER
46-
// If building with TextDecoder, we have already computed the string length above, so test loop end condition against that
50+
// If building with TextDecoder, we have already computed the string length
51+
// above, so test loop end condition against that
4752
while (idx < endPtr) {
4853
#else
4954
while (!(idx >= endIdx)) {
@@ -54,8 +59,10 @@ function UTF8ArrayToString(heapOrArray, idx, maxBytesToRead) {
5459
// https://tools.ietf.org/html/rfc3629
5560
var u0 = heapOrArray[idx++];
5661
#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.
62+
// If not building with TextDecoder enabled, we don't know the string
63+
// length, so scan for \0 byte.
64+
// If building with TextDecoder, we know exactly at what byte index the
65+
// string ends, so checking for nulls here would be redundant.
5966
if (!u0) return str;
6067
#endif
6168
if (!(u0 & 0x80)) { str += String.fromCharCode(u0); continue; }
@@ -82,19 +89,19 @@ function UTF8ArrayToString(heapOrArray, idx, maxBytesToRead) {
8289
#endif // TEXTDECODER == 2
8390
}
8491

85-
// Given a pointer 'ptr' to a null-terminated UTF8-encoded string in the emscripten HEAP, returns a
86-
// copy of that string as a Javascript String object.
87-
// maxBytesToRead: an optional length that specifies the maximum number of bytes to read. You can omit
88-
// this parameter to scan the string until the first \0 byte. If maxBytesToRead is
89-
// passed, and the string at [ptr, ptr+maxBytesToReadr[ contains a null byte in the
90-
// middle, then the string will cut short at that byte index (i.e. maxBytesToRead will
91-
// not produce a string of exact length [ptr, ptr+maxBytesToRead[)
92-
// N.B. mixing frequent uses of UTF8ToString() with and without maxBytesToRead may
93-
// throw JS JIT optimizations off, so it is worth to consider consistently using one
94-
// style or the other.
9592
/**
93+
* Given a pointer 'ptr' to a null-terminated UTF8-encoded string in the
94+
* emscripten HEAP, returns a copy of that string as a Javascript String object.
95+
*
9696
* @param {number} ptr
97-
* @param {number=} maxBytesToRead
97+
* @param {number=} maxBytesToRead - An optional length that specifies the
98+
* maximum number of bytes to read. You can omit this parameter to scan the
99+
* string until the first \0 byte. If maxBytesToRead is passed, and the string
100+
* at [ptr, ptr+maxBytesToReadr[ contains a null byte in the middle, then the
101+
* string will cut short at that byte index (i.e. maxBytesToRead will not
102+
* produce a string of exact length [ptr, ptr+maxBytesToRead[) N.B. mixing
103+
* frequent uses of UTF8ToString() with and without maxBytesToRead may throw
104+
* JS JIT optimizations off, so it is worth to consider consistently using one
98105
* @return {string}
99106
*/
100107
function UTF8ToString(ptr, maxBytesToRead) {
@@ -111,32 +118,47 @@ function UTF8ToString(ptr, maxBytesToRead) {
111118
#endif
112119
}
113120

114-
// Copies the given Javascript String object 'str' to the given byte array at address 'outIdx',
115-
// encoded in UTF8 form and null-terminated. The copy will require at most str.length*4+1 bytes of space in the HEAP.
116-
// Use the function lengthBytesUTF8 to compute the exact number of bytes (excluding null terminator) that this function will write.
117-
// Parameters:
118-
// str: the Javascript string to copy.
119-
// heap: the array to copy to. Each index in this array is assumed to be one 8-byte element.
120-
// outIdx: The starting offset in the array to begin the copying.
121-
// maxBytesToWrite: The maximum number of bytes this function can write to the array.
122-
// This count should include the null terminator,
123-
// i.e. if maxBytesToWrite=1, only the null terminator will be written and nothing else.
124-
// maxBytesToWrite=0 does not write any bytes to the output, not even the null terminator.
125-
// Returns the number of bytes written, EXCLUDING the null terminator.
126-
121+
/**
122+
* Copies the given Javascript String object 'str' to the given byte array at
123+
* address 'outIdx', encoded in UTF8 form and null-terminated. The copy will
124+
* require at most str.length*4+1 bytes of space in the HEAP. Use the function
125+
* lengthBytesUTF8 to compute the exact number of bytes (excluding null
126+
* terminator) that this function will write.
127+
*
128+
* @param {string} str - The Javascript string to copy.
129+
* @param {ArrayBufferView|Array<number>} heap - The array to copy to. Each
130+
* index in this array is assumed
131+
* to be one 8-byte element.
132+
* @param {number} outIdx - The starting offset in the array to begin the copying.
133+
* @param {number} maxBytesToWrite - The maximum number of bytes this function
134+
* can write to the array. This count should
135+
* include the null terminator, i.e. if
136+
* maxBytesToWrite=1, only the null terminator
137+
* will be written and nothing else.
138+
* maxBytesToWrite=0 does not write any bytes
139+
* to the output, not even the null
140+
* terminator.
141+
* @return {number} The number of bytes written, EXCLUDING the null terminator.
142+
*/
127143
function stringToUTF8Array(str, heap, outIdx, maxBytesToWrite) {
128144
#if CAN_ADDRESS_2GB
129145
outIdx >>>= 0;
130146
#endif
131-
if (!(maxBytesToWrite > 0)) // Parameter maxBytesToWrite is not optional. Negative values, 0, null, undefined and false each don't write out any bytes.
147+
// Parameter maxBytesToWrite is not optional. Negative values, 0, null,
148+
// undefined and false each don't write out any bytes.
149+
if (!(maxBytesToWrite > 0))
132150
return 0;
133151

134152
var startIdx = outIdx;
135153
var endIdx = outIdx + maxBytesToWrite - 1; // -1 for string null terminator.
136154
for (var i = 0; i < str.length; ++i) {
137-
// Gotcha: charCodeAt returns a 16-bit word that is a UTF-16 encoded code unit, not a Unicode code point of the character! So decode UTF16->UTF32->UTF8.
155+
// Gotcha: charCodeAt returns a 16-bit word that is a UTF-16 encoded code
156+
// unit, not a Unicode code point of the character! So decode
157+
// UTF16->UTF32->UTF8.
138158
// See http://unicode.org/faq/utf_bom.html#utf16-3
139-
// For UTF8 byte structure, see http://en.wikipedia.org/wiki/UTF-8#Description and https://www.ietf.org/rfc/rfc2279.txt and https://tools.ietf.org/html/rfc3629
159+
// For UTF8 byte structure, see http://en.wikipedia.org/wiki/UTF-8#Description
160+
// and https://www.ietf.org/rfc/rfc2279.txt
161+
// and https://tools.ietf.org/html/rfc3629
140162
var u = str.charCodeAt(i); // possibly a lead surrogate
141163
if (u >= 0xD800 && u <= 0xDFFF) {
142164
var u1 = str.charCodeAt(++i);
@@ -170,23 +192,35 @@ function stringToUTF8Array(str, heap, outIdx, maxBytesToWrite) {
170192
return outIdx - startIdx;
171193
}
172194

173-
// Copies the given Javascript String object 'str' to the emscripten HEAP at address 'outPtr',
174-
// null-terminated and encoded in UTF8 form. The copy will require at most str.length*4+1 bytes of space in the HEAP.
175-
// Use the function lengthBytesUTF8 to compute the exact number of bytes (excluding null terminator) that this function will write.
176-
// Returns the number of bytes written, EXCLUDING the null terminator.
177-
195+
/**
196+
* Copies the given Javascript String object 'str' to the emscripten HEAP at
197+
* address 'outPtr', null-terminated and encoded in UTF8 form. The copy will
198+
* require at most str.length*4+1 bytes of space in the HEAP.
199+
* Use the function lengthBytesUTF8 to compute the exact number of bytes
200+
* (excluding null terminator) that this function will write.
201+
*
202+
* @return {number} The number of bytes written, EXCLUDING the null terminator.
203+
*/
178204
function stringToUTF8(str, outPtr, maxBytesToWrite) {
179205
#if ASSERTIONS
180206
assert(typeof maxBytesToWrite == 'number', 'stringToUTF8(str, outPtr, maxBytesToWrite) is missing the third parameter that specifies the length of the output buffer!');
181207
#endif
182208
return stringToUTF8Array(str, {{{ heapAndOffset('HEAPU8', 'outPtr') }}}, maxBytesToWrite);
183209
}
184210

185-
// Returns the number of bytes the given Javascript string takes if encoded as a UTF8 byte array, EXCLUDING the null terminator byte.
211+
/**
212+
* Returns the number of bytes the given Javascript string takes if encoded as a
213+
* UTF8 byte array, EXCLUDING the null terminator byte.
214+
*
215+
* @param {string} str - JavaScript string to operator on
216+
* @return {number} Length, in bytes, of the UTF8 encoded string.
217+
*/
186218
function lengthBytesUTF8(str) {
187219
var len = 0;
188220
for (var i = 0; i < str.length; ++i) {
189-
// Gotcha: charCodeAt returns a 16-bit word that is a UTF-16 encoded code unit, not a Unicode code point of the character! So decode UTF16->UTF32->UTF8.
221+
// Gotcha: charCodeAt returns a 16-bit word that is a UTF-16 encoded code
222+
// unit, not a Unicode code point of the character! So decode
223+
// UTF16->UTF32->UTF8.
190224
// See http://unicode.org/faq/utf_bom.html#utf16-3
191225
var c = str.charCodeAt(i); // possibly a lead surrogate
192226
if (c <= 0x7F) {

0 commit comments

Comments
 (0)