]> git.saurik.com Git - wxWidgets.git/blob - src/common/tokenzr.cpp
1. bug in wxString::find_first_of() fixed
[wxWidgets.git] / src / common / tokenzr.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: tokenzr.cpp
3 // Purpose: String tokenizer
4 // Author: Guilhem Lavaux
5 // Modified by: Vadim Zeitlin
6 // Created: 04/22/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "tokenzr.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #include "wx/tokenzr.h"
32
33 // ============================================================================
34 // implementation
35 // ============================================================================
36
37 // ----------------------------------------------------------------------------
38 // wxStringTokenizer construction
39 // ----------------------------------------------------------------------------
40
41 wxStringTokenizer::wxStringTokenizer(const wxString& to_tokenize,
42 const wxString& delims,
43 bool ret_delims)
44 {
45 SetString(to_tokenize, delims, ret_delims);
46 }
47
48 void wxStringTokenizer::SetString(const wxString& to_tokenize,
49 const wxString& delims,
50 bool ret_delim)
51 {
52 m_string = to_tokenize;
53 m_delims = delims;
54 m_retdelims = ret_delim;
55 m_pos = 0;
56
57 // empty string doesn't have any tokens
58 m_hasMore = !m_string.empty();
59 }
60
61 wxStringTokenizer::~wxStringTokenizer()
62 {
63 }
64
65 // ----------------------------------------------------------------------------
66 // count the number of tokens in the string
67 // ----------------------------------------------------------------------------
68
69 size_t wxStringTokenizer::CountTokens() const
70 {
71 size_t pos = 0;
72 size_t count = 0;
73 for ( ;; )
74 {
75 pos = m_string.find_first_of(m_delims, pos);
76 if ( pos == wxString::npos )
77 break;
78
79 count++; // one more token found
80
81 pos++; // skip delimiter
82 }
83
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() )
88 {
89 count++;
90 }
91
92 return count;
93 }
94
95 // ----------------------------------------------------------------------------
96 // token extraction
97 // ----------------------------------------------------------------------------
98
99 wxString wxStringTokenizer::GetNextToken()
100 {
101 wxString token;
102 if ( HasMoreTokens() )
103 {
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 )
107 {
108 // return the delimiter too
109 pos2 = pos + 1;
110 }
111 else
112 {
113 pos2 = m_string.length();
114
115 // no more tokens in this string
116 m_hasMore = FALSE;
117 }
118
119 token = wxString(m_string, m_retdelims ? pos2 : pos);
120
121 // remove token with the following it delimiter from string
122 m_string.erase(0, pos2);
123
124 // keep track of the position in the original string too
125 m_pos += pos2;
126 }
127 //else: no more tokens, return empty token
128
129 return token;
130 }