MCPcopy Create free account
hub / github.com/cinder/Cinder / codePointToUTF8

Function codePointToUTF8

src/jsoncpp/jsoncpp.cpp:100–126  ·  view source on GitHub ↗

Converts a unicode code-point to UTF-8.

Source from the content-addressed store, hash-verified

98
99/// Converts a unicode code-point to UTF-8.
100static inline std::string codePointToUTF8(unsigned int cp) {
101 std::string result;
102
103 // based on description from http://en.wikipedia.org/wiki/UTF-8
104
105 if (cp <= 0x7f) {
106 result.resize(1);
107 result[0] = static_cast<char>(cp);
108 } else if (cp <= 0x7FF) {
109 result.resize(2);
110 result[1] = static_cast<char>(0x80 | (0x3f & cp));
111 result[0] = static_cast<char>(0xC0 | (0x1f & (cp >> 6)));
112 } else if (cp <= 0xFFFF) {
113 result.resize(3);
114 result[2] = static_cast<char>(0x80 | (0x3f & cp));
115 result[1] = 0x80 | static_cast<char>((0x3f & (cp >> 6)));
116 result[0] = 0xE0 | static_cast<char>((0xf & (cp >> 12)));
117 } else if (cp <= 0x10FFFF) {
118 result.resize(4);
119 result[3] = static_cast<char>(0x80 | (0x3f & cp));
120 result[2] = static_cast<char>(0x80 | (0x3f & (cp >> 6)));
121 result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 12)));
122 result[0] = static_cast<char>(0xF0 | (0x7 & (cp >> 18)));
123 }
124
125 return result;
126}
127
128/// Returns true if ch is a control character (in range [0,32[).
129static inline bool isControlCharacter(char ch) { return ch > 0 && ch <= 0x1F; }

Callers 1

decodeStringMethod · 0.85

Calls 1

resizeMethod · 0.45

Tested by

no test coverage detected