mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-07 01:06:45 +08:00
CP_ACP => CP_UTF8
This commit is contained in:
parent
8170836534
commit
c9d92b5364
@ -52,51 +52,21 @@ auto to_tchar(ipc::string &&external) -> IsSameChar<T, ipc::wstring> {
|
|||||||
if (external.empty()) {
|
if (external.empty()) {
|
||||||
return {}; // noconv
|
return {}; // noconv
|
||||||
}
|
}
|
||||||
#if 0 // backup
|
/**
|
||||||
auto &fcodecvt = std::use_facet<std::codecvt<wchar_t, char, std::mbstate_t>>(std::locale());
|
* CP_ACP : The system default Windows ANSI code page.
|
||||||
std::mbstate_t mb {};
|
* CP_MACCP : The current system Macintosh code page.
|
||||||
ipc::wstring internal(external.size(), '\0');
|
* CP_OEMCP : The current system OEM code page.
|
||||||
char const *first = &external[0], *last = &external[external.size()];
|
* CP_SYMBOL : Symbol code page (42).
|
||||||
std::size_t len = 0;
|
* CP_THREAD_ACP: The Windows ANSI code page for the current thread.
|
||||||
while (first != last) {
|
* CP_UTF7 : UTF-7. Use this value only when forced by a 7-bit transport mechanism. Use of UTF-8 is preferred.
|
||||||
wchar_t *start = &internal[len], *end = &internal[internal.size()], *to_next;
|
* CP_UTF8 : UTF-8.
|
||||||
auto ret = fcodecvt.in(mb, first, last, first,
|
*/
|
||||||
start, end , to_next);
|
int size_needed = ::MultiByteToWideChar(CP_UTF8, 0, &external[0], (int)external.size(), NULL, 0);
|
||||||
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<size_t>(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<size_t>(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);
|
|
||||||
if (size_needed <= 0) {
|
if (size_needed <= 0) {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
ipc::wstring internal(size_needed, L'\0');
|
ipc::wstring internal(size_needed, L'\0');
|
||||||
::MultiByteToWideChar(CP_ACP, 0, &external[0], (int)external.size(), &internal[0], size_needed);
|
::MultiByteToWideChar(CP_UTF8, 0, &external[0], (int)external.size(), &internal[0], size_needed);
|
||||||
#endif
|
|
||||||
return internal;
|
return internal;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
#if defined(WIN64) || defined(_WIN64) || defined(__WIN64__) || \
|
#if defined(WIN64) || defined(_WIN64) || defined(__WIN64__) || \
|
||||||
defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) || \
|
defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) || \
|
||||||
defined(WINCE) || defined(_WIN32_WCE)
|
defined(WINCE) || defined(_WIN32_WCE)
|
||||||
@ -11,16 +10,19 @@
|
|||||||
#include "libipc/platform/to_tchar.h"
|
#include "libipc/platform/to_tchar.h"
|
||||||
|
|
||||||
TEST(Platform, to_tchar) {
|
TEST(Platform, to_tchar) {
|
||||||
char const *utf8 = "hello world, 你好,こんにちは";
|
char const utf8[] = {
|
||||||
wchar_t const *wstr = L"hello world, 你好,こんにちは";
|
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<char>(utf8);
|
ipc::string str = ipc::detail::to_tchar<char>(utf8);
|
||||||
EXPECT_STREQ(str.c_str(), utf8);
|
EXPECT_STREQ(str.c_str(), utf8);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
ipc::wstring wtr = ipc::detail::to_tchar<wchar_t>(utf8);
|
ipc::wstring wtr = ipc::detail::to_tchar<wchar_t>(utf8);
|
||||||
//std::wcout.imbue(std::locale());
|
|
||||||
//std::wcout << wtr << "\n";
|
|
||||||
EXPECT_STREQ(wtr.c_str(), wstr);
|
EXPECT_STREQ(wtr.c_str(), wstr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user