MCPcopy Create free account
hub / github.com/Snapchat/Valdi / codePointToUTF8

Function codePointToUTF8

valdi_core/src/valdi_core/cpp/Utils/JSONReader.cpp:14–40  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

12namespace Valdi {
13
14static inline std::string codePointToUTF8(unsigned int cp) {
15 std::string result;
16
17 // based on description from http://en.wikipedia.org/wiki/UTF-8
18
19 if (cp <= 0x7f) {
20 result.resize(1);
21 result[0] = static_cast<char>(cp);
22 } else if (cp <= 0x7FF) {
23 result.resize(2);
24 result[1] = static_cast<char>(0x80 | (0x3f & cp));
25 result[0] = static_cast<char>(0xC0 | (0x1f & (cp >> 6)));
26 } else if (cp <= 0xFFFF) {
27 result.resize(3);
28 result[2] = static_cast<char>(0x80 | (0x3f & cp));
29 result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 6)));
30 result[0] = static_cast<char>(0xE0 | (0xf & (cp >> 12)));
31 } else if (cp <= 0x10FFFF) {
32 result.resize(4);
33 result[3] = static_cast<char>(0x80 | (0x3f & cp));
34 result[2] = static_cast<char>(0x80 | (0x3f & (cp >> 6)));
35 result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 12)));
36 result[0] = static_cast<char>(0xF0 | (0x7 & (cp >> 18)));
37 }
38
39 return result;
40}
41
42JSONReader::JSONReader(std::string_view input) : _parser(input) {}
43

Callers 1

decodeStringMethod · 0.85

Calls 1

resizeMethod · 0.45

Tested by

no test coverage detected