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

Function lengthBytesUTF8

mkbitmap/mkbitmap.js:1262–1281  ·  view source on GitHub ↗
(str)

Source from the content-addressed store, hash-verified

1260
1261
1262 function lengthBytesUTF8(str) {
1263 var len = 0;
1264 for (var i = 0; i < str.length; ++i) {
1265 // Gotcha: charCodeAt returns a 16-bit word that is a UTF-16 encoded code
1266 // unit, not a Unicode code point of the character! So decode
1267 // UTF16->UTF32->UTF8.
1268 // See http://unicode.org/faq/utf_bom.html#utf16-3
1269 var c = str.charCodeAt(i); // possibly a lead surrogate
1270 if (c <= 0x7F) {
1271 len++;
1272 } else if (c <= 0x7FF) {
1273 len += 2;
1274 } else if (c >= 0xD800 && c <= 0xDFFF) {
1275 len += 4; ++i;
1276 } else {
1277 len += 3;
1278 }
1279 }
1280 return len;
1281 }
1282
1283 function stringToUTF8Array(str, heap, outIdx, maxBytesToWrite) {
1284 assert(typeof str === 'string');

Callers 2

intArrayFromStringFunction · 0.70
mkbitmap.jsFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected