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