MCPcopy Create free account
hub / github.com/ThatOpen/engine_web-ifc / utf16_from_utf8

Function utf16_from_utf8

src/cpp/web-ifc/parsing/string_parsing.cpp:17–50  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

15
16
17 std::u16string utf16_from_utf8(const std::string& utf8) {
18 std::u16string utf16;
19 size_t i = 0;
20
21 while (i < utf8.size()) {
22 char16_t ch = 0;
23 unsigned char byte = utf8[i];
24
25 if (byte < 0x80) {
26 // 1-byte character (ASCII)
27 ch = byte;
28 i += 1;
29 }
30 else if ((byte & 0xE0) == 0xC0) {
31 // 2-byte character
32 if (i + 1 >= utf8.size()) throw std::runtime_error("Invalid UTF-8 sequence");
33 ch = ((byte & 0x1F) << 6) | (utf8[i + 1] & 0x3F);
34 i += 2;
35 }
36 else if ((byte & 0xF0) == 0xE0) {
37 // 3-byte character
38 if (i + 2 >= utf8.size()) throw std::runtime_error("Invalid UTF-8 sequence");
39 ch = ((byte & 0x0F) << 12) | ((utf8[i + 1] & 0x3F) << 6) | (utf8[i + 2] & 0x3F);
40 i += 3;
41 }
42 else {
43 throw std::runtime_error("Unsupported UTF-8 sequence");
44 }
45
46 utf16.push_back(ch);
47 }
48
49 return utf16;
50 }
51
52 void encodeCharacters(std::ostringstream &stream,std::string &data)
53 {

Callers 1

encodeCharactersFunction · 0.85

Calls 1

sizeMethod · 0.80

Tested by

no test coverage detected