| 116 | } |
| 117 | |
| 118 | size_t EncodeUTF8(char32 c, char *output) { |
| 119 | if (c <= 0x7F) { |
| 120 | *output = static_cast<char>(c); |
| 121 | return 1; |
| 122 | } |
| 123 | |
| 124 | if (c <= 0x7FF) { |
| 125 | output[1] = 0x80 | (c & 0x3F); |
| 126 | c >>= 6; |
| 127 | output[0] = 0xC0 | c; |
| 128 | return 2; |
| 129 | } |
| 130 | |
| 131 | // if `c` is out-of-range, convert it to REPLACEMENT CHARACTER (U+FFFD). |
| 132 | // This treatment is the same as the original runetochar. |
| 133 | if (c > 0x10FFFF) c = kUnicodeError; |
| 134 | |
| 135 | if (c <= 0xFFFF) { |
| 136 | output[2] = 0x80 | (c & 0x3F); |
| 137 | c >>= 6; |
| 138 | output[1] = 0x80 | (c & 0x3F); |
| 139 | c >>= 6; |
| 140 | output[0] = 0xE0 | c; |
| 141 | return 3; |
| 142 | } |
| 143 | |
| 144 | output[3] = 0x80 | (c & 0x3F); |
| 145 | c >>= 6; |
| 146 | output[2] = 0x80 | (c & 0x3F); |
| 147 | c >>= 6; |
| 148 | output[1] = 0x80 | (c & 0x3F); |
| 149 | c >>= 6; |
| 150 | output[0] = 0xF0 | c; |
| 151 | |
| 152 | return 4; |
| 153 | } |
| 154 | |
| 155 | std::string UnicodeCharToUTF8(const char32 c) { return UnicodeTextToUTF8({c}); } |
| 156 |
no outgoing calls