Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / java / util / StringTokenizer.java
blob90f97dba5161247ac770f723d290e2c7d80d5381
1 /* StringTokenizer -- breaks a String into tokens
2 Copyright (C) 1998, 1999, 2001, 2002, 2005 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)
9 any later version.
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
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.util;
41 /**
42 * 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
44 * returned. This is much simpler than {@link java.io.StreamTokenizer}.
46 * <p>You may change the delimiter set on the fly by calling
47 * nextToken(String). But the semantic is quite difficult; it even
48 * depends on calling <code>hasMoreTokens()</code>. You should call
49 * <code>hasMoreTokens()</code> before, otherwise the old delimiters
50 * after the last token are candidates for being returned.
52 * <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
54 * single character.
56 * @author Jochen Hoenicke
57 * @author Warren Levy (warrenl@cygnus.com)
58 * @see java.io.StreamTokenizer
59 * @status updated to 1.4
61 public class StringTokenizer implements Enumeration
63 // WARNING: StringTokenizer is a CORE class in the bootstrap cycle. See the
64 // comments in vm/reference/java/lang/Runtime for implications of this fact.
66 /**
67 * The position in the str, where we currently are.
69 private int pos;
71 /**
72 * The string that should be split into tokens.
74 private final String str;
76 /**
77 * The length of the string.
79 private final int len;
81 /**
82 * The string containing the delimiter characters.
84 private String delim;
86 /**
87 * Tells, if we should return the delimiters.
89 private final boolean retDelims;
91 /**
92 * Creates a new StringTokenizer for the string <code>str</code>,
93 * that should split on the default delimiter set (space, tab,
94 * newline, return and formfeed), and which doesn't return the
95 * delimiters.
97 * @param str The string to split
98 * @throws NullPointerException if str is null
100 public StringTokenizer(String str)
102 this(str, " \t\n\r\f", false);
106 * Create a new StringTokenizer, that splits the given string on
107 * the given delimiter characters. It doesn't return the delimiter
108 * characters.
110 * @param str the string to split
111 * @param delim a string containing all delimiter characters
112 * @throws NullPointerException if either argument is null
114 public StringTokenizer(String str, String delim)
116 this(str, delim, false);
120 * Create a new StringTokenizer, that splits the given string on
121 * the given delimiter characters. If you set
122 * <code>returnDelims</code> to <code>true</code>, the delimiter
123 * characters are returned as tokens of their own. The delimiter
124 * tokens always consist of a single character.
126 * @param str the string to split
127 * @param delim a string containing all delimiter characters
128 * @param returnDelims tells, if you want to get the delimiters
129 * @throws NullPointerException if str or delim is null
131 public StringTokenizer(String str, String delim, boolean returnDelims)
133 len = str.length();
134 this.str = str;
135 // The toString() hack causes the NullPointerException.
136 this.delim = delim.toString();
137 this.retDelims = returnDelims;
138 this.pos = 0;
142 * Tells if there are more tokens.
144 * @return true if the next call of nextToken() will succeed
146 public boolean hasMoreTokens()
148 if (! retDelims)
150 while (pos < len && delim.indexOf(str.charAt(pos)) >= 0)
151 pos++;
153 return pos < len;
157 * Returns the nextToken, changing the delimiter set to the given
158 * <code>delim</code>. The change of the delimiter set is
159 * permanent, ie. the next call of nextToken(), uses the same
160 * delimiter set.
162 * @param delim a string containing the new delimiter characters
163 * @return the next token with respect to the new delimiter characters
164 * @throws NoSuchElementException if there are no more tokens
165 * @throws NullPointerException if delim is null
167 public String nextToken(String delim) throws NoSuchElementException
169 this.delim = delim;
170 return nextToken();
174 * Returns the nextToken of the string.
176 * @return the next token with respect to the current delimiter characters
177 * @throws NoSuchElementException if there are no more tokens
179 public String nextToken() throws NoSuchElementException
181 if (pos < len && delim.indexOf(str.charAt(pos)) >= 0)
183 if (retDelims)
184 return str.substring(pos, ++pos);
185 while (++pos < len && delim.indexOf(str.charAt(pos)) >= 0);
187 if (pos < len)
189 int start = pos;
190 while (++pos < len && delim.indexOf(str.charAt(pos)) < 0);
192 return str.substring(start, pos);
194 throw new NoSuchElementException();
198 * This does the same as hasMoreTokens. This is the
199 * <code>Enumeration</code> interface method.
201 * @return true, if the next call of nextElement() will succeed
202 * @see #hasMoreTokens()
204 public boolean hasMoreElements()
206 return hasMoreTokens();
210 * This does the same as nextTokens. This is the
211 * <code>Enumeration</code> interface method.
213 * @return the next token with respect to the current delimiter characters
214 * @throws NoSuchElementException if there are no more tokens
215 * @see #nextToken()
217 public Object nextElement() throws NoSuchElementException
219 return nextToken();
223 * This counts the number of remaining tokens in the string, with
224 * respect to the current delimiter set.
226 * @return the number of times <code>nextTokens()</code> will succeed
227 * @see #nextToken()
229 public int countTokens()
231 int count = 0;
232 int delimiterCount = 0;
233 boolean tokenFound = false; // Set when a non-delimiter is found
234 int tmpPos = pos;
236 // Note for efficiency, we count up the delimiters rather than check
237 // retDelims every time we encounter one. That way, we can
238 // just do the conditional once at the end of the method
239 while (tmpPos < len)
241 if (delim.indexOf(str.charAt(tmpPos++)) >= 0)
243 if (tokenFound)
245 // Got to the end of a token
246 count++;
247 tokenFound = false;
249 delimiterCount++; // Increment for this delimiter
251 else
253 tokenFound = true;
254 // Get to the end of the token
255 while (tmpPos < len
256 && delim.indexOf(str.charAt(tmpPos)) < 0)
257 ++tmpPos;
261 // Make sure to count the last token
262 if (tokenFound)
263 count++;
265 // if counting delmiters add them into the token count
266 return retDelims ? count + delimiterCount : count;
268 } // class StringTokenizer