// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
-#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
- #pragma implementation "string.h"
-#endif
-
/*
* About ref counting:
* 1) all empty strings use g_strEmpty, nRefs = -1 (set in Init())
#include <string.h>
#include <stdlib.h>
+#ifndef __WXMSW__
+#include <errno.h>
+#endif
+
#ifdef __SALFORDC__
#include <clib.h>
#endif
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;
}
// 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;
wxStringBase strTmp;
strTmp.reserve(length()); // micro optimisation to avoid multiple mem allocs
- if ( nStart != 0 )
- strTmp.append(c_str(), nStart);
+ //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);
- strTmp.append(c_str() + nStart + nLen);
+
+ for(size_t i2 = nStart + nLen; i2 < length(); ++i2)
+ strTmp.append(1, this->c_str()[i2]);
swap(strTmp);
return *this;
}
// replace first (or all) occurences of some substring with another one
-size_t
-wxString::Replace(const wxChar *szOld, const wxChar *szNew, bool bReplaceAll)
+size_t wxString::Replace(const wxChar *szOld,
+ const wxChar *szNew, bool bReplaceAll)
{
// if we tried to replace an empty string we'd enter an infinite loop below
wxCHECK_MSG( szOld && *szOld && szNew, 0,
_T("wxString::Replace(): invalid parameter") );
- size_t uiCount = 0; // count of replacements made
-
- size_t uiOldLen = wxStrlen(szOld);
+ size_t uiCount = 0; // count of replacements made
- wxString strTemp;
- const wxChar *pCurrent = c_str();
- const wxChar *pSubstr;
- while ( *pCurrent != wxT('\0') ) {
- pSubstr = wxStrstr(pCurrent, szOld);
- if ( pSubstr == NULL ) {
- // strTemp is unused if no replacements were made, so avoid the copy
- if ( uiCount == 0 )
- return 0;
-
- strTemp += pCurrent; // copy the rest
- break; // exit the loop
- }
- else {
- // take chars before match
- size_type len = strTemp.length();
- strTemp.append(pCurrent, pSubstr - pCurrent);
- if ( strTemp.length() != (size_t)(len + pSubstr - pCurrent) ) {
- wxFAIL_MSG( _T("out of memory in wxString::Replace") );
- return 0;
- }
- strTemp += szNew;
- pCurrent = pSubstr + uiOldLen; // restart after match
+ size_t uiOldLen = wxStrlen(szOld);
+ size_t uiNewLen = wxStrlen(szNew);
- uiCount++;
+ size_t dwPos = 0;
- // stop now?
- if ( !bReplaceAll ) {
- strTemp += pCurrent; // copy the rest
- break; // exit the loop
- }
+ while ( this->c_str()[dwPos] != wxT('\0') )
+ {
+ //DO NOT USE STRSTR HERE
+ //this string can contain embedded null characters,
+ //so strstr will function incorrectly
+ dwPos = find(szOld, dwPos);
+ if ( dwPos == npos )
+ break; // exit the loop
+ else
+ {
+ //replace this occurance of the old string with the new one
+ replace(dwPos, uiOldLen, szNew, uiNewLen);
+
+ //move up pos past the old string
+ dwPos += uiOldLen;
+
+ //increase replace count
+ ++uiCount;
+
+ // stop now?
+ if ( !bReplaceAll )
+ break; // exit the loop
+ }
}
- }
-
- // only done if there were replacements, otherwise would have returned above
- swap(strTemp);
- return uiCount;
+ return uiCount;
}
bool wxString::IsAscii() const
int wxString::Printf(const wxChar *pszFormat, ...)
{
- va_list argptr;
- va_start(argptr, pszFormat);
+ va_list argptr;
+ va_start(argptr, pszFormat);
- int iLen = PrintfV(pszFormat, argptr);
+ int iLen = PrintfV(pszFormat, argptr);
- va_end(argptr);
+ va_end(argptr);
- return iLen;
+ return iLen;
}
int wxString::PrintfV(const wxChar* pszFormat, va_list argptr)
{
int size = 1024;
- int len;
for ( ;; )
{
+ wxStringBuffer tmp(*this, size + 1);
+ wxChar* buf = tmp;
+
+ if ( !buf )
{
- wxStringBuffer tmp(*this, size + 1);
- wxChar* buf = tmp;
+ // out of memory
+ return -1;
+ }
- if ( !buf )
- {
- // out of memory
- return -1;
- }
+ // wxVsnprintf() may modify the original arg pointer, so pass it
+ // only a copy
+ va_list argptrcopy;
+ wxVaCopy(argptrcopy, argptr);
+ int len = wxVsnprintf(buf, size, pszFormat, argptrcopy);
+ va_end(argptrcopy);
- // wxVsnprintf() may modify the original arg pointer, so pass it
- // only a copy
- va_list argptrcopy;
- wxVaCopy(argptrcopy, argptr);
- len = wxVsnprintf(buf, size, pszFormat, argptrcopy);
- va_end(argptrcopy);
-
- // some implementations of vsnprintf() don't NUL terminate
- // the string if there is not enough space for it so
- // always do it manually
- buf[size] = _T('\0');
- }
+ // some implementations of vsnprintf() don't NUL terminate
+ // the string if there is not enough space for it so
+ // always do it manually
+ buf[size] = _T('\0');
// 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 nor MacOS (not X)
+ // not OS/2 (not Innotek libc).
+#if !defined(__WXMSW__) && !defined(__WXPALMOS__) && !defined( __VMS ) && !(defined(__WXMAC__) && !defined(__WXMAC_OSX__)) && !(defined(__EMX__) && !defined(__INNOTEK_LIBC__))
+ || errno != EOVERFLOW
+#endif
+ )
{
// ok, there was enough space
break;
#include "wx/arrstr.h"
+wxArrayString::wxArrayString(size_t sz, const wxChar** a)
+{
+#if !wxUSE_STL
+ Init(false);
+#endif
+ for (size_t i=0; i < sz; i++)
+ Add(a[i]);
+}
+
+wxArrayString::wxArrayString(size_t sz, const wxString* a)
+{
+#if !wxUSE_STL
+ Init(false);
+#endif
+ for (size_t i=0; i < sz; i++)
+ Add(a[i]);
+}
+
#if !wxUSE_STL
// size increment = min(50% of current size, ARRAY_MAXSIZE_INCREMENT)