+ else {
+ nLen += EXTRA_ALLOC;
+
+ pData = (wxStringData *)
+ realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar));
+
+ if ( pData == NULL ) {
+ // allocation failure handled by caller
+ // keep previous data since reallocation failed
+ return false;
+ }
+
+ // it's not important if the pointer changed or not (the check for this
+ // is not faster than assigning to m_pchData in all cases)
+ pData->nAllocLength = nLen;
+ m_pchData = pData->data();
+ }
+ }
+ //else: we've already got enough
+ return true;
+}
+
+wxStringBase::iterator wxStringBase::begin()
+{
+ if (length() > 0)
+ CopyBeforeWrite();
+ return m_pchData;
+}
+
+wxStringBase::iterator wxStringBase::end()
+{
+ if (length() > 0)
+ CopyBeforeWrite();
+ return m_pchData + length();
+}
+
+wxStringBase::iterator wxStringBase::erase(iterator it)
+{
+ size_type idx = it - begin();
+ erase(idx, 1);
+ return begin() + idx;
+}
+
+wxStringBase& wxStringBase::erase(size_t nStart, size_t nLen)
+{
+ wxASSERT(nStart <= length());
+ size_t strLen = length() - nStart;
+ // delete nLen or up to the end of the string characters
+ nLen = strLen < nLen ? strLen : nLen;
+ wxString strTmp(c_str(), nStart);
+ strTmp.append(c_str() + nStart + nLen, length() - nStart - nLen);
+
+ swap(strTmp);
+ return *this;
+}
+
+wxStringBase& wxStringBase::insert(size_t nPos, const wxChar *sz, size_t n)
+{
+ wxASSERT( nPos <= length() );
+
+ if ( n == npos ) n = wxStrlen(sz);
+ if ( n == 0 ) return *this;
+
+ if ( !CopyBeforeWrite() || !Alloc(length() + n) ) {
+ wxFAIL_MSG( _T("out of memory in wxStringBase::insert") );
+ }
+
+ memmove(m_pchData + nPos + n, m_pchData + nPos,
+ (length() - nPos) * sizeof(wxChar));
+ memcpy(m_pchData + nPos, sz, n * sizeof(wxChar));
+ GetStringData()->nDataLength = length() + n;
+ m_pchData[length()] = '\0';
+
+ return *this;
+}
+
+void wxStringBase::swap(wxStringBase& str)
+{
+ wxChar* tmp = str.m_pchData;
+ str.m_pchData = m_pchData;
+ m_pchData = tmp;
+}
+
+size_t wxStringBase::find(const wxStringBase& str, size_t nStart) const
+{
+ wxASSERT( str.GetStringData()->IsValid() );
+ wxASSERT( nStart <= length() );
+
+ //anchor
+ const wxChar* p = (const wxChar*)wxMemchr(c_str() + nStart,
+ str.c_str()[0],
+ length() - nStart);
+
+ if(!p)
+ return npos;
+
+ while(p - c_str() + str.length() <= length() &&
+ wxMemcmp(p, str.c_str(), str.length()) )
+ {
+ //anchor again
+ p = (const wxChar*)wxMemchr(++p,
+ str.c_str()[0],
+ length() - (p - c_str()));
+
+ if(!p)
+ return npos;
+ }
+
+ return (p - c_str() + str.length() <= length()) ? p - c_str() : npos;
+}
+
+size_t wxStringBase::find(const wxChar* sz, size_t nStart, size_t n) const
+{
+ return find(wxStringBase(sz, n), nStart);
+}
+
+size_t wxStringBase::find(wxChar ch, size_t nStart) const
+{
+ wxASSERT( nStart <= length() );
+
+ const wxChar *p = (const wxChar*)wxMemchr(c_str() + nStart, ch, length() - nStart);
+
+ return p == NULL ? npos : p - c_str();
+}
+
+size_t wxStringBase::rfind(const wxStringBase& str, size_t nStart) const
+{
+ wxASSERT( str.GetStringData()->IsValid() );
+ wxASSERT( nStart == npos || nStart <= length() );
+
+ if ( length() >= str.length() )
+ {
+ // avoids a corner case later
+ if ( length() == 0 && str.length() == 0 )
+ return 0;
+
+ // "top" is the point where search starts from
+ size_t top = length() - str.length();
+
+ if ( nStart == npos )
+ nStart = length() - 1;
+ if ( nStart < top )
+ top = nStart;
+
+ const wxChar *cursor = c_str() + top;
+ do
+ {
+ if ( wxMemcmp(cursor, str.c_str(),
+ str.length()) == 0 )
+ {
+ return cursor - c_str();
+ }
+ } while ( cursor-- > c_str() );
+ }
+
+ return npos;
+}
+
+size_t wxStringBase::rfind(const wxChar* sz, size_t nStart, size_t n) const
+{
+ return rfind(wxStringBase(sz, n), nStart);
+}
+
+size_t wxStringBase::rfind(wxChar ch, size_t nStart) const
+{
+ if ( nStart == npos )
+ {
+ nStart = length();
+ }
+ else
+ {
+ wxASSERT( nStart <= length() );
+ }
+
+ const wxChar *actual;
+ for ( actual = c_str() + ( nStart == npos ? length() : nStart + 1 );
+ actual > c_str(); --actual )
+ {
+ if ( *(actual - 1) == ch )
+ return (actual - 1) - c_str();
+ }
+
+ return npos;
+}
+
+size_t wxStringBase::find_first_of(const wxChar* sz, size_t nStart) const
+{
+ wxASSERT(nStart <= length());
+
+ size_t len = wxStrlen(sz);
+
+ size_t i;
+ for(i = nStart; i < this->length(); ++i)
+ {
+ if (wxMemchr(sz, *(c_str() + i), len))
+ break;
+ }
+
+ if(i == this->length())
+ return npos;
+ else
+ return i;
+}
+
+size_t wxStringBase::find_first_of(const wxChar* sz, size_t nStart,
+ size_t n) const
+{
+ return find_first_of(wxStringBase(sz, n), nStart);
+}
+
+size_t wxStringBase::find_last_of(const wxChar* sz, size_t nStart) const
+{
+ if ( nStart == npos )
+ {
+ nStart = length() - 1;
+ }
+ else
+ {
+ wxASSERT_MSG( nStart <= length(),
+ _T("invalid index in find_last_of()") );
+ }
+
+ size_t len = wxStrlen(sz);
+
+ for ( const wxChar *p = c_str() + nStart; p >= c_str(); --p )
+ {
+ if ( wxMemchr(sz, *p, len) )
+ return p - c_str();
+ }
+
+ return npos;
+}
+
+size_t wxStringBase::find_last_of(const wxChar* sz, size_t nStart,
+ size_t n) const
+{
+ return find_last_of(wxStringBase(sz, n), nStart);
+}
+
+size_t wxStringBase::find_first_not_of(const wxChar* sz, size_t nStart) const
+{
+ if ( nStart == npos )
+ {
+ nStart = length();
+ }
+ else
+ {
+ wxASSERT( nStart <= length() );
+ }
+
+ size_t len = wxStrlen(sz);
+
+ size_t i;
+ for(i = nStart; i < this->length(); ++i)
+ {
+ if (!wxMemchr(sz, *(c_str() + i), len))
+ break;
+ }
+
+ if(i == this->length())
+ return npos;
+ else
+ return i;
+}
+
+size_t wxStringBase::find_first_not_of(const wxChar* sz, size_t nStart,
+ size_t n) const
+{
+ return find_first_not_of(wxStringBase(sz, n), nStart);
+}
+
+size_t wxStringBase::find_first_not_of(wxChar ch, size_t nStart) const
+{
+ wxASSERT( nStart <= length() );
+
+ for ( const wxChar *p = c_str() + nStart; *p; p++ )
+ {
+ if ( *p != ch )
+ return p - c_str();
+ }
+
+ return npos;
+}
+
+size_t wxStringBase::find_last_not_of(const wxChar* sz, size_t nStart) const
+{
+ if ( nStart == npos )
+ {
+ nStart = length() - 1;
+ }
+ else
+ {
+ wxASSERT( nStart <= length() );
+ }
+
+ size_t len = wxStrlen(sz);
+
+ for ( const wxChar *p = c_str() + nStart; p >= c_str(); --p )
+ {
+ if ( !wxMemchr(sz, *p,len) )
+ return p - c_str();
+ }
+
+ return npos;
+}
+
+size_t wxStringBase::find_last_not_of(const wxChar* sz, size_t nStart,
+ size_t n) const
+{
+ return find_last_not_of(wxStringBase(sz, n), nStart);
+}
+
+size_t wxStringBase::find_last_not_of(wxChar ch, size_t nStart) const
+{
+ if ( nStart == npos )
+ {
+ nStart = length() - 1;
+ }
+ else
+ {
+ wxASSERT( nStart <= length() );
+ }
+
+ for ( const wxChar *p = c_str() + nStart; p >= c_str(); --p )
+ {
+ if ( *p != ch )
+ return p - c_str();
+ }
+
+ return npos;
+}
+
+wxStringBase& wxStringBase::replace(size_t nStart, size_t nLen,
+ const wxChar *sz)
+{
+ wxASSERT_MSG( nStart <= length(),
+ _T("index out of bounds in wxStringBase::replace") );
+ size_t strLen = length() - nStart;
+ nLen = strLen < nLen ? strLen : nLen;
+
+ wxStringBase strTmp;
+ strTmp.reserve(length()); // micro optimisation to avoid multiple mem allocs
+
+ if ( nStart != 0 )
+ strTmp.append(c_str(), nStart);
+ strTmp.append(sz);
+ strTmp.append(c_str() + nStart + nLen);
+
+ swap(strTmp);
+ return *this;
+}
+
+wxStringBase& wxStringBase::replace(size_t nStart, size_t nLen,
+ size_t nCount, wxChar ch)
+{
+ return replace(nStart, nLen, wxStringBase(nCount, ch).c_str());
+}
+
+wxStringBase& wxStringBase::replace(size_t nStart, size_t nLen,
+ const wxStringBase& str,
+ size_t nStart2, size_t nLen2)
+{
+ return replace(nStart, nLen, str.substr(nStart2, nLen2));
+}
+
+wxStringBase& wxStringBase::replace(size_t nStart, size_t nLen,
+ const wxChar* sz, size_t nCount)
+{
+ return replace(nStart, nLen, wxStringBase(sz, nCount).c_str());
+}
+
+wxStringBase wxStringBase::substr(size_t nStart, size_t nLen) const
+{
+ if ( nLen == npos )
+ nLen = length() - nStart;
+ return wxStringBase(*this, nStart, nLen);
+}
+
+// assigns one string to another
+wxStringBase& wxStringBase::operator=(const wxStringBase& stringSrc)
+{
+ wxASSERT( stringSrc.GetStringData()->IsValid() );
+
+ // don't copy string over itself
+ if ( m_pchData != stringSrc.m_pchData ) {
+ if ( stringSrc.GetStringData()->IsEmpty() ) {
+ Reinit();
+ }
+ else {
+ // adjust references
+ GetStringData()->Unlock();
+ m_pchData = stringSrc.m_pchData;
+ GetStringData()->Lock();
+ }
+ }
+
+ return *this;
+}
+
+// assigns a single character
+wxStringBase& wxStringBase::operator=(wxChar ch)
+{
+ if ( !AssignCopy(1, &ch) ) {
+ wxFAIL_MSG( _T("out of memory in wxStringBase::operator=(wxChar)") );
+ }
+ return *this;
+}
+
+// assigns C string
+wxStringBase& wxStringBase::operator=(const wxChar *psz)
+{
+ if ( !AssignCopy(wxStrlen(psz), psz) ) {
+ wxFAIL_MSG( _T("out of memory in wxStringBase::operator=(const wxChar *)") );
+ }
+ return *this;
+}
+
+// helper function: does real copy
+bool wxStringBase::AssignCopy(size_t nSrcLen, const wxChar *pszSrcData)
+{
+ if ( nSrcLen == 0 ) {
+ Reinit();
+ }
+ else {
+ if ( !AllocBeforeWrite(nSrcLen) ) {
+ // allocation failure handled by caller
+ return false;
+ }
+ memcpy(m_pchData, pszSrcData, nSrcLen*sizeof(wxChar));
+ GetStringData()->nDataLength = nSrcLen;
+ m_pchData[nSrcLen] = wxT('\0');
+ }
+ return true;
+}
+
+// ---------------------------------------------------------------------------
+// string concatenation
+// ---------------------------------------------------------------------------
+
+// add something to this string
+bool wxStringBase::ConcatSelf(size_t nSrcLen, const wxChar *pszSrcData,
+ size_t nMaxLen)
+{
+ STATISTICS_ADD(SummandLength, nSrcLen);
+
+ nSrcLen = nSrcLen < nMaxLen ? nSrcLen : nMaxLen;
+
+ // concatenating an empty string is a NOP
+ if ( nSrcLen > 0 ) {
+ wxStringData *pData = GetStringData();
+ size_t nLen = pData->nDataLength;
+ size_t nNewLen = nLen + nSrcLen;
+
+ // alloc new buffer if current is too small
+ if ( pData->IsShared() ) {
+ STATISTICS_ADD(ConcatHit, 0);
+
+ // we have to allocate another buffer
+ wxStringData* pOldData = GetStringData();
+ if ( !AllocBuffer(nNewLen) ) {
+ // allocation failure handled by caller
+ return false;
+ }
+ memcpy(m_pchData, pOldData->data(), nLen*sizeof(wxChar));
+ pOldData->Unlock();
+ }
+ else if ( nNewLen > pData->nAllocLength ) {
+ STATISTICS_ADD(ConcatHit, 0);
+
+ reserve(nNewLen);
+ // we have to grow the buffer
+ if ( capacity() < nNewLen ) {
+ // allocation failure handled by caller
+ return false;
+ }
+ }
+ else {
+ STATISTICS_ADD(ConcatHit, 1);
+
+ // the buffer is already big enough
+ }
+
+ // should be enough space
+ wxASSERT( nNewLen <= GetStringData()->nAllocLength );
+
+ // fast concatenation - all is done in our buffer
+ memcpy(m_pchData + nLen, pszSrcData, nSrcLen*sizeof(wxChar));
+
+ m_pchData[nNewLen] = wxT('\0'); // put terminating '\0'
+ GetStringData()->nDataLength = nNewLen; // and fix the length
+ }
+ //else: the string to append was empty
+ return true;
+}
+
+// ---------------------------------------------------------------------------
+// simple sub-string extraction
+// ---------------------------------------------------------------------------
+
+// helper function: clone the data attached to this string
+bool wxStringBase::AllocCopy(wxString& dest, int nCopyLen, int nCopyIndex) const
+{
+ if ( nCopyLen == 0 ) {
+ dest.Init();
+ }
+ else {
+ if ( !dest.AllocBuffer(nCopyLen) ) {
+ // allocation failure handled by caller
+ return false;
+ }
+ memcpy(dest.m_pchData, m_pchData + nCopyIndex, nCopyLen*sizeof(wxChar));
+ }
+ return true;
+}
+
+#endif // !wxUSE_STL
+
+#if !wxUSE_STL || !defined(HAVE_STD_STRING_COMPARE)
+
+#if !wxUSE_STL
+ #define STRINGCLASS wxStringBase
+#else
+ #define STRINGCLASS wxString
+#endif
+
+static inline int wxDoCmp(const wxChar* s1, size_t l1,
+ const wxChar* s2, size_t l2)
+{
+ if( l1 == l2 )
+ return wxMemcmp(s1, s2, l1);
+ else if( l1 < l2 )
+ {
+ int ret = wxMemcmp(s1, s2, l1);
+ return ret == 0 ? -1 : ret;
+ }
+ else if( l1 > l2 )
+ {
+ int ret = wxMemcmp(s1, s2, l2);
+ return ret == 0 ? +1 : ret;
+ }
+
+ wxFAIL; // must never get there
+ return 0; // quiet compilers
+}
+
+int STRINGCLASS::compare(const wxStringBase& str) const
+{
+ return ::wxDoCmp(data(), length(), str.data(), str.length());
+}
+
+int STRINGCLASS::compare(size_t nStart, size_t nLen,
+ const wxStringBase& str) const
+{
+ wxASSERT(nStart <= length());
+ size_type strLen = length() - nStart;
+ nLen = strLen < nLen ? strLen : nLen;
+ return ::wxDoCmp(data() + nStart, nLen, str.data(), str.length());
+}
+
+int STRINGCLASS::compare(size_t nStart, size_t nLen,
+ const wxStringBase& str,
+ size_t nStart2, size_t nLen2) const
+{
+ wxASSERT(nStart <= length());
+ wxASSERT(nStart2 <= str.length());
+ size_type strLen = length() - nStart,
+ strLen2 = str.length() - nStart2;
+ nLen = strLen < nLen ? strLen : nLen;
+ nLen2 = strLen2 < nLen2 ? strLen2 : nLen2;
+ return ::wxDoCmp(data() + nStart, nLen, str.data() + nStart2, nLen2);
+}
+
+int STRINGCLASS::compare(const wxChar* sz) const
+{
+ size_t nLen = wxStrlen(sz);
+ return ::wxDoCmp(data(), length(), sz, nLen);
+}
+
+int STRINGCLASS::compare(size_t nStart, size_t nLen,
+ const wxChar* sz, size_t nCount) const
+{
+ wxASSERT(nStart <= length());
+ size_type strLen = length() - nStart;
+ nLen = strLen < nLen ? strLen : nLen;
+ if( nCount == npos )
+ nCount = wxStrlen(sz);
+
+ return ::wxDoCmp(data() + nStart, nLen, sz, nCount);
+}
+
+#undef STRINGCLASS
+
+#endif // !wxUSE_STL || !defined(HAVE_STD_STRING_COMPARE)
+
+// ===========================================================================
+// wxString class core
+// ===========================================================================
+
+// ---------------------------------------------------------------------------
+// common conversion routines
+// ---------------------------------------------------------------------------
+
+size_t wxString::WorstEncodingCase(size_t len, const wxMBConv& WXUNUSED(conv))
+{
+ //Worst case for UTF7
+ return len * 5;
+}
+
+#if wxUSE_WCHAR_T
+
+//Convert a wide character string of a specified length
+//to a multi-byte character string, ignoring intermittent null characters
+//returns the actual length of the string
+inline size_t wxMbstr(char* szBuffer, const wchar_t* szString,
+ size_t nStringLen, wxMBConv& conv)
+{
+ const wchar_t* szEnd = szString + nStringLen + 1;
+ const wchar_t* szPos = szString;
+ const wchar_t* szStart = szPos;
+
+ size_t nActualLength = 0;
+
+ //Convert the string until the length() is reached, continuing the
+ //loop every time a null character is reached
+ while(szPos != szEnd)
+ {
+ wxASSERT(szPos < szEnd); //something is _really_ screwed up if this rings true
+
+ //Get the length of the current (sub)string
+ size_t nLen = conv.WC2MB(NULL, szPos, 0);
+
+ //Invalid conversion?
+ if( nLen == (size_t)-1 )
+ {
+ szBuffer[0] = '\0';
+ return 0;
+ }
+
+ //Increase the actual length (+1 for current null character)
+ nActualLength += nLen + 1;
+
+ //If this is true it means buffer overflow
+ wxASSERT( nActualLength <= wxString::WorstEncodingCase(nStringLen, conv) + 1 );
+
+ //Convert the current (sub)string
+ if(conv.WC2MB(&szBuffer[szPos - szStart], szPos, nLen + 1) == (size_t)-1 )
+ {
+ //error - return empty buffer
+ wxFAIL_MSG(wxT("Error converting wide-character string to a multi-byte string"));
+ szBuffer[0] = '\0';
+ return 0;
+ }
+
+ //Increment to next (sub)string
+ //Note that we have to use wxWcslen here instead of nLen
+ //here because XX2XX gives us the size of the output buffer,
+ //not neccessarly the length of the string
+ szPos += wxWcslen(szPos) + 1;
+ }
+
+ return nActualLength - 1; //success - return actual length
+}
+
+//Convert a multi-byte character string of a specified length
+//to a wide character string, ignoring intermittent null characters
+//returns the actual length
+inline size_t wxWcstr( wchar_t* szBuffer, const char* szString,
+ size_t nStringLen, wxMBConv& conv)
+{
+ const char* szEnd = szString + nStringLen + 1;
+ const char* szPos = szString;
+ const char* szStart = szPos;
+
+ size_t nActualLength = 0;
+
+ //Convert the string until the length() is reached, continuing the
+ //loop every time a null character is reached
+ while(szPos != szEnd)
+ {
+ wxASSERT(szPos < szEnd); //something is _really_ screwed up if this rings true
+
+ //Get the length of the current (sub)string
+ size_t nLen = conv.MB2WC(NULL, szPos, 0);
+
+ //Invalid conversion?
+ if( nLen == (size_t)-1 )
+ {
+ szBuffer[0] = '\0';
+ return 0;
+ }
+
+ //Increase the actual length (+1 for current null character)
+ nActualLength += nLen + 1;
+
+ //If this is true it means buffer overflow
+ wxASSERT(nActualLength <= nStringLen + 1);
+
+ //Convert the current (sub)string
+ if ( conv.MB2WC(&szBuffer[szPos - szStart], szPos, nLen + 1) == (size_t)-1 )
+ {
+ //error - return empty buffer
+ wxFAIL_MSG(wxT("Error converting multi-byte string to a wide-character string"));
+ szBuffer[0] = '\0';
+ return 0;
+ }
+
+ //Increment to next (sub)string
+ //Note that we have to use strlen here instead of nLen
+ //here because XX2XX gives us the size of the output buffer,
+ //not neccessarly the length of the string
+ szPos += strlen(szPos) + 1;
+ }
+
+ return nActualLength - 1; //success - return actual length
+}
+
+#endif //wxUSE_WCHAR_T
+
+// ---------------------------------------------------------------------------
+// construction and conversion
+// ---------------------------------------------------------------------------
+
+#if wxUSE_UNICODE
+
+// from multibyte string
+wxString::wxString(const char *psz, wxMBConv& conv, size_t nLength)
+{
+ // if nLength != npos, then we have to make a NULL-terminated copy
+ // of first nLength bytes of psz first because the input buffer to MB2WC
+ // must always be NULL-terminated:
+ wxCharBuffer inBuf((const char *)NULL);
+ if (nLength != npos)
+ {
+ wxASSERT( psz != NULL );
+ wxCharBuffer tmp(nLength);
+ memcpy(tmp.data(), psz, nLength);
+ tmp.data()[nLength] = '\0';
+ inBuf = tmp;
+ psz = inBuf.data();
+ }
+
+ // first get the size of the buffer we need
+ size_t nLen;
+ if ( psz )
+ {
+ // calculate the needed size ourselves or use the provided one
+ if (nLength == npos)
+ nLen = strlen(psz);
+ else
+ nLen = nLength;
+ }
+ else
+ {
+ // nothing to convert
+ nLen = 0;
+ }