]>
git.saurik.com Git - wxWidgets.git/blob - src/common/tokenzr.cpp
95eb0d586072f8c5a10cf4741cd7abb0271e0304
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: String tokenizer
4 // Author: Guilhem Lavaux
5 // Modified by: Gregory Pietsch
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "tokenzr.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
23 #include "wx/tokenzr.h"
25 wxStringTokenizer::wxStringTokenizer(const wxString
& to_tokenize
,
26 const wxString
& delims
,
29 m_string
= to_tokenize
;
31 m_retdelims
= ret_delims
;
35 wxStringTokenizer::~wxStringTokenizer()
39 int wxStringTokenizer::CountTokens() const
45 while (pos
< m_string
.length()) {
46 // while we're still counting ...
47 at_delim
= (m_delims
.find(m_string
.at(pos
)) < m_delims
.length());
48 // are we at a delimiter? if so, move to the next nondelimiter;
49 // if not, move to the next delimiter. If the find_first_of
50 // and find_first_not_of methods fail, pos will be assigned
51 // npos (0xFFFFFFFF) which will terminate the loop on the next
52 // go-round unless we have a really long string, which is unlikely
53 pos
= at_delim
? m_string
.find_first_not_of(m_delims
, pos
)
54 : m_string
.find_first_of(m_delims
, pos
);
57 // if we're retaining delimiters, increment count
62 // if we're not retaining delimiters and at a token, inc count
69 bool wxStringTokenizer::HasMoreTokens()
73 : m_string
.find_first_not_of(m_delims
) < m_string
.length());
76 wxString
wxStringTokenizer::NextToken()
81 if ( m_string
.IsEmpty() )
83 pos
= m_string
.find_first_not_of(m_delims
);
85 // we're retaining delimiters (unusual behavior, IMHO)
87 // first char is a non-delimiter
88 pos
= m_string
.find_first_of(m_delims
);
90 // we're not retaining delimiters
91 m_string
.erase(0, pos
);
93 if (m_string
.IsEmpty())
95 pos
= m_string
.find_first_of(m_delims
);
97 if (pos
<= m_string
.length()) {
98 r_string
= m_string
.substr(0, pos
);
99 m_string
.erase(0, pos
);
103 m_pos
+= m_string
.length();