MCPcopy Create free account
hub / github.com/ZLMediaKit/pymkui / stringToUTF8Array

Function stringToUTF8Array

frontend/js/lib/decoder.js:302–335  ·  view source on GitHub ↗
(str, heap, outIdx, maxBytesToWrite)

Source from the content-addressed store, hash-verified

300 return ptr ? UTF8ArrayToString(HEAPU8, ptr, maxBytesToRead) : "";
301 }
302 function stringToUTF8Array(str, heap, outIdx, maxBytesToWrite) {
303 if (!(maxBytesToWrite > 0)) return 0;
304 var startIdx = outIdx;
305 var endIdx = outIdx + maxBytesToWrite - 1;
306 for (var i = 0; i < str.length; ++i) {
307 var u = str.charCodeAt(i);
308 if (u >= 55296 && u <= 57343) {
309 var u1 = str.charCodeAt(++i);
310 u = 65536 + ((u & 1023) << 10) | u1 & 1023;
311 }
312 if (u <= 127) {
313 if (outIdx >= endIdx) break;
314 heap[outIdx++] = u;
315 } else if (u <= 2047) {
316 if (outIdx + 1 >= endIdx) break;
317 heap[outIdx++] = 192 | u >> 6;
318 heap[outIdx++] = 128 | u & 63;
319 } else if (u <= 65535) {
320 if (outIdx + 2 >= endIdx) break;
321 heap[outIdx++] = 224 | u >> 12;
322 heap[outIdx++] = 128 | u >> 6 & 63;
323 heap[outIdx++] = 128 | u & 63;
324 } else {
325 if (outIdx + 3 >= endIdx) break;
326 if (u > 1114111) warnOnce("Invalid Unicode code point 0x" + u.toString(16) + " encountered when serializing a JS string to a UTF-8 string in wasm memory! (Valid unicode code points should be in range 0-0x10FFFF).");
327 heap[outIdx++] = 240 | u >> 18;
328 heap[outIdx++] = 128 | u >> 12 & 63;
329 heap[outIdx++] = 128 | u >> 6 & 63;
330 heap[outIdx++] = 128 | u & 63;
331 }
332 }
333 heap[outIdx] = 0;
334 return outIdx - startIdx;
335 }
336 function stringToUTF8(str, outPtr, maxBytesToWrite) {
337 assert(typeof maxBytesToWrite == "number", "stringToUTF8(str, outPtr, maxBytesToWrite) is missing the third parameter that specifies the length of the output buffer!");
338 return stringToUTF8Array(str, HEAPU8, outPtr, maxBytesToWrite);

Callers 4

stringToUTF8Function · 0.85
allocateUTF8Function · 0.85
decoder.jsFile · 0.85
intArrayFromStringFunction · 0.85

Calls 1

warnOnceFunction · 0.85

Tested by

no test coverage detected