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

Function lengthBytesUTF8

js/sql-debug.js:870–892  ·  view source on GitHub ↗
(str)

Source from the content-addressed store, hash-verified

868// Returns the number of bytes the given Javascript string takes if encoded as a UTF8 byte array, EXCLUDING the null terminator byte.
869
870function lengthBytesUTF8(str) {
871 var len = 0;
872 for (var i = 0; i < str.length; ++i) {
873 // 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.
874 // See http://unicode.org/faq/utf_bom.html#utf16-3
875 var u = str.charCodeAt(i); // possibly a lead surrogate
876 if (u >= 0xD800 && u <= 0xDFFF) u = 0x10000 + ((u & 0x3FF) << 10) | (str.charCodeAt(++i) & 0x3FF);
877 if (u <= 0x7F) {
878 ++len;
879 } else if (u <= 0x7FF) {
880 len += 2;
881 } else if (u <= 0xFFFF) {
882 len += 3;
883 } else if (u <= 0x1FFFFF) {
884 len += 4;
885 } else if (u <= 0x3FFFFFF) {
886 len += 5;
887 } else {
888 len += 6;
889 }
890 }
891 return len;
892}
893
894
895// Given a pointer 'ptr' to a null-terminated UTF16LE-encoded string in the emscripten HEAP, returns

Callers 4

writeStringToMemoryFunction · 0.70
sql-debug.jsFile · 0.70
___syscall183Function · 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…