1 |
/* java.util.StringTokenizer |
/* StringTokenizer -- breaks a String into tokens |
2 |
Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc. |
Copyright (C) 1998, 1999, 2001, 2002 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
39 |
package java.util; |
package java.util; |
40 |
|
|
41 |
/** |
/** |
42 |
* This class splits a string into tokens. The caller can set on which |
* This class splits a string into tokens. The caller can set on which |
43 |
* delimiters the string should be split and if the delimiters should be |
* delimiters the string should be split and if the delimiters should be |
44 |
* returned. |
* returned. This is much simpler than {@link java.io.StreamTokenizer}. |
45 |
* |
* |
46 |
* You may change the delimiter set on the fly by calling |
* <p>You may change the delimiter set on the fly by calling |
47 |
* nextToken(String). But the semantic is quite difficult; it even |
* nextToken(String). But the semantic is quite difficult; it even |
48 |
* depends on calling <code>hasMoreTokens()</code>. You should call |
* depends on calling <code>hasMoreTokens()</code>. You should call |
49 |
* <code>hasMoreTokens()</code> before, otherwise the old delimiters |
* <code>hasMoreTokens()</code> before, otherwise the old delimiters |
50 |
* after the last token are returned. |
* after the last token are candidates for being returned. |
51 |
* |
* |
52 |
* If you want to get the delimiters, you have to use the three argument |
* <p>If you want to get the delimiters, you have to use the three argument |
53 |
* constructor. The delimiters are returned as token consisting of a |
* constructor. The delimiters are returned as token consisting of a |
54 |
* single character. |
* single character. |
55 |
* |
* |
56 |
* @author Jochen Hoenicke |
* @author Jochen Hoenicke |
57 |
* @author Warren Levy <warrenl@cygnus.com> |
* @author Warren Levy <warrenl@cygnus.com> |
58 |
|
* @see java.io.StreamTokenizer |
59 |
|
* @status updated to 1.4 |
60 |
*/ |
*/ |
61 |
public class StringTokenizer implements Enumeration |
public class StringTokenizer implements Enumeration |
62 |
{ |
{ |
64 |
* The position in the str, where we currently are. |
* The position in the str, where we currently are. |
65 |
*/ |
*/ |
66 |
private int pos; |
private int pos; |
67 |
|
|
68 |
/** |
/** |
69 |
* The string that should be split into tokens. |
* The string that should be split into tokens. |
70 |
*/ |
*/ |
71 |
private String str; |
private String str; |
72 |
|
|
73 |
/** |
/** |
74 |
* The string containing the delimiter characters. |
* The string containing the delimiter characters. |
75 |
*/ |
*/ |
76 |
private String delim; |
private String delim; |
77 |
|
|
78 |
/** |
/** |
79 |
* Tells, if we should return the delimiters. |
* Tells, if we should return the delimiters. |
80 |
*/ |
*/ |
81 |
private boolean retDelims; |
private boolean retDelims; |
82 |
|
|
|
/*{ |
|
|
invariant { |
|
|
pos >= 0 :: "position is negative"; |
|
|
pos <= str.length() :: "position is out of string"; |
|
|
str != null :: "String is null"; |
|
|
delim != null :: "Delimiters are null"; |
|
|
} |
|
|
} */ |
|
|
|
|
83 |
/** |
/** |
84 |
* Creates a new StringTokenizer for the string <code>str</code>, |
* Creates a new StringTokenizer for the string <code>str</code>, |
85 |
* that should split on the default delimiter set (space, tap, |
* that should split on the default delimiter set (space, tab, |
86 |
* newline, return and formfeed), and which doesn't return the |
* newline, return and formfeed), and which doesn't return the |
87 |
* delimiters. |
* delimiters. |
88 |
* @param str The string to split. |
* |
89 |
|
* @param str The string to split |
90 |
|
* @throws NullPointerException if str is null |
91 |
*/ |
*/ |
92 |
public StringTokenizer(String str) |
public StringTokenizer(String str) |
|
/*{ require { str != null :: "str must not be null"; } } */ |
|
93 |
{ |
{ |
94 |
this(str, " \t\n\r\f", false); |
this(str, " \t\n\r\f", false); |
95 |
} |
} |
96 |
|
|
97 |
/** |
/** |
98 |
* Create a new StringTokenizer, that splits the given string on |
* Create a new StringTokenizer, that splits the given string on |
99 |
* the given delimiter characters. It doesn't return the delimiter |
* the given delimiter characters. It doesn't return the delimiter |
100 |
* characters. |
* characters. |
101 |
* |
* |
102 |
* @param str The string to split. |
* @param str the string to split |
103 |
* @param delim A string containing all delimiter characters. |
* @param delim a string containing all delimiter characters |
104 |
|
* @throws NullPointerException if either argument is null |
105 |
*/ |
*/ |
106 |
public StringTokenizer(String str, String delim) |
public StringTokenizer(String str, String delim) |
|
/*{ require { str != null :: "str must not be null"; |
|
|
delim != null :: "delim must not be null"; } } */ |
|
107 |
{ |
{ |
108 |
this(str, delim, false); |
this(str, delim, false); |
109 |
} |
} |
115 |
* characters are returned as tokens of their own. The delimiter |
* characters are returned as tokens of their own. The delimiter |
116 |
* tokens always consist of a single character. |
* tokens always consist of a single character. |
117 |
* |
* |
118 |
* @param str The string to split. |
* @param str the string to split |
119 |
* @param delim A string containing all delimiter characters. |
* @param delim a string containing all delimiter characters |
120 |
* @param returnDelims Tells, if you want to get the delimiters. |
* @param returnDelims tells, if you want to get the delimiters |
121 |
|
* @throws NullPointerException if str or delim is null |
122 |
*/ |
*/ |
123 |
public StringTokenizer(String str, String delim, boolean returnDelims) |
public StringTokenizer(String str, String delim, boolean returnDelims) |
|
/*{ require { str != null :: "str must not be null"; |
|
|
delim != null :: "delim must not be null"; } } */ |
|
124 |
{ |
{ |
125 |
this.str = str; |
this.str = str; |
126 |
this.delim = delim; |
this.delim = delim; |
130 |
|
|
131 |
/** |
/** |
132 |
* Tells if there are more tokens. |
* Tells if there are more tokens. |
133 |
* @return True, if the next call of nextToken() succeeds, false otherwise. |
* |
134 |
|
* @return true if the next call of nextToken() will succeed |
135 |
*/ |
*/ |
136 |
public boolean hasMoreTokens() |
public boolean hasMoreTokens() |
137 |
{ |
{ |
138 |
if (!retDelims) |
if (! retDelims) |
139 |
{ |
{ |
140 |
while (pos < str.length() && delim.indexOf(str.charAt(pos)) > -1) |
while (pos < str.length() && delim.indexOf(str.charAt(pos)) > -1) |
141 |
{ |
pos++; |
|
pos++; |
|
|
} |
|
142 |
} |
} |
143 |
return pos < str.length(); |
return pos < str.length(); |
144 |
} |
} |
148 |
* <code>delim</code>. The change of the delimiter set is |
* <code>delim</code>. The change of the delimiter set is |
149 |
* permanent, ie. the next call of nextToken(), uses the same |
* permanent, ie. the next call of nextToken(), uses the same |
150 |
* delimiter set. |
* delimiter set. |
151 |
* @param delim a string containing the new delimiter characters. |
* |
152 |
* @return the next token with respect to the new delimiter characters. |
* @param delim a string containing the new delimiter characters |
153 |
* @exception NoSuchElementException if there are no more tokens. |
* @return the next token with respect to the new delimiter characters |
154 |
|
* @throws NoSuchElementException if there are no more tokens |
155 |
*/ |
*/ |
156 |
public String nextToken(String delim) throws NoSuchElementException |
public String nextToken(String delim) throws NoSuchElementException |
|
/*{ require { hasMoreTokens() :: "no more Tokens available"; |
|
|
ensure { $return != null && $return.length() > 0; } } */ |
|
157 |
{ |
{ |
158 |
this.delim = delim; |
this.delim = delim; |
159 |
return nextToken(); |
return nextToken(); |
161 |
|
|
162 |
/** |
/** |
163 |
* Returns the nextToken of the string. |
* Returns the nextToken of the string. |
164 |
* @param delim a string containing the new delimiter characters. |
* |
165 |
* @return the next token with respect to the new delimiter characters. |
* @return the next token with respect to the current delimiter characters |
166 |
* @exception NoSuchElementException if there are no more tokens. |
* @throws NoSuchElementException if there are no more tokens |
167 |
*/ |
*/ |
168 |
public String nextToken() throws NoSuchElementException |
public String nextToken() throws NoSuchElementException |
|
/*{ require { hasMoreTokens() :: "no more Tokens available"; |
|
|
ensure { $return != null && $return.length() > 0; } } */ |
|
169 |
{ |
{ |
170 |
if (pos < str.length() && delim.indexOf(str.charAt(pos)) > -1) |
if (pos < str.length() && delim.indexOf(str.charAt(pos)) > -1) |
171 |
{ |
{ |
172 |
if (retDelims) |
if (retDelims) |
173 |
return str.substring(pos, ++pos); |
return str.substring(pos, ++pos); |
174 |
|
|
175 |
while (++pos < str.length() && delim.indexOf(str.charAt(pos)) > -1) |
while (++pos < str.length() && delim.indexOf(str.charAt(pos)) > -1); |
|
{ |
|
|
/* empty */ |
|
|
} |
|
176 |
} |
} |
177 |
if (pos < str.length()) |
if (pos < str.length()) |
178 |
{ |
{ |
179 |
int start = pos; |
int start = pos; |
180 |
while (++pos < str.length() && delim.indexOf(str.charAt(pos)) == -1) |
while (++pos < str.length() && delim.indexOf(str.charAt(pos)) == -1); |
181 |
{ |
|
182 |
/* empty */ |
return str.substring(start, pos); |
|
} |
|
|
return str.substring(start, pos); |
|
183 |
} |
} |
184 |
throw new NoSuchElementException(); |
throw new NoSuchElementException(); |
185 |
} |
} |
187 |
/** |
/** |
188 |
* This does the same as hasMoreTokens. This is the |
* This does the same as hasMoreTokens. This is the |
189 |
* <code>Enumeration</code interface method. |
* <code>Enumeration</code interface method. |
190 |
* @return True, if the next call of nextElement() succeeds, false |
* |
191 |
* otherwise. |
* @return true, if the next call of nextElement() will succeed |
192 |
* @see #hasMoreTokens |
* @see #hasMoreTokens() |
193 |
*/ |
*/ |
194 |
public boolean hasMoreElements() |
public boolean hasMoreElements() |
195 |
{ |
{ |
199 |
/** |
/** |
200 |
* This does the same as nextTokens. This is the |
* This does the same as nextTokens. This is the |
201 |
* <code>Enumeration</code interface method. |
* <code>Enumeration</code interface method. |
202 |
* @return the next token with respect to the new delimiter characters. |
* |
203 |
* @exception NoSuchElementException if there are no more tokens. |
* @return the next token with respect to the current delimiter characters |
204 |
* @see #nextToken |
* @throws NoSuchElementException if there are no more tokens |
205 |
|
* @see #nextToken() |
206 |
*/ |
*/ |
207 |
public Object nextElement() throws NoSuchElementException |
public Object nextElement() throws NoSuchElementException |
208 |
{ |
{ |
212 |
/** |
/** |
213 |
* This counts the number of remaining tokens in the string, with |
* This counts the number of remaining tokens in the string, with |
214 |
* respect to the current delimiter set. |
* respect to the current delimiter set. |
215 |
* @return the number of times <code>nextTokens()</code> will |
* |
216 |
* succeed. |
* @return the number of times <code>nextTokens()</code> will succeed |
217 |
* @see #nextToken |
* @see #nextToken() |
218 |
*/ |
*/ |
219 |
public int countTokens() |
public int countTokens() |
220 |
{ |
{ |
221 |
int count = 0; |
int count = 0; |
222 |
int delimiterCount = 0; |
int delimiterCount = 0; |
223 |
boolean tokenFound = false; // Set when a non-delimiter is found |
boolean tokenFound = false; // Set when a non-delimiter is found |
224 |
int tmpPos = pos; |
int tmpPos = pos; |
225 |
|
|
226 |
// Note for efficiency, we count up the delimiters rather than check |
// Note for efficiency, we count up the delimiters rather than check |
228 |
// just do the conditional once at the end of the method |
// just do the conditional once at the end of the method |
229 |
while (tmpPos < str.length()) |
while (tmpPos < str.length()) |
230 |
{ |
{ |
231 |
if (delim.indexOf(str.charAt(tmpPos++)) > -1) |
if (delim.indexOf(str.charAt(tmpPos++)) > -1) |
232 |
{ |
{ |
233 |
if (tokenFound) |
if (tokenFound) |
234 |
{ |
{ |
235 |
// Got to the end of a token |
// Got to the end of a token |
236 |
count++; |
count++; |
237 |
tokenFound = false; |
tokenFound = false; |
238 |
} |
} |
239 |
|
|
240 |
delimiterCount++; // Increment for this delimiter |
delimiterCount++; // Increment for this delimiter |
241 |
} |
} |
242 |
else |
else |
243 |
{ |
{ |
244 |
tokenFound = true; |
tokenFound = true; |
245 |
|
|
246 |
// Get to the end of the token |
// Get to the end of the token |
247 |
while (tmpPos < str.length() |
while (tmpPos < str.length() |
248 |
&& delim.indexOf(str.charAt(tmpPos)) == -1) |
&& delim.indexOf(str.charAt(tmpPos)) == -1) |
249 |
++tmpPos; |
++tmpPos; |
250 |
} |
} |
251 |
} |
} |
252 |
|
|
253 |
// Make sure to count the last token |
// Make sure to count the last token |
254 |
if (tokenFound) |
if (tokenFound) |
255 |
count++; |
count++; |
256 |
|
|