MCPcopy Create free account
hub / github.com/WolfireGames/overgrowth / codePointToUTF8

Function codePointToUTF8

Source/JSON/jsoncpp.cpp:106–132  ·  view source on GitHub ↗

Converts a unicode code-point to UTF-8.

Source from the content-addressed store, hash-verified

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

Callers 1

decodeStringMethod · 0.70

Calls 1

resizeMethod · 0.45

Tested by

no test coverage detected