]> git.saurik.com Git - wxWidgets.git/blame - src/common/tokenzr.cpp
oops, undid last commit, the extra test is unneded (but a comment explaining why...
[wxWidgets.git] / src / common / tokenzr.cpp
CommitLineData
f4ada568
GL
1/////////////////////////////////////////////////////////////////////////////
2// Name: tokenzr.cpp
3// Purpose: String tokenizer
4// Author: Guilhem Lavaux
1e6feb95 5// Modified by: Vadim Zeitlin (almost full rewrite)
f4ada568
GL
6// Created: 04/22/98
7// RCS-ID: $Id$
8// Copyright: (c) Guilhem Lavaux
65571936 9// Licence: wxWindows licence
f4ada568
GL
10/////////////////////////////////////////////////////////////////////////////
11
bbf8fc53
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
fcc6dddd
JS
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
85833f5c 24 #pragma hdrstop
fcc6dddd
JS
25#endif
26
f4ada568 27#include "wx/tokenzr.h"
df5168c4 28#include "wx/arrstr.h"
f4ada568 29
3f8e5072
JS
30// Required for wxIs... functions
31#include <ctype.h>
32
bbf8fc53
VZ
33// ============================================================================
34// implementation
35// ============================================================================
36
37// ----------------------------------------------------------------------------
38// wxStringTokenizer construction
39// ----------------------------------------------------------------------------
40
7c968cee 41wxStringTokenizer::wxStringTokenizer(const wxString& str,
f4ada568 42 const wxString& delims,
7c968cee 43 wxStringTokenizerMode mode)
bbf8fc53 44{
7c968cee 45 SetString(str, delims, mode);
bbf8fc53
VZ
46}
47
7c968cee 48void wxStringTokenizer::SetString(const wxString& str,
bbf8fc53 49 const wxString& delims,
7c968cee 50 wxStringTokenizerMode mode)
f4ada568 51{
7c968cee
VZ
52 if ( mode == wxTOKEN_DEFAULT )
53 {
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)
58 const wxChar *p;
59 for ( p = delims.c_str(); *p; p++ )
60 {
61 if ( !wxIsspace(*p) )
62 break;
63 }
64
65 if ( *p )
66 {
67 // not whitespace char in delims
68 mode = wxTOKEN_RET_EMPTY;
69 }
70 else
71 {
72 // only whitespaces
73 mode = wxTOKEN_STRTOK;
74 }
75 }
76
85833f5c 77 m_delims = delims;
7c968cee 78 m_mode = mode;
bbf8fc53 79
7c968cee 80 Reinit(str);
f4ada568
GL
81}
82
7c968cee 83void wxStringTokenizer::Reinit(const wxString& str)
f4ada568 84{
7c968cee
VZ
85 wxASSERT_MSG( IsOk(), _T("you should call SetString() first") );
86
87 m_string = str;
88 m_pos = 0;
4626c57c 89 m_lastDelim = _T('\0');
f4ada568
GL
90}
91
bbf8fc53 92// ----------------------------------------------------------------------------
7c968cee 93// access to the tokens
bbf8fc53
VZ
94// ----------------------------------------------------------------------------
95
7c968cee
VZ
96// do we have more of them?
97bool wxStringTokenizer::HasMoreTokens() const
f4ada568 98{
cb719f2e 99 wxCHECK_MSG( IsOk(), false, _T("you should call SetString() first") );
7c968cee 100
4626c57c 101 if ( m_string.find_first_not_of(m_delims, m_pos) != wxString::npos )
bbf8fc53 102 {
4626c57c
VZ
103 // there are non delimiter characters left, so we do have more tokens
104 return true;
7c968cee 105 }
4626c57c
VZ
106
107 switch ( m_mode )
7c968cee 108 {
4626c57c
VZ
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();
114
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');
123
124 case wxTOKEN_INVALID:
125 case wxTOKEN_DEFAULT:
126 wxFAIL_MSG( _T("unexpected tokenizer mode") );
127 // fall through
128
129 case wxTOKEN_STRTOK:
130 // never return empty delimiters
131 break;
7c968cee 132 }
4626c57c
VZ
133
134 return false;
7c968cee 135}
bbf8fc53 136
4626c57c 137// count the number of (remaining) tokens in the string
7c968cee
VZ
138size_t wxStringTokenizer::CountTokens() const
139{
140 wxCHECK_MSG( IsOk(), 0, _T("you should call SetString() first") );
bbf8fc53 141
7c968cee 142 // VZ: this function is IMHO not very useful, so it's probably not very
4626c57c
VZ
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
7c968cee 145 // in all modes
4626c57c 146 wxStringTokenizer tkz(m_string.c_str() + m_pos, m_delims, m_mode);
bbf8fc53 147
7c968cee 148 size_t count = 0;
4626c57c 149 while ( tkz.HasMoreTokens() )
bbf8fc53
VZ
150 {
151 count++;
7c968cee 152
4626c57c 153 (void)tkz.GetNextToken();
bbf8fc53
VZ
154 }
155
156 return count;
157}
158
159// ----------------------------------------------------------------------------
160// token extraction
161// ----------------------------------------------------------------------------
162
163wxString wxStringTokenizer::GetNextToken()
164{
165 wxString token;
7c968cee 166 do
bbf8fc53 167 {
7c968cee 168 if ( !HasMoreTokens() )
85833f5c 169 {
7c968cee 170 break;
85833f5c 171 }
4626c57c 172
7c968cee 173 // find the end of this token
4626c57c 174 size_t pos = m_string.find_first_of(m_delims, m_pos);
7c968cee
VZ
175
176 // and the start of the next one
177 if ( pos == wxString::npos )
85833f5c 178 {
7c968cee
VZ
179 // no more delimiters, the token is everything till the end of
180 // string
4626c57c 181 token.assign(m_string, m_pos, wxString::npos);
7c968cee 182
4626c57c
VZ
183 // skip the token
184 m_pos = m_string.length();
bbf8fc53 185
4626c57c
VZ
186 // it wasn't terminated
187 m_lastDelim = _T('\0');
85833f5c 188 }
4626c57c 189 else // we found a delimiter at pos
7c968cee 190 {
7c968cee 191 // in wxTOKEN_RET_DELIMS mode we return the delimiter character
4626c57c
VZ
192 // with token, otherwise leave it out
193 size_t len = pos - m_pos;
194 if ( m_mode == wxTOKEN_RET_DELIMS )
195 len++;
196
197 token.assign(m_string, m_pos, len);
dab58492 198
4626c57c
VZ
199 // skip the token and the trailing delimiter
200 m_pos = pos + 1;
bbf8fc53 201
4626c57c 202 m_lastDelim = m_string[pos];
7c968cee 203 }
85833f5c 204 }
4626c57c 205 while ( !AllowEmpty() && token.empty() );
bbf8fc53
VZ
206
207 return token;
f4ada568 208}
1e6feb95
VZ
209
210// ----------------------------------------------------------------------------
211// public functions
212// ----------------------------------------------------------------------------
213
214wxArrayString wxStringTokenize(const wxString& str,
215 const wxString& delims,
216 wxStringTokenizerMode mode)
217{
218 wxArrayString tokens;
219 wxStringTokenizer tk(str, delims, mode);
220 while ( tk.HasMoreTokens() )
221 {
222 tokens.Add(tk.GetNextToken());
223 }
224
225 return tokens;
226}