1 /* java.util.StringTokenizer
2 Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 As a special exception, if you link this library with other files to
22 produce an executable, this library does not by itself cause the
23 resulting executable to be covered by the GNU General Public License.
24 This exception does not however invalidate any other reasons why the
25 executable file might be covered by the GNU General Public License. */
31 * This class splits a string into tokens. The caller can set on which
32 * delimiters the string should be split and if the delimiters should be
35 * You may change the delimiter set on the fly by calling
36 * nextToken(String). But the semantic is quite difficult; it even
37 * depends on calling <code>hasMoreTokens()</code>. You should call
38 * <code>hasMoreTokens()</code> before, otherwise the old delimiters
39 * after the last token are returned.
41 * If you want to get the delimiters, you have to use the three argument
42 * constructor. The delimiters are returned as token consisting of a
45 * @author Jochen Hoenicke
46 * @author Warren Levy <warrenl@cygnus.com>
48 public class StringTokenizer
implements Enumeration
51 * The position in the str, where we currently are.
55 * The string that should be split into tokens.
59 * The string containing the delimiter characters.
63 * Tells, if we should return the delimiters.
65 private boolean retDelims
;
69 pos >= 0 :: "position is negative";
70 pos <= str.length() :: "position is out of string";
71 str != null :: "String is null";
72 delim != null :: "Delimiters are null";
77 * Creates a new StringTokenizer for the string <code>str</code>,
78 * that should split on the default delimiter set (space, tap,
79 * newline, return and formfeed), and which doesn't return the
81 * @param str The string to split.
83 public StringTokenizer(String str
)
84 /*{ require { str != null :: "str must not be null"; } } */
86 this(str
, " \t\n\r\f", false);
90 * Create a new StringTokenizer, that splits the given string on
91 * the given delimiter characters. It doesn't return the delimiter
94 * @param str The string to split.
95 * @param delim A string containing all delimiter characters.
97 public StringTokenizer(String str
, String delim
)
98 /*{ require { str != null :: "str must not be null";
99 delim != null :: "delim must not be null"; } } */
101 this(str
, delim
, false);
105 * Create a new StringTokenizer, that splits the given string on
106 * the given delimiter characters. If you set
107 * <code>returnDelims</code> to <code>true</code>, the delimiter
108 * characters are returned as tokens of their own. The delimiter
109 * tokens always consist of a single character.
111 * @param str The string to split.
112 * @param delim A string containing all delimiter characters.
113 * @param returnDelims Tells, if you want to get the delimiters.
115 public StringTokenizer(String str
, String delim
, boolean returnDelims
)
116 /*{ require { str != null :: "str must not be null";
117 delim != null :: "delim must not be null"; } } */
121 this.retDelims
= returnDelims
;
126 * Tells if there are more tokens.
127 * @return True, if the next call of nextToken() succeeds, false otherwise.
129 public boolean hasMoreTokens()
133 while (pos
< str
.length() && delim
.indexOf(str
.charAt(pos
)) > -1)
138 return pos
< str
.length();
142 * Returns the nextToken, changing the delimiter set to the given
143 * <code>delim</code>. The change of the delimiter set is
144 * permanent, ie. the next call of nextToken(), uses the same
146 * @param delim a string containing the new delimiter characters.
147 * @return the next token with respect to the new delimiter characters.
148 * @exception NoSuchElementException if there are no more tokens.
150 public String
nextToken(String delim
) throws NoSuchElementException
151 /*{ require { hasMoreTokens() :: "no more Tokens available";
152 ensure { $return != null && $return.length() > 0; } } */
159 * Returns the nextToken of the string.
160 * @param delim a string containing the new delimiter characters.
161 * @return the next token with respect to the new delimiter characters.
162 * @exception NoSuchElementException if there are no more tokens.
164 public String
nextToken() throws NoSuchElementException
165 /*{ require { hasMoreTokens() :: "no more Tokens available";
166 ensure { $return != null && $return.length() > 0; } } */
168 if (pos
< str
.length() && delim
.indexOf(str
.charAt(pos
)) > -1)
171 return str
.substring(pos
, ++pos
);
173 while (++pos
< str
.length() && delim
.indexOf(str
.charAt(pos
)) > -1)
178 if (pos
< str
.length())
181 while (++pos
< str
.length() && delim
.indexOf(str
.charAt(pos
)) == -1)
185 return str
.substring(start
, pos
);
187 throw new NoSuchElementException();
191 * This does the same as hasMoreTokens. This is the
192 * <code>Enumeration</code interface method.
193 * @return True, if the next call of nextElement() succeeds, false
195 * @see #hasMoreTokens
197 public boolean hasMoreElements()
199 return hasMoreTokens();
203 * This does the same as nextTokens. This is the
204 * <code>Enumeration</code interface method.
205 * @return the next token with respect to the new delimiter characters.
206 * @exception NoSuchElementException if there are no more tokens.
209 public Object
nextElement() throws NoSuchElementException
215 * This counts the number of remaining tokens in the string, with
216 * respect to the current delimiter set.
217 * @return the number of times <code>nextTokens()</code> will
221 public int countTokens()
224 int delimiterCount
= 0;
225 boolean tokenFound
= false; // Set when a non-delimiter is found
228 // Note for efficiency, we count up the delimiters rather than check
229 // retDelims every time we encounter one. That way, we can
230 // just do the conditional once at the end of the method
231 while (tmpPos
< str
.length())
233 if (delim
.indexOf(str
.charAt(tmpPos
++)) > -1)
237 // Got to the end of a token
242 delimiterCount
++; // Increment for this delimiter
248 // Get to the end of the token
249 while (tmpPos
< str
.length()
250 && delim
.indexOf(str
.charAt(tmpPos
)) == -1)
255 // Make sure to count the last token
259 // if counting delmiters add them into the token count
260 return retDelims ? count
+ delimiterCount
: count
;