]> git.saurik.com Git - wxWidgets.git/commitdiff
bogus assert removed, optimized (and removed a bug in process of doing it)
authorVadim Zeitlin <vadim@wxwidgets.org>
Sat, 3 Oct 1998 00:25:17 +0000 (00:25 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sat, 3 Oct 1998 00:25:17 +0000 (00:25 +0000)
wxString::Trim

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@799 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/string.h
src/common/string.cpp

index 645e8c49c18a4da7f49e873682971682c506f59d..477586d7fc319ebf7e97ccfec80980ba2b4381dd 100644 (file)
@@ -257,10 +257,10 @@ public:
   size_t Len() const { return GetStringData()->nDataLength; }
     /// string contains any characters?
   bool IsEmpty() const { return Len() == 0; }
   size_t Len() const { return GetStringData()->nDataLength; }
     /// string contains any characters?
   bool IsEmpty() const { return Len() == 0; }
-    /// reinitialize string (and free data!)
+    /// reinitialize string (and free memory)
   void Empty()
   {
   void Empty()
   {
-    if ( GetStringData()->nDataLength != 0 )
+    if ( !IsEmpty() )
       Reinit();
 
     wxASSERT( GetStringData()->nDataLength == 0 );
       Reinit();
 
     wxASSERT( GetStringData()->nDataLength == 0 );
index 88f44a8a8ea733812af09c03b78b2e167fb55946..56808169b19521d747ab0806d05b36873b0f6f33 100644 (file)
@@ -136,7 +136,7 @@ NAMESPACE istream& operator>>(NAMESPACE istream& is, wxString& WXUNUSED(str))
   {
   public:
     Averager(const char *sz) { m_sz = sz; m_nTotal = m_nCount = 0; }
   {
   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(size_t n) { m_nTotal += n; m_nCount++; }
    { printf("wxString: average %s = %f\n", m_sz, ((float)m_nTotal)/m_nCount); }
 
     void Add(size_t n) { m_nTotal += n; m_nCount++; }
@@ -775,30 +775,40 @@ wxString& wxString::MakeLower()
 // trims spaces (in the sense of isspace) from left or right side
 wxString& wxString::Trim(bool bFromRight)
 {
 // trims spaces (in the sense of isspace) from left or right side
 wxString& wxString::Trim(bool bFromRight)
 {
-  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
+  // first check if we're going to modify the string at all
+  if ( !IsEmpty() && 
+       ( 
+        (bFromRight && isspace(GetChar(Len() - 1))) ||
+        (!bFromRight && isspace(GetChar(0u)))
+       )
+     )
   {
   {
-    // 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;
   }
 
   return *this;