X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/a7ea63e21fda950c658a477d59d0818aab993fc9..48271822ef3d56c8f91af882b68fd1b674a8e8e6:/src/common/stringimpl.cpp?ds=sidebyside diff --git a/src/common/stringimpl.cpp b/src/common/stringimpl.cpp index f16811fa7a..af847b54e1 100644 --- a/src/common/stringimpl.cpp +++ b/src/common/stringimpl.cpp @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: src/common/string.cpp +// Name: src/common/stringimpl.cpp // Purpose: wxString class // Author: Vadim Zeitlin, Ryan Norton // Modified by: @@ -30,6 +30,7 @@ #ifndef WX_PRECOMP #include "wx/stringimpl.h" + #include "wx/wxcrt.h" #endif #include @@ -56,12 +57,10 @@ #define wxStringMemcpy memcpy #define wxStringMemcmp memcmp #define wxStringMemchr memchr - #define wxStringStrlen strlen #else #define wxStringMemcpy wxTmemcpy #define wxStringMemcmp wxTmemcmp #define wxStringMemchr wxTmemchr - #define wxStringStrlen wxStrlen #endif @@ -80,6 +79,10 @@ const size_t wxStringImpl::npos = (size_t) -1; #if wxUSE_STL_BASED_WXSTRING +// FIXME-UTF8: get rid of this, have only one wxEmptyString +#if wxUSE_UNICODE_UTF8 +extern const wxStringCharType WXDLLIMPEXP_BASE *wxEmptyStringImpl = ""; +#endif extern const wxChar WXDLLIMPEXP_BASE *wxEmptyString = _T(""); #else @@ -90,11 +93,17 @@ extern const wxChar WXDLLIMPEXP_BASE *wxEmptyString = _T(""); static const struct { wxStringData data; - wxChar dummy; + wxStringCharType dummy; } g_strEmpty = { {-1, 0, 0}, wxT('\0') }; // empty C style string: points to 'string data' byte of g_strEmpty -extern const wxChar WXDLLIMPEXP_BASE *wxEmptyString = &g_strEmpty.dummy; +#if wxUSE_UNICODE_UTF8 +// FIXME-UTF8: get rid of this, have only one wxEmptyString +extern const wxStringCharType WXDLLIMPEXP_BASE *wxEmptyStringImpl = &g_strEmpty.dummy; +extern const wxChar WXDLLIMPEXP_BASE *wxEmptyString = _T(""); +#else +extern const wxStringCharType WXDLLIMPEXP_BASE *wxEmptyString = &g_strEmpty.dummy; +#endif #endif @@ -111,7 +120,7 @@ extern const wxChar WXDLLIMPEXP_BASE *wxEmptyString = &g_strEmpty.dummy; class Averager { public: - Averager(const wxChar *sz) { m_sz = sz; m_nTotal = m_nCount = 0; } + 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); } @@ -119,7 +128,7 @@ extern const wxChar WXDLLIMPEXP_BASE *wxEmptyString = &g_strEmpty.dummy; private: size_t m_nCount, m_nTotal; - const wxChar *m_sz; + const wxStringCharType *m_sz; } g_averageLength("allocation size"), g_averageSummandLength("summand length"), g_averageConcatHit("hit probability in concat"), @@ -147,7 +156,8 @@ void wxStringData::Free() // =========================================================================== // takes nLength elements of psz starting at nPos -void wxStringImpl::InitWith(const wxChar *psz, size_t nPos, size_t nLength) +void wxStringImpl::InitWith(const wxStringCharType *psz, + size_t nPos, size_t nLength) { Init(); @@ -170,17 +180,15 @@ void wxStringImpl::InitWith(const wxChar *psz, size_t nPos, size_t nLength) } } -// poor man's iterators are "void *" pointers -wxStringImpl::wxStringImpl(const void *pStart, const void *pEnd) +wxStringImpl::wxStringImpl(const_iterator first, const_iterator last) { - if ( pEnd >= pStart ) + if ( last >= first ) { - InitWith((const wxChar *)pStart, 0, - (const wxChar *)pEnd - (const wxChar *)pStart); + InitWith(first, 0, last - first); } else { - wxFAIL_MSG( _T("pStart is not before pEnd") ); + wxFAIL_MSG( _T("first must be before last") ); Init(); } } @@ -203,7 +211,7 @@ bool wxStringImpl::AllocBuffer(size_t nLen) wxASSERT( nLen > 0 ); // make sure that we don't overflow - wxASSERT( nLen < (INT_MAX / sizeof(wxChar)) - + wxASSERT( nLen < (INT_MAX / sizeof(wxStringCharType)) - (sizeof(wxStringData) + EXTRA_ALLOC + 1) ); STATISTICS_ADD(Length, nLen); @@ -212,7 +220,7 @@ bool wxStringImpl::AllocBuffer(size_t nLen) // 1) one extra character for '\0' termination // 2) sizeof(wxStringData) for housekeeping info wxStringData* pData = (wxStringData*) - malloc(sizeof(wxStringData) + (nLen + EXTRA_ALLOC + 1)*sizeof(wxChar)); + malloc(sizeof(wxStringData) + (nLen + EXTRA_ALLOC + 1)*sizeof(wxStringCharType)); if ( pData == NULL ) { // allocation failures are handled by the caller @@ -271,7 +279,8 @@ bool wxStringImpl::AllocBeforeWrite(size_t nLen) nLen += EXTRA_ALLOC; pData = (wxStringData*) - realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar)); + realloc(pData, + sizeof(wxStringData) + (nLen + 1)*sizeof(wxStringCharType)); if ( pData == NULL ) { // allocation failures are handled by the caller @@ -300,6 +309,7 @@ wxStringImpl& wxStringImpl::append(size_t n, wxStringCharType ch) if ( !Alloc(len + n) || !CopyBeforeWrite() ) { wxFAIL_MSG( _T("out of memory in wxStringImpl::append") ); + return *this; } GetStringData()->nDataLength = len + n; m_pchData[len + n] = '\0'; @@ -332,7 +342,7 @@ bool wxStringImpl::Alloc(size_t nLen) nLen += EXTRA_ALLOC; pData = (wxStringData *) - malloc(sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar)); + malloc(sizeof(wxStringData) + (nLen + 1)*sizeof(wxStringCharType)); if ( pData == NULL ) { // allocation failure handled by caller @@ -353,14 +363,14 @@ bool wxStringImpl::Alloc(size_t nLen) return false; } // +1 to copy the terminator, too - memcpy(m_pchData, pData->data(), (nOldLen+1)*sizeof(wxChar)); + memcpy(m_pchData, pData->data(), (nOldLen+1)*sizeof(wxStringCharType)); GetStringData()->nDataLength = nOldLen; } else { nLen += EXTRA_ALLOC; pData = (wxStringData *) - realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar)); + realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(wxStringCharType)); if ( pData == NULL ) { // allocation failure handled by caller @@ -412,7 +422,8 @@ wxStringImpl& wxStringImpl::erase(size_t nStart, size_t nLen) return *this; } -wxStringImpl& wxStringImpl::insert(size_t nPos, const wxChar *sz, size_t n) +wxStringImpl& wxStringImpl::insert(size_t nPos, + const wxStringCharType *sz, size_t n) { wxASSERT( nPos <= length() ); @@ -421,11 +432,12 @@ wxStringImpl& wxStringImpl::insert(size_t nPos, const wxChar *sz, size_t n) if ( !Alloc(length() + n) || !CopyBeforeWrite() ) { wxFAIL_MSG( _T("out of memory in wxStringImpl::insert") ); + return *this; } memmove(m_pchData + nPos + n, m_pchData + nPos, - (length() - nPos) * sizeof(wxChar)); - memcpy(m_pchData + nPos, sz, n * sizeof(wxChar)); + (length() - nPos) * sizeof(wxStringCharType)); + memcpy(m_pchData + nPos, sz, n * sizeof(wxStringCharType)); GetStringData()->nDataLength = length() + n; m_pchData[length()] = '\0'; @@ -487,7 +499,8 @@ size_t wxStringImpl::find(const wxStringImpl& str, size_t nStart) const return p - c_str() + nLenOther <= nLen ? p - c_str() : npos; } -size_t wxStringImpl::find(const wxChar* sz, size_t nStart, size_t n) const +size_t wxStringImpl::find(const wxStringCharType* sz, + size_t nStart, size_t n) const { return find(wxStringImpl(sz, n), nStart); } @@ -534,7 +547,8 @@ size_t wxStringImpl::rfind(const wxStringImpl& str, size_t nStart) const return npos; } -size_t wxStringImpl::rfind(const wxChar* sz, size_t nStart, size_t n) const +size_t wxStringImpl::rfind(const wxStringCharType* sz, + size_t nStart, size_t n) const { return rfind(wxStringImpl(sz, n), nStart); } @@ -562,7 +576,7 @@ size_t wxStringImpl::rfind(wxStringCharType ch, size_t nStart) const } wxStringImpl& wxStringImpl::replace(size_t nStart, size_t nLen, - const wxChar *sz) + const wxStringCharType *sz) { wxASSERT_MSG( nStart <= length(), _T("index out of bounds in wxStringImpl::replace") ); @@ -607,7 +621,7 @@ wxStringImpl& wxStringImpl::replace(size_t nStart, size_t nLen, } wxStringImpl& wxStringImpl::replace(size_t nStart, size_t nLen, - const wxChar* sz, size_t nCount) + const wxStringCharType* sz, size_t nCount) { return replace(nStart, nLen, wxStringImpl(sz, nCount).c_str()); } @@ -643,24 +657,25 @@ wxStringImpl& wxStringImpl::operator=(const wxStringImpl& stringSrc) // assigns a single character wxStringImpl& wxStringImpl::operator=(wxStringCharType ch) { - wxChar c(ch); + wxStringCharType c(ch); if ( !AssignCopy(1, &c) ) { - wxFAIL_MSG( _T("out of memory in wxStringImpl::operator=(wxChar)") ); + wxFAIL_MSG( _T("out of memory in wxStringImpl::operator=(wxStringCharType)") ); } return *this; } // assigns C string -wxStringImpl& wxStringImpl::operator=(const wxChar *psz) +wxStringImpl& wxStringImpl::operator=(const wxStringCharType *psz) { if ( !AssignCopy(wxStrlen(psz), psz) ) { - wxFAIL_MSG( _T("out of memory in wxStringImpl::operator=(const wxChar *)") ); + wxFAIL_MSG( _T("out of memory in wxStringImpl::operator=(const wxStringCharType *)") ); } return *this; } // helper function: does real copy -bool wxStringImpl::AssignCopy(size_t nSrcLen, const wxChar *pszSrcData) +bool wxStringImpl::AssignCopy(size_t nSrcLen, + const wxStringCharType *pszSrcData) { if ( nSrcLen == 0 ) { Reinit(); @@ -670,7 +685,7 @@ bool wxStringImpl::AssignCopy(size_t nSrcLen, const wxChar *pszSrcData) // allocation failure handled by caller return false; } - memcpy(m_pchData, pszSrcData, nSrcLen*sizeof(wxChar)); + memcpy(m_pchData, pszSrcData, nSrcLen*sizeof(wxStringCharType)); GetStringData()->nDataLength = nSrcLen; m_pchData[nSrcLen] = wxT('\0'); } @@ -682,7 +697,8 @@ bool wxStringImpl::AssignCopy(size_t nSrcLen, const wxChar *pszSrcData) // --------------------------------------------------------------------------- // add something to this string -bool wxStringImpl::ConcatSelf(size_t nSrcLen, const wxChar *pszSrcData, +bool wxStringImpl::ConcatSelf(size_t nSrcLen, + const wxStringCharType *pszSrcData, size_t nMaxLen) { STATISTICS_ADD(SummandLength, nSrcLen); @@ -705,7 +721,7 @@ bool wxStringImpl::ConcatSelf(size_t nSrcLen, const wxChar *pszSrcData, // allocation failure handled by caller return false; } - memcpy(m_pchData, pOldData->data(), nLen*sizeof(wxChar)); + memcpy(m_pchData, pOldData->data(), nLen*sizeof(wxStringCharType)); pOldData->Unlock(); } else if ( nNewLen > pData->nAllocLength ) { @@ -728,7 +744,7 @@ bool wxStringImpl::ConcatSelf(size_t nSrcLen, const wxChar *pszSrcData, wxASSERT( nNewLen <= GetStringData()->nAllocLength ); // fast concatenation - all is done in our buffer - memcpy(m_pchData + nLen, pszSrcData, nSrcLen*sizeof(wxChar)); + memcpy(m_pchData + nLen, pszSrcData, nSrcLen*sizeof(wxStringCharType)); m_pchData[nNewLen] = wxT('\0'); // put terminating '\0' GetStringData()->nDataLength = nNewLen; // and fix the length @@ -737,9 +753,8 @@ bool wxStringImpl::ConcatSelf(size_t nSrcLen, const wxChar *pszSrcData, return true; } -#if !wxUSE_UNICODE_UTF8 // get the pointer to writable buffer of (at least) nLen bytes -wxChar *wxStringImpl::DoGetWriteBuf(size_t nLen) +wxStringCharType *wxStringImpl::DoGetWriteBuf(size_t nLen) { if ( !AllocBeforeWrite(nLen) ) { // allocation failure handled by caller @@ -769,6 +784,5 @@ void wxStringImpl::DoUngetWriteBuf(size_t nLen) pData->nDataLength = nLen; pData->Validate(true); } -#endif // !wxUSE_UNICODE_UTF8 #endif // !wxUSE_STL_BASED_WXSTRING