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") );
89 m_lastDelim
= _T('\0');
92 // ----------------------------------------------------------------------------
93 // access to the tokens
94 // ----------------------------------------------------------------------------
96 // do we have more of them?
97 bool wxStringTokenizer::HasMoreTokens() const
99 wxCHECK_MSG( IsOk(), false, _T("you should call SetString() first") );
101 if ( m_string
.find_first_not_of(m_delims
, m_pos
) != wxString::npos
)
103 // there are non delimiter characters left, so we do have more tokens
109 case wxTOKEN_RET_EMPTY
:
110 case wxTOKEN_RET_DELIMS
:
111 // special hack for wxTOKEN_RET_EMPTY: we should return the initial
112 // empty token even if there are only delimiters after it
113 return m_pos
== 0 && !m_string
.empty();
115 case wxTOKEN_RET_EMPTY_ALL
:
116 // special hack for wxTOKEN_RET_EMPTY_ALL: we can know if we had
117 // already returned the trailing empty token after the last
118 // delimiter by examining m_lastDelim: it is set to NUL if we run
119 // up to the end of the string in GetNextToken(), but if it is not
120 // NUL yet we still have this last token to return even if m_pos is
121 // already at m_string.length()
122 return m_pos
< m_string
.length() || m_lastDelim
!= _T('\0');
124 case wxTOKEN_INVALID
:
125 case wxTOKEN_DEFAULT
:
126 wxFAIL_MSG( _T("unexpected tokenizer mode") );
130 // never return empty delimiters
137 // count the number of (remaining) tokens in the string
138 size_t wxStringTokenizer::CountTokens() const
140 wxCHECK_MSG( IsOk(), 0, _T("you should call SetString() first") );
142 // VZ: this function is IMHO not very useful, so it's probably not very
143 // important if its implementation here is not as efficient as it
144 // could be -- but OTOH like this we're sure to get the correct answer
146 wxStringTokenizer
tkz(m_string
.c_str() + m_pos
, m_delims
, m_mode
);
149 while ( tkz
.HasMoreTokens() )
153 (void)tkz
.GetNextToken();
159 // ----------------------------------------------------------------------------
161 // ----------------------------------------------------------------------------
163 wxString
wxStringTokenizer::GetNextToken()
168 if ( !HasMoreTokens() )
173 // find the end of this token
174 size_t pos
= m_string
.find_first_of(m_delims
, m_pos
);
176 // and the start of the next one
177 if ( pos
== wxString::npos
)
179 // no more delimiters, the token is everything till the end of
181 token
.assign(m_string
, m_pos
, wxString::npos
);
184 m_pos
= m_string
.length();
186 // it wasn't terminated
187 m_lastDelim
= _T('\0');
189 else // we found a delimiter at pos
191 // in wxTOKEN_RET_DELIMS mode we return the delimiter character
192 // with token, otherwise leave it out
193 size_t len
= pos
- m_pos
;
194 if ( m_mode
== wxTOKEN_RET_DELIMS
)
197 token
.assign(m_string
, m_pos
, len
);
199 // skip the token and the trailing delimiter
202 m_lastDelim
= m_string
[pos
];
205 while ( !AllowEmpty() && token
.empty() );
210 // ----------------------------------------------------------------------------
212 // ----------------------------------------------------------------------------
214 wxArrayString
wxStringTokenize(const wxString
& str
,
215 const wxString
& delims
,
216 wxStringTokenizerMode mode
)
218 wxArrayString tokens
;
219 wxStringTokenizer
tk(str
, delims
, mode
);
220 while ( tk
.HasMoreTokens() )
222 tokens
.Add(tk
.GetNextToken());