]>
git.saurik.com Git - wxWidgets.git/blob - src/common/tokenzr.cpp
0601d5d0e5f762c9d84624ab38d2713923f1a0d3
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 // ============================================================================
35 // ============================================================================
37 // ----------------------------------------------------------------------------
38 // wxStringTokenizer construction
39 // ----------------------------------------------------------------------------
41 wxStringTokenizer::wxStringTokenizer(const wxString
& to_tokenize
,
42 const wxString
& delims
,
45 SetString(to_tokenize
, delims
, ret_delims
);
48 void wxStringTokenizer::SetString(const wxString
& to_tokenize
,
49 const wxString
& delims
,
52 m_string
= to_tokenize
;
54 m_retdelims
= ret_delim
;
57 // empty string doesn't have any tokens
58 m_hasMore
= !m_string
.empty();
61 wxStringTokenizer::~wxStringTokenizer()
65 // ----------------------------------------------------------------------------
66 // count the number of tokens in the string
67 // ----------------------------------------------------------------------------
69 size_t wxStringTokenizer::CountTokens() const
75 pos
= m_string
.find_first_of(m_delims
, pos
);
76 if ( pos
== wxString::npos
)
79 count
++; // one more token found
81 pos
++; // skip delimiter
84 // normally, we didn't count the last token in the loop above - so add it
85 // unless the string was empty from the very beginning, in which case it
86 // still has 0 (and not 1) tokens
87 if ( !m_string
.empty() )
95 // ----------------------------------------------------------------------------
97 // ----------------------------------------------------------------------------
99 wxString
wxStringTokenizer::GetNextToken()
102 if ( HasMoreTokens() )
104 size_t pos
= m_string
.find_first_of(m_delims
); // end of token
105 size_t pos2
; // start of the next one
106 if ( pos
!= wxString::npos
)
108 // return the delimiter too
113 pos2
= m_string
.length();
115 // no more tokens in this string
119 token
= wxString(m_string
, m_retdelims
? pos2
: pos
);
121 // remove token with the following it delimiter from string
122 m_string
.erase(0, pos2
);
124 // keep track of the position in the original string too
127 //else: no more tokens, return empty token