MCPcopy Create free account
hub / github.com/Samsung/UTopia / codePointToUTF8

Function codePointToUTF8

external/jsoncpp/jsoncpp.cpp:119–145  ·  view source on GitHub ↗

Converts a unicode code-point to UTF-8.

Source from the content-addressed store, hash-verified

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

Callers 1

decodeStringMethod · 0.85

Calls 1

resizeMethod · 0.80

Tested by

no test coverage detected