! @brief get codepoint from 4 hex characters following `\u` For input "\u c1 c2 c3 c4" the codepoint is: (c1 * 0x1000) + (c2 * 0x0100) + (c3 * 0x0010) + c4 = (c1 << 12) + (c2 << 8) + (c3 << 4) + (c4 << 0) Furthermore, the possible characters '0'..'9', 'A'..'F', and 'a'..'f' must be converted to the integers 0x0..0x9, 0xA..0xF, 0xA..0xF, resp. The conversion is done
| 7472 | non-hex character) |
| 7473 | */ |
| 7474 | int get_codepoint() |
| 7475 | { |
| 7476 | // this function only makes sense after reading `\u` |
| 7477 | JSON_ASSERT(current == 'u'); |
| 7478 | int codepoint = 0; |
| 7479 | |
| 7480 | const auto factors = { 12u, 8u, 4u, 0u }; |
| 7481 | for (const auto factor : factors) |
| 7482 | { |
| 7483 | get(); |
| 7484 | |
| 7485 | if (current >= '0' && current <= '9') |
| 7486 | { |
| 7487 | codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x30u) << factor); |
| 7488 | } |
| 7489 | else if (current >= 'A' && current <= 'F') |
| 7490 | { |
| 7491 | codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x37u) << factor); |
| 7492 | } |
| 7493 | else if (current >= 'a' && current <= 'f') |
| 7494 | { |
| 7495 | codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x57u) << factor); |
| 7496 | } |
| 7497 | else |
| 7498 | { |
| 7499 | return -1; |
| 7500 | } |
| 7501 | } |
| 7502 | |
| 7503 | JSON_ASSERT(0x0000 <= codepoint && codepoint <= 0xFFFF); |
| 7504 | return codepoint; |
| 7505 | } |
| 7506 | |
| 7507 | /*! |
| 7508 | @brief check if the next byte(s) are inside a given range |