- Timestamp:
- Apr 27, 2004, 8:39:34 PM (22 years ago)
- Location:
- branches/GNU/src/gcc
- Files:
-
- 2 edited
-
. (modified) (1 prop)
-
libjava/java/util/StringTokenizer.java (modified) (13 diffs, 1 prop)
Legend:
- Unmodified
- Added
- Removed
-
branches/GNU/src/gcc
- Property svn:ignore
-
old new 26 26 configure.vr 27 27 configure.vrs 28 28 29 Makefile 29 dir.info30 30 lost+found 31 31 update.out
-
- Property svn:ignore
-
branches/GNU/src/gcc/libjava/java/util/StringTokenizer.java
-
Property cvs2svn:cvs-rev
changed from
1.1to1.1.1.2
r1390 r1391 1 /* java.util.StringTokenizer2 Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc.1 /* 2 Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc. 3 3 4 4 This file is part of GNU Classpath. … … 40 40 41 41 /** 42 * This class splits a string into tokens. The caller can set on which 42 * This class splits a string into tokens. The caller can set on which 43 43 * delimiters the string should be split and if the delimiters should be 44 * returned. 44 * returned. 45 45 * 46 * You may change the delimiter set on the fly by calling46 * You may change the delimiter set on the fly by calling 47 47 * nextToken(String). But the semantic is quite difficult; it even 48 48 * depends on calling <code>hasMoreTokens()</code>. You should call 49 49 * <code>hasMoreTokens()</code> before, otherwise the old delimiters 50 * after the last token are returned.50 * after the last token are returned. 51 51 * 52 * If you want to get the delimiters, you have to use the three argument52 * If you want to get the delimiters, you have to use the three argument 53 53 * constructor. The delimiters are returned as token consisting of a 54 * single character. 54 * single character. 55 55 * 56 56 * @author Jochen Hoenicke 57 57 * @author Warren Levy <[email protected]> 58 59 58 60 */ 59 61 public class StringTokenizer implements Enumeration 60 62 { 63 64 65 61 66 /** 62 67 * The position in the str, where we currently are. 63 68 */ 64 69 private int pos; 70 65 71 /** 66 72 * The string that should be split into tokens. 67 73 */ 68 private String str; 74 private final String str; 75 76 /** 77 * The length of the string. 78 */ 79 private final int len; 80 69 81 /** 70 82 * The string containing the delimiter characters. 71 83 */ 72 84 private String delim; 85 73 86 /** 74 87 * Tells, if we should return the delimiters. 75 88 */ 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 } */ 89 private final boolean retDelims; 86 90 87 91 /** 88 92 * Creates a new StringTokenizer for the string <code>str</code>, 89 * that should split on the default delimiter set (space, ta p,93 * that should split on the default delimiter set (space, ta, 90 94 * newline, return and formfeed), and which doesn't return the 91 95 * delimiters. 92 * @param str The string to split. 96 * 97 * @param str The string to split 98 * @throws NullPointerException if str is null 93 99 */ 94 100 public StringTokenizer(String str) 95 /*{ require { str != null :: "str must not be null"; } } */96 101 { 97 102 this(str, " \t\n\r\f", false); … … 99 104 100 105 /** 101 * Create a new StringTokenizer, that splits the given string on 106 * Create a new StringTokenizer, that splits the given string on 102 107 * the given delimiter characters. It doesn't return the delimiter 103 108 * characters. 104 109 * 105 * @param str The string to split. 106 * @param delim A string containing all delimiter 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 107 113 */ 108 114 public StringTokenizer(String str, String delim) 109 /*{ require { str != null :: "str must not be null";110 delim != null :: "delim must not be null"; } } */111 115 { 112 116 this(str, delim, false); … … 120 124 * tokens always consist of a single character. 121 125 * 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. 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 125 130 */ 126 131 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 { 132 { 133 len = str.length(); 130 134 this.str = str; 131 this.delim = delim; 135 // The toString() hack causes the NullPointerException. 136 this.delim = delim.toString(); 132 137 this.retDelims = returnDelims; 133 138 this.pos = 0; … … 136 141 /** 137 142 * Tells if there are more tokens. 138 * @return True, if the next call of nextToken() succeeds, false otherwise. 143 * 144 * @return true if the next call of nextToken() will succeed 139 145 */ 140 146 public boolean hasMoreTokens() 141 147 { 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(); 148 if (! retDelims) 149 { 150 while (pos < len && delim.indexOf(str.charAt(pos)) >= 0) 151 pos++; 152 } 153 return pos < len; 150 154 } 151 155 … … 155 159 * permanent, ie. the next call of nextToken(), uses the same 156 160 * 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. 161 * 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 160 166 */ 161 167 public String nextToken(String delim) throws NoSuchElementException 162 /*{ require { hasMoreTokens() :: "no more Tokens available";163 ensure { $return != null && $return.length() > 0; } } */164 168 { 165 169 this.delim = delim; … … 169 173 /** 170 174 * 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.175 * 176 * @return the next token with respect to the 177 * @ 174 178 */ 175 179 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); 180 { 181 if (pos < len && delim.indexOf(str.charAt(pos)) >= 0) 182 { 183 if (retDelims) 184 return str.substring(pos, ++pos); 185 while (++pos < len && delim.indexOf(str.charAt(pos)) >= 0); 186 } 187 if (pos < len) 188 { 189 int start = pos; 190 while (++pos < len && delim.indexOf(str.charAt(pos)) < 0); 191 192 return str.substring(start, pos); 197 193 } 198 194 throw new NoSuchElementException(); … … 202 198 * This does the same as hasMoreTokens. This is the 203 199 * <code>Enumeration</code interface method. 204 * @return True, if the next call of nextElement() succeeds, false205 * otherwise.206 * @see #hasMoreTokens 200 * 201 * 202 * @see #hasMoreTokens 207 203 */ 208 204 public boolean hasMoreElements() … … 214 210 * This does the same as nextTokens. This is the 215 211 * <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 212 * 213 * @return the next token with respect to the current delimiter characters 214 * @throws NoSuchElementException if there are no more tokens 215 * @see #nextToken() 219 216 */ 220 217 public Object nextElement() throws NoSuchElementException … … 226 223 * This counts the number of remaining tokens in the string, with 227 224 * respect to the current delimiter set. 228 * @return the number of times <code>nextTokens()</code> will229 * succeed.230 * @see #nextToken 225 * 226 * 227 * @see #nextToken 231 228 */ 232 229 public int countTokens() … … 234 231 int count = 0; 235 232 int delimiterCount = 0; 236 boolean tokenFound = false; // Set when a non-delimiter is found233 boolean tokenFound = false;// Set when a non-delimiter is found 237 234 int tmpPos = pos; 238 235 … … 240 237 // retDelims every time we encounter one. That way, we can 241 238 // 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 239 while (tmpPos < len) 240 { 241 if (delim.indexOf(str.charAt(tmpPos++)) >= 0) 242 { 243 if (tokenFound) 244 { 245 // Got to the end of a token 246 count++; 247 tokenFound = false; 248 } 249 delimiterCount++; // Increment for this delimiter 250 } 251 else 252 { 253 tokenFound = true; 254 // Get to the end of the token 255 while (tmpPos < len 256 && delim.indexOf(str.charAt(tmpPos)) < 0) 257 ++tmpPos; 258 } 259 } 260 261 // Make sure to count the last token 267 262 if (tokenFound) 268 263 count++; … … 271 266 return retDelims ? count + delimiterCount : count; 272 267 } 273 } 268 } -
Property cvs2svn:cvs-rev
changed from
Note:
See TracChangeset
for help on using the changeset viewer.
