Converts a unicode code-point to UTF-8.
| 37 | |
| 38 | /// Converts a unicode code-point to UTF-8. |
| 39 | static inline String codePointToUTF8(unsigned int cp) { |
| 40 | String result; |
| 41 | |
| 42 | // based on description from http://en.wikipedia.org/wiki/UTF-8 |
| 43 | |
| 44 | if (cp <= 0x7f) { |
| 45 | result.resize(1); |
| 46 | result[0] = static_cast<char>(cp); |
| 47 | } else if (cp <= 0x7FF) { |
| 48 | result.resize(2); |
| 49 | result[1] = static_cast<char>(0x80 | (0x3f & cp)); |
| 50 | result[0] = static_cast<char>(0xC0 | (0x1f & (cp >> 6))); |
| 51 | } else if (cp <= 0xFFFF) { |
| 52 | result.resize(3); |
| 53 | result[2] = static_cast<char>(0x80 | (0x3f & cp)); |
| 54 | result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 6))); |
| 55 | result[0] = static_cast<char>(0xE0 | (0xf & (cp >> 12))); |
| 56 | } else if (cp <= 0x10FFFF) { |
| 57 | result.resize(4); |
| 58 | result[3] = static_cast<char>(0x80 | (0x3f & cp)); |
| 59 | result[2] = static_cast<char>(0x80 | (0x3f & (cp >> 6))); |
| 60 | result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 12))); |
| 61 | result[0] = static_cast<char>(0xF0 | (0x7 & (cp >> 18))); |
| 62 | } |
| 63 | |
| 64 | return result; |
| 65 | } |
| 66 | |
| 67 | enum { |
| 68 | /// Constant that specify the size of the buffer that must be passed to |
no test coverage detected
searching dependent graphs…