1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: String tokenizer
4 // Author: Guilhem Lavaux
5 // Modified by: Vadim Zeitlin
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "tokenzr.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
31 #include "wx/tokenzr.h"
33 // ============================================================================
35 // ============================================================================
37 // ----------------------------------------------------------------------------
38 // wxStringTokenizer construction
39 // ----------------------------------------------------------------------------
41 wxStringTokenizer::wxStringTokenizer(const wxString
& str
,
42 const wxString
& delims
,
43 wxStringTokenizerMode mode
)
45 SetString(str
, delims
, mode
);
48 void wxStringTokenizer::SetString(const wxString
& str
,
49 const wxString
& delims
,
50 wxStringTokenizerMode mode
)
52 if ( mode
== wxTOKEN_DEFAULT
)
54 // by default, we behave like strtok() if the delimiters are only
55 // whitespace characters and as wxTOKEN_RET_EMPTY otherwise (for
56 // whitespace delimiters, strtok() behaviour is better because we want
57 // to count consecutive spaces as one delimiter)
59 for ( p
= delims
.c_str(); *p
; p
++ )
67 // not whitespace char in delims
68 mode
= wxTOKEN_RET_EMPTY
;
73 mode
= wxTOKEN_STRTOK
;
83 void wxStringTokenizer::Reinit(const wxString
& str
)
85 wxASSERT_MSG( IsOk(), _T("you should call SetString() first") );
90 // empty string doesn't have any tokens
91 m_hasMore
= !m_string
.empty();
94 // ----------------------------------------------------------------------------
95 // access to the tokens
96 // ----------------------------------------------------------------------------
98 // do we have more of them?
99 bool wxStringTokenizer::HasMoreTokens() const
101 wxCHECK_MSG( IsOk(), FALSE
, _T("you should call SetString() first") );
103 if ( m_string
.find_first_not_of(m_delims
) == wxString::npos
)
105 // no non empty tokens left, but in wxTOKEN_RET_EMPTY_ALL mode we
106 // still may return TRUE if GetNextToken() wasn't called yet for the
107 // last trailing empty token
108 return m_mode
== wxTOKEN_RET_EMPTY_ALL
? m_hasMore
: FALSE
;
112 // there are non delimiter characters left, hence we do have more
118 // count the number of tokens in the string
119 size_t wxStringTokenizer::CountTokens() const
121 wxCHECK_MSG( IsOk(), 0, _T("you should call SetString() first") );
123 // VZ: this function is IMHO not very useful, so it's probably not very
124 // important if it's implementation here is not as efficient as it
125 // could be - but OTOH like this we're sure to get the correct answer
127 wxStringTokenizer
*self
= (wxStringTokenizer
*)this; // const_cast
128 wxString stringInitial
= m_string
;
131 while ( self
->HasMoreTokens() )
135 (void)self
->GetNextToken();
138 self
->Reinit(stringInitial
);
143 // ----------------------------------------------------------------------------
145 // ----------------------------------------------------------------------------
147 wxString
wxStringTokenizer::GetNextToken()
149 // strtok() doesn't return empty tokens, all other modes do
150 bool allowEmpty
= m_mode
!= wxTOKEN_STRTOK
;
155 if ( !HasMoreTokens() )
159 // find the end of this token
160 size_t pos
= m_string
.find_first_of(m_delims
);
162 // and the start of the next one
163 if ( pos
== wxString::npos
)
165 // no more delimiters, the token is everything till the end of
169 m_pos
+= m_string
.length();
172 // no more tokens in this string, even in wxTOKEN_RET_EMPTY_ALL
173 // mode (we will return the trailing one right now in this case)
178 size_t pos2
= pos
+ 1;
180 // in wxTOKEN_RET_DELIMS mode we return the delimiter character
182 token
= wxString(m_string
, m_mode
== wxTOKEN_RET_DELIMS
? pos2
185 // remove token with the following it delimiter from string
186 m_string
.erase(0, pos2
);
188 // keep track of the position in the original string too
192 while ( !allowEmpty
&& token
.empty() );