X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/3168a13f907e99a97a835514ab1832f0151f040c..08ee50db7c47941e8dec81ae81e357083a3d606e:/src/common/string.cpp?ds=sidebyside diff --git a/src/common/string.cpp b/src/common/string.cpp index c2246e4991..3cb10f3186 100644 --- a/src/common/string.cpp +++ b/src/common/string.cpp @@ -32,21 +32,27 @@ #endif #ifndef WX_PRECOMP -#include "wx/defs.h" -#include "wx/string.h" + #include "wx/defs.h" + #include "wx/string.h" + #include "wx/intl.h" #endif #include #include #include +#ifdef wxUSE_WCSRTOMBS + #include // for wcsrtombs(), see comments where it's used +#endif // GNU + #ifdef WXSTRING_IS_WXOBJECT IMPLEMENT_DYNAMIC_CLASS(wxString, wxObject) #endif //WXSTRING_IS_WXOBJECT // allocating extra space for each string consumes more memory but speeds up // the concatenation operations (nLen is the current string's length) -#define EXTRA_ALLOC 16 +// NB: EXTRA_ALLOC must be >= 0! +#define EXTRA_ALLOC (19 - nLen % 16) // --------------------------------------------------------------------------- // static class variables definition @@ -60,13 +66,17 @@ // static data // ---------------------------------------------------------------------------- -// for an empty string, GetStringData() will return this address -static int g_strEmpty[] = { -1, // ref count (locked) - 0, // current length - 0, // allocated memory - 0 }; // string data +// for an empty string, GetStringData() will return this address: this +// structure has the same layout as wxStringData and it's data() method will +// return the empty string (dummy pointer) +static const struct +{ + wxStringData data; + char dummy; +} g_strEmpty = { {-1, 0, 0}, '\0' }; + // empty C style string: points to 'string data' byte of g_strEmpty -extern const char *g_szNul = (const char *)(&g_strEmpty[3]); +extern const char *g_szNul = &g_strEmpty.dummy; // ---------------------------------------------------------------------------- // global functions @@ -86,6 +96,18 @@ extern const char *g_szNul = (const char *)(&g_strEmpty[3]); #define NAMESPACE #endif //Visual C++ +#if wxUSE_IOSTREAMH +#include +#define NAMESPACE +#else +#include +# ifdef _MSC_VER + using namespace std; +# endif +#define NAMESPACE std:: +#endif + + NAMESPACE istream& operator>>(NAMESPACE istream& is, wxString& WXUNUSED(str)) { #if 0 @@ -130,13 +152,13 @@ NAMESPACE istream& operator>>(NAMESPACE istream& is, wxString& WXUNUSED(str)) { public: Averager(const char *sz) { m_sz = sz; m_nTotal = m_nCount = 0; } - ~Averager() + ~Averager() { printf("wxString: average %s = %f\n", m_sz, ((float)m_nTotal)/m_nCount); } - void Add(uint n) { m_nTotal += n; m_nCount++; } + void Add(size_t n) { m_nTotal += n; m_nCount++; } private: - uint m_nCount, m_nTotal; + size_t m_nCount, m_nTotal; const char *m_sz; } g_averageLength("allocation size"), g_averageSummandLength("summand length"), @@ -210,7 +232,17 @@ wxString::wxString(const void *pStart, const void *pEnd) wxString::wxString(const wchar_t *pwz) { // first get necessary size - size_t nLen = wcstombs(NULL, pwz, 0); + + // NB: GNU libc5 wcstombs() is completely broken, don't use it (it doesn't + // honor the 3rd parameter, thus it will happily crash here). +#ifdef wxUSE_WCSRTOMBS + // don't know if it's really needed (or if we can pass NULL), but better safe + // than quick + mbstate_t mbstate; + size_t nLen = wcsrtombs((char *) NULL, &pwz, 0, &mbstate); +#else // !GNU libc + size_t nLen = wcstombs((char *) NULL, pwz, 0); +#endif // GNU // empty? if ( nLen != 0 ) { @@ -253,12 +285,12 @@ void wxString::CopyBeforeWrite() if ( pData->IsShared() ) { pData->Unlock(); // memory not freed because shared - uint nLen = pData->nDataLength; + size_t nLen = pData->nDataLength; AllocBuffer(nLen); memcpy(m_pchData, pData->data(), nLen*sizeof(char)); } - wxASSERT( !pData->IsShared() ); // we must be the only owner + wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner } // must be called before replacing contents of this string @@ -278,17 +310,26 @@ void wxString::AllocBeforeWrite(size_t nLen) } // allocate enough memory for nLen characters -void wxString::Alloc(uint nLen) +void wxString::Alloc(size_t nLen) { wxStringData *pData = GetStringData(); if ( pData->nAllocLength <= nLen ) { - if ( pData->IsEmpty() ) - AllocBuffer(nLen); + if ( pData->IsEmpty() ) { + nLen += EXTRA_ALLOC; + + wxStringData* pData = (wxStringData*) + malloc(sizeof(wxStringData) + (nLen + 1)*sizeof(char)); + pData->nRefs = 1; + pData->nDataLength = 0; + pData->nAllocLength = nLen; + m_pchData = pData->data(); // data starts after wxStringData + m_pchData[0u] = '\0'; + } else if ( pData->IsShared() ) { pData->Unlock(); // memory not freed because shared - uint nLen = pData->nDataLength; + size_t nOldLen = pData->nDataLength; AllocBuffer(nLen); - memcpy(m_pchData, pData->data(), nLen*sizeof(char)); + memcpy(m_pchData, pData->data(), nOldLen*sizeof(char)); } else { nLen += EXTRA_ALLOC; @@ -314,14 +355,20 @@ void wxString::Alloc(uint nLen) void wxString::Shrink() { wxStringData *pData = GetStringData(); - void *p = realloc(pData, sizeof(wxStringData) + - (pData->nDataLength + 1)*sizeof(char)); + + // this variable is unused in release build, so avoid the compiler warning by + // just not declaring it +#ifdef __WXDEBUG__ + void *p = +#endif + realloc(pData, sizeof(wxStringData) + (pData->nDataLength + 1)*sizeof(char)); + wxASSERT( p != NULL ); // can't free memory? wxASSERT( p == pData ); // we're decrementing the size - block shouldn't move! } // get the pointer to writable buffer of (at least) nLen bytes -char *wxString::GetWriteBuf(uint nLen) +char *wxString::GetWriteBuf(size_t nLen) { AllocBeforeWrite(nLen); @@ -420,43 +467,44 @@ void wxString::ConcatSelf(int nSrcLen, const char *pszSrcData) { STATISTICS_ADD(SummandLength, nSrcLen); - // concatenating an empty string is a NOP, but it happens quite rarely, - // so we don't waste our time checking for it - // if ( nSrcLen > 0 ) - wxStringData *pData = GetStringData(); - uint nLen = pData->nDataLength; - uint 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(); - AllocBuffer(nNewLen); - memcpy(m_pchData, pOldData->data(), nLen*sizeof(char)); - pOldData->Unlock(); - } - else if ( nNewLen > pData->nAllocLength ) { - STATISTICS_ADD(ConcatHit, 0); + // 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(); + AllocBuffer(nNewLen); + memcpy(m_pchData, pOldData->data(), nLen*sizeof(char)); + pOldData->Unlock(); + } + else if ( nNewLen > pData->nAllocLength ) { + STATISTICS_ADD(ConcatHit, 0); - // we have to grow the buffer - Alloc(nNewLen); - } - else { - STATISTICS_ADD(ConcatHit, 1); + // we have to grow the buffer + Alloc(nNewLen); + } + else { + STATISTICS_ADD(ConcatHit, 1); - // the buffer is already big enough - } + // the buffer is already big enough + } - // should be enough space - wxASSERT( nNewLen <= GetStringData()->nAllocLength ); + // should be enough space + wxASSERT( nNewLen <= GetStringData()->nAllocLength ); - // fast concatenation - all is done in our buffer - memcpy(m_pchData + nLen, pszSrcData, nSrcLen*sizeof(char)); + // fast concatenation - all is done in our buffer + memcpy(m_pchData + nLen, pszSrcData, nSrcLen*sizeof(char)); - m_pchData[nNewLen] = '\0'; // put terminating '\0' - GetStringData()->nDataLength = nNewLen; // and fix the length + m_pchData[nNewLen] = '\0'; // put terminating '\0' + GetStringData()->nDataLength = nNewLen; // and fix the length + } + //else: the string to append was empty } /* @@ -542,20 +590,32 @@ void wxString::AllocCopy(wxString& dest, int nCopyLen, int nCopyIndex) const } // extract string of length nCount starting at nFirst -// default value of nCount is 0 and means "till the end" wxString wxString::Mid(size_t nFirst, size_t nCount) const { + wxStringData *pData = GetStringData(); + size_t nLen = pData->nDataLength; + + // default value of nCount is STRING_MAXLEN and means "till the end" + if ( nCount == STRING_MAXLEN ) + { + nCount = nLen - nFirst; + } + // out-of-bounds requests return sensible things - if ( nCount == 0 ) - nCount = GetStringData()->nDataLength - nFirst; + if ( nFirst + nCount > nLen ) + { + nCount = nLen - nFirst; + } - if ( nFirst + nCount > (size_t)GetStringData()->nDataLength ) - nCount = GetStringData()->nDataLength - nFirst; - if ( nFirst > (size_t)GetStringData()->nDataLength ) + if ( nFirst > nLen ) + { + // AllocCopy() will return empty string nCount = 0; + } wxString dest; AllocCopy(dest, nCount, nFirst); + return dest; } @@ -631,11 +691,11 @@ wxString wxString::After(char ch) const } // replace first (or all) occurences of some substring with another one -uint wxString::Replace(const char *szOld, const char *szNew, bool bReplaceAll) +size_t wxString::Replace(const char *szOld, const char *szNew, bool bReplaceAll) { - uint uiCount = 0; // count of replacements made + size_t uiCount = 0; // count of replacements made - uint uiOldLen = Strlen(szOld); + size_t uiOldLen = Strlen(szOld); wxString strTemp; const char *pCurrent = m_pchData; @@ -741,30 +801,40 @@ wxString& wxString::MakeLower() // trims spaces (in the sense of isspace) from left or right side wxString& wxString::Trim(bool bFromRight) { - CopyBeforeWrite(); - - if ( bFromRight ) + // first check if we're going to modify the string at all + if ( !IsEmpty() && + ( + (bFromRight && isspace(GetChar(Len() - 1))) || + (!bFromRight && isspace(GetChar(0u))) + ) + ) { - // find last non-space character - char *psz = m_pchData + GetStringData()->nDataLength - 1; - while ( isspace(*psz) && (psz >= m_pchData) ) - psz--; - - // truncate at trailing space start - *++psz = '\0'; - GetStringData()->nDataLength = psz - m_pchData; - } - else - { - // find first non-space character - const char *psz = m_pchData; - while ( isspace(*psz) ) - psz++; - - // fix up data and length - int nDataLength = GetStringData()->nDataLength - (psz - m_pchData); - memmove(m_pchData, psz, (nDataLength + 1)*sizeof(char)); - GetStringData()->nDataLength = nDataLength; + // ok, there is at least one space to trim + CopyBeforeWrite(); + + if ( bFromRight ) + { + // find last non-space character + char *psz = m_pchData + GetStringData()->nDataLength - 1; + while ( isspace(*psz) && (psz >= m_pchData) ) + psz--; + + // truncate at trailing space start + *++psz = '\0'; + GetStringData()->nDataLength = psz - m_pchData; + } + else + { + // find first non-space character + const char *psz = m_pchData; + while ( isspace(*psz) ) + psz++; + + // fix up data and length + int nDataLength = GetStringData()->nDataLength - (psz - m_pchData); + memmove(m_pchData, psz, (nDataLength + 1)*sizeof(char)); + GetStringData()->nDataLength = nDataLength; + } } return *this; @@ -869,7 +939,7 @@ bool wxString::Matches(const char *pszMask) const return TRUE; // are there any other metacharacters in the mask? - uint uiLenMask; + size_t uiLenMask; const char *pEndMask = strpbrk(pszMask, "*?"); if ( pEndMask != NULL ) { @@ -913,13 +983,15 @@ wxString& wxString::insert(size_t nPos, const wxString& str) wxASSERT( str.GetStringData()->IsValid() ); wxASSERT( nPos <= Len() ); - wxString strTmp; - char *pc = strTmp.GetWriteBuf(Len() + str.Len()); - strncpy(pc, c_str(), nPos); - strcpy(pc + nPos, str); - strcpy(pc + nPos + str.Len(), c_str() + nPos); - strTmp.UngetWriteBuf(); - *this = strTmp; + if ( !str.IsEmpty() ) { + wxString strTmp; + char *pc = strTmp.GetWriteBuf(Len() + str.Len()); + strncpy(pc, c_str(), nPos); + strcpy(pc + nPos, str); + strcpy(pc + nPos + str.Len(), c_str() + nPos); + strTmp.UngetWriteBuf(); + *this = strTmp; + } return *this; } @@ -1058,36 +1130,32 @@ wxArrayString::wxArrayString() { m_nSize = m_nCount = 0; - m_pItems = NULL; + m_pItems = (char **) NULL; } // copy ctor wxArrayString::wxArrayString(const wxArrayString& src) { - m_nSize = src.m_nSize; - m_nCount = src.m_nCount; - - if ( m_nSize != 0 ) - m_pItems = new char *[m_nSize]; - else - m_pItems = NULL; + m_nSize = + m_nCount = 0; + m_pItems = (char **) NULL; - if ( m_nCount != 0 ) - memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(char *)); + *this = src; } -// copy operator +// assignment operator wxArrayString& wxArrayString::operator=(const wxArrayString& src) { - DELETEA(m_pItems); + if ( m_nSize > 0 ) + Clear(); - m_nSize = src.m_nSize; - m_nCount = src.m_nCount; + if ( src.m_nCount > ARRAY_DEFAULT_INITIAL_SIZE ) + Alloc(src.m_nCount); - if ( m_nSize != 0 ) - m_pItems = new char *[m_nCount]; - else - m_pItems = NULL; + // we can't just copy the pointers here because otherwise we would share + // the strings with another array + for ( size_t n = 0; n < src.m_nCount; n++ ) + Add(src[n]); if ( m_nCount != 0 ) memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(char *)); @@ -1106,8 +1174,13 @@ void wxArrayString::Grow() m_pItems = new char *[m_nSize]; } else { + // otherwise when it's called for the first time, nIncrement would be 0 + // and the array would never be expanded + wxASSERT( ARRAY_DEFAULT_INITIAL_SIZE != 0 ); + // add 50% but not too much - size_t nIncrement = m_nSize >> 1; + size_t nIncrement = m_nSize < ARRAY_DEFAULT_INITIAL_SIZE + ? ARRAY_DEFAULT_INITIAL_SIZE : m_nSize >> 1; if ( nIncrement > ARRAY_MAXSIZE_INCREMENT ) nIncrement = ARRAY_MAXSIZE_INCREMENT; m_nSize += nIncrement; @@ -1117,7 +1190,7 @@ void wxArrayString::Grow() memcpy(pNew, m_pItems, m_nCount*sizeof(char *)); // delete old memory (but do not release the strings!) - DELETEA(m_pItems); + wxDELETEA(m_pItems); m_pItems = pNew; } @@ -1147,8 +1220,7 @@ void wxArrayString::Clear() m_nSize = m_nCount = 0; - DELETEA(m_pItems); - m_pItems = NULL; + wxDELETEA(m_pItems); } // dtor @@ -1156,7 +1228,7 @@ wxArrayString::~wxArrayString() { Free(); - DELETEA(m_pItems); + wxDELETEA(m_pItems); } // pre-allocates memory (frees the previous data!) @@ -1167,7 +1239,7 @@ void wxArrayString::Alloc(size_t nSize) // only if old buffer was not big enough if ( nSize > m_nSize ) { Free(); - DELETEA(m_pItems); + wxDELETEA(m_pItems); m_pItems = new char *[nSize]; m_nSize = nSize; } @@ -1180,7 +1252,7 @@ int wxArrayString::Index(const char *sz, bool bCase, bool bFromEnd) const { if ( bFromEnd ) { if ( m_nCount > 0 ) { - uint ui = m_nCount; + size_t ui = m_nCount; do { if ( STRING(m_pItems[--ui])->IsSameAs(sz, bCase) ) return ui; @@ -1189,7 +1261,7 @@ int wxArrayString::Index(const char *sz, bool bCase, bool bFromEnd) const } } else { - for( uint ui = 0; ui < m_nCount; ui++ ) { + for( size_t ui = 0; ui < m_nCount; ui++ ) { if( STRING(m_pItems[ui])->IsSameAs(sz, bCase) ) return ui; } @@ -1215,7 +1287,7 @@ void wxArrayString::Insert(const wxString& str, size_t nIndex) { wxASSERT( str.GetStringData()->IsValid() ); - wxCHECK_RET( nIndex <= m_nCount, "bad index in wxArrayString::Insert" ); + wxCHECK_RET( nIndex <= m_nCount, ("bad index in wxArrayString::Insert") ); Grow(); @@ -1231,7 +1303,7 @@ void wxArrayString::Insert(const wxString& str, size_t nIndex) // removes item from array (by index) void wxArrayString::Remove(size_t nIndex) { - wxCHECK_RET( nIndex <= m_nCount, "bad index in wxArrayString::Remove" ); + wxCHECK_RET( nIndex <= m_nCount, _("bad index in wxArrayString::Remove") ); // release our lock Item(nIndex).GetStringData()->Unlock(); @@ -1247,14 +1319,14 @@ void wxArrayString::Remove(const char *sz) int iIndex = Index(sz); wxCHECK_RET( iIndex != NOT_FOUND, - "removing inexistent element in wxArrayString::Remove" ); + _("removing inexistent element in wxArrayString::Remove") ); - Remove((size_t)iIndex); + Remove(iIndex); } // sort array elements using passed comparaison function -void wxArrayString::Sort(bool bCase, bool bReverse) +void wxArrayString::Sort(bool WXUNUSED(bCase), bool WXUNUSED(bReverse) ) { //@@@@ TO DO //qsort(m_pItems, m_nCount, sizeof(char *), fCmp);