| 153 | } |
| 154 | |
| 155 | int DecodeUtf8Codepoint(const char* text, uint32_t* outCodepoint) |
| 156 | { |
| 157 | if (outCodepoint) |
| 158 | *outCodepoint = 0xFFFD; |
| 159 | if (!text || !*text) |
| 160 | return 0; |
| 161 | |
| 162 | const unsigned char lead = static_cast<unsigned char>(text[0]); |
| 163 | if (lead < 0x80) |
| 164 | { |
| 165 | if (outCodepoint) |
| 166 | *outCodepoint = lead; |
| 167 | return 1; |
| 168 | } |
| 169 | |
| 170 | const int required = Utf8SequenceLength(lead); |
| 171 | const int bytes = Utf8CodepointBytes(text); |
| 172 | if (bytes <= 0) |
| 173 | return 0; |
| 174 | if (required == 1 || bytes < required) |
| 175 | return bytes; |
| 176 | |
| 177 | switch (required) |
| 178 | { |
| 179 | case 2: |
| 180 | if (outCodepoint) |
| 181 | *outCodepoint = ((lead & 0x1F) << 6) | (static_cast<unsigned char>(text[1]) & 0x3F); |
| 182 | return 2; |
| 183 | case 3: |
| 184 | if (outCodepoint) |
| 185 | { |
| 186 | *outCodepoint = ((lead & 0x0F) << 12) | ((static_cast<unsigned char>(text[1]) & 0x3F) << 6) | |
| 187 | (static_cast<unsigned char>(text[2]) & 0x3F); |
| 188 | } |
| 189 | return 3; |
| 190 | case 4: |
| 191 | if (outCodepoint) |
| 192 | { |
| 193 | *outCodepoint = ((lead & 0x07) << 18) | ((static_cast<unsigned char>(text[1]) & 0x3F) << 12) | |
| 194 | ((static_cast<unsigned char>(text[2]) & 0x3F) << 6) | |
| 195 | (static_cast<unsigned char>(text[3]) & 0x3F); |
| 196 | } |
| 197 | return 4; |
| 198 | default: |
| 199 | return 1; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | int CountUtf8Codepoints(const char* text) |
| 204 | { |