| 31 | } |
| 32 | |
| 33 | std::wstring utf8string2WideString(const std::string& str) { |
| 34 | std::wstring w_ret_string; |
| 35 | for (unsigned int i = 0; i < str.size();) { |
| 36 | auto byte = static_cast<unsigned char>(str[i]); |
| 37 | if ((byte & 0x80U) == 0) { |
| 38 | // 1-byte character |
| 39 | w_ret_string.push_back(static_cast<wchar_t>(byte)); |
| 40 | ++i; |
| 41 | } else if ((byte & 0xE0U) == 0xC0) { |
| 42 | // 2-byte character |
| 43 | if (i + 1 < str.size()) { |
| 44 | wchar_t wc = (static_cast<wchar_t>(byte & 0x1FU) << 6U) | (static_cast<wchar_t>(str[i + 1] & 0x3FU)); |
| 45 | w_ret_string.push_back(wc); |
| 46 | i += 2; |
| 47 | } else { |
| 48 | break; |
| 49 | } |
| 50 | } else if ((byte & 0xF0U) == 0xE0U) { |
| 51 | // 3-byte character |
| 52 | if (i + 2 < str.size()) { |
| 53 | wchar_t wc = (static_cast<wchar_t>(byte & 0x0FU) << 12U) | (static_cast<wchar_t>(str[i + 1] & 0x3FU) << 6U) |
| 54 | | (static_cast<wchar_t>(str[i + 2] & 0x3FU)); |
| 55 | w_ret_string.push_back(wc); |
| 56 | i += 3; |
| 57 | } else { |
| 58 | break; |
| 59 | } |
| 60 | } else if ((byte & 0xF8U) == 0xF0U) { |
| 61 | // 4-byte character |
| 62 | if (i + 3 < str.size()) { |
| 63 | wchar_t wc = (static_cast<wchar_t>(byte & 0x07U) << 18U) | (static_cast<wchar_t>(str[i + 1] & 0x3FU) << 12U) |
| 64 | | (static_cast<wchar_t>(str[i + 2] & 0x3FU) << 6U) | (static_cast<wchar_t>(str[i + 3] & 0x3FU)); |
| 65 | w_ret_string.push_back(wc); |
| 66 | i += 4; |
| 67 | } else { |
| 68 | break; |
| 69 | } |
| 70 | } else { |
| 71 | // Invalid UTF-8 sequence |
| 72 | ++i; |
| 73 | } |
| 74 | } |
| 75 | return w_ret_string; |
| 76 | } |
| 77 | |
| 78 | void makeBytes2UnicodeMap(std::unordered_map<std::wint_t, wchar_t>& dict) { |
| 79 | std::vector<std::wint_t> bs((ord(L"~") - ord(L"!") + 1) + (ord(L"¬") - ord(L"¡") + 1) + (ord(L"ÿ") - ord(L"®") + 1)); |
no test coverage detected