]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/string.cpp
wxWindow::OnSize() removed
[wxWidgets.git] / src / common / string.cpp
index 10e64bad12762504489c2aa6672d2b2f119192e9..1c8cd5319c82235e404bfe8b2f609e8a7710327f 100644 (file)
@@ -153,7 +153,7 @@ wxString::wxString(char ch, size_t nLength)
 
   if ( nLength > 0 ) {
     AllocBuffer(nLength);
-    
+
     wxASSERT( sizeof(char) == 1 );  // can't use memset if not
 
     memset(m_pchData, ch, nLength);
@@ -193,9 +193,11 @@ wxString::wxString(const unsigned char* psz, size_t 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
@@ -282,23 +284,34 @@ void wxString::AllocBeforeWrite(size_t nLen)
   wxASSERT( nLen != 0 );  // doesn't make any sense
 
   // must not share string and must have enough space
-  register wxStringData* pData = GetStringData();  
+  register wxStringData* pData = GetStringData();
   if ( pData->IsShared() || (nLen > pData->nAllocLength) ) {
     // can't work with old buffer, get new one
     pData->Unlock();
     AllocBuffer(nLen);
   }
 
-  wxASSERT( !pData->IsShared() );  // we must be the only owner
+  wxASSERT( !GetStringData()->IsShared() );  // we must be the only owner
 }
 
 // get the pointer to writable buffer of (at least) nLen bytes
-char *wxString::GetWriteBuf(size_t nLen)
+char *wxString::GetWriteBuf(int 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()
 {
@@ -332,6 +345,8 @@ void wxString::AssignCopy(size_t nSrcLen, const char *pszSrcData)
 // 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() ) {
@@ -429,6 +444,8 @@ void wxString::ConcatSelf(int nSrcLen, const char *pszSrcData)
 
 void wxString::operator+=(const wxString& string)
 {
+  wxASSERT( string.GetStringData()->IsValid() );
+
   ConcatSelf(string.Len(), string);
 }
 
@@ -448,6 +465,8 @@ void wxString::operator+=(char ch)
 
 wxString& wxString::operator<<(const wxString& string)
 {
+  wxASSERT( string.GetStringData()->IsValid() );
+
   ConcatSelf(string.Len(), string);
   return *this;
 }
@@ -473,6 +492,9 @@ wxString& wxString::operator<<(char ch)
 
 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);
@@ -481,6 +503,8 @@ wxString operator+(const wxString& string1, const wxString& string2)
 
 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;
@@ -488,6 +512,8 @@ wxString operator+(const wxString& string1, char ch)
 
 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;
@@ -495,6 +521,8 @@ wxString operator+(char ch, const wxString& string)
 
 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);
@@ -503,6 +531,8 @@ wxString operator+(const wxString& string, const char *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);
@@ -837,7 +867,7 @@ int wxString::Find(const char *pszSub) const
 }
 
 // ---------------------------------------------------------------------------
-// formatted input/output
+// formatted output
 // ---------------------------------------------------------------------------
 int wxString::Printf(const char *pszFormat, ...)
 {
@@ -853,7 +883,7 @@ int wxString::Printf(const char *pszFormat, ...)
 
 int wxString::PrintfV(const char* pszFormat, va_list argptr)
 {
-  static char s_szScratch[1024]; // @@@@ shouldn't use fixed-size buffer!
+  static char s_szScratch[1024];
 
   int iLen = vsprintf(s_szScratch, pszFormat, argptr);
   AllocBeforeWrite(iLen);
@@ -862,6 +892,7 @@ int wxString::PrintfV(const char* pszFormat, va_list argptr)
   return iLen;
 }
 
+#if 0
 int wxString::Scanf(const char *pszFormat, ...) const
 {
   va_list argptr;
@@ -876,7 +907,75 @@ int wxString::Scanf(const char *pszFormat, ...) const
 
 int wxString::ScanfV(const char *pszFormat, va_list argptr) const
 {
+#ifdef __WXMSW__
+  wxMessageBox("ScanfV not implemented");
+  return 0;
+#else
   return vsscanf(c_str(), pszFormat, argptr);
+#endif
+}
+#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';
 }
 
 // ---------------------------------------------------------------------------
@@ -886,10 +985,11 @@ int wxString::ScanfV(const char *pszFormat, va_list argptr) const
 
 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() + 1);
+  char *pc = strTmp.GetWriteBuf(Len() + str.Len());
   strncpy(pc, c_str(), nPos);
   strcpy(pc + nPos, str);
   strcpy(pc + nPos + str.Len(), c_str() + nPos);
@@ -900,6 +1000,7 @@ wxString& wxString::insert(size_t nPos, const wxString& str)
 
 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);
@@ -926,6 +1027,7 @@ size_t wxString::find(char ch, size_t nStart) const
 
 size_t wxString::rfind(const wxString& str, size_t nStart) const
 {
+  wxASSERT( str.GetStringData()->IsValid() );
   wxASSERT( nStart <= Len() );
 
   // # could be quicker than that
@@ -1000,7 +1102,7 @@ wxString& wxString::replace(size_t nStart, size_t nLen, size_t nCount, char ch)
 }
 
 wxString& wxString::replace(size_t nStart, size_t nLen, 
-                        const wxString& str, size_t nStart2, size_t nLen2)
+                            const wxString& str, size_t nStart2, size_t nLen2)
 {
   return replace(nStart, nLen, str.substr(nStart2, nLen2));
 }
@@ -1149,7 +1251,7 @@ void wxArrayString::Alloc(size_t nSize)
 
 // searches the array for an item (forward or backwards)
 
-// Robert Roebling (changed to bool from Bool)
+// Robert Roebling (changed to bool from bool)
 
 int wxArrayString::Index(const char *sz, bool bCase, bool bFromEnd) const
 {
@@ -1174,27 +1276,31 @@ int wxArrayString::Index(const char *sz, bool bCase, bool bFromEnd) const
 }
 
 // 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)
 {
-  wxCHECK( nIndex <= m_nCount );
+  wxASSERT( str.GetStringData()->IsValid() );
+
+  wxCHECK_RET( nIndex <= m_nCount, "bad index in wxArrayString::Insert" );
 
   Grow();
 
   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++;
 }
@@ -1202,7 +1308,7 @@ void wxArrayString::Insert(const wxString& src, size_t nIndex)
 // removes item from array (by index)
 void wxArrayString::Remove(size_t nIndex)
 {
-  wxCHECK( nIndex <= m_nCount );
+  wxCHECK_RET( nIndex <= m_nCount, "bad index in wxArrayString::Remove" );
 
   // release our lock
   Item(nIndex).GetStringData()->Unlock();
@@ -1217,15 +1323,14 @@ void wxArrayString::Remove(const char *sz)
 {
   int iIndex = Index(sz);
 
-  wxCHECK( iIndex != NOT_FOUND );
+  wxCHECK_RET( iIndex != NOT_FOUND,
+               "removing inexistent element in wxArrayString::Remove" );
 
   Remove((size_t)iIndex);
 }
 
 // sort array elements using passed comparaison function
 
-// Robert Roebling (changed to bool from Bool)
-
 void wxArrayString::Sort(bool bCase, bool bReverse)
 {
   //@@@@ TO DO