You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
if((u0&0xF8)!=0xF0)warnOnce('Invalid UTF-8 leading byte '+ptrToString(u0)+' encountered when deserializing a UTF-8 string in wasm memory to a JS string!');
// Parameter maxBytesToWrite is not optional. Negative values, 0, null,
161
+
// undefined and false each don't write out any bytes.
162
+
if(!(maxBytesToWrite>0))
163
+
return0;
164
+
165
+
varstartIdx=outIdx;
166
+
varendIdx=outIdx+maxBytesToWrite-1;// -1 for string null terminator.
167
+
for(vari=0;i<str.length;++i){
168
+
// Gotcha: charCodeAt returns a 16-bit word that is a UTF-16 encoded code
169
+
// unit, not a Unicode code point of the character! So decode
170
+
// UTF16->UTF32->UTF8.
171
+
// See http://unicode.org/faq/utf_bom.html#utf16-3
172
+
// For UTF8 byte structure, see http://en.wikipedia.org/wiki/UTF-8#Description
173
+
// and https://www.ietf.org/rfc/rfc2279.txt
174
+
// and https://tools.ietf.org/html/rfc3629
175
+
varu=str.charCodeAt(i);// possibly a lead surrogate
176
+
if(u>=0xD800&&u<=0xDFFF){
177
+
varu1=str.charCodeAt(++i);
178
+
u=0x10000+((u&0x3FF)<<10)|(u1&0x3FF);
179
+
}
180
+
if(u<=0x7F){
181
+
if(outIdx>=endIdx)break;
182
+
heap[outIdx++]=u;
183
+
}elseif(u<=0x7FF){
184
+
if(outIdx+1>=endIdx)break;
185
+
heap[outIdx++]=0xC0|(u>>6);
186
+
heap[outIdx++]=0x80|(u&63);
187
+
}elseif(u<=0xFFFF){
188
+
if(outIdx+2>=endIdx)break;
189
+
heap[outIdx++]=0xE0|(u>>12);
190
+
heap[outIdx++]=0x80|((u>>6)&63);
191
+
heap[outIdx++]=0x80|(u&63);
192
+
}else{
193
+
if(outIdx+3>=endIdx)break;
194
+
#if ASSERTIONS
195
+
if(u>0x10FFFF)warnOnce('Invalid Unicode code point '+ptrToString(u)+' encountered when serializing a JS string to a UTF-8 string in wasm memory! (Valid unicode code points should be in range 0-0x10FFFF).');
196
+
#endif
197
+
heap[outIdx++]=0xF0|(u>>18);
198
+
heap[outIdx++]=0x80|((u>>12)&63);
199
+
heap[outIdx++]=0x80|((u>>6)&63);
200
+
heap[outIdx++]=0x80|(u&63);
201
+
}
202
+
}
203
+
// Null-terminate the pointer to the buffer.
204
+
heap[outIdx]=0;
205
+
returnoutIdx-startIdx;
206
+
},
207
+
208
+
/**
209
+
* Copies the given Javascript String object 'str' to the emscripten HEAP at
210
+
* address 'outPtr', null-terminated and encoded in UTF8 form. The copy will
211
+
* require at most str.length*4+1 bytes of space in the HEAP.
212
+
* Use the function lengthBytesUTF8 to compute the exact number of bytes
213
+
* (excluding null terminator) that this function will write.
214
+
*
215
+
* @return {number} The number of bytes written, EXCLUDING the null terminator.
assert(typeofmaxBytesToWrite=='number','stringToUTF8(str, outPtr, maxBytesToWrite) is missing the third parameter that specifies the length of the output buffer!');
0 commit comments