more efficient implementation of ToAscii/FromAscii in UTF8 build

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@45664 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Václav Slavík
2007-04-26 12:00:43 +00:00
parent 2523e9b700
commit c1eada835a

View File

@@ -955,18 +955,23 @@ wxString wxString::FromAscii(const char *ascii)
if (!ascii) if (!ascii)
return wxEmptyString; return wxEmptyString;
size_t len = strlen( ascii ); size_t len = strlen(ascii);
wxString res; wxString res;
if ( len ) if ( len )
{ {
wxStringBuffer buf(res, len); wxImplStringBuffer buf(res, len);
wxStringCharType *dest = buf;
wchar_t *dest = buf;
for ( ;; ) for ( ;; )
{ {
if ( (*dest++ = (wchar_t)(unsigned char)*ascii++) == L'\0' ) unsigned char c = (unsigned char)*ascii++;
wxASSERT_MSG( c < 0x80,
_T("Non-ASCII value passed to FromAscii().") );
*dest++ = (wchar_t)c;
if ( c == '\0' )
break; break;
} }
} }
@@ -978,35 +983,36 @@ wxString wxString::FromAscii(const char ascii)
{ {
// What do we do with '\0' ? // What do we do with '\0' ?
wxString res; unsigned char c = (unsigned char)ascii;
res += (wchar_t)(unsigned char) ascii;
return res; wxASSERT_MSG( c < 0x80, _T("Non-ASCII value passed to FromAscii().") );
// NB: the cast to wchar_t causes interpretation of 'ascii' as Latin1 value
return wxString(wxUniChar((wchar_t)c));
} }
const wxCharBuffer wxString::ToAscii() const const wxCharBuffer wxString::ToAscii() const
{ {
// this will allocate enough space for the terminating NUL too // this will allocate enough space for the terminating NUL too
wxCharBuffer buffer(length()); wxCharBuffer buffer(length());
char *dest = buffer.data(); char *dest = buffer.data();
const wchar_t *pwc = c_str(); for ( const_iterator i = begin(); i != end(); ++i )
for ( ;; )
{ {
*dest++ = (char)(*pwc > SCHAR_MAX ? wxT('_') : *pwc); wxUniChar c(*i);
// FIXME-UTF8: unify substituted char ('_') with wxUniChar ('?')
*dest++ = c.IsAscii() ? (char)c : '_';
// the output string can't have embedded NULs anyhow, so we can safely // the output string can't have embedded NULs anyhow, so we can safely
// stop at first of them even if we do have any // stop at first of them even if we do have any
if ( !*pwc++ ) if ( !c )
break; break;
} }
return buffer; return buffer;
} }
#endif // Unicode #endif // wxUSE_UNICODE
// extract string of length nCount starting at nFirst // extract string of length nCount starting at nFirst
wxString wxString::Mid(size_t nFirst, size_t nCount) const wxString wxString::Mid(size_t nFirst, size_t nCount) const