Skip to content

Commit c9d92b5

Browse files
committed
CP_ACP => CP_UTF8
1 parent 8170836 commit c9d92b5

File tree

2 files changed

+19
-47
lines changed

2 files changed

+19
-47
lines changed

src/libipc/platform/to_tchar.h

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -52,51 +52,21 @@ auto to_tchar(ipc::string &&external) -> IsSameChar<T, ipc::wstring> {
5252
if (external.empty()) {
5353
return {}; // noconv
5454
}
55-
#if 0 // backup
56-
auto &fcodecvt = std::use_facet<std::codecvt<wchar_t, char, std::mbstate_t>>(std::locale());
57-
std::mbstate_t mb {};
58-
ipc::wstring internal(external.size(), '\0');
59-
char const *first = &external[0], *last = &external[external.size()];
60-
std::size_t len = 0;
61-
while (first != last) {
62-
wchar_t *start = &internal[len], *end = &internal[internal.size()], *to_next;
63-
auto ret = fcodecvt.in(mb, first, last, first,
64-
start, end , to_next);
65-
switch (ret) {
66-
// no conversion, just copy code values
67-
case std::codecvt_base::noconv:
68-
internal.resize(len);
69-
for (; first != last; ++first) {
70-
internal.push_back((wchar_t)(unsigned char)*first);
71-
}
72-
break;
73-
// conversion completed
74-
case std::codecvt_base::ok:
75-
len += static_cast<size_t>(to_next - start);
76-
internal.resize(len);
77-
break;
78-
// not enough space in the output buffer or unexpected end of source buffer
79-
case std::codecvt_base::partial:
80-
if (to_next <= start) {
81-
throw std::range_error{"[to_tchar] bad conversion"};
82-
}
83-
len += static_cast<size_t>(to_next - start);
84-
internal.resize(internal.size() + external.size());
85-
break;
86-
// encountered a character that could not be converted
87-
default: // error
88-
throw std::range_error{"[to_tchar] bad conversion"};
89-
}
90-
}
91-
#else
92-
// CP_ACP: The system default Windows ANSI code page.
93-
int size_needed = ::MultiByteToWideChar(CP_ACP, 0, &external[0], (int)external.size(), NULL, 0);
55+
/**
56+
* CP_ACP : The system default Windows ANSI code page.
57+
* CP_MACCP : The current system Macintosh code page.
58+
* CP_OEMCP : The current system OEM code page.
59+
* CP_SYMBOL : Symbol code page (42).
60+
* CP_THREAD_ACP: The Windows ANSI code page for the current thread.
61+
* CP_UTF7 : UTF-7. Use this value only when forced by a 7-bit transport mechanism. Use of UTF-8 is preferred.
62+
* CP_UTF8 : UTF-8.
63+
*/
64+
int size_needed = ::MultiByteToWideChar(CP_UTF8, 0, &external[0], (int)external.size(), NULL, 0);
9465
if (size_needed <= 0) {
9566
return {};
9667
}
9768
ipc::wstring internal(size_needed, L'\0');
98-
::MultiByteToWideChar(CP_ACP, 0, &external[0], (int)external.size(), &internal[0], size_needed);
99-
#endif
69+
::MultiByteToWideChar(CP_UTF8, 0, &external[0], (int)external.size(), &internal[0], size_needed);
10070
return internal;
10171
}
10272

test/test_platform.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
#if defined(WIN64) || defined(_WIN64) || defined(__WIN64__) || \
32
defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) || \
43
defined(WINCE) || defined(_WIN32_WCE)
@@ -11,18 +10,21 @@
1110
#include "libipc/platform/to_tchar.h"
1211

1312
TEST(Platform, to_tchar) {
14-
char const *utf8 = "hello world, 你好,こんにちは";
15-
wchar_t const *wstr = L"hello world, 你好,こんにちは";
13+
char const utf8[] = {
14+
0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2c, 0x20, 0xe6, 0xb5, 0xa3,
15+
0xe7, 0x8a, 0xb2, 0xe3, 0x82, 0xbd, 0xe9, 0x94, 0x9b, 0xe5, 0xb1, 0xbb, 0xe4, 0xba, 0xbe, 0xe9,
16+
0x8a, 0x88, 0xe6, 0x92, 0xb1, 0xe4, 0xbc, 0x80, 0xe9, 0x8a, 0x87, 0xc2, 0xb0, 0xe4, 0xbc, 0x85,
17+
0x00,
18+
};
19+
wchar_t const wstr[] = L"hello world, 你好,こんにちは";
1620
{
1721
ipc::string str = ipc::detail::to_tchar<char>(utf8);
1822
EXPECT_STREQ(str.c_str(), utf8);
1923
}
2024
{
2125
ipc::wstring wtr = ipc::detail::to_tchar<wchar_t>(utf8);
22-
//std::wcout.imbue(std::locale());
23-
//std::wcout << wtr << "\n";
2426
EXPECT_STREQ(wtr.c_str(), wstr);
2527
}
2628
}
2729

28-
#endif/*WIN*/
30+
#endif/*WIN*/

0 commit comments

Comments
 (0)