| 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 | { |
no test coverage detected