X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/53faea431638ee5c0013a2c888215c751fa6437b..ef3a5e0aae003c29941c7d41ed6becea4287752d:/src/common/tokenzr.cpp diff --git a/src/common/tokenzr.cpp b/src/common/tokenzr.cpp index 95eb0d5860..32c220a69a 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: Gregory Pietsch +// 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 @@ -22,86 +30,193 @@ #include "wx/tokenzr.h" -wxStringTokenizer::wxStringTokenizer(const wxString& to_tokenize, +// Required for wxIs... functions +#include + +// ============================================================================ +// implementation +// ============================================================================ + +// ---------------------------------------------------------------------------- +// wxStringTokenizer construction +// ---------------------------------------------------------------------------- + +wxStringTokenizer::wxStringTokenizer(const wxString& str, const wxString& delims, - bool ret_delims) + wxStringTokenizerMode mode) +{ + SetString(str, delims, mode); +} + +void wxStringTokenizer::SetString(const wxString& str, + const wxString& delims, + wxStringTokenizerMode mode) { - m_string = to_tokenize; + if ( mode == wxTOKEN_DEFAULT ) + { + // 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 ( !wxIsspace(*p) ) + break; + } + + if ( *p ) + { + // not whitespace char in delims + mode = wxTOKEN_RET_EMPTY; + } + else + { + // only whitespaces + mode = wxTOKEN_STRTOK; + } + } + m_delims = delims; - m_retdelims = ret_delims; + m_mode = mode; + + Reinit(str); +} + +void wxStringTokenizer::Reinit(const wxString& str) +{ + 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(); +} + +// ---------------------------------------------------------------------------- +// access to the tokens +// ---------------------------------------------------------------------------- + +// do we have more of them? +bool wxStringTokenizer::HasMoreTokens() const +{ + wxCHECK_MSG( IsOk(), FALSE, _T("you should call SetString() first") ); + + if ( m_string.find_first_not_of(m_delims) == wxString::npos ) + { + // 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; + } } -wxStringTokenizer::~wxStringTokenizer() +// count the number of tokens in the string +size_t wxStringTokenizer::CountTokens() const { + wxCHECK_MSG( IsOk(), 0, _T("you should call SetString() first") ); + + // 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; + + size_t count = 0; + while ( self->HasMoreTokens() ) + { + count++; + + (void)self->GetNextToken(); + } + + self->Reinit(stringInitial); + + return count; } -int wxStringTokenizer::CountTokens() const +// ---------------------------------------------------------------------------- +// token extraction +// ---------------------------------------------------------------------------- + +wxString wxStringTokenizer::GetNextToken() { - size_t pos = 0; - int count = 0; - bool at_delim; - - while (pos < m_string.length()) { - // while we're still counting ... - at_delim = (m_delims.find(m_string.at(pos)) < m_delims.length()); - // are we at a delimiter? if so, move to the next nondelimiter; - // if not, move to the next delimiter. If the find_first_of - // and find_first_not_of methods fail, pos will be assigned - // npos (0xFFFFFFFF) which will terminate the loop on the next - // go-round unless we have a really long string, which is unlikely - pos = at_delim ? m_string.find_first_not_of(m_delims, pos) - : m_string.find_first_of(m_delims, pos); - if (m_retdelims) + // strtok() doesn't return empty tokens, all other modes do + bool allowEmpty = m_mode != wxTOKEN_STRTOK; + + wxString token; + do + { + if ( !HasMoreTokens() ) { - // if we're retaining delimiters, increment count - count++; + 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 ) + { + // 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 { - // if we're not retaining delimiters and at a token, inc count - count += (!at_delim); + 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; } } - return count; -} + while ( !allowEmpty && token.empty() ); -bool wxStringTokenizer::HasMoreTokens() -{ - return (m_retdelims - ? !m_string.IsEmpty() - : m_string.find_first_not_of(m_delims) < m_string.length()); + return token; } -wxString wxStringTokenizer::NextToken() +// ---------------------------------------------------------------------------- +// public functions +// ---------------------------------------------------------------------------- + +wxArrayString wxStringTokenize(const wxString& str, + const wxString& delims, + wxStringTokenizerMode mode) { - size_t pos; - wxString r_string; - - if ( m_string.IsEmpty() ) - return m_string; - pos = m_string.find_first_not_of(m_delims); - if ( m_retdelims ) { - // we're retaining delimiters (unusual behavior, IMHO) - if (pos == 0) - // first char is a non-delimiter - pos = m_string.find_first_of(m_delims); - } else { - // we're not retaining delimiters - m_string.erase(0, pos); - m_pos += pos; - if (m_string.IsEmpty()) - return m_string; - pos = m_string.find_first_of(m_delims); - } - if (pos <= m_string.length()) { - r_string = m_string.substr(0, pos); - m_string.erase(0, pos); - m_pos += pos; - } else { - r_string = m_string; - m_pos += m_string.length(); - m_string.Empty(); + wxArrayString tokens; + wxStringTokenizer tk(str, delims, mode); + while ( tk.HasMoreTokens() ) + { + tokens.Add(tk.GetNextToken()); } - return r_string; + + return tokens; }