(string, units)
| 113972 | return str; |
| 113973 | } |
| 113974 | function utf8ToBytes(string, units) { |
| 113975 | units = units || Infinity; |
| 113976 | var codePoint; |
| 113977 | var length = string.length; |
| 113978 | var leadSurrogate = null; |
| 113979 | var bytes = []; |
| 113980 | for(var i = 0; i < length; ++i){ |
| 113981 | codePoint = string.charCodeAt(i); |
| 113982 | // is surrogate component |
| 113983 | if (codePoint > 0xD7FF && codePoint < 0xE000) { |
| 113984 | // last char was a lead |
| 113985 | if (!leadSurrogate) { |
| 113986 | // no lead yet |
| 113987 | if (codePoint > 0xDBFF) { |
| 113988 | // unexpected trail |
| 113989 | if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD); |
| 113990 | continue; |
| 113991 | } else if (i + 1 === length) { |
| 113992 | // unpaired lead |
| 113993 | if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD); |
| 113994 | continue; |
| 113995 | } |
| 113996 | // valid lead |
| 113997 | leadSurrogate = codePoint; |
| 113998 | continue; |
| 113999 | } |
| 114000 | // 2 leads in a row |
| 114001 | if (codePoint < 0xDC00) { |
| 114002 | if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD); |
| 114003 | leadSurrogate = codePoint; |
| 114004 | continue; |
| 114005 | } |
| 114006 | // valid surrogate pair |
| 114007 | codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000; |
| 114008 | } else if (leadSurrogate) // valid bmp char, but last char was a lead |
| 114009 | { |
| 114010 | if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD); |
| 114011 | } |
| 114012 | leadSurrogate = null; |
| 114013 | // encode utf8 |
| 114014 | if (codePoint < 0x80) { |
| 114015 | if ((units -= 1) < 0) break; |
| 114016 | bytes.push(codePoint); |
| 114017 | } else if (codePoint < 0x800) { |
| 114018 | if ((units -= 2) < 0) break; |
| 114019 | bytes.push(codePoint >> 0x6 | 0xC0, codePoint & 0x3F | 0x80); |
| 114020 | } else if (codePoint < 0x10000) { |
| 114021 | if ((units -= 3) < 0) break; |
| 114022 | bytes.push(codePoint >> 0xC | 0xE0, codePoint >> 0x6 & 0x3F | 0x80, codePoint & 0x3F | 0x80); |
| 114023 | } else if (codePoint < 0x110000) { |
| 114024 | if ((units -= 4) < 0) break; |
| 114025 | bytes.push(codePoint >> 0x12 | 0xF0, codePoint >> 0xC & 0x3F | 0x80, codePoint >> 0x6 & 0x3F | 0x80, codePoint & 0x3F | 0x80); |
| 114026 | } else throw new Error("Invalid code point"); |
| 114027 | } |
| 114028 | return bytes; |
| 114029 | } |
| 114030 | function asciiToBytes(str) { |
| 114031 | var byteArray = []; |
no outgoing calls
no test coverage detected