(string, units)
| 11358 | } |
| 11359 | |
| 11360 | function utf8ToBytes (string, units) { |
| 11361 | units = units || Infinity |
| 11362 | var codePoint |
| 11363 | var length = string.length |
| 11364 | var leadSurrogate = null |
| 11365 | var bytes = [] |
| 11366 | |
| 11367 | for (var i = 0; i < length; ++i) { |
| 11368 | codePoint = string.charCodeAt(i) |
| 11369 | |
| 11370 | // is surrogate component |
| 11371 | if (codePoint > 0xD7FF && codePoint < 0xE000) { |
| 11372 | // last char was a lead |
| 11373 | if (!leadSurrogate) { |
| 11374 | // no lead yet |
| 11375 | if (codePoint > 0xDBFF) { |
| 11376 | // unexpected trail |
| 11377 | if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) |
| 11378 | continue |
| 11379 | } else if (i + 1 === length) { |
| 11380 | // unpaired lead |
| 11381 | if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) |
| 11382 | continue |
| 11383 | } |
| 11384 | |
| 11385 | // valid lead |
| 11386 | leadSurrogate = codePoint |
| 11387 | |
| 11388 | continue |
| 11389 | } |
| 11390 | |
| 11391 | // 2 leads in a row |
| 11392 | if (codePoint < 0xDC00) { |
| 11393 | if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) |
| 11394 | leadSurrogate = codePoint |
| 11395 | continue |
| 11396 | } |
| 11397 | |
| 11398 | // valid surrogate pair |
| 11399 | codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000 |
| 11400 | } else if (leadSurrogate) { |
| 11401 | // valid bmp char, but last char was a lead |
| 11402 | if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) |
| 11403 | } |
| 11404 | |
| 11405 | leadSurrogate = null |
| 11406 | |
| 11407 | // encode utf8 |
| 11408 | if (codePoint < 0x80) { |
| 11409 | if ((units -= 1) < 0) break |
| 11410 | bytes.push(codePoint) |
| 11411 | } else if (codePoint < 0x800) { |
| 11412 | if ((units -= 2) < 0) break |
| 11413 | bytes.push( |
| 11414 | codePoint >> 0x6 | 0xC0, |
| 11415 | codePoint & 0x3F | 0x80 |
| 11416 | ) |
| 11417 | } else if (codePoint < 0x10000) { |
no outgoing calls
no test coverage detected