X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/52de37c78f31bc03f8b96090932c7ce8c3907d29..472137baebae6c011ad7d70285de454b9ce7796d:/src/common/stringimpl.cpp?ds=inline diff --git a/src/common/stringimpl.cpp b/src/common/stringimpl.cpp index eb90ea7eae..d0a7165ce7 100644 --- a/src/common/stringimpl.cpp +++ b/src/common/stringimpl.cpp @@ -30,6 +30,7 @@ #ifndef WX_PRECOMP #include "wx/stringimpl.h" + #include "wx/wxcrt.h" #endif #include @@ -41,10 +42,6 @@ #include #include -#ifdef __SALFORDC__ - #include -#endif - // allocating extra space for each string consumes more memory but speeds up // the concatenation operations (nLen is the current string's length) // NB: EXTRA_ALLOC must be >= 0! @@ -114,19 +111,26 @@ extern const wxStringCharType WXDLLIMPEXP_BASE *wxEmptyString = &g_strEmpty.dumm // ---------------------------------------------------------------------------- // this small class is used to gather statistics for performance tuning + +// uncomment this to enable gathering of some statistics about wxString +// efficiency //#define WXSTRING_STATISTICS + #ifdef WXSTRING_STATISTICS class Averager { public: Averager(const wxStringCharType *sz) { m_sz = sz; m_nTotal = m_nCount = 0; } ~Averager() - { wxPrintf("wxString: average %s = %f\n", m_sz, ((float)m_nTotal)/m_nCount); } + { + wxPrintf("wxString %s: total = %lu, average = %f\n", + m_sz, m_nTotal, ((float)m_nTotal)/m_nCount); + } void Add(size_t n) { m_nTotal += n; m_nCount++; } private: - size_t m_nCount, m_nTotal; + unsigned long m_nCount, m_nTotal; const wxStringCharType *m_sz; } g_averageLength("allocation size"), g_averageSummandLength("summand length"), @@ -183,7 +187,7 @@ wxStringImpl::wxStringImpl(const_iterator first, const_iterator last) { if ( last >= first ) { - InitWith(first, 0, last - first); + InitWith(first.GetPtr(), 0, last - first); } else { @@ -210,8 +214,8 @@ bool wxStringImpl::AllocBuffer(size_t nLen) wxASSERT( nLen > 0 ); // make sure that we don't overflow - wxASSERT( nLen < (INT_MAX / sizeof(wxStringCharType)) - - (sizeof(wxStringData) + EXTRA_ALLOC + 1) ); + wxCHECK( nLen < (INT_MAX / sizeof(wxStringCharType)) - + (sizeof(wxStringData) + EXTRA_ALLOC + 1), false ); STATISTICS_ADD(Length, nLen); @@ -338,6 +342,8 @@ bool wxStringImpl::Alloc(size_t nLen) wxStringData *pData = GetStringData(); if ( pData->nAllocLength <= nLen ) { if ( pData->IsEmpty() ) { + STATISTICS_ADD(Length, nLen); + nLen += EXTRA_ALLOC; pData = (wxStringData *) @@ -575,54 +581,45 @@ size_t wxStringImpl::rfind(wxStringCharType ch, size_t nStart) const } wxStringImpl& wxStringImpl::replace(size_t nStart, size_t nLen, - const wxStringCharType *sz) -{ - wxASSERT_MSG( nStart <= length(), - _T("index out of bounds in wxStringImpl::replace") ); - size_t strLen = length() - nStart; - nLen = strLen < nLen ? strLen : nLen; - - wxStringImpl strTmp; - strTmp.reserve(length()); // micro optimisation to avoid multiple mem allocs - - //This is kind of inefficient, but its pretty good considering... - //we don't want to use character access operators here because on STL - //it will freeze the reference count of strTmp, which means a deep copy - //at the end when swap is called - // - //Also, we can't use append with the full character pointer and must - //do it manually because this string can contain null characters - for(size_t i1 = 0; i1 < nStart; ++i1) - strTmp.append(1, this->c_str()[i1]); - - //its safe to do the full version here because - //sz must be a normal c string - strTmp.append(sz); - - for(size_t i2 = nStart + nLen; i2 < length(); ++i2) - strTmp.append(1, this->c_str()[i2]); - - swap(strTmp); - return *this; -} - -wxStringImpl& wxStringImpl::replace(size_t nStart, size_t nLen, - size_t nCount, wxStringCharType ch) + const wxStringCharType *sz, size_t nCount) { - return replace(nStart, nLen, wxStringImpl(nCount, ch).c_str()); -} + // check and adjust parameters + const size_t lenOld = length(); -wxStringImpl& wxStringImpl::replace(size_t nStart, size_t nLen, - const wxStringImpl& str, - size_t nStart2, size_t nLen2) -{ - return replace(nStart, nLen, str.substr(nStart2, nLen2)); -} + wxASSERT_MSG( nStart <= lenOld, + _T("index out of bounds in wxStringImpl::replace") ); + size_t nEnd = nStart + nLen; + if ( nLen > lenOld - nStart ) + { + // nLen may be out of range, as it can be npos, just clump it down + nLen = lenOld - nStart; + nEnd = lenOld; + } -wxStringImpl& wxStringImpl::replace(size_t nStart, size_t nLen, - const wxStringCharType* sz, size_t nCount) -{ - return replace(nStart, nLen, wxStringImpl(sz, nCount).c_str()); + if ( nCount == npos ) + nCount = wxStrlen(sz); + + // build the new string from 3 pieces: part of this string before nStart, + // the new substring and the part of this string after nStart+nLen + wxStringImpl tmp; + const size_t lenNew = lenOld + nCount - nLen; + if ( lenNew ) + { + tmp.AllocBuffer(lenOld + nCount - nLen); + + wxStringCharType *dst = tmp.m_pchData; + memcpy(dst, m_pchData, nStart*sizeof(wxStringCharType)); + dst += nStart; + + memcpy(dst, sz, nCount*sizeof(wxStringCharType)); + dst += nCount; + + memcpy(dst, m_pchData + nEnd, (lenOld - nEnd)*sizeof(wxStringCharType)); + } + + // and replace this string contents with the new one + swap(tmp); + return *this; } wxStringImpl wxStringImpl::substr(size_t nStart, size_t nLen) const @@ -708,6 +705,17 @@ bool wxStringImpl::ConcatSelf(size_t nSrcLen, if ( nSrcLen > 0 ) { wxStringData *pData = GetStringData(); size_t nLen = pData->nDataLength; + + // take special care when appending part of this string to itself: the code + // below reallocates our buffer and this invalidates pszSrcData pointer so + // we have to copy it in another temporary string in this case (but avoid + // doing this unnecessarily) + if ( pszSrcData >= m_pchData && pszSrcData < m_pchData + nLen ) + { + wxStringImpl tmp(pszSrcData, nSrcLen); + return ConcatSelf(nSrcLen, tmp.m_pchData, nSrcLen); + } + size_t nNewLen = nLen + nSrcLen; // alloc new buffer if current is too small