/** * <p> * Sets the position in the target text at which the next search will start. * This method clears any previous match. * </p> * @param position position from which to start the next search * @exception IndexOutOfBoundsException thrown if argument position is out * of the target text range. * @see #getIndex * @stable ICU 2.8 */ public void setIndex(int position) { if (position < search_.beginIndex() || position > search_.endIndex()) { throw new IndexOutOfBoundsException( "setIndex(int) expected position to be between " + search_.beginIndex() + " and " + search_.endIndex()); } search_.reset_ = false; search_.setMatchedLength(0); search_.matchedIndex_ = DONE; }
search_.setTarget(text); search_.matchedIndex_ = DONE; search_.setMatchedLength(0); search_.reset_ = true; search_.isForwardSearching_ = true; if (search_.breakIter() != null) { search_.breakIter().setText((CharacterIterator)text.clone());
/** * Returns the text that was matched by the most recent call to * {@link #first}, {@link #next}, {@link #previous}, or {@link #last}. * If the iterator is not pointing at a valid match (e.g. just after * construction or after {@link #DONE} has been returned, * returns an empty string. * * @return the substring in the target test of the most recent match, * or null if there is no match currently. * @see #first * @see #next * @see #previous * @see #last * @stable ICU 2.0 */ public String getMatchedText() { if (search_.matchedLength() > 0) { int limit = search_.matchedIndex_ + search_.matchedLength(); StringBuilder result = new StringBuilder(search_.matchedLength()); CharacterIterator it = search_.text(); it.setIndex(search_.matchedIndex_); while (it.getIndex() < limit) { result.append(it.current()); it.next(); } it.setIndex(search_.matchedIndex_); return result.toString(); } return null; }
private boolean search(int startIdx, Match m) { || startIdx < search_.beginIndex() || startIdx > search_.endIndex()) { throw new IllegalArgumentException("search(" + startIdx + ", m) - expected position to be between " + search_.beginIndex() + " and " + search_.endIndex());
|| startIdx < search_.beginIndex() || startIdx > search_.endIndex()) { throw new IllegalArgumentException("searchBackwards(" + startIdx + ", m) - expected position to be between " + search_.beginIndex() + " and " + search_.endIndex()); if (startIdx < search_.endIndex()) { BreakIterator bi = search_.internalBreakIter_; int next = bi.following(startIdx);
search_.setMatchedLength(0); search_.matchedIndex_ = DONE;
int index; // offset in ICU4C if (search_.reset_) { index = search_.endIndex(); // m_search_->textLength in ICU4C search_.isForwardSearching_ = false; search_.reset_ = false; int startIdx = search_.beginIndex(); if (index == startIdx || matchindex == startIdx) { matchindex += search_.matchedLength() - 2;
int index = getIndex(); // offset = getOffset() in ICU4C int matchindex = search_.matchedIndex_; int matchlength = search_.matchedLength(); search_.reset_ = false; if (search_.isForwardSearching_) { int endIdx = search_.endIndex(); if (index == endIdx || matchindex == endIdx || (matchindex != DONE &&
search_.matchedIndex_ = search_.matchedIndex_ == DONE ? getIndex() : search_.matchedIndex_ + 1; search_.setMatchedLength(0); textIter_.setOffset(search_.matchedIndex_); if (search_.matchedIndex_ == search_.endIndex()) { search_.matchedIndex_ = DONE; if (search_.matchedLength() <= 0) { textIter_.setOffset(search_.endIndex()); } else { textIter_.setOffset(search_.matchedIndex_);
textIter_.setText(search_.text()); search_.setMatchedLength(0); search_.matchedIndex_ = DONE; search_.isOverlap_ = false;
textOffset = search_.matchedIndex_ + search_.matchedLength() - 1; } else { if (searchBackwards(textOffset, match)) { search_.matchedIndex_ = match.start_; search_.setMatchedLength(match.limit_ - match.start_); return true; } else {
search_.setTarget(target); search_.setBreakIter(breaker); if (search_.breakIter() != null) { search_.breakIter().setText((CharacterIterator)target.clone()); search_.reset_ = true; search_.matchedIndex_ = DONE; search_.setMatchedLength(0);
/** * {@inheritDoc} * @stable ICU 2.8 */ @Override protected int handlePrevious(int position) { if (pattern_.CELength_ == 0) { search_.matchedIndex_ = search_.matchedIndex_ == DONE ? getIndex() : search_.matchedIndex_; if (search_.matchedIndex_ == search_.beginIndex()) { setMatchNotFound(); } else { search_.matchedIndex_--; textIter_.setOffset(search_.matchedIndex_); search_.setMatchedLength(0); } } else { textIter_.setOffset(position); if (search_.isCanonicalMatch_) { // *could* use exact match here since extra accents *not* allowed! handlePreviousCanonical(); } else { handlePreviousExact(); } } return search_.matchedIndex_; }
/** * Set the BreakIterator that will be used to restrict the points * at which matches are detected. * * @param breakiter A BreakIterator that will be used to restrict the * points at which matches are detected. If a match is * found, but the match's start or end index is not a * boundary as determined by the {@link BreakIterator}, * the match will be rejected and another will be searched * for. If this parameter is <tt>null</tt>, no break * detection is attempted. * @see BreakIterator * @stable ICU 2.0 */ public void setBreakIterator(BreakIterator breakiter) { search_.setBreakIter(breakiter); if (search_.breakIter() != null) { // Create a clone of CharacterItearator, so it won't // affect the position currently held by search_.text() if (search_.text() != null) { search_.breakIter().setText((CharacterIterator)search_.text().clone()); } } }
/** * Sets the {@link RuleBasedCollator} to be used for language-specific searching. * <p> * The iterator's position will not be changed by this method. * @param collator to use for this <tt>StringSearch</tt> * @throws IllegalArgumentException thrown when collator is null * @see #getCollator * @stable ICU 2.0 */ public void setCollator(RuleBasedCollator collator) { if (collator == null) { throw new IllegalArgumentException("Collator can not be null"); } collator_ = collator; ceMask_ = getMask(collator_.getStrength()); ULocale collLocale = collator.getLocale(ULocale.VALID_LOCALE); search_.internalBreakIter_ = BreakIterator.getCharacterInstance(collLocale == null ? ULocale.ROOT : collLocale); search_.internalBreakIter_.setText((CharacterIterator)search_.text().clone()); // We need to create a clone toShift_ = collator.isAlternateHandlingShifted(); variableTop_ = collator.getVariableTop(); textIter_ = new CollationElementIterator(pattern_.text_, collator); utilIter_ = new CollationElementIterator(pattern_.text_, collator); // initialize() _after_ setting the iterators for the new collator. initialize(); }
/** * Resets the iteration. * Search will begin at the start of the text string if a forward * iteration is initiated before a backwards iteration. Otherwise if a * backwards iteration is initiated before a forwards iteration, the * search will begin at the end of the text string. * * @stable ICU 2.0 */ public void reset() { setMatchNotFound(); setIndex(search_.beginIndex()); search_.isOverlap_ = false; search_.isCanonicalMatch_ = false; search_.elementComparisonType_ = ElementComparisonType.STANDARD_ELEMENT_COMPARISON; search_.isForwardSearching_ = true; search_.reset_ = true; }
/** * Returns the length of text in the string which matches the search * pattern. This call returns a valid result only after a successful call * to {@link #first}, {@link #next}, {@link #previous}, or {@link #last}. * Just after construction, or after a searching method returns * {@link #DONE}, this method will return 0. * * @return The length of the match in the target text, or 0 if there * is no match currently. * @see #first * @see #next * @see #previous * @see #last * @stable ICU 2.0 */ public int getMatchLength() { return search_.matchedLength(); }
/** * Returns the first index at which the string text matches the search * pattern. The iterator is adjusted so that its current index (as * returned by {@link #getIndex()}) is the match position if one * * was found. * If a match is not found, {@link #DONE} will be returned and * the iterator will be adjusted to the index {@link #DONE}. * @return The character index of the first match, or * {@link #DONE} if there are no matches. * * @see #getIndex * @stable ICU 2.0 */ public final int first() { int startIdx = search_.beginIndex(); setIndex(startIdx); return handleNext(startIdx); }
/** * Returns the BreakIterator that is used to restrict the indexes at which * matches are detected. This will be the same object that was passed to * the constructor or to {@link #setBreakIterator}. * If the {@link BreakIterator} has not been set, <tt>null</tt> will be returned. * See {@link #setBreakIterator} for more information. * * @return the BreakIterator set to restrict logic matches * @see #setBreakIterator * @see BreakIterator * @stable ICU 2.0 */ public BreakIterator getBreakIterator() { return search_.breakIter(); }
/** * Returns the last index in the target text at which it matches the * search pattern. The iterator is adjusted so that its current index * (as returned by {@link #getIndex}) is the match position if one was * found. * If a match is not found, {@link #DONE} will be returned and * the iterator will be adjusted to the index {@link #DONE}. * * @return The index of the first match, or {@link #DONE} if * there are no matches. * @see #getIndex * @stable ICU 2.0 */ public final int last() { int endIdx = search_.endIndex(); setIndex(endIdx); return handlePrevious(endIdx); }