diff --git a/include/wx/string.h b/include/wx/string.h index 3e8567d18f..c3992d5f47 100644 --- a/include/wx/string.h +++ b/include/wx/string.h @@ -836,32 +836,37 @@ public: // find first/last occurence of any character in the set - // - size_t find_first_of(const wxString& str, size_t nStart = 0) const; - // + // as strpbrk() but starts at nStart, returns npos if not found + size_t find_first_of(const wxString& str, size_t nStart = 0) const + { return find_first_of(str.c_str(), nStart); } + // same as above size_t find_first_of(const wxChar* sz, size_t nStart = 0) const; // same as find(char, size_t) - size_t find_first_of(wxChar c, size_t nStart = 0) const; - // - size_t find_last_of (const wxString& str, size_t nStart = npos) const; - // - size_t find_last_of (const wxChar* s, size_t nStart = npos) const; - // same as rfind(char, size_t) - size_t find_last_of (wxChar c, size_t nStart = npos) const; + size_t find_first_of(wxChar c, size_t nStart = 0) const + { return find(c, nStart); } + // find the last (starting from nStart) char from str in this string + size_t find_last_of (const wxString& str, size_t nStart = npos) const + { return find_last_of(str.c_str(), nStart); } + // same as above + size_t find_last_of (const wxChar* sz, size_t nStart = npos) const; + // same as above + size_t find_last_of(wxChar c, size_t nStart = npos) const + { return rfind(c, nStart); } // find first/last occurence of any character not in the set - // - size_t find_first_not_of(const wxString& str, size_t nStart = 0) const; - // - size_t find_first_not_of(const wxChar* s, size_t nStart = 0) const; - // + // as strspn() (starting from nStart), returns npos on failure + size_t find_first_not_of(const wxString& str, size_t nStart = 0) const + { return find_first_not_of(str.c_str(), nStart); } + // same as above + size_t find_first_not_of(const wxChar* sz, size_t nStart = 0) const; + // same as above size_t find_first_not_of(wxChar ch, size_t nStart = 0) const; - // + // as strcspn() size_t find_last_not_of(const wxString& str, size_t nStart=npos) const; - // - size_t find_last_not_of(const wxChar* s, size_t nStart = npos) const; - // + // same as above + size_t find_last_not_of(const wxChar* sz, size_t nStart = npos) const; + // same as above size_t find_last_not_of(wxChar ch, size_t nStart = npos) const; // All compare functions return -1, 0 or 1 if the [sub]string is less, diff --git a/src/common/string.cpp b/src/common/string.cpp index db8db9a004..3c3e2b6d1e 100644 --- a/src/common/string.cpp +++ b/src/common/string.cpp @@ -1410,7 +1410,7 @@ size_t wxString::rfind(const wxString& str, size_t nStart) const wxASSERT( str.GetStringData()->IsValid() ); wxASSERT( nStart <= Len() ); - // # could be quicker than that + // TODO could be made much quicker than that const wxChar *p = c_str() + (nStart == npos ? Len() : nStart); while ( p >= c_str() + str.Len() ) { if ( wxStrncmp(p - str.Len(), str, str.Len()) == 0 ) @@ -1425,19 +1425,131 @@ size_t wxString::rfind(const wxString& str, size_t nStart) const #if !defined(__VISUALC__) || defined(__WIN32__) size_t wxString::rfind(const wxChar* sz, size_t nStart, size_t n) const { - return rfind(wxString(sz, n == npos ? 0 : n), nStart); + return rfind(wxString(sz, n == npos ? 0 : n), nStart); } size_t wxString::rfind(wxChar ch, size_t nStart) const { - wxASSERT( nStart <= Len() ); + if ( nStart == npos ) + { + nStart = Len(); + } + else + { + wxASSERT( nStart <= Len() ); + } - const wxChar *p = wxStrrchr(c_str() + nStart, ch); + const wxChar *p = wxStrrchr(c_str(), ch); - return p == NULL ? npos : p - c_str(); + if ( p == NULL ) + return npos; + + size_t result = p - c_str(); + return ( result > nStart ) ? npos : result; } #endif // VC++ 1.5 +size_t wxString::find_first_of(const wxChar* sz, size_t nStart) const +{ + const char *start = c_str() + nStart; + char *firstOf = strpbrk(start, sz); + if ( firstOf ) + return firstOf - start; + else + return npos; +} + +size_t wxString::find_last_of(const wxChar* sz, size_t nStart) const +{ + if ( nStart == npos ) + { + nStart = Len(); + } + else + { + wxASSERT( nStart <= Len() ); + } + + for ( const char *p = c_str() + length() - 1; p >= c_str(); p-- ) + { + if ( wxStrchr(sz, *p) ) + return p - c_str(); + } + + return npos; +} + +size_t wxString::find_first_not_of(const wxChar* sz, size_t nStart) const +{ + if ( nStart == npos ) + { + nStart = Len(); + } + else + { + wxASSERT( nStart <= Len() ); + } + + size_t nAccept = strspn(c_str() + nStart, sz); + if ( nAccept >= length() - nStart ) + return npos; + else + return nAccept; +} + +size_t wxString::find_first_not_of(wxChar ch, size_t nStart) const +{ + wxASSERT( nStart <= Len() ); + + for ( const char *p = c_str() + nStart; *p; p++ ) + { + if ( *p != ch ) + return p - c_str(); + } + + return npos; +} + +size_t wxString::find_last_not_of(const wxChar* sz, size_t nStart) const +{ + if ( nStart == npos ) + { + nStart = Len(); + } + else + { + wxASSERT( nStart <= Len() ); + } + + for ( const char *p = c_str() + nStart - 1; p >= c_str(); p-- ) + { + if ( !wxStrchr(sz, *p) ) + return p - c_str(); + } + + return npos; +} + +size_t wxString::find_last_not_of(wxChar ch, size_t nStart) const +{ + if ( nStart == npos ) + { + nStart = Len(); + } + else + { + wxASSERT( nStart <= Len() ); + } + + for ( const char *p = c_str() + nStart - 1; p >= c_str(); p-- ) + { + if ( *p != ch ) + return p - c_str(); + } + + return npos; +} + wxString wxString::substr(size_t nStart, size_t nLen) const { // npos means 'take all'