diff --git a/src/libipc/platform/to_tchar.h b/src/libipc/platform/to_tchar.h index d9a1fb6..3cc2840 100755 --- a/src/libipc/platform/to_tchar.h +++ b/src/libipc/platform/to_tchar.h @@ -52,51 +52,21 @@ auto to_tchar(ipc::string &&external) -> IsSameChar { if (external.empty()) { return {}; // noconv } -#if 0 // backup - auto &fcodecvt = std::use_facet>(std::locale()); - std::mbstate_t mb {}; - ipc::wstring internal(external.size(), '\0'); - char const *first = &external[0], *last = &external[external.size()]; - std::size_t len = 0; - while (first != last) { - wchar_t *start = &internal[len], *end = &internal[internal.size()], *to_next; - auto ret = fcodecvt.in(mb, first, last, first, - start, end , to_next); - switch (ret) { - // no conversion, just copy code values - case std::codecvt_base::noconv: - internal.resize(len); - for (; first != last; ++first) { - internal.push_back((wchar_t)(unsigned char)*first); - } - break; - // conversion completed - case std::codecvt_base::ok: - len += static_cast(to_next - start); - internal.resize(len); - break; - // not enough space in the output buffer or unexpected end of source buffer - case std::codecvt_base::partial: - if (to_next <= start) { - throw std::range_error{"[to_tchar] bad conversion"}; - } - len += static_cast(to_next - start); - internal.resize(internal.size() + external.size()); - break; - // encountered a character that could not be converted - default: // error - throw std::range_error{"[to_tchar] bad conversion"}; - } - } -#else - // CP_ACP: The system default Windows ANSI code page. - int size_needed = ::MultiByteToWideChar(CP_ACP, 0, &external[0], (int)external.size(), NULL, 0); + /** + * CP_ACP : The system default Windows ANSI code page. + * CP_MACCP : The current system Macintosh code page. + * CP_OEMCP : The current system OEM code page. + * CP_SYMBOL : Symbol code page (42). + * CP_THREAD_ACP: The Windows ANSI code page for the current thread. + * CP_UTF7 : UTF-7. Use this value only when forced by a 7-bit transport mechanism. Use of UTF-8 is preferred. + * CP_UTF8 : UTF-8. + */ + int size_needed = ::MultiByteToWideChar(CP_UTF8, 0, &external[0], (int)external.size(), NULL, 0); if (size_needed <= 0) { return {}; } ipc::wstring internal(size_needed, L'\0'); - ::MultiByteToWideChar(CP_ACP, 0, &external[0], (int)external.size(), &internal[0], size_needed); -#endif + ::MultiByteToWideChar(CP_UTF8, 0, &external[0], (int)external.size(), &internal[0], size_needed); return internal; } diff --git a/test/test_platform.cpp b/test/test_platform.cpp index 4cbff16..c368690 100644 --- a/test/test_platform.cpp +++ b/test/test_platform.cpp @@ -1,4 +1,3 @@ - #if defined(WIN64) || defined(_WIN64) || defined(__WIN64__) || \ defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) || \ defined(WINCE) || defined(_WIN32_WCE) @@ -11,18 +10,21 @@ #include "libipc/platform/to_tchar.h" TEST(Platform, to_tchar) { - char const *utf8 = "hello world, 你好,こんにちは"; - wchar_t const *wstr = L"hello world, 你好,こんにちは"; + char const utf8[] = { + 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2c, 0x20, 0xe6, 0xb5, 0xa3, + 0xe7, 0x8a, 0xb2, 0xe3, 0x82, 0xbd, 0xe9, 0x94, 0x9b, 0xe5, 0xb1, 0xbb, 0xe4, 0xba, 0xbe, 0xe9, + 0x8a, 0x88, 0xe6, 0x92, 0xb1, 0xe4, 0xbc, 0x80, 0xe9, 0x8a, 0x87, 0xc2, 0xb0, 0xe4, 0xbc, 0x85, + 0x00, + }; + wchar_t const wstr[] = L"hello world, 你好,こんにちは"; { ipc::string str = ipc::detail::to_tchar(utf8); EXPECT_STREQ(str.c_str(), utf8); } { ipc::wstring wtr = ipc::detail::to_tchar(utf8); - //std::wcout.imbue(std::locale()); - //std::wcout << wtr << "\n"; EXPECT_STREQ(wtr.c_str(), wstr); } } -#endif/*WIN*/ +#endif/*WIN*/ \ No newline at end of file