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 // 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 wxTOKEN_RET_EMPTY_ALL mode we
109 // still may return TRUE if GetNextToken() wasn't called yet for the
110 // last trailing empty token
111 return m_mode
== wxTOKEN_RET_EMPTY_ALL
? m_hasMore
: FALSE
;
115 // there are non delimiter characters left, hence we do have more
121 // count the number of tokens in the string
122 size_t wxStringTokenizer::CountTokens() const
124 wxCHECK_MSG( IsOk(), 0, _T("you should call SetString() first") );
126 // VZ: this function is IMHO not very useful, so it's probably not very
127 // important if it's implementation here is not as efficient as it
128 // could be - but OTOH like this we're sure to get the correct answer
130 wxStringTokenizer
*self
= (wxStringTokenizer
*)this; // const_cast
131 wxString stringInitial
= m_string
;
134 while ( self
->HasMoreTokens() )
138 (void)self
->GetNextToken();
141 self
->Reinit(stringInitial
);
146 // ----------------------------------------------------------------------------
148 // ----------------------------------------------------------------------------
150 wxString
wxStringTokenizer::GetNextToken()
152 // strtok() doesn't return empty tokens, all other modes do
153 bool allowEmpty
= m_mode
!= wxTOKEN_STRTOK
;
158 if ( !HasMoreTokens() )
162 // find the end of this token
163 size_t pos
= m_string
.find_first_of(m_delims
);
165 // and the start of the next one
166 if ( pos
== wxString::npos
)
168 // no more delimiters, the token is everything till the end of
172 m_pos
+= m_string
.length();
175 // no more tokens in this string, even in wxTOKEN_RET_EMPTY_ALL
176 // mode (we will return the trailing one right now in this case)
181 size_t pos2
= pos
+ 1;
183 // in wxTOKEN_RET_DELIMS mode we return the delimiter character
185 token
= wxString(m_string
, m_mode
== wxTOKEN_RET_DELIMS
? pos2
188 // remove token with the following it delimiter from string
189 m_string
.erase(0, pos2
);
191 // keep track of the position in the original string too
195 while ( !allowEmpty
&& token
.empty() );