| 12 | namespace Valdi { |
| 13 | |
| 14 | static inline std::string codePointToUTF8(unsigned int cp) { |
| 15 | std::string result; |
| 16 | |
| 17 | // based on description from http://en.wikipedia.org/wiki/UTF-8 |
| 18 | |
| 19 | if (cp <= 0x7f) { |
| 20 | result.resize(1); |
| 21 | result[0] = static_cast<char>(cp); |
| 22 | } else if (cp <= 0x7FF) { |
| 23 | result.resize(2); |
| 24 | result[1] = static_cast<char>(0x80 | (0x3f & cp)); |
| 25 | result[0] = static_cast<char>(0xC0 | (0x1f & (cp >> 6))); |
| 26 | } else if (cp <= 0xFFFF) { |
| 27 | result.resize(3); |
| 28 | result[2] = static_cast<char>(0x80 | (0x3f & cp)); |
| 29 | result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 6))); |
| 30 | result[0] = static_cast<char>(0xE0 | (0xf & (cp >> 12))); |
| 31 | } else if (cp <= 0x10FFFF) { |
| 32 | result.resize(4); |
| 33 | result[3] = static_cast<char>(0x80 | (0x3f & cp)); |
| 34 | result[2] = static_cast<char>(0x80 | (0x3f & (cp >> 6))); |
| 35 | result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 12))); |
| 36 | result[0] = static_cast<char>(0xF0 | (0x7 & (cp >> 18))); |
| 37 | } |
| 38 | |
| 39 | return result; |
| 40 | } |
| 41 | |
| 42 | JSONReader::JSONReader(std::string_view input) : _parser(input) {} |
| 43 | |