Allow specifying the replacement character in wxString::ToAscii()

Don't hardcode "_" as the replacement character but accept the one specified
by the user.

Closes https://github.com/wxWidgets/wxWidgets/pull/116
This commit is contained in:
Stefano D. Mtangoo 2016-01-02 17:32:22 +01:00 committed by Vadim Zeitlin
parent cfd4da8fdb
commit 28bc1bdac0
4 changed files with 13 additions and 8 deletions

View File

@ -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:

View File

@ -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

View File

@ -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.

View File

@ -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