diff --git a/docs/changes.txt b/docs/changes.txt index ad38eb027b..80d6edda36 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -75,6 +75,7 @@ All: - Add wxART_FULL_SCREEN standard bitmap (Igor Korot). - Fix wxStringTokenizer copy ctor and assignment operator. - Added wxASSERT_MSG_AT() and wxFAIL_MSG_AT() macros. +- Accept replacement character in wxString::ToAscii() (Stefano D. Mtangoo). Unix: diff --git a/include/wx/string.h b/include/wx/string.h index d6eb81baa1..c23586e314 100644 --- a/include/wx/string.h +++ b/include/wx/string.h @@ -1556,13 +1556,13 @@ public: static wxString FromAscii(const char *ascii, size_t len); static wxString FromAscii(const char *ascii); static wxString FromAscii(char ascii); - const wxScopedCharBuffer ToAscii() const; + const wxScopedCharBuffer ToAscii(char replaceWith = '_') const; #else // ANSI static wxString FromAscii(const char *ascii) { return wxString( ascii ); } static wxString FromAscii(const char *ascii, size_t len) { return wxString( ascii, len ); } static wxString FromAscii(char ascii) { return wxString( ascii ); } - const char *ToAscii() const { return c_str(); } + const char *ToAscii(char WXUNUSED(replaceWith) = '_') const { return c_str(); } #endif // Unicode/!Unicode // also provide unsigned char overloads as signed/unsigned doesn't matter diff --git a/interface/wx/string.h b/interface/wx/string.h index 951719f524..fbd4e455c3 100644 --- a/interface/wx/string.h +++ b/interface/wx/string.h @@ -739,17 +739,21 @@ public: a wxCharBuffer (Unicode builds only) or a C string (ANSI builds). Note that this conversion is only lossless if the string contains only - ASCII characters as all the non-ASCII ones are replaced with the @c '_' - (underscore) character. + ASCII characters as all the non-ASCII ones are replaced with the (same) + provided replacement character. Use mb_str() or utf8_str() to convert to other encodings. + + @param replaceWith + The character used to replace any non-ASCII characters, default to + underscore (@c "_"). This parameter is new since wxWidgets 3.1.0. */ - const char* ToAscii() const; + const char* ToAscii(char replaceWith = '_') const; /** @overload */ - const wxCharBuffer ToAscii() const; + const wxCharBuffer ToAscii(char replaceWith = '_') const; /** Return the string as an std::string in current locale encoding. diff --git a/src/common/string.cpp b/src/common/string.cpp index 82354db3ab..930b488162 100644 --- a/src/common/string.cpp +++ b/src/common/string.cpp @@ -1205,7 +1205,7 @@ wxString wxString::FromAscii(char ascii) return wxString(wxUniChar((wchar_t)c)); } -const wxScopedCharBuffer wxString::ToAscii() const +const wxScopedCharBuffer wxString::ToAscii(char replaceWith) const { // this will allocate enough space for the terminating NUL too wxCharBuffer buffer(length()); @@ -1215,7 +1215,7 @@ const wxScopedCharBuffer wxString::ToAscii() const { wxUniChar c(*i); // FIXME-UTF8: unify substituted char ('_') with wxUniChar ('?') - *dest++ = c.IsAscii() ? (char)c : '_'; + *dest++ = c.IsAscii() ? (char)c : replaceWith; // the output string can't have embedded NULs anyhow, so we can safely // stop at first of them even if we do have any