MCPcopy Create free account
hub / github.com/SOUI2/soui / codePointToUTF8

Function codePointToUTF8

third-part/jsoncpp/src/lib_json/json_tool.h:36–62  ·  view source on GitHub ↗

Converts a unicode code-point to UTF-8.

Source from the content-addressed store, hash-verified

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

Callers 1

decodeStringMethod · 0.85

Calls 1

resizeMethod · 0.45

Tested by

no test coverage detected