From 04392d7aab3f83e4a5e81c5e1657d4b0e8b929e6 Mon Sep 17 00:00:00 2001 From: Syn Wee Quek Date: Sat, 22 Jun 2002 07:46:59 +0000 Subject: [PATCH] ICU-1897 added internal match offset tracker X-SVN-Rev: 8925 --- .../src/com/ibm/icu/text/SearchIterator.java | 33 +++++++------ icu4j/src/com/ibm/icu/text/StringSearch.java | 47 +++++++++++++++---- 2 files changed, 57 insertions(+), 23 deletions(-) diff --git a/icu4j/src/com/ibm/icu/text/SearchIterator.java b/icu4j/src/com/ibm/icu/text/SearchIterator.java index 2f8dce21da3..dce19193b37 100755 --- a/icu4j/src/com/ibm/icu/text/SearchIterator.java +++ b/icu4j/src/com/ibm/icu/text/SearchIterator.java @@ -5,8 +5,8 @@ ******************************************************************************* * * $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/text/SearchIterator.java,v $ - * $Date: 2002/06/21 23:56:48 $ - * $Revision: 1.7 $ + * $Date: 2002/06/22 07:46:58 $ + * $Revision: 1.8 $ * ***************************************************************************************** */ @@ -168,6 +168,7 @@ public abstract class SearchIterator targetText.setIndex(targetText.getBeginIndex()); matchLength = 0; m_reset_ = true; + m_isForwardSearching_ = true; if (breakIterator != null) { breakIterator.setText(targetText); } @@ -202,7 +203,7 @@ public abstract class SearchIterator */ public int getMatchStart() { - return targetText.getIndex(); + return m_lastMatchStart_; } /** @@ -289,8 +290,7 @@ public abstract class SearchIterator public String getMatchedText() { if (matchLength > 0) { - int start = targetText.getIndex(); - int limit = start + matchLength; + int limit = m_lastMatchStart_ + matchLength; StringBuffer result = new StringBuffer(matchLength); result.append(targetText.current()); targetText.next(); @@ -298,7 +298,7 @@ public abstract class SearchIterator result.append(targetText.current()); targetText.next(); } - targetText.setIndex(start); + targetText.setIndex(m_lastMatchStart_); return result.toString(); } return null; @@ -339,6 +339,7 @@ public abstract class SearchIterator // not enough characters to match matchLength = 0; targetText.setIndex(targetText.getEndIndex()); + m_lastMatchStart_ = DONE; return DONE; } m_reset_ = false; @@ -360,7 +361,8 @@ public abstract class SearchIterator if (start == DONE) { start = targetText.getBeginIndex(); } - return handleNext(start); + m_lastMatchStart_ = handleNext(start); + return m_lastMatchStart_; } /** @@ -403,24 +405,22 @@ public abstract class SearchIterator // string. the iterator would have been set to offset textLength if // a match is not found. m_isForwardSearching_ = false; - if (start != DONE) { + if (start != targetText.getEndIndex()) { return start; } - start = targetText.getEndIndex(); } else { - if (start == DONE) { - return DONE; - } - if (start == targetText.getBeginIndex()) { + if (start == targetText.getBeginIndex()) { // not enough characters to match matchLength = 0; targetText.setIndex(targetText.getBeginIndex()); + m_lastMatchStart_ = DONE; return DONE; } } - return handlePrevious(start); + m_lastMatchStart_ = handlePrevious(start); + return m_lastMatchStart_; } /** @@ -626,6 +626,7 @@ public abstract class SearchIterator breakIterator.setText(target); } matchLength = 0; + m_lastMatchStart_ = DONE; m_isOverlap_ = false; m_isForwardSearching_ = true; m_reset_ = true; @@ -716,4 +717,8 @@ public abstract class SearchIterator * If setIndex() is not called, this value will be DONE. */ private int m_setOffset_; + /** + * Offset of the beginning of the last match + */ + private int m_lastMatchStart_; } diff --git a/icu4j/src/com/ibm/icu/text/StringSearch.java b/icu4j/src/com/ibm/icu/text/StringSearch.java index e33989c1621..c7fdb75f3d8 100755 --- a/icu4j/src/com/ibm/icu/text/StringSearch.java +++ b/icu4j/src/com/ibm/icu/text/StringSearch.java @@ -5,8 +5,8 @@ ******************************************************************************* * * $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/text/StringSearch.java,v $ - * $Date: 2002/06/21 23:56:48 $ - * $Revision: 1.7 $ + * $Date: 2002/06/22 07:46:59 $ + * $Revision: 1.8 $ * ***************************************************************************************** */ @@ -174,6 +174,7 @@ public final class StringSearch extends SearchIterator { super(target, breakiter); m_textBeginOffset_ = targetText.getBeginIndex(); + m_textLimitOffset_ = targetText.getEndIndex(); m_collator_ = collator; m_colEIter_ = m_collator_.getCollationElementIterator(target); m_utilColEIter_ = collator.getCollationElementIterator(""); @@ -422,6 +423,7 @@ public final class StringSearch extends SearchIterator { super.setTarget(text); m_textBeginOffset_ = targetText.getBeginIndex(); + m_textLimitOffset_ = targetText.getEndIndex(); m_colEIter_.setText(targetText); } @@ -558,6 +560,14 @@ public final class StringSearch extends SearchIterator if (matchLength != 0) { start += matchLength; } + else { + // we must have reversed direction after we reached the start + // of the target text + // see SearchIterator next(), it checks the bounds and returns + // if it exceeds the range. It does not allow setting of + // m_matchedIndex + m_matchedIndex_ = DONE; + } // status checked below if (m_isCanonicalMatch_) { @@ -568,7 +578,12 @@ public final class StringSearch extends SearchIterator handleNextExact(start); } } - targetText.setIndex(m_matchedIndex_); + if (m_matchedIndex_ == DONE) { + targetText.setIndex(m_textLimitOffset_); + } + else { + targetText.setIndex(m_matchedIndex_); + } return m_matchedIndex_; } @@ -605,6 +620,14 @@ public final class StringSearch extends SearchIterator } } else { + if (matchLength == 0) { + // we must have reversed direction after we reached the end + // of the target text + // see SearchIterator next(), it checks the bounds and returns + // if it exceeds the range. It does not allow setting of + // m_matchedIndex + m_matchedIndex_ = DONE; + } if (m_isCanonicalMatch_) { // can't use exact here since extra accents are allowed. handlePreviousCanonical(start); @@ -614,7 +637,12 @@ public final class StringSearch extends SearchIterator } } - targetText.setIndex(m_matchedIndex_); + if (m_matchedIndex_ == DONE) { + targetText.setIndex(m_textBeginOffset_); + } + else { + targetText.setIndex(m_matchedIndex_); + } return m_matchedIndex_; } @@ -1408,18 +1436,19 @@ public final class StringSearch extends SearchIterator /** * Checks to see if the match is repeated * @param start new match start index - * @param end new match end index + * @param limit new match limit index * @return true if the the match is repeated, false otherwise */ - private final boolean checkRepeatedMatch(int start, int end) + private final boolean checkRepeatedMatch(int start, int limit) { if (m_matchedIndex_ == DONE) { return false; } - int lastmatchlimit = m_matchedIndex_ + matchLength; + int end = limit - 1; // last character in the match + int lastmatchend = m_matchedIndex_ + matchLength - 1; if (!isOverlapping()) { - return (start >= m_matchedIndex_ && start <= lastmatchlimit) - || (end >= m_matchedIndex_ && end <= lastmatchlimit); + return (start >= m_matchedIndex_ && start <= lastmatchend) + || (end >= m_matchedIndex_ && end <= lastmatchend); } return start == m_matchedIndex_;