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"
33 // Required for wxIs... functions
36 // ============================================================================
38 // ============================================================================
40 // ----------------------------------------------------------------------------
41 // wxStringTokenizer construction
42 // ----------------------------------------------------------------------------
44 wxStringTokenizer::wxStringTokenizer(const wxString
& str
,
45 const wxString
& delims
,
46 wxStringTokenizerMode mode
)
48 SetString(str
, delims
, mode
);
51 void wxStringTokenizer::SetString(const wxString
& str
,
52 const wxString
& delims
,
53 wxStringTokenizerMode mode
)
55 if ( mode
== wxTOKEN_DEFAULT
)
57 // by default, we behave like strtok() if the delimiters are only
58 // whitespace characters and as wxTOKEN_RET_EMPTY otherwise (for
59 // whitespace delimiters, strtok() behaviour is better because we want
60 // to count consecutive spaces as one delimiter)
62 for ( p
= delims
.c_str(); *p
; p
++ )
70 // not whitespace char in delims
71 mode
= wxTOKEN_RET_EMPTY
;
76 mode
= wxTOKEN_STRTOK
;
86 void wxStringTokenizer::Reinit(const wxString
& str
)
88 wxASSERT_MSG( IsOk(), _T("you should call SetString() first") );
93 // empty string doesn't have any tokens
94 m_hasMore
= !m_string
.empty();
97 // ----------------------------------------------------------------------------
98 // access to the tokens
99 // ----------------------------------------------------------------------------
101 // do we have more of them?
102 bool wxStringTokenizer::HasMoreTokens() const
104 wxCHECK_MSG( IsOk(), FALSE
, _T("you should call SetString() first") );
106 if ( m_string
.find_first_not_of(m_delims
) == wxString::npos
)
108 // no non empty tokens left, but in 2 cases we still may return TRUE if
109 // GetNextToken() wasn't called yet for this empty token:
111 // a) in wxTOKEN_RET_EMPTY_ALL mode we always do it
112 // b) in wxTOKEN_RET_EMPTY mode we do it in the special case of a
113 // string containing only the delimiter: then there is an empty
114 // token just before it
115 return (m_mode
== wxTOKEN_RET_EMPTY_ALL
) ||
116 (m_mode
== wxTOKEN_RET_EMPTY
&& m_pos
== 0)
121 // there are non delimiter characters left, hence we do have more
127 // count the number of tokens in the string
128 size_t wxStringTokenizer::CountTokens() const
130 wxCHECK_MSG( IsOk(), 0, _T("you should call SetString() first") );
132 // VZ: this function is IMHO not very useful, so it's probably not very
133 // important if it's implementation here is not as efficient as it
134 // could be - but OTOH like this we're sure to get the correct answer
136 wxStringTokenizer
*self
= (wxStringTokenizer
*)this; // const_cast
137 wxString stringInitial
= m_string
;
140 while ( self
->HasMoreTokens() )
144 (void)self
->GetNextToken();
147 self
->Reinit(stringInitial
);
152 // ----------------------------------------------------------------------------
154 // ----------------------------------------------------------------------------
156 wxString
wxStringTokenizer::GetNextToken()
158 // strtok() doesn't return empty tokens, all other modes do
159 bool allowEmpty
= m_mode
!= wxTOKEN_STRTOK
;
164 if ( !HasMoreTokens() )
168 // find the end of this token
169 size_t pos
= m_string
.find_first_of(m_delims
);
171 // and the start of the next one
172 if ( pos
== wxString::npos
)
174 // no more delimiters, the token is everything till the end of
178 m_pos
+= m_string
.length();
181 // no more tokens in this string, even in wxTOKEN_RET_EMPTY_ALL
182 // mode (we will return the trailing one right now in this case)
187 size_t pos2
= pos
+ 1;
189 // in wxTOKEN_RET_DELIMS mode we return the delimiter character
191 token
= wxString(m_string
, m_mode
== wxTOKEN_RET_DELIMS
? pos2
194 // remove token with the following it delimiter from string
195 m_string
.erase(0, pos2
);
197 // keep track of the position in the original string too
201 while ( !allowEmpty
&& token
.empty() );
206 // ----------------------------------------------------------------------------
208 // ----------------------------------------------------------------------------
210 wxArrayString
wxStringTokenize(const wxString
& str
,
211 const wxString
& delims
,
212 wxStringTokenizerMode mode
)
214 wxArrayString tokens
;
215 wxStringTokenizer
tk(str
, delims
, mode
);
216 while ( tk
.HasMoreTokens() )
218 tokens
.Add(tk
.GetNextToken());