1 7 8 package com.ibm.icu.util; 9 10 import java.util.Enumeration ; 11 import java.util.NoSuchElementException ; 12 import com.ibm.icu.text.UnicodeSet; 13 import com.ibm.icu.text.UTF16; 14 15 100 public final class StringTokenizer implements Enumeration 101 { 102 104 119 public StringTokenizer(String str, UnicodeSet delim, boolean returndelims) 120 { 121 this(str, delim, returndelims, false); 122 } 123 124 146 public StringTokenizer(String str, UnicodeSet delim, boolean returndelims, boolean coalescedelims) 147 { 148 m_source_ = str; 149 m_length_ = str.length(); 150 if (delim == null) { 151 m_delimiters_ = EMPTY_DELIMITER_; 152 } 153 else { 154 m_delimiters_ = delim; 155 } 156 m_returnDelimiters_ = returndelims; 157 m_coalesceDelimiters_ = coalescedelims; 158 m_tokenOffset_ = -1; 159 m_tokenSize_ = -1; 160 if (m_length_ == 0) { 161 m_nextOffset_ = -1; 163 } 164 else { 165 m_nextOffset_ = 0; 166 if (!returndelims) { 167 m_nextOffset_ = getNextNonDelimiter(0); 168 } 169 } 170 } 171 172 182 public StringTokenizer(String str, UnicodeSet delim) 183 { 184 this(str, delim, false, false); 185 } 186 187 202 public StringTokenizer(String str, String delim, boolean returndelims) 203 { 204 this(str, delim, returndelims, false); } 206 207 229 public StringTokenizer(String str, String delim, boolean returndelims, boolean coalescedelims) 230 { 231 m_delimiters_ = EMPTY_DELIMITER_; 233 if (delim != null && delim.length() > 0) { 234 m_delimiters_ = new UnicodeSet(); 235 m_delimiters_.addAll(delim); 236 checkDelimiters(); 237 } 238 m_coalesceDelimiters_ = coalescedelims; 239 m_source_ = str; 240 m_length_ = str.length(); 241 m_returnDelimiters_ = returndelims; 242 m_tokenOffset_ = -1; 243 m_tokenSize_ = -1; 244 if (m_length_ == 0) { 245 m_nextOffset_ = -1; 247 } 248 else { 249 m_nextOffset_ = 0; 250 if (!returndelims) { 251 m_nextOffset_ = getNextNonDelimiter(0); 252 } 253 } 254 } 255 256 266 public StringTokenizer(String str, String delim) 267 { 268 this(str, delim, false, false); 270 } 271 272 283 public StringTokenizer(String str) 284 { 285 this(str, DEFAULT_DELIMITERS_, false, false); 286 } 287 288 290 300 public boolean hasMoreTokens() 301 { 302 return m_nextOffset_ >= 0; 303 } 304 305 312 public String nextToken() 313 { 314 if (m_tokenOffset_ < 0) { 315 if (m_nextOffset_ < 0) { 316 throw new NoSuchElementException ("No more tokens in String"); 317 } 318 if (m_returnDelimiters_) { 320 int tokenlimit = 0; 321 int c = UTF16.charAt(m_source_, m_nextOffset_); 322 boolean contains = delims == null 323 ? m_delimiters_.contains(c) 324 : c < delims.length && delims[c]; 325 if (contains) { 326 if (m_coalesceDelimiters_) { 327 tokenlimit = getNextNonDelimiter(m_nextOffset_); 328 } else { 329 tokenlimit = m_nextOffset_ + UTF16.getCharCount(c); 330 if (tokenlimit == m_length_) { 331 tokenlimit = -1; 332 } 333 } 334 } 335 else { 336 tokenlimit = getNextDelimiter(m_nextOffset_); 337 } 338 String result; 339 if (tokenlimit < 0) { 340 result = m_source_.substring(m_nextOffset_); 341 } 342 else { 343 result = m_source_.substring(m_nextOffset_, tokenlimit); 344 } 345 m_nextOffset_ = tokenlimit; 346 return result; 347 } 348 else { 349 int tokenlimit = getNextDelimiter(m_nextOffset_); 350 String result; 351 if (tokenlimit < 0) { 352 result = m_source_.substring(m_nextOffset_); 353 m_nextOffset_ = tokenlimit; 354 } 355 else { 356 result = m_source_.substring(m_nextOffset_, tokenlimit); 357 m_nextOffset_ = getNextNonDelimiter(tokenlimit); 358 } 359 360 return result; 361 } 362 } 363 if (m_tokenOffset_ >= m_tokenSize_) { 365 throw new NoSuchElementException ("No more tokens in String"); 366 } 367 String result; 368 if (m_tokenLimit_[m_tokenOffset_] >= 0) { 369 result = m_source_.substring(m_tokenStart_[m_tokenOffset_], 370 m_tokenLimit_[m_tokenOffset_]); 371 } 372 else { 373 result = m_source_.substring(m_tokenStart_[m_tokenOffset_]); 374 } 375 m_tokenOffset_ ++; 376 m_nextOffset_ = -1; 377 if (m_tokenOffset_ < m_tokenSize_) { 378 m_nextOffset_ = m_tokenStart_[m_tokenOffset_]; 379 } 380 return result; 381 } 382 383 397 public String nextToken(String delim) 398 { 399 m_delimiters_ = EMPTY_DELIMITER_; 400 if (delim != null && delim.length() > 0) { 401 m_delimiters_ = new UnicodeSet(); 402 m_delimiters_.addAll(delim); 403 } 404 return nextToken(m_delimiters_); 405 } 406 407 421 public String nextToken(UnicodeSet delim) 422 { 423 m_delimiters_ = delim; 424 checkDelimiters(); 425 m_tokenOffset_ = -1; 426 m_tokenSize_ = -1; 427 if (!m_returnDelimiters_) { 428 m_nextOffset_ = getNextNonDelimiter(m_nextOffset_); 429 } 430 return nextToken(); 431 } 432 433 442 public boolean hasMoreElements() 443 { 444 return hasMoreTokens(); 445 } 446 447 458 public Object nextElement() 459 { 460 return nextToken(); 461 } 462 463 472 public int countTokens() 473 { 474 int result = 0; 475 if (hasMoreTokens()) { 476 if (m_tokenOffset_ >= 0) { 477 return m_tokenSize_ - m_tokenOffset_; 478 } 479 if (m_tokenStart_ == null) { 480 m_tokenStart_ = new int[TOKEN_SIZE_]; 481 m_tokenLimit_ = new int[TOKEN_SIZE_]; 482 } 483 do { 484 if (m_tokenStart_.length == result) { 485 int temptokenindex[] = m_tokenStart_; 486 int temptokensize[] = m_tokenLimit_; 487 int originalsize = temptokenindex.length; 488 int newsize = originalsize + TOKEN_SIZE_; 489 m_tokenStart_ = new int[newsize]; 490 m_tokenLimit_ = new int[newsize]; 491 System.arraycopy(temptokenindex, 0, m_tokenStart_, 0, 492 originalsize); 493 System.arraycopy(temptokensize, 0, m_tokenLimit_, 0, 494 originalsize); 495 } 496 m_tokenStart_[result] = m_nextOffset_; 497 if (m_returnDelimiters_) { 498 int c = UTF16.charAt(m_source_, m_nextOffset_); 499 boolean contains = delims == null 500 ? m_delimiters_.contains(c) 501 : c < delims.length && delims[c]; 502 if (contains) { 503 if (m_coalesceDelimiters_) { 504 m_tokenLimit_[result] = getNextNonDelimiter( 505 m_nextOffset_); 506 } else { 507 int p = m_nextOffset_ + 1; 508 if (p == m_length_) { 509 p = -1; 510 } 511 m_tokenLimit_[result] = p; 512 513 } 514 } 515 else { 516 m_tokenLimit_[result] = getNextDelimiter(m_nextOffset_); 517 } 518 m_nextOffset_ = m_tokenLimit_[result]; 519 } 520 else { 521 m_tokenLimit_[result] = getNextDelimiter(m_nextOffset_); 522 m_nextOffset_ = getNextNonDelimiter(m_tokenLimit_[result]); 523 } 524 result ++; 525 } while (m_nextOffset_ >= 0); 526 m_tokenOffset_ = 0; 527 m_tokenSize_ = result; 528 m_nextOffset_ = m_tokenStart_[0]; 529 } 530 return result; 531 } 532 533 535 539 private int m_tokenOffset_; 540 544 private int m_tokenSize_; 545 551 private int m_tokenStart_[]; 552 557 private int m_tokenLimit_[]; 558 561 private UnicodeSet m_delimiters_; 562 565 private String m_source_; 566 569 private int m_length_; 570 573 private int m_nextOffset_; 574 577 private boolean m_returnDelimiters_; 578 579 582 private boolean m_coalesceDelimiters_; 583 584 587 private static final UnicodeSet DEFAULT_DELIMITERS_ 588 = new UnicodeSet("[ \t\n\r\f]", false); 589 592 private static final int TOKEN_SIZE_ = 100; 593 596 private static final UnicodeSet EMPTY_DELIMITER_ = new UnicodeSet(); 597 598 600 607 private int getNextDelimiter(int offset) 608 { 609 if (offset >= 0) { 610 int result = offset; 611 int c = 0; 612 if (delims == null) { 613 do { 614 c = UTF16.charAt(m_source_, result); 615 if (m_delimiters_.contains(c)) { 616 break; 617 } 618 result ++; 619 } while (result < m_length_); 620 } else { 621 do { 622 c = UTF16.charAt(m_source_, result); 623 if (c < delims.length && delims[c]) { 624 break; 625 } 626 result ++; 627 } while (result < m_length_); 628 } 629 if (result < m_length_) { 630 return result; 631 } 632 } 633 return -1 - m_length_; 634 } 635 636 643 private int getNextNonDelimiter(int offset) 644 { 645 if (offset >= 0) { 646 int result = offset; 647 int c = 0; 648 if (delims == null) { 649 do { 650 c = UTF16.charAt(m_source_, result); 651 if (!m_delimiters_.contains(c)) { 652 break; 653 } 654 result ++; 655 } while (result < m_length_); 656 } else { 657 do { 658 c = UTF16.charAt(m_source_, result); 659 if (!(c < delims.length && delims[c])) { 660 break; 661 } 662 result ++; 663 } while (result < m_length_); 664 } 665 if (result < m_length_) { 666 return result; 667 } 668 } 669 return -1 - m_length_; 670 } 671 672 void checkDelimiters() { 673 if (m_delimiters_ == null || m_delimiters_.size() == 0) { 674 delims = new boolean[0]; 675 } else { 676 int maxChar = m_delimiters_.getRangeEnd(m_delimiters_.getRangeCount()-1); 677 if (maxChar < 0x7f) { 678 delims = new boolean[maxChar+1]; 679 for (int i = 0, ch; -1 != (ch = m_delimiters_.charAt(i)); ++i) { 680 delims[ch] = true; 681 } 682 } else { 683 delims = null; 684 } 685 } 686 } 687 private boolean[] delims; 688 } 689 | Popular Tags |