// Created: 29/01/98
// RCS-ID: $Id$
// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
-// Licence: wxWindows license
+// Licence: wxWindows license
/////////////////////////////////////////////////////////////////////////////
#ifdef __GNUG__
sb->sungetc();
break;
}
-
+
str += ch;
if ( --w == 1 )
break;
memcpy(m_pchData, psz + nPos, nLength*sizeof(char));
}
}
-
+
// take first nLength characters of C string psz
// (default value of STRING_MAXLEN means take all the string)
wxString::wxString(const char *psz, size_t nLength)
wxString::wxString(const unsigned char* psz, size_t nLength)
{
InitWith((const char *)psz, 0, nLength);
-}
-
+}
+
#ifdef STD_STRING_COMPATIBILITY
// ctor from a substring
// poor man's iterators are "void *" pointers
wxString::wxString(const void *pStart, const void *pEnd)
{
- InitWith((const char *)pStart, 0,
+ InitWith((const char *)pStart, 0,
(const char *)pEnd - (const char *)pStart);
}
// allocate memory:
// 1) one extra character for '\0' termination
// 2) sizeof(wxStringData) for housekeeping info
- wxStringData* pData = (wxStringData*)new char[sizeof(wxStringData) +
- (nLen + 1)*sizeof(char)];
+ wxStringData* pData = (wxStringData*)malloc(sizeof(wxStringData) +
+ (nLen + 1)*sizeof(char));
pData->nRefs = 1;
pData->data()[nLen] = '\0';
pData->nDataLength = nLen;
m_pchData = pData->data(); // data starts after wxStringData
}
-// releases the string memory and reinits it
-void wxString::Reinit()
-{
- GetStringData()->Unlock();
- Init();
-}
-
-// wrapper around wxString::Reinit
-void wxString::Empty()
-{
- if ( GetStringData()->nDataLength != 0 )
- Reinit();
-
- wxASSERT( GetStringData()->nDataLength == 0 );
- wxASSERT( GetStringData()->nAllocLength == 0 );
-}
-
// must be called before changing this string
void wxString::CopyBeforeWrite()
{
wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
}
+// allocate enough memory for nLen characters
+void wxString::Alloc(uint nLen)
+{
+ wxStringData *pData = GetStringData();
+ if ( pData->nAllocLength <= nLen ) {
+ if ( pData->IsEmpty() )
+ AllocBuffer(nLen);
+ else {
+ wxStringData *p = (wxStringData *)
+ realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(char));
+ if ( p != NULL && p != pData ) {
+ // the call succeeded but the pointer changed
+ pData->Unlock();
+ free(pData);
+
+ p->nRefs = 1;
+ p->nAllocLength = nLen;
+ m_pchData = p->data();
+ }
+ //else: do nothing: either the call failed or the pointer is unchanged
+ }
+ }
+ //else: we've already got enough
+}
+
+// shrink to minimal size (releasing extra memory)
+void wxString::Shrink()
+{
+ wxStringData *pData = GetStringData();
+ void *p = realloc(pData, sizeof(wxStringData) +
+ (pData->nDataLength + 1)*sizeof(char));
+ if ( p == NULL ) // huh? can't unallocate memory? unlikely but possible.
+ return;
+ 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(int nLen)
+char *wxString::GetWriteBuf(uint nLen)
{
AllocBeforeWrite(nLen);
// assignment operators
// ---------------------------------------------------------------------------
-// helper function: does real copy
+// helper function: does real copy
void wxString::AssignCopy(size_t nSrcLen, const char *pszSrcData)
{
if ( nSrcLen == 0 ) {
// concatenate two sources
// NB: assume that 'this' is a new wxString object
void wxString::ConcatCopy(int nSrc1Len, const char *pszSrc1Data,
- int nSrc2Len, const char *pszSrc2Data)
+ int nSrc2Len, const char *pszSrc2Data)
{
int nNewLen = nSrc1Len + nSrc2Len;
if ( nNewLen != 0 )
register wxStringData *pData = GetStringData();
// alloc new buffer if current is too small
- if ( pData->IsShared() ||
+ if ( pData->IsShared() ||
pData->nDataLength + nSrcLen > pData->nAllocLength ) {
// we have to grow the buffer, use the ConcatCopy routine
// (which will allocate memory)
}
/*
- * string may be concatenated with other string, C string or a character
- */
-
-void wxString::operator+=(const wxString& string)
-{
- wxASSERT( string.GetStringData()->IsValid() );
-
- ConcatSelf(string.Len(), string);
-}
-
-void wxString::operator+=(const char *psz)
-{
- ConcatSelf(Strlen(psz), psz);
-}
-
-void wxString::operator+=(char ch)
-{
- ConcatSelf(1, &ch);
-}
-
-/*
- * Same as above but return the result
- */
-
-wxString& wxString::operator<<(const wxString& string)
-{
- wxASSERT( string.GetStringData()->IsValid() );
-
- ConcatSelf(string.Len(), string);
- return *this;
-}
-
-wxString& wxString::operator<<(const char *psz)
-{
- ConcatSelf(Strlen(psz), psz);
- return *this;
-}
-
-wxString& wxString::operator<<(char ch)
-{
- ConcatSelf(1, &ch);
- return *this;
-}
-
-/*
* concatenation functions come in 5 flavours:
* string + string
* char + string and string + char
wxString strTemp;
const char *pCurrent = m_pchData;
- const char *pSubstr;
+ const char *pSubstr;
while ( *pCurrent != '\0' ) {
pSubstr = strstr(pCurrent, szOld);
if ( pSubstr == NULL ) {
}
return(TRUE);
}
-
+
bool wxString::IsWord() const
{
const char *s = (const char*) *this;
}
return(TRUE);
}
-
+
bool wxString::IsNumber() const
{
const char *s = (const char*) *this;
return(TRUE);
}
-// kludge: we don't have declaraton of wxStringData here, so we add offsets
-// manually to get to the "length" field of wxStringData structure
-bool wxString::IsEmpty() const { return Len() == 0; }
-
wxString wxString::Strip(stripType w) const
{
wxString s = *this;
return s;
}
-/// case-insensitive strcmp() (platform independent)
-int Stricmp(const char *psz1, const char *psz2)
-{
-#if defined(_MSC_VER)
- return _stricmp(psz1, psz2);
-#elif defined(__BORLANDC__)
- return stricmp(psz1, psz2);
-#elif defined(__UNIX__) || defined(__GNUWIN32__)
- return strcasecmp(psz1, psz2);
-#else
- // almost all compilers/libraries provide this function (unfortunately under
- // different names), that's why we don't implement our own which will surely
- // be more efficient than this code (uncomment to use):
- /*
- register char c1, c2;
- do {
- c1 = tolower(*psz1++);
- c2 = tolower(*psz2++);
- } while ( c1 && (c1 == c2) );
-
- return c1 - c2;
- */
-
- #error "Please define string case-insensitive compare for your OS/compiler"
-#endif // OS/compiler
-}
-
// ---------------------------------------------------------------------------
// case conversion
// ---------------------------------------------------------------------------
wxString& wxString::MakeLower()
{
CopyBeforeWrite();
-
+
for ( char *p = m_pchData; *p; p++ )
*p = (char)tolower(*p);
strcpy(pc + nPos + str.Len(), c_str() + nPos);
strTmp.UngetWriteBuf();
*this = strTmp;
-
- return *this;
+
+ return *this;
}
size_t wxString::find(const wxString& str, size_t nStart) const
wxASSERT( nStart <= Len() );
const char *p = strstr(c_str() + nStart, str);
-
+
return p == NULL ? npos : p - c_str();
}
return find(wxString(sz, n == npos ? 0 : n), nStart);
}
#endif
-
+
size_t wxString::find(char ch, size_t nStart) const
{
wxASSERT( nStart <= Len() );
const char *p = strchr(c_str() + nStart, ch);
-
+
return p == NULL ? npos : p - c_str();
}
return p - str.Len() - c_str();
p--;
}
-
+
return npos;
}
-
+
// VC++ 1.5 can't cope with the default argument in the header.
#if ! (defined(_MSC_VER) && !defined(__WIN32__))
size_t wxString::rfind(const char* sz, size_t nStart, size_t n) const
wxASSERT( nStart <= Len() );
const char *p = strrchr(c_str() + nStart, ch);
-
+
return p == NULL ? npos : p - c_str();
}
#endif
strTmp.append(c_str(), nStart);
strTmp += sz;
strTmp.append(c_str() + nStart + nLen);
-
+
*this = strTmp;
return *this;
}
return replace(nStart, nLen, wxString(ch, nCount));
}
-wxString& wxString::replace(size_t nStart, size_t nLen,
+wxString& wxString::replace(size_t nStart, size_t nLen,
const wxString& str, size_t nStart2, size_t nLen2)
{
return replace(nStart, nLen, str.substr(nStart2, nLen2));
}
-wxString& wxString::replace(size_t nStart, size_t nLen,
+wxString& wxString::replace(size_t nStart, size_t nLen,
const char* sz, size_t nCount)
{
return replace(nStart, nLen, wxString(sz, nCount));
m_nCount = src.m_nCount;
if ( m_nSize != 0 )
- m_pItems = new char *[m_nSize];
+ m_pItems = new char *[m_nCount];
else
m_pItems = NULL;
{
Free();
- m_nSize =
+ m_nSize =
m_nCount = 0;
DELETEA(m_pItems);
Grow();
- memmove(&m_pItems[nIndex + 1], &m_pItems[nIndex],
+ memmove(&m_pItems[nIndex + 1], &m_pItems[nIndex],
(m_nCount - nIndex)*sizeof(char *));
str.GetStringData()->Lock();
// release our lock
Item(nIndex).GetStringData()->Unlock();
- memmove(&m_pItems[nIndex], &m_pItems[nIndex + 1],
+ memmove(&m_pItems[nIndex], &m_pItems[nIndex + 1],
(m_nCount - nIndex - 1)*sizeof(char *));
m_nCount--;
}