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

Function stringToUTF8Array

js/sql-debug.js:805–855  ·  view source on GitHub ↗
(str, outU8Array, outIdx, maxBytesToWrite)

Source from the content-addressed store, hash-verified

803// Returns the number of bytes written, EXCLUDING the null terminator.
804
805function stringToUTF8Array(str, outU8Array, outIdx, maxBytesToWrite) {
806 if (!(maxBytesToWrite > 0)) // Parameter maxBytesToWrite is not optional. Negative values, 0, null, undefined and false each don't write out any bytes.
807 return 0;
808
809 var startIdx = outIdx;
810 var endIdx = outIdx + maxBytesToWrite - 1; // -1 for string null terminator.
811 for (var i = 0; i < str.length; ++i) {
812 // 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.
813 // See http://unicode.org/faq/utf_bom.html#utf16-3
814 // 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
815 var u = str.charCodeAt(i); // possibly a lead surrogate
816 if (u >= 0xD800 && u <= 0xDFFF) u = 0x10000 + ((u & 0x3FF) << 10) | (str.charCodeAt(++i) & 0x3FF);
817 if (u <= 0x7F) {
818 if (outIdx >= endIdx) break;
819 outU8Array[outIdx++] = u;
820 } else if (u <= 0x7FF) {
821 if (outIdx + 1 >= endIdx) break;
822 outU8Array[outIdx++] = 0xC0 | (u >> 6);
823 outU8Array[outIdx++] = 0x80 | (u & 63);
824 } else if (u <= 0xFFFF) {
825 if (outIdx + 2 >= endIdx) break;
826 outU8Array[outIdx++] = 0xE0 | (u >> 12);
827 outU8Array[outIdx++] = 0x80 | ((u >> 6) & 63);
828 outU8Array[outIdx++] = 0x80 | (u & 63);
829 } else if (u <= 0x1FFFFF) {
830 if (outIdx + 3 >= endIdx) break;
831 outU8Array[outIdx++] = 0xF0 | (u >> 18);
832 outU8Array[outIdx++] = 0x80 | ((u >> 12) & 63);
833 outU8Array[outIdx++] = 0x80 | ((u >> 6) & 63);
834 outU8Array[outIdx++] = 0x80 | (u & 63);
835 } else if (u <= 0x3FFFFFF) {
836 if (outIdx + 4 >= endIdx) break;
837 outU8Array[outIdx++] = 0xF8 | (u >> 24);
838 outU8Array[outIdx++] = 0x80 | ((u >> 18) & 63);
839 outU8Array[outIdx++] = 0x80 | ((u >> 12) & 63);
840 outU8Array[outIdx++] = 0x80 | ((u >> 6) & 63);
841 outU8Array[outIdx++] = 0x80 | (u & 63);
842 } else {
843 if (outIdx + 5 >= endIdx) break;
844 outU8Array[outIdx++] = 0xFC | (u >> 30);
845 outU8Array[outIdx++] = 0x80 | ((u >> 24) & 63);
846 outU8Array[outIdx++] = 0x80 | ((u >> 18) & 63);
847 outU8Array[outIdx++] = 0x80 | ((u >> 12) & 63);
848 outU8Array[outIdx++] = 0x80 | ((u >> 6) & 63);
849 outU8Array[outIdx++] = 0x80 | (u & 63);
850 }
851 }
852 // Null-terminate the pointer to the buffer.
853 outU8Array[outIdx] = 0;
854 return outIdx - startIdx;
855}
856
857
858// Copies the given Javascript String object 'str' to the emscripten HEAP at address 'outPtr',

Callers 3

stringToUTF8Function · 0.70
sql-debug.jsFile · 0.70
intArrayFromStringFunction · 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…