1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: String tokenizer
4 // Author: Guilhem Lavaux
5 // Modified by: Vadim Zeitlin (almost full rewrite)
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"
32 #include "wx/arrstr.h"
34 // Required for wxIs... functions
37 // ============================================================================
39 // ============================================================================
41 // ----------------------------------------------------------------------------
42 // wxStringTokenizer construction
43 // ----------------------------------------------------------------------------
45 wxStringTokenizer::wxStringTokenizer(const wxString
& str
,
46 const wxString
& delims
,
47 wxStringTokenizerMode mode
)
49 SetString(str
, delims
, mode
);
52 void wxStringTokenizer::SetString(const wxString
& str
,
53 const wxString
& delims
,
54 wxStringTokenizerMode mode
)
56 if ( mode
== wxTOKEN_DEFAULT
)
58 // by default, we behave like strtok() if the delimiters are only
59 // whitespace characters and as wxTOKEN_RET_EMPTY otherwise (for
60 // whitespace delimiters, strtok() behaviour is better because we want
61 // to count consecutive spaces as one delimiter)
63 for ( p
= delims
.c_str(); *p
; p
++ )
71 // not whitespace char in delims
72 mode
= wxTOKEN_RET_EMPTY
;
77 mode
= wxTOKEN_STRTOK
;
87 void wxStringTokenizer::Reinit(const wxString
& str
)
89 wxASSERT_MSG( IsOk(), _T("you should call SetString() first") );
94 // empty string doesn't have any tokens
95 m_hasMore
= !m_string
.empty();
98 // ----------------------------------------------------------------------------
99 // access to the tokens
100 // ----------------------------------------------------------------------------
102 // do we have more of them?
103 bool wxStringTokenizer::HasMoreTokens() const
105 wxCHECK_MSG( IsOk(), FALSE
, _T("you should call SetString() first") );
107 if ( m_string
.find_first_not_of(m_delims
) == wxString::npos
)
109 // no non empty tokens left, but in 2 cases we still may return TRUE if
110 // GetNextToken() wasn't called yet for this empty token:
112 // a) in wxTOKEN_RET_EMPTY_ALL mode we always do it
113 // b) in wxTOKEN_RET_EMPTY mode we do it in the special case of a
114 // string containing only the delimiter: then there is an empty
115 // token just before it
116 return (m_mode
== wxTOKEN_RET_EMPTY_ALL
) ||
117 (m_mode
== wxTOKEN_RET_EMPTY
&& m_pos
== 0)
122 // there are non delimiter characters left, hence we do have more
128 // count the number of tokens in the string
129 size_t wxStringTokenizer::CountTokens() const
131 wxCHECK_MSG( IsOk(), 0, _T("you should call SetString() first") );
133 // VZ: this function is IMHO not very useful, so it's probably not very
134 // important if it's implementation here is not as efficient as it
135 // could be - but OTOH like this we're sure to get the correct answer
137 wxStringTokenizer
*self
= (wxStringTokenizer
*)this; // const_cast
138 wxString stringInitial
= m_string
;
141 while ( self
->HasMoreTokens() )
145 (void)self
->GetNextToken();
148 self
->Reinit(stringInitial
);
153 // ----------------------------------------------------------------------------
155 // ----------------------------------------------------------------------------
157 wxString
wxStringTokenizer::GetNextToken()
159 // strtok() doesn't return empty tokens, all other modes do
160 bool allowEmpty
= m_mode
!= wxTOKEN_STRTOK
;
165 if ( !HasMoreTokens() )
169 // find the end of this token
170 size_t pos
= m_string
.find_first_of(m_delims
);
172 // and the start of the next one
173 if ( pos
== wxString::npos
)
175 // no more delimiters, the token is everything till the end of
179 m_pos
+= m_string
.length();
182 // no more tokens in this string, even in wxTOKEN_RET_EMPTY_ALL
183 // mode (we will return the trailing one right now in this case)
188 size_t pos2
= pos
+ 1;
190 // in wxTOKEN_RET_DELIMS mode we return the delimiter character
192 token
= wxString(m_string
, m_mode
== wxTOKEN_RET_DELIMS
? pos2
195 // remove token with the following it delimiter from string
196 m_string
.erase(0, pos2
);
198 // keep track of the position in the original string too
202 while ( !allowEmpty
&& token
.empty() );
207 // ----------------------------------------------------------------------------
209 // ----------------------------------------------------------------------------
211 wxArrayString
wxStringTokenize(const wxString
& str
,
212 const wxString
& delims
,
213 wxStringTokenizerMode mode
)
215 wxArrayString tokens
;
216 wxStringTokenizer
tk(str
, delims
, mode
);
217 while ( tk
.HasMoreTokens() )
219 tokens
.Add(tk
.GetNextToken());