// 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__
0, // current length
0, // allocated memory
0 }; // string data
-// empty string shares memory with g_strEmpty
-static wxStringData *g_strNul = (wxStringData*)&g_strEmpty;
// empty C style string: points to 'string data' byte of g_strEmpty
extern const char *g_szNul = (const char *)(&g_strEmpty[3]);
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
-wxString::wxString(const wxString& s, size_t nPos, size_t nLen)
+wxString::wxString(const wxString& str, size_t nPos, size_t nLen)
{
- InitWith(s.c_str(), nPos, nLen == npos ? 0 : nLen);
+ wxASSERT( str.GetStringData()->IsValid() );
+
+ InitWith(str.c_str(), nPos, nLen == npos ? 0 : nLen);
}
// 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(size_t nLen)
+char *wxString::GetWriteBuf(uint nLen)
{
AllocBeforeWrite(nLen);
+
+ wxASSERT( GetStringData()->nRefs == 1 );
+ GetStringData()->Validate(FALSE);
+
return m_pchData;
}
+// put string back in a reasonable state after GetWriteBuf
+void wxString::UngetWriteBuf()
+{
+ GetStringData()->nDataLength = strlen(m_pchData);
+ GetStringData()->Validate(TRUE);
+}
+
// dtor frees memory if no other strings use it
wxString::~wxString()
{
// assignment operators
// ---------------------------------------------------------------------------
-// helper function: does real copy
+// helper function: does real copy
void wxString::AssignCopy(size_t nSrcLen, const char *pszSrcData)
{
if ( nSrcLen == 0 ) {
// assigns one string to another
wxString& wxString::operator=(const wxString& stringSrc)
{
+ wxASSERT( stringSrc.GetStringData()->IsValid() );
+
// don't copy string over itself
if ( m_pchData != stringSrc.m_pchData ) {
if ( stringSrc.GetStringData()->IsEmpty() ) {
// 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)
-{
- 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)
-{
- 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 operator+(const wxString& string1, const wxString& string2)
{
+ wxASSERT( string1.GetStringData()->IsValid() );
+ wxASSERT( string2.GetStringData()->IsValid() );
+
wxString s;
s.ConcatCopy(string1.GetStringData()->nDataLength, string1.m_pchData,
string2.GetStringData()->nDataLength, string2.m_pchData);
wxString operator+(const wxString& string1, char ch)
{
+ wxASSERT( string1.GetStringData()->IsValid() );
+
wxString s;
s.ConcatCopy(string1.GetStringData()->nDataLength, string1.m_pchData, 1, &ch);
return s;
wxString operator+(char ch, const wxString& string)
{
+ wxASSERT( string.GetStringData()->IsValid() );
+
wxString s;
s.ConcatCopy(1, &ch, string.GetStringData()->nDataLength, string.m_pchData);
return s;
wxString operator+(const wxString& string, const char *psz)
{
+ wxASSERT( string.GetStringData()->IsValid() );
+
wxString s;
s.ConcatCopy(string.GetStringData()->nDataLength, string.m_pchData,
Strlen(psz), psz);
wxString operator+(const char *psz, const wxString& string)
{
+ wxASSERT( string.GetStringData()->IsValid() );
+
wxString s;
s.ConcatCopy(Strlen(psz), psz,
string.GetStringData()->nDataLength, string.m_pchData);
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);
int wxString::ScanfV(const char *pszFormat, va_list argptr) const
{
-#ifdef __WINDOWS__
+#ifdef __WXMSW__
wxMessageBox("ScanfV not implemented");
return 0;
#else
}
#endif
+// ----------------------------------------------------------------------------
+// misc other operations
+// ----------------------------------------------------------------------------
+bool wxString::Matches(const char *pszMask) const
+{
+ // check char by char
+ const char *pszTxt;
+ for ( pszTxt = c_str(); *pszMask != '\0'; pszMask++, pszTxt++ ) {
+ switch ( *pszMask ) {
+ case '?':
+ if ( *pszTxt == '\0' )
+ return FALSE;
+
+ pszTxt++;
+ pszMask++;
+ break;
+
+ case '*':
+ {
+ // ignore special chars immediately following this one
+ while ( *pszMask == '*' || *pszMask == '?' )
+ pszMask++;
+
+ // if there is nothing more, match
+ if ( *pszMask == '\0' )
+ return TRUE;
+
+ // are there any other metacharacters in the mask?
+ uint uiLenMask;
+ const char *pEndMask = strpbrk(pszMask, "*?");
+
+ if ( pEndMask != NULL ) {
+ // we have to match the string between two metachars
+ uiLenMask = pEndMask - pszMask;
+ }
+ else {
+ // we have to match the remainder of the string
+ uiLenMask = strlen(pszMask);
+ }
+
+ wxString strToMatch(pszMask, uiLenMask);
+ const char* pMatch = strstr(pszTxt, strToMatch);
+ if ( pMatch == NULL )
+ return FALSE;
+
+ // -1 to compensate "++" in the loop
+ pszTxt = pMatch + uiLenMask - 1;
+ pszMask += uiLenMask - 1;
+ }
+ break;
+
+ default:
+ if ( *pszMask != *pszTxt )
+ return FALSE;
+ break;
+ }
+ }
+
+ // match only if nothing left
+ return *pszTxt == '\0';
+}
+
// ---------------------------------------------------------------------------
// standard C++ library string functions
// ---------------------------------------------------------------------------
wxString& wxString::insert(size_t nPos, const wxString& str)
{
+ wxASSERT( str.GetStringData()->IsValid() );
wxASSERT( nPos <= Len() );
wxString strTmp;
strncpy(pc, c_str(), nPos);
strcpy(pc + nPos, str);
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( str.GetStringData()->IsValid() );
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();
}
size_t wxString::rfind(const wxString& str, size_t nStart) const
{
+ wxASSERT( str.GetStringData()->IsValid() );
wxASSERT( nStart <= Len() );
// # could be quicker than that
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,
- const wxString& str, size_t nStart2, size_t nLen2)
+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);
}
// add item at the end
-void wxArrayString::Add(const wxString& src)
+void wxArrayString::Add(const wxString& str)
{
+ wxASSERT( str.GetStringData()->IsValid() );
+
Grow();
// the string data must not be deleted!
- src.GetStringData()->Lock();
- m_pItems[m_nCount++] = (char *)src.c_str();
+ str.GetStringData()->Lock();
+ m_pItems[m_nCount++] = (char *)str.c_str();
}
// add item at the given position
-void wxArrayString::Insert(const wxString& src, size_t nIndex)
+void wxArrayString::Insert(const wxString& str, size_t nIndex)
{
+ wxASSERT( str.GetStringData()->IsValid() );
+
wxCHECK_RET( nIndex <= m_nCount, "bad index in wxArrayString::Insert" );
Grow();
- memmove(&m_pItems[nIndex + 1], &m_pItems[nIndex],
+ memmove(&m_pItems[nIndex + 1], &m_pItems[nIndex],
(m_nCount - nIndex)*sizeof(char *));
- src.GetStringData()->Lock();
- m_pItems[nIndex] = (char *)src.c_str();
+ str.GetStringData()->Lock();
+ m_pItems[nIndex] = (char *)str.c_str();
m_nCount++;
}
// 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--;
}