Add wxString::utf8_string()

This adds a yet another conversion function, which is not ideal, but
still better than having to write ToStdString(wxConvUTF8) every time for
losslessly converting wxString to std::string: not only this is too
long, but it's also too easy to forget to specify wxConvUTF8, resulting
in data loss when using non-UTF-8 locale.
This commit is contained in:
Vadim Zeitlin
2021-03-06 15:12:07 +01:00
parent 0aacc6a9ab
commit 0f8e976ac3
3 changed files with 24 additions and 2 deletions

View File

@@ -571,10 +571,13 @@ TEST_CASE("StdString::Conversion", "[stdstring]")
std::string s9("\xF0\x9F\x90\xB1\0\xE7\x8C\xAB", 9); /* U+1F431 U+0000 U+732B */
wxString s10 = wxString::FromUTF8(s9);
CHECK( s10.ToStdString(wxConvUTF8) == s9 );
CHECK( s10.utf8_string() == s9 );
#endif // wxUSE_UNICODE
std::string s11("xyz\0\xFF", 5); /* an invalid UTF-8 sequence */
CHECK( "" == wxString::FromUTF8(s11) );
CHECK( wxString("bye").utf8_string() == std::string("bye") );
}
#endif // wxUSE_STD_STRING