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 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/tokenzr.h"
28 #include "wx/arrstr.h"
30 // Required for wxIs... functions
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 2 cases we still may return true if
106 // GetNextToken() wasn't called yet for this empty token:
108 // a) in wxTOKEN_RET_EMPTY_ALL mode we always do it
109 // b) in wxTOKEN_RET_EMPTY mode we do it in the special case of a
110 // string containing only the delimiter: then there is an empty
111 // token just before it
112 return (m_mode
== wxTOKEN_RET_EMPTY_ALL
) ||
113 (m_mode
== wxTOKEN_RET_EMPTY
&& m_pos
== 0)
118 // there are non delimiter characters left, hence we do have more
124 // count the number of tokens in the string
125 size_t wxStringTokenizer::CountTokens() const
127 wxCHECK_MSG( IsOk(), 0, _T("you should call SetString() first") );
129 // VZ: this function is IMHO not very useful, so it's probably not very
130 // important if it's implementation here is not as efficient as it
131 // could be - but OTOH like this we're sure to get the correct answer
133 wxStringTokenizer
*self
= (wxStringTokenizer
*)this; // const_cast
134 wxString stringInitial
= m_string
;
137 while ( self
->HasMoreTokens() )
141 (void)self
->GetNextToken();
144 self
->Reinit(stringInitial
);
149 // ----------------------------------------------------------------------------
151 // ----------------------------------------------------------------------------
153 wxString
wxStringTokenizer::GetNextToken()
155 // strtok() doesn't return empty tokens, all other modes do
156 bool allowEmpty
= m_mode
!= wxTOKEN_STRTOK
;
161 if ( !HasMoreTokens() )
165 // find the end of this token
166 size_t pos
= m_string
.find_first_of(m_delims
);
168 // and the start of the next one
169 if ( pos
== wxString::npos
)
171 // no more delimiters, the token is everything till the end of
175 m_pos
+= m_string
.length();
178 // no more tokens in this string, even in wxTOKEN_RET_EMPTY_ALL
179 // mode (we will return the trailing one right now in this case)
184 size_t pos2
= pos
+ 1;
186 // in wxTOKEN_RET_DELIMS mode we return the delimiter character
188 token
= wxString(m_string
, m_mode
== wxTOKEN_RET_DELIMS
? pos2
191 // remove token with the following it delimiter from string
192 m_string
.erase(0, pos2
);
194 // keep track of the position in the original string too
198 while ( !allowEmpty
&& token
.empty() );
203 // ----------------------------------------------------------------------------
205 // ----------------------------------------------------------------------------
207 wxArrayString
wxStringTokenize(const wxString
& str
,
208 const wxString
& delims
,
209 wxStringTokenizerMode mode
)
211 wxArrayString tokens
;
212 wxStringTokenizer
tk(str
, delims
, mode
);
213 while ( tk
.HasMoreTokens() )
215 tokens
.Add(tk
.GetNextToken());