MCPcopy Create free account
hub / github.com/Illumina/hap.py / codePointToUTF8

Function codePointToUTF8

external/jsoncpp/jsoncpp.cpp:99–125  ·  view source on GitHub ↗

Converts a unicode code-point to UTF-8.

Source from the content-addressed store, hash-verified

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