1 7 8 package java.util; 9 10 import java.lang.*; 11 12 85 public 86 class StringTokenizer implements Enumeration <Object > { 87 private int currentPosition; 88 private int newPosition; 89 private int maxPosition; 90 private String str; 91 private String delimiters; 92 private boolean retDelims; 93 private boolean delimsChanged; 94 95 105 private int maxDelimCodePoint; 106 107 113 private boolean hasSurrogates = false; 114 115 120 private int[] delimiterCodePoints; 121 122 125 private void setMaxDelimCodePoint() { 126 if (delimiters == null) { 127 maxDelimCodePoint = 0; 128 return; 129 } 130 131 int m = 0; 132 int c; 133 int count = 0; 134 for (int i = 0; i < delimiters.length(); i += Character.charCount(c)) { 135 c = delimiters.charAt(i); 136 if (c >= Character.MIN_HIGH_SURROGATE && c <= Character.MAX_LOW_SURROGATE) { 137 c = delimiters.codePointAt(i); 138 hasSurrogates = true; 139 } 140 if (m < c) 141 m = c; 142 count++; 143 } 144 maxDelimCodePoint = m; 145 146 if (hasSurrogates) { 147 delimiterCodePoints = new int[count]; 148 for (int i = 0, j = 0; i < count; i++, j += Character.charCount(c)) { 149 c = delimiters.codePointAt(j); 150 delimiterCodePoints[i] = c; 151 } 152 } 153 } 154 155 177 public StringTokenizer(String str, String delim, boolean returnDelims) { 178 currentPosition = 0; 179 newPosition = -1; 180 delimsChanged = false; 181 this.str = str; 182 maxPosition = str.length(); 183 delimiters = delim; 184 retDelims = returnDelims; 185 setMaxDelimCodePoint(); 186 } 187 188 203 public StringTokenizer(String str, String delim) { 204 this(str, delim, false); 205 } 206 207 218 public StringTokenizer(String str) { 219 this(str, " \t\n\r\f", false); 220 } 221 222 227 private int skipDelimiters(int startPos) { 228 if (delimiters == null) 229 throw new NullPointerException (); 230 231 int position = startPos; 232 while (!retDelims && position < maxPosition) { 233 if (!hasSurrogates) { 234 char c = str.charAt(position); 235 if ((c > maxDelimCodePoint) || (delimiters.indexOf(c) < 0)) 236 break; 237 position++; 238 } else { 239 int c = str.codePointAt(position); 240 if ((c > maxDelimCodePoint) || !isDelimiter(c)) { 241 break; 242 } 243 position += Character.charCount(c); 244 } 245 } 246 return position; 247 } 248 249 253 private int scanToken(int startPos) { 254 int position = startPos; 255 while (position < maxPosition) { 256 if (!hasSurrogates) { 257 char c = str.charAt(position); 258 if ((c <= maxDelimCodePoint) && (delimiters.indexOf(c) >= 0)) 259 break; 260 position++; 261 } else { 262 int c = str.codePointAt(position); 263 if ((c <= maxDelimCodePoint) && isDelimiter(c)) 264 break; 265 position += Character.charCount(c); 266 } 267 } 268 if (retDelims && (startPos == position)) { 269 if (!hasSurrogates) { 270 char c = str.charAt(position); 271 if ((c <= maxDelimCodePoint) && (delimiters.indexOf(c) >= 0)) 272 position++; 273 } else { 274 int c = str.codePointAt(position); 275 if ((c <= maxDelimCodePoint) && isDelimiter(c)) 276 position += Character.charCount(c); 277 } 278 } 279 return position; 280 } 281 282 private boolean isDelimiter(int codePoint) { 283 for (int i = 0; i < delimiterCodePoints.length; i++) { 284 if (delimiterCodePoints[i] == codePoint) { 285 return true; 286 } 287 } 288 return false; 289 } 290 291 300 public boolean hasMoreTokens() { 301 306 newPosition = skipDelimiters(currentPosition); 307 return (newPosition < maxPosition); 308 } 309 310 317 public String nextToken() { 318 323 324 currentPosition = (newPosition >= 0 && !delimsChanged) ? 325 newPosition : skipDelimiters(currentPosition); 326 327 328 delimsChanged = false; 329 newPosition = -1; 330 331 if (currentPosition >= maxPosition) 332 throw new NoSuchElementException (); 333 int start = currentPosition; 334 currentPosition = scanToken(currentPosition); 335 return str.substring(start, currentPosition); 336 } 337 338 353 public String nextToken(String delim) { 354 delimiters = delim; 355 356 357 delimsChanged = true; 358 359 setMaxDelimCodePoint(); 360 return nextToken(); 361 } 362 363 373 public boolean hasMoreElements() { 374 return hasMoreTokens(); 375 } 376 377 389 public Object nextElement() { 390 return nextToken(); 391 } 392 393 402 public int countTokens() { 403 int count = 0; 404 int currpos = currentPosition; 405 while (currpos < maxPosition) { 406 currpos = skipDelimiters(currpos); 407 if (currpos >= maxPosition) 408 break; 409 currpos = scanToken(currpos); 410 count++; 411 } 412 return count; 413 } 414 } 415 | Popular Tags |