| 1 | /* java.util.StringTokenizer
|
|---|
| 2 | Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 4 | This file is part of GNU Classpath.
|
|---|
| 5 |
|
|---|
| 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.
|
|---|
| 10 |
|
|---|
| 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.
|
|---|
| 15 |
|
|---|
| 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.
|
|---|
| 20 |
|
|---|
| 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.
|
|---|
| 25 |
|
|---|
| 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. */
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | package java.util;
|
|---|
| 40 |
|
|---|
| 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.
|
|---|
| 45 | *
|
|---|
| 46 | * 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 returned.
|
|---|
| 51 | *
|
|---|
| 52 | * 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.
|
|---|
| 55 | *
|
|---|
| 56 | * @author Jochen Hoenicke
|
|---|
| 57 | * @author Warren Levy <[email protected]>
|
|---|
| 58 | */
|
|---|
| 59 | public class StringTokenizer implements Enumeration
|
|---|
| 60 | {
|
|---|
| 61 | /**
|
|---|
| 62 | * The position in the str, where we currently are.
|
|---|
| 63 | */
|
|---|
| 64 | private int pos;
|
|---|
| 65 | /**
|
|---|
| 66 | * The string that should be split into tokens.
|
|---|
| 67 | */
|
|---|
| 68 | private String str;
|
|---|
| 69 | /**
|
|---|
| 70 | * The string containing the delimiter characters.
|
|---|
| 71 | */
|
|---|
| 72 | private String delim;
|
|---|
| 73 | /**
|
|---|
| 74 | * Tells, if we should return the delimiters.
|
|---|
| 75 | */
|
|---|
| 76 | private boolean retDelims;
|
|---|
| 77 |
|
|---|
| 78 | /*{
|
|---|
| 79 | invariant {
|
|---|
| 80 | pos >= 0 :: "position is negative";
|
|---|
| 81 | pos <= str.length() :: "position is out of string";
|
|---|
| 82 | str != null :: "String is null";
|
|---|
| 83 | delim != null :: "Delimiters are null";
|
|---|
| 84 | }
|
|---|
| 85 | } */
|
|---|
| 86 |
|
|---|
| 87 | /**
|
|---|
| 88 | * Creates a new StringTokenizer for the string <code>str</code>,
|
|---|
| 89 | * that should split on the default delimiter set (space, tap,
|
|---|
| 90 | * newline, return and formfeed), and which doesn't return the
|
|---|
| 91 | * delimiters.
|
|---|
| 92 | * @param str The string to split.
|
|---|
| 93 | */
|
|---|
| 94 | public StringTokenizer(String str)
|
|---|
| 95 | /*{ require { str != null :: "str must not be null"; } } */
|
|---|
| 96 | {
|
|---|
| 97 | this(str, " \t\n\r\f", false);
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | /**
|
|---|
| 101 | * Create a new StringTokenizer, that splits the given string on
|
|---|
| 102 | * the given delimiter characters. It doesn't return the delimiter
|
|---|
| 103 | * characters.
|
|---|
| 104 | *
|
|---|
| 105 | * @param str The string to split.
|
|---|
| 106 | * @param delim A string containing all delimiter characters.
|
|---|
| 107 | */
|
|---|
| 108 | public StringTokenizer(String str, String delim)
|
|---|
| 109 | /*{ require { str != null :: "str must not be null";
|
|---|
| 110 | delim != null :: "delim must not be null"; } } */
|
|---|
| 111 | {
|
|---|
| 112 | this(str, delim, false);
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | /**
|
|---|
| 116 | * Create a new StringTokenizer, that splits the given string on
|
|---|
| 117 | * the given delimiter characters. If you set
|
|---|
| 118 | * <code>returnDelims</code> to <code>true</code>, the delimiter
|
|---|
| 119 | * characters are returned as tokens of their own. The delimiter
|
|---|
| 120 | * tokens always consist of a single character.
|
|---|
| 121 | *
|
|---|
| 122 | * @param str The string to split.
|
|---|
| 123 | * @param delim A string containing all delimiter characters.
|
|---|
| 124 | * @param returnDelims Tells, if you want to get the delimiters.
|
|---|
| 125 | */
|
|---|
| 126 | public StringTokenizer(String str, String delim, boolean returnDelims)
|
|---|
| 127 | /*{ require { str != null :: "str must not be null";
|
|---|
| 128 | delim != null :: "delim must not be null"; } } */
|
|---|
| 129 | {
|
|---|
| 130 | this.str = str;
|
|---|
| 131 | this.delim = delim;
|
|---|
| 132 | this.retDelims = returnDelims;
|
|---|
| 133 | this.pos = 0;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | /**
|
|---|
| 137 | * Tells if there are more tokens.
|
|---|
| 138 | * @return True, if the next call of nextToken() succeeds, false otherwise.
|
|---|
| 139 | */
|
|---|
| 140 | public boolean hasMoreTokens()
|
|---|
| 141 | {
|
|---|
| 142 | if (!retDelims)
|
|---|
| 143 | {
|
|---|
| 144 | while (pos < str.length() && delim.indexOf(str.charAt(pos)) > -1)
|
|---|
| 145 | {
|
|---|
| 146 | pos++;
|
|---|
| 147 | }
|
|---|
| 148 | }
|
|---|
| 149 | return pos < str.length();
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | /**
|
|---|
| 153 | * Returns the nextToken, changing the delimiter set to the given
|
|---|
| 154 | * <code>delim</code>. The change of the delimiter set is
|
|---|
| 155 | * permanent, ie. the next call of nextToken(), uses the same
|
|---|
| 156 | * delimiter set.
|
|---|
| 157 | * @param delim a string containing the new delimiter characters.
|
|---|
| 158 | * @return the next token with respect to the new delimiter characters.
|
|---|
| 159 | * @exception NoSuchElementException if there are no more tokens.
|
|---|
| 160 | */
|
|---|
| 161 | public String nextToken(String delim) throws NoSuchElementException
|
|---|
| 162 | /*{ require { hasMoreTokens() :: "no more Tokens available";
|
|---|
| 163 | ensure { $return != null && $return.length() > 0; } } */
|
|---|
| 164 | {
|
|---|
| 165 | this.delim = delim;
|
|---|
| 166 | return nextToken();
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | /**
|
|---|
| 170 | * Returns the nextToken of the string.
|
|---|
| 171 | * @param delim a string containing the new delimiter characters.
|
|---|
| 172 | * @return the next token with respect to the new delimiter characters.
|
|---|
| 173 | * @exception NoSuchElementException if there are no more tokens.
|
|---|
| 174 | */
|
|---|
| 175 | public String nextToken() throws NoSuchElementException
|
|---|
| 176 | /*{ require { hasMoreTokens() :: "no more Tokens available";
|
|---|
| 177 | ensure { $return != null && $return.length() > 0; } } */
|
|---|
| 178 | {
|
|---|
| 179 | if (pos < str.length() && delim.indexOf(str.charAt(pos)) > -1)
|
|---|
| 180 | {
|
|---|
| 181 | if (retDelims)
|
|---|
| 182 | return str.substring(pos, ++pos);
|
|---|
| 183 |
|
|---|
| 184 | while (++pos < str.length() && delim.indexOf(str.charAt(pos)) > -1)
|
|---|
| 185 | {
|
|---|
| 186 | /* empty */
|
|---|
| 187 | }
|
|---|
| 188 | }
|
|---|
| 189 | if (pos < str.length())
|
|---|
| 190 | {
|
|---|
| 191 | int start = pos;
|
|---|
| 192 | while (++pos < str.length() && delim.indexOf(str.charAt(pos)) == -1)
|
|---|
| 193 | {
|
|---|
| 194 | /* empty */
|
|---|
| 195 | }
|
|---|
| 196 | return str.substring(start, pos);
|
|---|
| 197 | }
|
|---|
| 198 | throw new NoSuchElementException();
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | /**
|
|---|
| 202 | * This does the same as hasMoreTokens. This is the
|
|---|
| 203 | * <code>Enumeration</code interface method.
|
|---|
| 204 | * @return True, if the next call of nextElement() succeeds, false
|
|---|
| 205 | * otherwise.
|
|---|
| 206 | * @see #hasMoreTokens
|
|---|
| 207 | */
|
|---|
| 208 | public boolean hasMoreElements()
|
|---|
| 209 | {
|
|---|
| 210 | return hasMoreTokens();
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | /**
|
|---|
| 214 | * This does the same as nextTokens. This is the
|
|---|
| 215 | * <code>Enumeration</code interface method.
|
|---|
| 216 | * @return the next token with respect to the new delimiter characters.
|
|---|
| 217 | * @exception NoSuchElementException if there are no more tokens.
|
|---|
| 218 | * @see #nextToken
|
|---|
| 219 | */
|
|---|
| 220 | public Object nextElement() throws NoSuchElementException
|
|---|
| 221 | {
|
|---|
| 222 | return nextToken();
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | /**
|
|---|
| 226 | * This counts the number of remaining tokens in the string, with
|
|---|
| 227 | * respect to the current delimiter set.
|
|---|
| 228 | * @return the number of times <code>nextTokens()</code> will
|
|---|
| 229 | * succeed.
|
|---|
| 230 | * @see #nextToken
|
|---|
| 231 | */
|
|---|
| 232 | public int countTokens()
|
|---|
| 233 | {
|
|---|
| 234 | int count = 0;
|
|---|
| 235 | int delimiterCount = 0;
|
|---|
| 236 | boolean tokenFound = false; // Set when a non-delimiter is found
|
|---|
| 237 | int tmpPos = pos;
|
|---|
| 238 |
|
|---|
| 239 | // Note for efficiency, we count up the delimiters rather than check
|
|---|
| 240 | // retDelims every time we encounter one. That way, we can
|
|---|
| 241 | // just do the conditional once at the end of the method
|
|---|
| 242 | while (tmpPos < str.length())
|
|---|
| 243 | {
|
|---|
| 244 | if (delim.indexOf(str.charAt(tmpPos++)) > -1)
|
|---|
| 245 | {
|
|---|
| 246 | if (tokenFound)
|
|---|
| 247 | {
|
|---|
| 248 | // Got to the end of a token
|
|---|
| 249 | count++;
|
|---|
| 250 | tokenFound = false;
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | delimiterCount++; // Increment for this delimiter
|
|---|
| 254 | }
|
|---|
| 255 | else
|
|---|
| 256 | {
|
|---|
| 257 | tokenFound = true;
|
|---|
| 258 |
|
|---|
| 259 | // Get to the end of the token
|
|---|
| 260 | while (tmpPos < str.length()
|
|---|
| 261 | && delim.indexOf(str.charAt(tmpPos)) == -1)
|
|---|
| 262 | ++tmpPos;
|
|---|
| 263 | }
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| 266 | // Make sure to count the last token
|
|---|
| 267 | if (tokenFound)
|
|---|
| 268 | count++;
|
|---|
| 269 |
|
|---|
| 270 | // if counting delmiters add them into the token count
|
|---|
| 271 | return retDelims ? count + delimiterCount : count;
|
|---|
| 272 | }
|
|---|
| 273 | }
|
|---|