From 87da4680c711f1d4a7328d9f0693f056be2b875a Mon Sep 17 00:00:00 2001 From: James Turner Date: Sat, 14 Mar 2020 20:10:08 +0000 Subject: [PATCH] Tweaks to Windows string conversion Avoid use of code-cvt, and tolerate differences in compiler handling of string literals (tested Clanged and MSVC, hopefully GCC is kind) --- simgear/misc/strutils.cxx | 6 ++---- simgear/misc/strutils_test.cxx | 7 ++++++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/simgear/misc/strutils.cxx b/simgear/misc/strutils.cxx index 5062e242..208f422c 100644 --- a/simgear/misc/strutils.cxx +++ b/simgear/misc/strutils.cxx @@ -672,8 +672,7 @@ static std::string convertWStringToMultiByte(DWORD encoding, const std::wstring& std::wstring convertUtf8ToWString(const std::string& a) { #if defined(SG_WINDOWS) - std::wstring_convert, wchar_t> ucs2conv; - return ucs2conv.from_bytes(a); + return convertMultiByteToWString(CP_UTF8, a); #else assert(sizeof(wchar_t) == 4); std::wstring result; @@ -723,8 +722,7 @@ std::wstring convertUtf8ToWString(const std::string& a) std::string convertWStringToUtf8(const std::wstring& w) { #if defined(SG_WINDOWS) - std::wstring_convert, wchar_t> ucs2conv; - return ucs2conv.to_bytes(w); + return convertWStringToMultiByte(CP_UTF8, w); #else assert(sizeof(wchar_t) == 4); std::string result; diff --git a/simgear/misc/strutils_test.cxx b/simgear/misc/strutils_test.cxx index b78b9a63..cb371c47 100644 --- a/simgear/misc/strutils_test.cxx +++ b/simgear/misc/strutils_test.cxx @@ -458,7 +458,7 @@ void test_escape() " ab\\nc \\\\def\\t\\r \\\\ ghi\\\\"); // U+0152 is LATIN CAPITAL LIGATURE OE. The last word is Egg translated in // French and encoded in UTF-8 ('Œuf' if you can read UTF-8). - SG_CHECK_EQUAL(strutils::escape("Un \"Bel\" '\u0152uf'"), + SG_CHECK_EQUAL(strutils::escape(u8"Un \"Bel\" '\u0152uf'"), "Un \\\"Bel\\\" '\\305\\222uf'"); SG_CHECK_EQUAL(strutils::escape("\a\b\f\n\r\t\v"), "\\a\\b\\f\\n\\r\\t\\v"); @@ -628,6 +628,11 @@ void test_utf8Convert() std::wstring aRoundTrip = strutils::convertUtf8ToWString(utf8A); SG_VERIFY(a == aRoundTrip); + + + const auto wide2(L"\U0001f6eb\u2708\ufe0f\u2764\ufe0f"); + std::string utf8_2 = strutils::convertWStringToUtf8(wide2); + SG_VERIFY(utf8_2 == std::string("\xf0\x9f\x9b\xab\xe2\x9c\x88\xef\xb8\x8f\xe2\x9d\xa4\xef\xb8\x8f")); } void test_parseGeod()