bogus assert removed, optimized (and removed a bug in process of doing it)

wxString::Trim


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@799 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
1998-10-03 00:25:17 +00:00
parent 524bf39144
commit 2c3b684c2b
2 changed files with 34 additions and 24 deletions

View File

@@ -257,10 +257,10 @@ public:
size_t Len() const { return GetStringData()->nDataLength; } size_t Len() const { return GetStringData()->nDataLength; }
/// string contains any characters? /// string contains any characters?
bool IsEmpty() const { return Len() == 0; } 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(); Reinit();
wxASSERT( GetStringData()->nDataLength == 0 ); wxASSERT( GetStringData()->nDataLength == 0 );

View File

@@ -136,7 +136,7 @@ NAMESPACE istream& operator>>(NAMESPACE istream& is, wxString& WXUNUSED(str))
{ {
public: public:
Averager(const char *sz) { m_sz = sz; m_nTotal = m_nCount = 0; } 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); } { printf("wxString: average %s = %f\n", m_sz, ((float)m_nTotal)/m_nCount); }
void Add(size_t n) { m_nTotal += n; 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 // trims spaces (in the sense of isspace) from left or right side
wxString& wxString::Trim(bool bFromRight) wxString& wxString::Trim(bool bFromRight)
{ {
CopyBeforeWrite(); // first check if we're going to modify the string at all
if ( !IsEmpty() &&
if ( bFromRight ) (
(bFromRight && isspace(GetChar(Len() - 1))) ||
(!bFromRight && isspace(GetChar(0u)))
)
)
{ {
// find last non-space character // ok, there is at least one space to trim
char *psz = m_pchData + GetStringData()->nDataLength - 1; CopyBeforeWrite();
while ( isspace(*psz) && (psz >= m_pchData) )
psz--;
// truncate at trailing space start if ( bFromRight )
*++psz = '\0'; {
GetStringData()->nDataLength = psz - m_pchData; // find last non-space character
} char *psz = m_pchData + GetStringData()->nDataLength - 1;
else while ( isspace(*psz) && (psz >= m_pchData) )
{ psz--;
// find first non-space character
const char *psz = m_pchData;
while ( isspace(*psz) )
psz++;
// fix up data and length // truncate at trailing space start
int nDataLength = GetStringData()->nDataLength - (psz - m_pchData); *++psz = '\0';
memmove(m_pchData, psz, (nDataLength + 1)*sizeof(char)); GetStringData()->nDataLength = psz - m_pchData;
GetStringData()->nDataLength = nDataLength; }
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;