| 187 | } |
| 188 | |
| 189 | static unsigned int utf8ToCodepoint(const char*& s, const char* e) { |
| 190 | const unsigned int REPLACEMENT_CHARACTER = 0xFFFD; |
| 191 | |
| 192 | unsigned int firstByte = static_cast<unsigned char>(*s); |
| 193 | |
| 194 | if (firstByte < 0x80) |
| 195 | return firstByte; |
| 196 | |
| 197 | if (firstByte < 0xE0) { |
| 198 | if (e - s < 2) |
| 199 | return REPLACEMENT_CHARACTER; |
| 200 | |
| 201 | unsigned int calculated = |
| 202 | ((firstByte & 0x1F) << 6) | (static_cast<unsigned int>(s[1]) & 0x3F); |
| 203 | s += 1; |
| 204 | // oversized encoded characters are invalid |
| 205 | return calculated < 0x80 ? REPLACEMENT_CHARACTER : calculated; |
| 206 | } |
| 207 | |
| 208 | if (firstByte < 0xF0) { |
| 209 | if (e - s < 3) |
| 210 | return REPLACEMENT_CHARACTER; |
| 211 | |
| 212 | unsigned int calculated = ((firstByte & 0x0F) << 12) | |
| 213 | ((static_cast<unsigned int>(s[1]) & 0x3F) << 6) | |
| 214 | (static_cast<unsigned int>(s[2]) & 0x3F); |
| 215 | s += 2; |
| 216 | // surrogates aren't valid codepoints itself |
| 217 | // shouldn't be UTF-8 encoded |
| 218 | if (calculated >= 0xD800 && calculated <= 0xDFFF) |
| 219 | return REPLACEMENT_CHARACTER; |
| 220 | // oversized encoded characters are invalid |
| 221 | return calculated < 0x800 ? REPLACEMENT_CHARACTER : calculated; |
| 222 | } |
| 223 | |
| 224 | if (firstByte < 0xF8) { |
| 225 | if (e - s < 4) |
| 226 | return REPLACEMENT_CHARACTER; |
| 227 | |
| 228 | unsigned int calculated = ((firstByte & 0x07) << 18) | |
| 229 | ((static_cast<unsigned int>(s[1]) & 0x3F) << 12) | |
| 230 | ((static_cast<unsigned int>(s[2]) & 0x3F) << 6) | |
| 231 | (static_cast<unsigned int>(s[3]) & 0x3F); |
| 232 | s += 3; |
| 233 | // oversized encoded characters are invalid |
| 234 | return calculated < 0x10000 ? REPLACEMENT_CHARACTER : calculated; |
| 235 | } |
| 236 | |
| 237 | return REPLACEMENT_CHARACTER; |
| 238 | } |
| 239 | |
| 240 | static const char hex2[] = "000102030405060708090a0b0c0d0e0f" |
| 241 | "101112131415161718191a1b1c1d1e1f" |
no outgoing calls
no test coverage detected
searching dependent graphs…