(string, units)
| 1843 | } |
| 1844 | |
| 1845 | function utf8ToBytes(string, units) { |
| 1846 | units = units || Infinity |
| 1847 | var codePoint |
| 1848 | var length = string.length |
| 1849 | var leadSurrogate = null |
| 1850 | var bytes = [] |
| 1851 | var i = 0 |
| 1852 | |
| 1853 | for (; i < length; i++) { |
| 1854 | codePoint = string.charCodeAt(i) |
| 1855 | |
| 1856 | // is surrogate component |
| 1857 | if (codePoint > 0xd7ff && codePoint < 0xe000) { |
| 1858 | // last char was a lead |
| 1859 | if (leadSurrogate) { |
| 1860 | // 2 leads in a row |
| 1861 | if (codePoint < 0xdc00) { |
| 1862 | if ((units -= 3) > -1) bytes.push(0xef, 0xbf, 0xbd) |
| 1863 | leadSurrogate = codePoint |
| 1864 | continue |
| 1865 | } else { |
| 1866 | // valid surrogate pair |
| 1867 | codePoint = |
| 1868 | ((leadSurrogate - 0xd800) << 10) | |
| 1869 | (codePoint - 0xdc00) | |
| 1870 | 0x10000 |
| 1871 | leadSurrogate = null |
| 1872 | } |
| 1873 | } else { |
| 1874 | // no lead yet |
| 1875 | |
| 1876 | if (codePoint > 0xdbff) { |
| 1877 | // unexpected trail |
| 1878 | if ((units -= 3) > -1) bytes.push(0xef, 0xbf, 0xbd) |
| 1879 | continue |
| 1880 | } else if (i + 1 === length) { |
| 1881 | // unpaired lead |
| 1882 | if ((units -= 3) > -1) bytes.push(0xef, 0xbf, 0xbd) |
| 1883 | continue |
| 1884 | } else { |
| 1885 | // valid lead |
| 1886 | leadSurrogate = codePoint |
| 1887 | continue |
| 1888 | } |
| 1889 | } |
| 1890 | } else if (leadSurrogate) { |
| 1891 | // valid bmp char, but last char was a lead |
| 1892 | if ((units -= 3) > -1) bytes.push(0xef, 0xbf, 0xbd) |
| 1893 | leadSurrogate = null |
| 1894 | } |
| 1895 | |
| 1896 | // encode utf8 |
| 1897 | if (codePoint < 0x80) { |
| 1898 | if ((units -= 1) < 0) break |
| 1899 | bytes.push(codePoint) |
| 1900 | } else if (codePoint < 0x800) { |
| 1901 | if ((units -= 2) < 0) break |
| 1902 | bytes.push((codePoint >> 0x6) | 0xc0, (codePoint & 0x3f) | 0x80) |
no outgoing calls
no test coverage detected
searching dependent graphs…