// Name: tokenzr.cpp
// Purpose: String tokenizer
// Author: Guilhem Lavaux
-// Modified by:
+// Modified by: Vadim Zeitlin
// Created: 04/22/98
// RCS-ID: $Id$
// Copyright: (c) Guilhem Lavaux
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
+// ============================================================================
+// declarations
+// ============================================================================
+
+// ----------------------------------------------------------------------------
+// headers
+// ----------------------------------------------------------------------------
+
#ifdef __GNUG__
#pragma implementation "tokenzr.h"
#endif
#include "wx/tokenzr.h"
+// ============================================================================
+// implementation
+// ============================================================================
+
+// ----------------------------------------------------------------------------
+// wxStringTokenizer construction
+// ----------------------------------------------------------------------------
+
wxStringTokenizer::wxStringTokenizer(const wxString& to_tokenize,
const wxString& delims,
bool ret_delims)
+{
+ SetString(to_tokenize, delims, ret_delims);
+}
+
+void wxStringTokenizer::SetString(const wxString& to_tokenize,
+ const wxString& delims,
+ bool ret_delim)
{
m_string = to_tokenize;
m_delims = delims;
- m_retdelims = ret_delims;
+ m_retdelims = ret_delim;
+ m_pos = 0;
+
+ // empty string doesn't have any tokens
+ m_hasMore = !m_string.empty();
}
wxStringTokenizer::~wxStringTokenizer()
{
}
-off_t wxStringTokenizer::FindDelims(const wxString& str, const wxString& delims) const
+// ----------------------------------------------------------------------------
+// count the number of tokens in the string
+// ----------------------------------------------------------------------------
+
+size_t wxStringTokenizer::CountTokens() const
{
- for ( size_t i = 0; i < str.Length(); i++ )
+ size_t pos = 0;
+ size_t count = 0;
+ for ( ;; )
{
- char c = str[i];
-
- for ( size_t j = 0; j < delims.Length() ; j++ )
- {
- if ( delims[j] == c )
- return i;
- }
- }
-
- return -1;
-}
+ pos = m_string.find_first_of(m_delims, pos);
+ if ( pos == wxString::npos )
+ break;
-int wxStringTokenizer::CountTokens() const
-{
- wxString p_string = m_string;
- bool found = TRUE;
- int pos, count = 1;
+ count++; // one more token found
- if (p_string.Length() == 0)
- return 0;
+ pos++; // skip delimiter
+ }
- while (found)
+ // normally, we didn't count the last token in the loop above - so add it
+ // unless the string was empty from the very beginning, in which case it
+ // still has 0 (and not 1) tokens
+ if ( !m_string.empty() )
{
- pos = FindDelims(p_string, m_delims);
- if (pos != -1)
- {
- count++;
- p_string = p_string(0, pos);
- }
- else
- {
- found = FALSE;
- }
+ count++;
}
return count;
}
-bool wxStringTokenizer::HasMoreTokens()
-{
- return !m_string.IsEmpty();
-}
+// ----------------------------------------------------------------------------
+// token extraction
+// ----------------------------------------------------------------------------
-// needed to fix leading whitespace / mult. delims bugs
-void wxStringTokenizer::EatLeadingDelims()
+wxString wxStringTokenizer::GetNextToken()
{
- int pos;
-
- // while leading delims trim 'em from the left
- while ( ( pos = FindDelims(m_string, m_delims)) == 0 )
- {
- m_string = m_string.Mid((size_t)1);
- }
-}
-
-wxString wxStringTokenizer::NextToken()
-{
- off_t pos, pos2;
- wxString r_string;
-
- if ( m_string.IsEmpty() )
- return m_string;
-
- if ( !m_retdelims )
- EatLeadingDelims();
-
- pos = FindDelims(m_string, m_delims);
- if (pos == -1)
+ wxString token;
+ if ( HasMoreTokens() )
{
- r_string = m_string;
- m_string = wxEmptyString;
-
- return r_string;
- }
-
- if (m_retdelims)
- {
- if (!pos)
+ size_t pos = m_string.find_first_of(m_delims); // end of token
+ size_t pos2; // start of the next one
+ if ( pos != wxString::npos )
{
- pos++;
- pos2 = 1;
+ // return the delimiter too
+ pos2 = pos + 1;
}
else
{
- pos2 = pos;
+ pos2 = m_string.length();
+
+ // no more tokens in this string
+ m_hasMore = FALSE;
}
- }
- else
- {
- pos2 = pos + 1;
- }
- r_string = m_string.Left((size_t)pos);
- m_string = m_string.Mid((size_t)pos2);
+ token = wxString(m_string, m_retdelims ? 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: no more tokens, return empty token
- return r_string;
+ return token;
}