MCPcopy Create free account
hub / github.com/ValveSoftware/Proton / codePointToUTF8

Function codePointToUTF8

vrclient_x64/jsoncpp.cpp:103–129  ·  view source on GitHub ↗

Converts a unicode code-point to UTF-8.

Source from the content-addressed store, hash-verified

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

Callers 1

decodeStringMethod · 0.85

Calls 1

resizeMethod · 0.80

Tested by

no test coverage detected