Converts a unicode code-point to UTF-8.
| 120 | |
| 121 | /// Converts a unicode code-point to UTF-8. |
| 122 | static inline JSONCPP_STRING codePointToUTF8(unsigned int cp) { |
| 123 | JSONCPP_STRING result; |
| 124 | |
| 125 | // based on description from http://en.wikipedia.org/wiki/UTF-8 |
| 126 | |
| 127 | if (cp <= 0x7f) { |
| 128 | result.resize(1); |
| 129 | result[0] = static_cast<char>(cp); |
| 130 | } else if (cp <= 0x7FF) { |
| 131 | result.resize(2); |
| 132 | result[1] = static_cast<char>(0x80 | (0x3f & cp)); |
| 133 | result[0] = static_cast<char>(0xC0 | (0x1f & (cp >> 6))); |
| 134 | } else if (cp <= 0xFFFF) { |
| 135 | result.resize(3); |
| 136 | result[2] = static_cast<char>(0x80 | (0x3f & cp)); |
| 137 | result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 6))); |
| 138 | result[0] = static_cast<char>(0xE0 | (0xf & (cp >> 12))); |
| 139 | } else if (cp <= 0x10FFFF) { |
| 140 | result.resize(4); |
| 141 | result[3] = static_cast<char>(0x80 | (0x3f & cp)); |
| 142 | result[2] = static_cast<char>(0x80 | (0x3f & (cp >> 6))); |
| 143 | result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 12))); |
| 144 | result[0] = static_cast<char>(0xF0 | (0x7 & (cp >> 18))); |
| 145 | } |
| 146 | |
| 147 | return result; |
| 148 | } |
| 149 | |
| 150 | enum { |
| 151 | /// Constant that specify the size of the buffer that must be passed to |