68 |
/** |
/** |
69 |
* The string that should be split into tokens. |
* The string that should be split into tokens. |
70 |
*/ |
*/ |
71 |
private String str; |
private final String str; |
72 |
|
|
73 |
|
/** |
74 |
|
* The length of the string. |
75 |
|
*/ |
76 |
|
private final int len; |
77 |
|
|
78 |
/** |
/** |
79 |
* The string containing the delimiter characters. |
* The string containing the delimiter characters. |
83 |
/** |
/** |
84 |
* Tells, if we should return the delimiters. |
* Tells, if we should return the delimiters. |
85 |
*/ |
*/ |
86 |
private boolean retDelims; |
private final boolean retDelims; |
87 |
|
|
88 |
/** |
/** |
89 |
* Creates a new StringTokenizer for the string <code>str</code>, |
* Creates a new StringTokenizer for the string <code>str</code>, |
127 |
*/ |
*/ |
128 |
public StringTokenizer(String str, String delim, boolean returnDelims) |
public StringTokenizer(String str, String delim, boolean returnDelims) |
129 |
{ |
{ |
130 |
|
len = str.length(); |
131 |
this.str = str; |
this.str = str; |
132 |
this.delim = delim; |
// The toString() hack causes the NullPointerException. |
133 |
|
this.delim = delim.toString(); |
134 |
this.retDelims = returnDelims; |
this.retDelims = returnDelims; |
135 |
this.pos = 0; |
this.pos = 0; |
136 |
} |
} |
144 |
{ |
{ |
145 |
if (! retDelims) |
if (! retDelims) |
146 |
{ |
{ |
147 |
while (pos < str.length() && delim.indexOf(str.charAt(pos)) > -1) |
while (pos < len && delim.indexOf(str.charAt(pos)) >= 0) |
148 |
pos++; |
pos++; |
149 |
} |
} |
150 |
return pos < str.length(); |
return pos < len; |
151 |
} |
} |
152 |
|
|
153 |
/** |
/** |
159 |
* @param delim a string containing the new delimiter characters |
* @param delim a string containing the new delimiter characters |
160 |
* @return the next token with respect to the new delimiter characters |
* @return the next token with respect to the new delimiter characters |
161 |
* @throws NoSuchElementException if there are no more tokens |
* @throws NoSuchElementException if there are no more tokens |
162 |
|
* @throws NullPointerException if delim is null |
163 |
*/ |
*/ |
164 |
public String nextToken(String delim) throws NoSuchElementException |
public String nextToken(String delim) throws NoSuchElementException |
165 |
{ |
{ |
175 |
*/ |
*/ |
176 |
public String nextToken() throws NoSuchElementException |
public String nextToken() throws NoSuchElementException |
177 |
{ |
{ |
178 |
if (pos < str.length() && delim.indexOf(str.charAt(pos)) > -1) |
if (pos < len && delim.indexOf(str.charAt(pos)) >= 0) |
179 |
{ |
{ |
180 |
if (retDelims) |
if (retDelims) |
181 |
return str.substring(pos, ++pos); |
return str.substring(pos, ++pos); |
182 |
|
while (++pos < len && delim.indexOf(str.charAt(pos)) >= 0); |
|
while (++pos < str.length() && delim.indexOf(str.charAt(pos)) > -1); |
|
183 |
} |
} |
184 |
if (pos < str.length()) |
if (pos < len) |
185 |
{ |
{ |
186 |
int start = pos; |
int start = pos; |
187 |
while (++pos < str.length() && delim.indexOf(str.charAt(pos)) == -1); |
while (++pos < len && delim.indexOf(str.charAt(pos)) < 0); |
188 |
|
|
189 |
return str.substring(start, pos); |
return str.substring(start, pos); |
190 |
} |
} |
233 |
// Note for efficiency, we count up the delimiters rather than check |
// Note for efficiency, we count up the delimiters rather than check |
234 |
// retDelims every time we encounter one. That way, we can |
// retDelims every time we encounter one. That way, we can |
235 |
// just do the conditional once at the end of the method |
// just do the conditional once at the end of the method |
236 |
while (tmpPos < str.length()) |
while (tmpPos < len) |
237 |
{ |
{ |
238 |
if (delim.indexOf(str.charAt(tmpPos++)) > -1) |
if (delim.indexOf(str.charAt(tmpPos++)) >= 0) |
239 |
{ |
{ |
240 |
if (tokenFound) |
if (tokenFound) |
241 |
{ |
{ |
243 |
count++; |
count++; |
244 |
tokenFound = false; |
tokenFound = false; |
245 |
} |
} |
|
|
|
246 |
delimiterCount++; // Increment for this delimiter |
delimiterCount++; // Increment for this delimiter |
247 |
} |
} |
248 |
else |
else |
249 |
{ |
{ |
250 |
tokenFound = true; |
tokenFound = true; |
|
|
|
251 |
// Get to the end of the token |
// Get to the end of the token |
252 |
while (tmpPos < str.length() |
while (tmpPos < len |
253 |
&& delim.indexOf(str.charAt(tmpPos)) == -1) |
&& delim.indexOf(str.charAt(tmpPos)) < 0) |
254 |
++tmpPos; |
++tmpPos; |
255 |
} |
} |
256 |
} |
} |