(string, units)
| 4262 | } |
| 4263 | |
| 4264 | function utf8ToBytes (string, units) { |
| 4265 | units = units || Infinity |
| 4266 | var codePoint |
| 4267 | var length = string.length |
| 4268 | var leadSurrogate = null |
| 4269 | var bytes = [] |
| 4270 | |
| 4271 | for (var i = 0; i < length; ++i) { |
| 4272 | codePoint = string.charCodeAt(i) |
| 4273 | |
| 4274 | // is surrogate component |
| 4275 | if (codePoint > 0xD7FF && codePoint < 0xE000) { |
| 4276 | // last char was a lead |
| 4277 | if (!leadSurrogate) { |
| 4278 | // no lead yet |
| 4279 | if (codePoint > 0xDBFF) { |
| 4280 | // unexpected trail |
| 4281 | if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) |
| 4282 | continue |
| 4283 | } else if (i + 1 === length) { |
| 4284 | // unpaired lead |
| 4285 | if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) |
| 4286 | continue |
| 4287 | } |
| 4288 | |
| 4289 | // valid lead |
| 4290 | leadSurrogate = codePoint |
| 4291 | |
| 4292 | continue |
| 4293 | } |
| 4294 | |
| 4295 | // 2 leads in a row |
| 4296 | if (codePoint < 0xDC00) { |
| 4297 | if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) |
| 4298 | leadSurrogate = codePoint |
| 4299 | continue |
| 4300 | } |
| 4301 | |
| 4302 | // valid surrogate pair |
| 4303 | codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000 |
| 4304 | } else if (leadSurrogate) { |
| 4305 | // valid bmp char, but last char was a lead |
| 4306 | if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) |
| 4307 | } |
| 4308 | |
| 4309 | leadSurrogate = null |
| 4310 | |
| 4311 | // encode utf8 |
| 4312 | if (codePoint < 0x80) { |
| 4313 | if ((units -= 1) < 0) break |
| 4314 | bytes.push(codePoint) |
| 4315 | } else if (codePoint < 0x800) { |
| 4316 | if ((units -= 2) < 0) break |
| 4317 | bytes.push( |
| 4318 | codePoint >> 0x6 | 0xC0, |
| 4319 | codePoint & 0x3F | 0x80 |
| 4320 | ) |
| 4321 | } else if (codePoint < 0x10000) { |
no outgoing calls
no test coverage detected
searching dependent graphs…