X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/c17bcb844883451b564446bf7843e7b3f862fe48..6c44548a8f39808a91fe363327b16b172891dcb9:/src/common/tokenzr.cpp?ds=sidebyside diff --git a/src/common/tokenzr.cpp b/src/common/tokenzr.cpp index 837243cac6..a8946b57a3 100644 --- a/src/common/tokenzr.cpp +++ b/src/common/tokenzr.cpp @@ -2,13 +2,21 @@ // Name: tokenzr.cpp // Purpose: String tokenizer // Author: Guilhem Lavaux -// Modified by: +// Modified by: Vadim Zeitlin (almost full rewrite) // Created: 04/22/98 // RCS-ID: $Id$ // Copyright: (c) Guilhem Lavaux // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// +// ============================================================================ +// declarations +// ============================================================================ + +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- + #ifdef __GNUG__ #pragma implementation "tokenzr.h" #endif @@ -21,118 +29,195 @@ #endif #include "wx/tokenzr.h" +#include "wx/arrstr.h" -wxStringTokenizer::wxStringTokenizer(const wxString& to_tokenize, - const wxString& delims, - bool ret_delims) -{ - m_string = to_tokenize; - m_delims = delims; - m_retdelims = ret_delims; -} +// Required for wxIs... functions +#include + +// ============================================================================ +// implementation +// ============================================================================ -wxStringTokenizer::~wxStringTokenizer() +// ---------------------------------------------------------------------------- +// wxStringTokenizer construction +// ---------------------------------------------------------------------------- + +wxStringTokenizer::wxStringTokenizer(const wxString& str, + const wxString& delims, + wxStringTokenizerMode mode) { + SetString(str, delims, mode); } -off_t wxStringTokenizer::FindDelims(const wxString& str, const wxString& delims) const +void wxStringTokenizer::SetString(const wxString& str, + const wxString& delims, + wxStringTokenizerMode mode) { - for ( size_t i = 0; i < str.Length(); i++ ) + if ( mode == wxTOKEN_DEFAULT ) { - wxChar c = str[i]; - - for ( size_t j = 0; j < delims.Length() ; j++ ) + // by default, we behave like strtok() if the delimiters are only + // whitespace characters and as wxTOKEN_RET_EMPTY otherwise (for + // whitespace delimiters, strtok() behaviour is better because we want + // to count consecutive spaces as one delimiter) + const wxChar *p; + for ( p = delims.c_str(); *p; p++ ) { - if ( delims[j] == c ) - return i; + if ( !wxIsspace(*p) ) + break; } - } - - return -1; -} -int wxStringTokenizer::CountTokens() const -{ - wxString p_string = m_string; - bool found = TRUE; - int pos, count = 1; - - if (p_string.Length() == 0) - return 0; - - while (found) - { - pos = FindDelims(p_string, m_delims); - if (pos != -1) + if ( *p ) { - count++; - p_string = p_string(0, pos); + // not whitespace char in delims + mode = wxTOKEN_RET_EMPTY; } else { - found = FALSE; + // only whitespaces + mode = wxTOKEN_STRTOK; } } - return count; + m_delims = delims; + m_mode = mode; + + Reinit(str); } -bool wxStringTokenizer::HasMoreTokens() +void wxStringTokenizer::Reinit(const wxString& str) { - return !m_string.IsEmpty(); + wxASSERT_MSG( IsOk(), _T("you should call SetString() first") ); + + m_string = str; + m_pos = 0; + + // empty string doesn't have any tokens + m_hasMore = !m_string.empty(); } -// needed to fix leading whitespace / mult. delims bugs -void wxStringTokenizer::EatLeadingDelims() +// ---------------------------------------------------------------------------- +// access to the tokens +// ---------------------------------------------------------------------------- + +// do we have more of them? +bool wxStringTokenizer::HasMoreTokens() const { - int pos; + wxCHECK_MSG( IsOk(), FALSE, _T("you should call SetString() first") ); - // while leading delims trim 'em from the left - while ( ( pos = FindDelims(m_string, m_delims)) == 0 ) + if ( m_string.find_first_not_of(m_delims) == wxString::npos ) { - m_string = m_string.Mid((size_t)1); + // no non empty tokens left, but in 2 cases we still may return TRUE if + // GetNextToken() wasn't called yet for this empty token: + // + // a) in wxTOKEN_RET_EMPTY_ALL mode we always do it + // b) in wxTOKEN_RET_EMPTY mode we do it in the special case of a + // string containing only the delimiter: then there is an empty + // token just before it + return (m_mode == wxTOKEN_RET_EMPTY_ALL) || + (m_mode == wxTOKEN_RET_EMPTY && m_pos == 0) + ? m_hasMore : FALSE; + } + else + { + // there are non delimiter characters left, hence we do have more + // tokens + return TRUE; } } -wxString wxStringTokenizer::NextToken() +// count the number of tokens in the string +size_t wxStringTokenizer::CountTokens() const { - off_t pos, pos2; - wxString r_string; - - if ( m_string.IsEmpty() ) - return m_string; + wxCHECK_MSG( IsOk(), 0, _T("you should call SetString() first") ); - if ( !m_retdelims ) - EatLeadingDelims(); + // VZ: this function is IMHO not very useful, so it's probably not very + // important if it's implementation here is not as efficient as it + // could be - but OTOH like this we're sure to get the correct answer + // in all modes + wxStringTokenizer *self = (wxStringTokenizer *)this; // const_cast + wxString stringInitial = m_string; - pos = FindDelims(m_string, m_delims); - if (pos == -1) + size_t count = 0; + while ( self->HasMoreTokens() ) { - r_string = m_string; - m_string = wxEmptyString; + count++; - return r_string; + (void)self->GetNextToken(); } - if (m_retdelims) + self->Reinit(stringInitial); + + return count; +} + +// ---------------------------------------------------------------------------- +// token extraction +// ---------------------------------------------------------------------------- + +wxString wxStringTokenizer::GetNextToken() +{ + // strtok() doesn't return empty tokens, all other modes do + bool allowEmpty = m_mode != wxTOKEN_STRTOK; + + wxString token; + do { - if (!pos) + if ( !HasMoreTokens() ) + { + break; + } + // find the end of this token + size_t pos = m_string.find_first_of(m_delims); + + // and the start of the next one + if ( pos == wxString::npos ) { - pos++; - pos2 = 1; + // no more delimiters, the token is everything till the end of + // string + token = m_string; + + m_pos += m_string.length(); + m_string.clear(); + + // no more tokens in this string, even in wxTOKEN_RET_EMPTY_ALL + // mode (we will return the trailing one right now in this case) + m_hasMore = FALSE; } else { - pos2 = pos; + size_t pos2 = pos + 1; + + // in wxTOKEN_RET_DELIMS mode we return the delimiter character + // with token + token = wxString(m_string, m_mode == wxTOKEN_RET_DELIMS ? pos2 + : pos); + + // remove token with the following it delimiter from string + m_string.erase(0, pos2); + + // keep track of the position in the original string too + m_pos += pos2; } } - else + while ( !allowEmpty && token.empty() ); + + return token; +} + +// ---------------------------------------------------------------------------- +// public functions +// ---------------------------------------------------------------------------- + +wxArrayString wxStringTokenize(const wxString& str, + const wxString& delims, + wxStringTokenizerMode mode) +{ + wxArrayString tokens; + wxStringTokenizer tk(str, delims, mode); + while ( tk.HasMoreTokens() ) { - pos2 = pos + 1; + tokens.Add(tk.GetNextToken()); } - r_string = m_string.Left((size_t)pos); - m_string = m_string.Mid((size_t)pos2); - - return r_string; + return tokens; }