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

@@ -1715,6 +1715,8 @@ public:
return wxString();
return FromImpl(utf8);
}
std::string utf8_string() const { return m_impl; }
#endif
const wxScopedCharBuffer utf8_str() const
@@ -1738,6 +1740,8 @@ public:
{ return FromUTF8(utf8.c_str(), utf8.length()); }
static wxString FromUTF8Unchecked(const std::string& utf8)
{ return FromUTF8Unchecked(utf8.c_str(), utf8.length()); }
std::string utf8_string() const { return ToStdString(wxMBConvUTF8()); }
#endif
const wxScopedCharBuffer utf8_str() const { return mb_str(wxMBConvUTF8()); }
#else // ANSI
@@ -1771,6 +1775,8 @@ public:
{ return FromUTF8(utf8.c_str(), utf8.length()); }
static wxString FromUTF8Unchecked(const std::string& utf8)
{ return FromUTF8Unchecked(utf8.c_str(), utf8.length()); }
std::string utf8_string() const { return ToStdString(wxMBConvUTF8()); }
#endif
const wxScopedCharBuffer utf8_str() const
{