From 6106210c8727d4340d2a059d11fa35540da6f0f7 Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Wed, 17 Oct 2018 07:31:11 -0700 Subject: [PATCH] UTF8: Correct MSW issue with previous commit Linux does not handle the resize command with wide-character extended table UTF-8. The solution did not work for W7-32bit. This is a compromise, attempting first the preferred, previous solution and falling back to the secondary solution. --- common/utf8.cpp | 15 +++++++++++++-- tools/utf8_tests.cpp | 21 ++++++++++++++++++--- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/common/utf8.cpp b/common/utf8.cpp index 56be44faa4..f3d7d118c6 100644 --- a/common/utf8.cpp +++ b/common/utf8.cpp @@ -201,9 +201,20 @@ bool IsUTF8( const char* aString ) UTF8::UTF8( const wchar_t* txt ) { - wxCharBuffer charbuf = wxSafeConvertWX2MB( txt ); + try + { + size_t len = wcslen( txt ) * 4 + 1; + char temp[len]; + wxConvUTF8.WC2MB( temp, txt, len ); + m_s.assign( temp ); + } + catch(...) + { + auto string = wxSafeConvertWX2MB( txt ); + m_s.assign( string ); + } - m_s.assign( charbuf.data() ); + m_s.shrink_to_fit(); } diff --git a/tools/utf8_tests.cpp b/tools/utf8_tests.cpp index e37925dfb8..be493c48ca 100644 --- a/tools/utf8_tests.cpp +++ b/tools/utf8_tests.cpp @@ -15,13 +15,13 @@ void callee( const wxString& aString ) int main( int argc, char** argv ) { - UTF8 bozo = "ü"; + UTF8 bozo = "This is a test of UTF-8: ü‱☺😕😱"; callee( bozo ); wxString s = bozo; - wxString b = bozo; + UTF8 b = s; if( s.IsEmpty() ) { @@ -30,8 +30,23 @@ int main( int argc, char** argv ) if( s != bozo.wx_str() ) { - printf( "string miscompare\n" ); + printf( "wxString conversion error\n" ); } + if( b != bozo ) + { + printf( "From string conversion error\n" ); + } + + auto pos = bozo.begin(); + auto end = bozo.end(); + + while( pos != end ) + { + printf( "%c", *pos++ ); + } + + printf("\n"); + return 0; }