From e015c2a31fb2b1e9df208a15ed74a21c3da86c9d Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 20 Aug 2002 15:09:27 +0000 Subject: [PATCH] rewrote wxString::To/FromAscii() to fix a few small problems git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@16626 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/string.h | 35 ++++++++++++----------- src/common/string.cpp | 66 +++++++++++++++++++++++++------------------ 2 files changed, 57 insertions(+), 44 deletions(-) diff --git a/include/wx/string.h b/include/wx/string.h index 988937c581..087a46e225 100644 --- a/include/wx/string.h +++ b/include/wx/string.h @@ -446,24 +446,27 @@ public: // implicit conversion to C string operator const wxChar*() const { return m_pchData; } + // explicit conversion to C string (use this with printf()!) const wxChar* c_str() const { return m_pchData; } - // identical to c_str() + // identical to c_str(), for wxWin 1.6x compatibility const wxChar* wx_str() const { return m_pchData; } - // identical to c_str() + // identical to c_str(), for MFC compatibility const wxChar* GetData() const { return m_pchData; } - // conversion to plain ascii: this is usefull for - // converting numbers or strings which are certain - // not to contain special chars (typically system - // functions, X atoms, environment variables etc.) + // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for + // converting numbers or strings which are certain not to contain special + // chars (typically system functions, X atoms, environment variables etc.) + // + // the behaviour of these functions with the strings containing anything + // else than 7 bit ASCII characters is undefined, use at your own risk. #if wxUSE_UNICODE - static wxString FromAscii( char *ascii ); + static wxString FromAscii(const char *ascii); const wxCharBuffer ToAscii() const; -#else - static wxString FromAscii( char *ascii ) { return wxString( ascii ); } - const char *ToAscii() const { return m_pchData; } -#endif +#else // ANSI + static wxString FromAscii(const char *ascii) { return wxString( ascii ); } + const char *ToAscii() const { return c_str(); } +#endif // Unicode/!Unicode // conversions with (possible) format convertions: have to return a // buffer with temporary data @@ -1147,20 +1150,20 @@ public: class WXDLLEXPORT wxStringBuffer { - DECLARE_NO_COPY_CLASS(wxStringBuffer) - public: wxStringBuffer(wxString& str, size_t lenWanted = 1024) : m_str(str), m_buf(NULL) { m_buf = m_str.GetWriteBuf(lenWanted); } - + ~wxStringBuffer() { m_str.UngetWriteBuf(); } - + operator wxChar*() const { return m_buf; } - + private: wxString& m_str; wxChar *m_buf; + + DECLARE_NO_COPY_CLASS(wxStringBuffer) }; // --------------------------------------------------------------------------- diff --git a/src/common/string.cpp b/src/common/string.cpp index b430829f06..77af363cb5 100644 --- a/src/common/string.cpp +++ b/src/common/string.cpp @@ -357,12 +357,12 @@ bool wxString::AllocBuffer(size_t nLen) // 2) sizeof(wxStringData) for housekeeping info wxStringData* pData = (wxStringData*) malloc(sizeof(wxStringData) + (nLen + EXTRA_ALLOC + 1)*sizeof(wxChar)); - + if ( pData == NULL ) { // allocation failures are handled by the caller return FALSE; } - + pData->nRefs = 1; pData->nDataLength = nLen; pData->nAllocLength = nLen + EXTRA_ALLOC; @@ -416,7 +416,7 @@ bool wxString::AllocBeforeWrite(size_t nLen) pData = (wxStringData*) realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar)); - + if ( pData == NULL ) { // allocation failures are handled by the caller // keep previous data since reallocation failed @@ -451,7 +451,7 @@ bool wxString::Alloc(size_t nLen) // allocation failure handled by caller return FALSE; } - + pData->nRefs = 1; pData->nDataLength = 0; pData->nAllocLength = nLen; @@ -759,43 +759,53 @@ wxString operator+(const wxChar *psz, const wxString& str) // =========================================================================== #if wxUSE_UNICODE -wxString wxString::FromAscii( char *ascii ) + +wxString wxString::FromAscii(const char *ascii) { if (!ascii) return wxEmptyString; - + size_t len = strlen( ascii ); wxString res; - res.AllocBuffer( len ); - wchar_t *dest = (wchar_t*)(const wchar_t*) res.c_str(); - - for (size_t i = 0; i < len+1; i++) - dest[i] = (wchar_t) ascii[i]; - + + if ( len ) + { + wxStringBuffer buf(res, len); + + wchar_t *dest = buf; + + for ( ;; ) + { + if ( (*dest++ = (wchar_t)(unsigned char)*ascii++) == L'\0' ) + break; + } + } + return res; } const wxCharBuffer wxString::ToAscii() const { - if (IsNull()) - return wxCharBuffer( (const char*)NULL ); + // this will allocate enough space for the terminating NUL too + wxCharBuffer buffer(length()); - size_t len = Len(); - wxCharBuffer buffer( len ); // allocates len+1 - - char *dest = (char*)(const char*) buffer; - - for (size_t i = 0; i < len+1; i++) + signed char *dest = (signed char *)buffer.data(); + + const wchar_t *pwc = c_str(); + for ( ;; ) { - if (m_pchData[i] > 127) - dest[i] = '_'; - else - dest[i] = (char) m_pchData[i]; + *dest++ = *pwc > SCHAR_MAX ? '_' : *pwc; + + // 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++ ) + break; } - + return buffer; } -#endif + +#endif // Unicode // --------------------------------------------------------------------------- // simple sub-string extraction @@ -1048,7 +1058,7 @@ wxString& wxString::MakeUpper() wxFAIL_MSG( _T("out of memory in wxString::MakeUpper") ); return *this; } - + for ( wxChar *p = m_pchData; *p; p++ ) *p = (wxChar)wxToupper(*p); @@ -2237,7 +2247,7 @@ size_t wxArrayString::Add(const wxString& str, size_t nInsert) Grow(nInsert); for (size_t i = 0; i < nInsert; i++) - { + { // the string data must not be deleted! str.GetStringData()->Lock(); -- 2.47.2