MCPcopy Index your code
hub / github.com/sql-js/sql.js / UTF8ArrayToString

Function UTF8ArrayToString

js/sql-debug.js:738–782  ·  view source on GitHub ↗
(u8Array, idx)

Source from the content-addressed store, hash-verified

736
737var UTF8Decoder = typeof TextDecoder !== 'undefined' ? new TextDecoder('utf8') : undefined;
738function UTF8ArrayToString(u8Array, idx) {
739 var endPtr = idx;
740 // TextDecoder needs to know the byte length in advance, it doesn't stop on null terminator by itself.
741 // Also, use the length info to avoid running tiny strings through TextDecoder, since .subarray() allocates garbage.
742 while (u8Array[endPtr]) ++endPtr;
743
744 if (endPtr - idx > 16 && u8Array.subarray && UTF8Decoder) {
745 return UTF8Decoder.decode(u8Array.subarray(idx, endPtr));
746 } else {
747 var u0, u1, u2, u3, u4, u5;
748
749 var str = '';
750 while (1) {
751 // 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
752 u0 = u8Array[idx++];
753 if (!u0) return str;
754 if (!(u0 & 0x80)) { str += String.fromCharCode(u0); continue; }
755 u1 = u8Array[idx++] & 63;
756 if ((u0 & 0xE0) == 0xC0) { str += String.fromCharCode(((u0 & 31) << 6) | u1); continue; }
757 u2 = u8Array[idx++] & 63;
758 if ((u0 & 0xF0) == 0xE0) {
759 u0 = ((u0 & 15) << 12) | (u1 << 6) | u2;
760 } else {
761 u3 = u8Array[idx++] & 63;
762 if ((u0 & 0xF8) == 0xF0) {
763 u0 = ((u0 & 7) << 18) | (u1 << 12) | (u2 << 6) | u3;
764 } else {
765 u4 = u8Array[idx++] & 63;
766 if ((u0 & 0xFC) == 0xF8) {
767 u0 = ((u0 & 3) << 24) | (u1 << 18) | (u2 << 12) | (u3 << 6) | u4;
768 } else {
769 u5 = u8Array[idx++] & 63;
770 u0 = ((u0 & 1) << 30) | (u1 << 24) | (u2 << 18) | (u3 << 12) | (u4 << 6) | u5;
771 }
772 }
773 }
774 if (u0 < 0x10000) {
775 str += String.fromCharCode(u0);
776 } else {
777 var ch = u0 - 0x10000;
778 str += String.fromCharCode(0xD800 | (ch >> 10), 0xDC00 | (ch & 0x3FF));
779 }
780 }
781 }
782}
783
784
785// Given a pointer 'ptr' to a null-terminated UTF8-encoded string in the emscripten HEAP, returns

Callers 2

UTF8ToStringFunction · 0.70
sql-debug.jsFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…