X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/2cbfa061b44c7460adf3f06d09b4fbf304ed3a87..2b1d737b3e9413217302cde932cbc3af91d79024:/src/common/string.cpp diff --git a/src/common/string.cpp b/src/common/string.cpp index 6b21ca1b09..c95d6729e8 100644 --- a/src/common/string.cpp +++ b/src/common/string.cpp @@ -43,6 +43,10 @@ #include #include +#ifndef __WXMSW__ +#include +#endif + #ifdef __SALFORDC__ #include #endif @@ -312,13 +316,15 @@ bool wxStringBase::AllocBeforeWrite(size_t nLen) pData->nAllocLength = nLen; m_pchData = pData->data(); } - - // now we have enough space, just update the string length - pData->nDataLength = nLen; } wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner + // it doesn't really matter what the string length is as it's going to be + // overwritten later but, for extra safety, set it to 0 for now as we may + // have some junk in m_pchData + GetStringData()->nDataLength = 0; + return true; } @@ -326,7 +332,7 @@ wxStringBase& wxStringBase::append(size_t n, wxChar ch) { size_type len = length(); - if ( !CopyBeforeWrite() || !Alloc(len + n) ) { + if ( !Alloc(len + n) || !CopyBeforeWrite() ) { wxFAIL_MSG( _T("out of memory in wxStringBase::append") ); } GetStringData()->nDataLength = len + n; @@ -380,7 +386,9 @@ bool wxStringBase::Alloc(size_t nLen) // allocation failure handled by caller return false; } - memcpy(m_pchData, pData->data(), nOldLen*sizeof(wxChar)); + // +1 to copy the terminator, too + memcpy(m_pchData, pData->data(), (nOldLen+1)*sizeof(wxChar)); + GetStringData()->nDataLength = nOldLen; } else { nLen += EXTRA_ALLOC; @@ -445,7 +453,7 @@ wxStringBase& wxStringBase::insert(size_t nPos, const wxChar *sz, size_t n) if ( n == npos ) n = wxStrlen(sz); if ( n == 0 ) return *this; - if ( !CopyBeforeWrite() || !Alloc(length() + n) ) { + if ( !Alloc(length() + n) || !CopyBeforeWrite() ) { wxFAIL_MSG( _T("out of memory in wxStringBase::insert") ); } @@ -481,8 +489,14 @@ size_t wxStringBase::find(const wxStringBase& str, size_t nStart) const while(p - c_str() + str.length() <= length() && wxTmemcmp(p, str.c_str(), str.length()) ) { + //Previosly passed as the first argument to wxTmemchr, + //but C/C++ standard does not specify evaluation order + //of arguments to functions - + //http://embedded.com/showArticle.jhtml?articleID=9900607 + ++p; + //anchor again - p = (const wxChar*)wxTmemchr(++p, + p = (const wxChar*)wxTmemchr(p, str.c_str()[0], length() - (p - c_str())); @@ -1024,7 +1038,7 @@ wxString::wxString(const char *psz, wxMBConv& conv, size_t nLength) size_t nRealSize; wxWCharBuffer theBuffer = conv.cMB2WC(psz, nLen, &nRealSize); - //Copy + //Copy if (nRealSize) assign( theBuffer.data() , nRealSize - 1 ); } @@ -1080,7 +1094,7 @@ wxString::wxString(const wchar_t *pwz, wxMBConv& conv, size_t nLength) size_t nRealSize; wxCharBuffer theBuffer = conv.cWC2MB(pwz, nLen, &nRealSize); - //Copy + //Copy if (nRealSize) assign( theBuffer.data() , nRealSize - 1 ); } @@ -1219,7 +1233,7 @@ wxString operator+(const wxString& str, const wxChar *psz) if ( !s.Alloc(wxStrlen(psz) + str.Len()) ) { wxFAIL_MSG( _T("out of memory in wxString::operator+") ); } - s = str; + s += str; s += psz; return s; @@ -1629,7 +1643,7 @@ inline int wxSafeIsspace(wxChar ch) { return (ch < 127) && wxIsspace(ch); } wxString& wxString::Trim(bool bFromRight) { // first check if we're going to modify the string at all - if ( !IsEmpty() && + if ( !empty() && ( (bFromRight && wxSafeIsspace(GetChar(Len() - 1))) || (!bFromRight && wxSafeIsspace(GetChar(0u))) @@ -1825,7 +1839,14 @@ int wxString::PrintfV(const wxChar* pszFormat, va_list argptr) // vsnprintf() may return either -1 (traditional Unix behaviour) or the // total number of characters which would have been written if the // buffer were large enough - if ( len >= 0 && len <= size ) + // also, it may return an errno may be something like EILSEQ, + // in which case we need to break out + if ( (len >= 0 && len <= size) + // No EOVERFLOW on Windows nor Palm 6.0 nor OpenVMS +#if !defined(__WXMSW__) && !defined(__WXPALMOS__) && !defined( __VMS ) + || errno != EOVERFLOW +#endif + ) { // ok, there was enough space break; @@ -2195,6 +2216,11 @@ wxString* wxArrayString::GetStringArray() const return array; } +void wxArrayString::Remove(size_t nIndex, size_t nRemove) +{ + RemoveAt(nIndex, nRemove); +} + #endif // WXWIN_COMPATIBILITY_2_4 // searches the array for an item (forward or backwards)