MCPcopy Create free account
hub / github.com/GoogleChrome/samples / stringToUTF8Array

Function stringToUTF8Array

sqlite-wasm-opfs/jswasm/sqlite3.js:415–453  ·  view source on GitHub ↗
(str, heap, outIdx, maxBytesToWrite)

Source from the content-addressed store, hash-verified

413
414
415function stringToUTF8Array(str, heap, outIdx, maxBytesToWrite) {
416 if (!(maxBytesToWrite > 0))
417 return 0;
418
419 var startIdx = outIdx;
420 var endIdx = outIdx + maxBytesToWrite - 1;
421 for (var i = 0; i < str.length; ++i) {
422
423
424
425 var u = str.charCodeAt(i);
426 if (u >= 0xD800 && u <= 0xDFFF) {
427 var u1 = str.charCodeAt(++i);
428 u = 0x10000 + ((u & 0x3FF) << 10) | (u1 & 0x3FF);
429 }
430 if (u <= 0x7F) {
431 if (outIdx >= endIdx) break;
432 heap[outIdx++] = u;
433 } else if (u <= 0x7FF) {
434 if (outIdx + 1 >= endIdx) break;
435 heap[outIdx++] = 0xC0 | (u >> 6);
436 heap[outIdx++] = 0x80 | (u & 63);
437 } else if (u <= 0xFFFF) {
438 if (outIdx + 2 >= endIdx) break;
439 heap[outIdx++] = 0xE0 | (u >> 12);
440 heap[outIdx++] = 0x80 | ((u >> 6) & 63);
441 heap[outIdx++] = 0x80 | (u & 63);
442 } else {
443 if (outIdx + 3 >= endIdx) break;
444 heap[outIdx++] = 0xF0 | (u >> 18);
445 heap[outIdx++] = 0x80 | ((u >> 12) & 63);
446 heap[outIdx++] = 0x80 | ((u >> 6) & 63);
447 heap[outIdx++] = 0x80 | (u & 63);
448 }
449 }
450
451 heap[outIdx] = 0;
452 return outIdx - startIdx;
453}
454
455
456

Callers 5

stringToUTF8Function · 0.70
intArrayFromStringFunction · 0.70
sqlite3.jsFile · 0.70
allocateUTF8Function · 0.70
allocateUTF8OnStackFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected