// 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
/////////////////////////////////////////////////////////////////////////////
-#ifdef __GNUG__
- #pragma implementation "tokenzr.h"
-#endif
+// ============================================================================
+// declarations
+// ============================================================================
+
+// ----------------------------------------------------------------------------
+// headers
+// ----------------------------------------------------------------------------
// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
#endif
#include "wx/tokenzr.h"
+#include "wx/arrstr.h"
+
+// Required for wxIs... functions
+#include <ctype.h>
+
+// ============================================================================
+// implementation
+// ============================================================================
-wxStringTokenizer::wxStringTokenizer(const wxString& to_tokenize,
+// ----------------------------------------------------------------------------
+// wxStringTokenizer construction
+// ----------------------------------------------------------------------------
+
+wxStringTokenizer::wxStringTokenizer(const wxString& str,
const wxString& delims,
- bool ret_delims)
+ wxStringTokenizerMode mode)
{
- m_string = to_tokenize;
- m_delims = delims;
- m_retdelims = ret_delims;
- m_pos = 0;
+ SetString(str, delims, mode);
}
-wxStringTokenizer::~wxStringTokenizer()
+void wxStringTokenizer::SetString(const wxString& str,
+ const wxString& delims,
+ wxStringTokenizerMode mode)
{
-}
+ 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;
+ }
-int wxStringTokenizer::CountTokens() const
-{
- 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)
+ if ( *p )
{
- // if we're retaining delimiters, increment count
- count++;
+ // not whitespace char in delims
+ mode = wxTOKEN_RET_EMPTY;
}
else
{
- // if we're not retaining delimiters and at a token, inc count
- count += (!at_delim);
+ // only whitespaces
+ mode = wxTOKEN_STRTOK;
}
}
- return count;
+
+ m_delims = 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;
+ m_lastDelim = _T('\0');
+}
+
+// ----------------------------------------------------------------------------
+// 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, m_pos) != wxString::npos )
+ {
+ // there are non delimiter characters left, so we do have more tokens
+ return true;
+ }
+
+ switch ( m_mode )
+ {
+ case wxTOKEN_RET_EMPTY:
+ case wxTOKEN_RET_DELIMS:
+ // special hack for wxTOKEN_RET_EMPTY: we should return the initial
+ // empty token even if there are only delimiters after it
+ return m_pos == 0 && !m_string.empty();
+
+ case wxTOKEN_RET_EMPTY_ALL:
+ // special hack for wxTOKEN_RET_EMPTY_ALL: we can know if we had
+ // already returned the trailing empty token after the last
+ // delimiter by examining m_lastDelim: it is set to NUL if we run
+ // up to the end of the string in GetNextToken(), but if it is not
+ // NUL yet we still have this last token to return even if m_pos is
+ // already at m_string.length()
+ return m_pos < m_string.length() || m_lastDelim != _T('\0');
+
+ case wxTOKEN_INVALID:
+ case wxTOKEN_DEFAULT:
+ wxFAIL_MSG( _T("unexpected tokenizer mode") );
+ // fall through
+
+ case wxTOKEN_STRTOK:
+ // never return empty delimiters
+ break;
+ }
+
+ return false;
}
-bool wxStringTokenizer::HasMoreTokens()
+// count the number of (remaining) tokens in the string
+size_t wxStringTokenizer::CountTokens() const
{
- return (m_retdelims
- ? !m_string.IsEmpty()
- : m_string.find_first_not_of(m_delims) < m_string.length());
+ 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 its 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 tkz(m_string.c_str() + m_pos, m_delims, m_mode);
+
+ size_t count = 0;
+ while ( tkz.HasMoreTokens() )
+ {
+ count++;
+
+ (void)tkz.GetNextToken();
+ }
+
+ return count;
}
-wxString wxStringTokenizer::NextToken()
+// ----------------------------------------------------------------------------
+// token extraction
+// ----------------------------------------------------------------------------
+
+wxString wxStringTokenizer::GetNextToken()
{
- 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);
+ wxString token;
+ do
+ {
+ if ( !HasMoreTokens() )
+ {
+ break;
+ }
+
+ // find the end of this token
+ size_t pos = m_string.find_first_of(m_delims, m_pos);
+
+ // and the start of the next one
+ if ( pos == wxString::npos )
+ {
+ // no more delimiters, the token is everything till the end of
+ // string
+ token.assign(m_string, m_pos, wxString::npos);
+
+ // skip the token
+ m_pos = m_string.length();
+
+ // it wasn't terminated
+ m_lastDelim = _T('\0');
+ }
+ else // we found a delimiter at pos
+ {
+ // in wxTOKEN_RET_DELIMS mode we return the delimiter character
+ // with token, otherwise leave it out
+ size_t len = pos - m_pos;
+ if ( m_mode == wxTOKEN_RET_DELIMS )
+ len++;
+
+ token.assign(m_string, m_pos, len);
+
+ // skip the token and the trailing delimiter
+ m_pos = pos + 1;
+
+ m_lastDelim = m_string[pos];
+ }
}
- 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();
+ 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() )
+ {
+ tokens.Add(tk.GetNextToken());
}
- return r_string;
+
+ return tokens;
}