| 2024 | } |
| 2025 | |
| 2026 | inline size_t to_utf8(int code, char *buff) { |
| 2027 | if (code < 0x0080) { |
| 2028 | buff[0] = (code & 0x7F); |
| 2029 | return 1; |
| 2030 | } else if (code < 0x0800) { |
| 2031 | buff[0] = static_cast<char>(0xC0 | ((code >> 6) & 0x1F)); |
| 2032 | buff[1] = static_cast<char>(0x80 | (code & 0x3F)); |
| 2033 | return 2; |
| 2034 | } else if (code < 0xD800) { |
| 2035 | buff[0] = static_cast<char>(0xE0 | ((code >> 12) & 0xF)); |
| 2036 | buff[1] = static_cast<char>(0x80 | ((code >> 6) & 0x3F)); |
| 2037 | buff[2] = static_cast<char>(0x80 | (code & 0x3F)); |
| 2038 | return 3; |
| 2039 | } else if (code < 0xE000) { // D800 - DFFF is invalid... |
| 2040 | return 0; |
| 2041 | } else if (code < 0x10000) { |
| 2042 | buff[0] = static_cast<char>(0xE0 | ((code >> 12) & 0xF)); |
| 2043 | buff[1] = static_cast<char>(0x80 | ((code >> 6) & 0x3F)); |
| 2044 | buff[2] = static_cast<char>(0x80 | (code & 0x3F)); |
| 2045 | return 3; |
| 2046 | } else if (code < 0x110000) { |
| 2047 | buff[0] = static_cast<char>(0xF0 | ((code >> 18) & 0x7)); |
| 2048 | buff[1] = static_cast<char>(0x80 | ((code >> 12) & 0x3F)); |
| 2049 | buff[2] = static_cast<char>(0x80 | ((code >> 6) & 0x3F)); |
| 2050 | buff[3] = static_cast<char>(0x80 | (code & 0x3F)); |
| 2051 | return 4; |
| 2052 | } |
| 2053 | |
| 2054 | // NOTREACHED |
| 2055 | return 0; |
| 2056 | } |
| 2057 | |
| 2058 | // NOTE: This code came up with the following stackoverflow post: |
| 2059 | // https://stackoverflow.com/questions/180947/base64-decode-snippet-in-c |