| 1902 | } |
| 1903 | |
| 1904 | inline size_t to_utf8(int code, char *buff) { |
| 1905 | if (code < 0x0080) { |
| 1906 | buff[0] = (code & 0x7F); |
| 1907 | return 1; |
| 1908 | } else if (code < 0x0800) { |
| 1909 | buff[0] = static_cast<char>(0xC0 | ((code >> 6) & 0x1F)); |
| 1910 | buff[1] = static_cast<char>(0x80 | (code & 0x3F)); |
| 1911 | return 2; |
| 1912 | } else if (code < 0xD800) { |
| 1913 | buff[0] = static_cast<char>(0xE0 | ((code >> 12) & 0xF)); |
| 1914 | buff[1] = static_cast<char>(0x80 | ((code >> 6) & 0x3F)); |
| 1915 | buff[2] = static_cast<char>(0x80 | (code & 0x3F)); |
| 1916 | return 3; |
| 1917 | } else if (code < 0xE000) { // D800 - DFFF is invalid... |
| 1918 | return 0; |
| 1919 | } else if (code < 0x10000) { |
| 1920 | buff[0] = static_cast<char>(0xE0 | ((code >> 12) & 0xF)); |
| 1921 | buff[1] = static_cast<char>(0x80 | ((code >> 6) & 0x3F)); |
| 1922 | buff[2] = static_cast<char>(0x80 | (code & 0x3F)); |
| 1923 | return 3; |
| 1924 | } else if (code < 0x110000) { |
| 1925 | buff[0] = static_cast<char>(0xF0 | ((code >> 18) & 0x7)); |
| 1926 | buff[1] = static_cast<char>(0x80 | ((code >> 12) & 0x3F)); |
| 1927 | buff[2] = static_cast<char>(0x80 | ((code >> 6) & 0x3F)); |
| 1928 | buff[3] = static_cast<char>(0x80 | (code & 0x3F)); |
| 1929 | return 4; |
| 1930 | } |
| 1931 | |
| 1932 | // NOTREACHED |
| 1933 | return 0; |
| 1934 | } |
| 1935 | |
| 1936 | // NOTE: This code came up with the following stackoverflow post: |
| 1937 | // https://stackoverflow.com/questions/180947/base64-decode-snippet-in-c |