From c1eada835ab8dc0ea7ea1b67a5015702463307fc Mon Sep 17 00:00:00 2001 From: =?utf8?q?V=C3=A1clav=20Slav=C3=ADk?= Date: Thu, 26 Apr 2007 12:00:43 +0000 Subject: [PATCH] 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 --- src/common/string.cpp | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/common/string.cpp b/src/common/string.cpp index b2969ff04f..717ce0b52c 100644 --- a/src/common/string.cpp +++ b/src/common/string.cpp @@ -955,19 +955,24 @@ wxString wxString::FromAscii(const char *ascii) if (!ascii) return wxEmptyString; - size_t len = strlen( ascii ); + size_t len = strlen(ascii); wxString res; if ( len ) { - wxStringBuffer buf(res, len); - - wchar_t *dest = buf; + wxImplStringBuffer buf(res, len); + wxStringCharType *dest = buf; for ( ;; ) { - if ( (*dest++ = (wchar_t)(unsigned char)*ascii++) == L'\0' ) - break; + 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; } } @@ -978,35 +983,36 @@ wxString wxString::FromAscii(const char ascii) { // What do we do with '\0' ? - wxString res; - res += (wchar_t)(unsigned char) ascii; + unsigned char c = (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 { // this will allocate enough space for the terminating NUL too wxCharBuffer buffer(length()); - - char *dest = buffer.data(); - const wchar_t *pwc = c_str(); - for ( ;; ) + for ( const_iterator i = begin(); i != end(); ++i ) { - *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 // stop at first of them even if we do have any - if ( !*pwc++ ) + if ( !c ) break; } return buffer; } -#endif // Unicode +#endif // wxUSE_UNICODE // extract string of length nCount starting at nFirst wxString wxString::Mid(size_t nFirst, size_t nCount) const -- 2.45.2