diff --git a/sol/compatibility/version.hpp b/sol/compatibility/version.hpp index e7c1947d..6ec934b8 100644 --- a/sol/compatibility/version.hpp +++ b/sol/compatibility/version.hpp @@ -24,10 +24,10 @@ #include -#if defined(_WIN32) || defined(_MSC_VER) +#if defined(_WIN32) || defined(_MSC_VER) || defined(__MINGW32__) #ifndef SOL_CODECVT_SUPPORT #define SOL_CODECVT_SUPPORT 1 -#endif // codecvt support +#endif // sol codecvt support #elif defined(__GNUC__) #if __GNUC__ >= 5 #ifndef SOL_CODECVT_SUPPORT diff --git a/sol/stack_get.hpp b/sol/stack_get.hpp index fa08fd09..266518cf 100644 --- a/sol/stack_get.hpp +++ b/sol/stack_get.hpp @@ -191,14 +191,17 @@ namespace sol { if (sizeof(wchar_t) == 2) { std::wstring_convert> convert; std::wstring r = convert.from_bytes(str, str + len); +#ifdef __MINGW32__ + // Fuck you, MinGW, and fuck you libstdc++ for introducing this absolutely asinine bug + // https://sourceforge.net/p/mingw-w64/bugs/538/ + // http://chat.stackoverflow.com/transcript/message/32271369#32271369 + for (auto& c : r) { + uint8_t* b = reinterpret_cast(&c); + std::swap(b[0], b[1]); + } +#endif return r; } - else if (sizeof(wchar_t) == 4) { - std::wstring_convert> convert; - std::wstring r = convert.from_bytes(str, str + len); - return r; - } - // ... Uh, what the fuck do I even do here? std::wstring_convert> convert; std::wstring r = convert.from_bytes(str, str + len); return r; @@ -270,7 +273,7 @@ namespace sol { return str.size() > 0 ? str[0] : '\0'; } }; -#endif // codecvt Header Support +#endif // codecvt header support template<> struct getter { diff --git a/sol/stack_push.hpp b/sol/stack_push.hpp index 647eafae..a35318d6 100644 --- a/sol/stack_push.hpp +++ b/sol/stack_push.hpp @@ -420,11 +420,6 @@ namespace sol { std::string u8str = convert.to_bytes(strb, stre); return stack::push(L, u8str); } - else if (sizeof(wchar_t) == 4) { - std::wstring_convert> convert; - std::string u8str = convert.to_bytes(strb, stre); - return stack::push(L, u8str); - } std::wstring_convert> convert; std::string u8str = convert.to_bytes(strb, stre); return stack::push(L, u8str); @@ -564,7 +559,7 @@ namespace sol { return stack::push(L, u32str.data(), u32str.data() + sz); } }; -#endif // Codecvt Header Support +#endif // codecvt Header Support template struct pusher> { diff --git a/test_strings.cpp b/test_strings.cpp index 429f7b4b..c0b28be8 100644 --- a/test_strings.cpp +++ b/test_strings.cpp @@ -3,6 +3,8 @@ #include #include +#include + struct test {}; template struct test_t {}; @@ -23,8 +25,10 @@ TEST_CASE("stack/strings", "test that strings can be roundtripped") { static const char16_t utf16str[] = { 0xD83C, 0xDF4C, 0x20, 0x6665, 0x20, 0x46, 0x6F, 0x6F, 0x20, 0xA9, 0x20, 0x62, 0x61, 0x72, 0x20, 0xD834, 0xDF06, 0x20, 0x62, 0x61, 0x7A, 0x20, 0x2603, 0x20, 0x71, 0x75, 0x78, 0x00 }; static const char32_t utf32str[] = { 0x1F34C, 0x0020, 0x6665, 0x0020, 0x0046, 0x006F, 0x006F, 0x0020, 0x00A9, 0x0020, 0x0062, 0x0061, 0x0072, 0x0020, 0x1D306, 0x0020, 0x0062, 0x0061, 0x007A, 0x0020, 0x2603, 0x0020, 0x0071, 0x0075, 0x0078, 0x00 }; #ifdef _WIN32 + std::cout << "win32 widestr" << std::endl; static const wchar_t widestr[] = { 0xD83C, 0xDF4C, 0x20, 0x6665, 0x20, 0x46, 0x6F, 0x6F, 0x20, 0xA9, 0x20, 0x62, 0x61, 0x72, 0x20, 0xD834, 0xDF06, 0x20, 0x62, 0x61, 0x7A, 0x20, 0x2603, 0x20, 0x71, 0x75, 0x78, 0x00 }; #else + std::cout << "non-windows widestr" << std::endl; static const wchar_t widestr[] = { 0x1F34C, 0x0020, 0x6665, 0x0020, 0x0046, 0x006F, 0x006F, 0x0020, 0x00A9, 0x0020, 0x0062, 0x0061, 0x0072, 0x0020, 0x1D306, 0x0020, 0x0062, 0x0061, 0x007A, 0x0020, 0x2603, 0x0020, 0x0071, 0x0075, 0x0078, 0x00 }; #endif static const std::string utf8str_s = utf8str; @@ -33,6 +37,12 @@ TEST_CASE("stack/strings", "test that strings can be roundtripped") { static const std::wstring widestr_s = widestr; #ifdef SOL_CODECVT_SUPPORT + std::cout << "sizeof(wchar_t): " << sizeof(wchar_t) << std::endl; + std::cout << "sizeof(char16_t): " << sizeof(char16_t) << std::endl; + std::cout << "sizeof(char32_t): " << sizeof(char32_t) << std::endl; + std::cout << "utf8str: " << utf8str << std::endl; + std::cout << "utf8str_s: " << utf8str_s << std::endl; + lua["utf8"] = utf8str; lua["utf16"] = utf16str; lua["utf32"] = utf32str; @@ -47,7 +57,7 @@ TEST_CASE("stack/strings", "test that strings can be roundtripped") { REQUIRE(utf16_to_utf8 == utf8str_s); REQUIRE(utf32_to_utf8 == utf8str_s); REQUIRE(wide_to_utf8 == utf8str_s); - + std::wstring utf8_to_wide = lua["utf8"]; std::wstring utf16_to_wide = lua["utf16"]; std::wstring utf32_to_wide = lua["utf32"];