(str, heap, outIdx, maxBytesToWrite)
| 1281 | } |
| 1282 | |
| 1283 | function stringToUTF8Array(str, heap, outIdx, maxBytesToWrite) { |
| 1284 | assert(typeof str === 'string'); |
| 1285 | // Parameter maxBytesToWrite is not optional. Negative values, 0, null, |
| 1286 | // undefined and false each don't write out any bytes. |
| 1287 | if (!(maxBytesToWrite > 0)) |
| 1288 | return 0; |
| 1289 | |
| 1290 | var startIdx = outIdx; |
| 1291 | var endIdx = outIdx + maxBytesToWrite - 1; // -1 for string null terminator. |
| 1292 | for (var i = 0; i < str.length; ++i) { |
| 1293 | // Gotcha: charCodeAt returns a 16-bit word that is a UTF-16 encoded code |
| 1294 | // unit, not a Unicode code point of the character! So decode |
| 1295 | // UTF16->UTF32->UTF8. |
| 1296 | // See http://unicode.org/faq/utf_bom.html#utf16-3 |
| 1297 | // For UTF8 byte structure, see http://en.wikipedia.org/wiki/UTF-8#Description |
| 1298 | // and https://www.ietf.org/rfc/rfc2279.txt |
| 1299 | // and https://tools.ietf.org/html/rfc3629 |
| 1300 | var u = str.charCodeAt(i); // possibly a lead surrogate |
| 1301 | if (u >= 0xD800 && u <= 0xDFFF) { |
| 1302 | var u1 = str.charCodeAt(++i); |
| 1303 | u = 0x10000 + ((u & 0x3FF) << 10) | (u1 & 0x3FF); |
| 1304 | } |
| 1305 | if (u <= 0x7F) { |
| 1306 | if (outIdx >= endIdx) break; |
| 1307 | heap[outIdx++] = u; |
| 1308 | } else if (u <= 0x7FF) { |
| 1309 | if (outIdx + 1 >= endIdx) break; |
| 1310 | heap[outIdx++] = 0xC0 | (u >> 6); |
| 1311 | heap[outIdx++] = 0x80 | (u & 63); |
| 1312 | } else if (u <= 0xFFFF) { |
| 1313 | if (outIdx + 2 >= endIdx) break; |
| 1314 | heap[outIdx++] = 0xE0 | (u >> 12); |
| 1315 | heap[outIdx++] = 0x80 | ((u >> 6) & 63); |
| 1316 | heap[outIdx++] = 0x80 | (u & 63); |
| 1317 | } else { |
| 1318 | if (outIdx + 3 >= endIdx) break; |
| 1319 | if (u > 0x10FFFF) warnOnce('Invalid Unicode code point ' + ptrToString(u) + ' encountered when serializing a JS string to a UTF-8 string in wasm memory! (Valid unicode code points should be in range 0-0x10FFFF).'); |
| 1320 | heap[outIdx++] = 0xF0 | (u >> 18); |
| 1321 | heap[outIdx++] = 0x80 | ((u >> 12) & 63); |
| 1322 | heap[outIdx++] = 0x80 | ((u >> 6) & 63); |
| 1323 | heap[outIdx++] = 0x80 | (u & 63); |
| 1324 | } |
| 1325 | } |
| 1326 | // Null-terminate the pointer to the buffer. |
| 1327 | heap[outIdx] = 0; |
| 1328 | return outIdx - startIdx; |
| 1329 | } |
| 1330 | /** @type {function(string, boolean=, number=)} */ |
| 1331 | function intArrayFromString(stringy, dontAddNull, length) { |
| 1332 | var len = length > 0 ? length : lengthBytesUTF8(stringy)+1; |
no test coverage detected