コード例 #1
0
  /**
   * Attempts to find the next subsequence of the input sequence that matches the pattern.
   *
   * <p>This method starts at the beginning of the input sequence or, if a previous invocation of
   * the method was successful and the matcher has not since been reset, at the first character not
   * matched by the previous match.
   *
   * @return the index of the first occurrence of the search string, starting at the specified
   *     offset, or -1 if no occurrence was found.
   */
  public int findNext() {
    // Don't match empty strings and don't match if we are at the end of the document.
    if (findString.length() == 0 || document.getLength() - findString.length() < startIndex) {
      return -1;
    }

    try {
      int nextMatch = 0; // index of next matching character

      // Iterate through all segments of the document starting from offset
      Segment text = new Segment();
      text.setPartialReturn(true);
      int offset = startIndex;
      int nleft = document.getLength() - startIndex;
      while (nleft > 0) {
        document.getText(offset, nleft, text);

        // Iterate through the characters in the current segment
        char next = text.first();
        for (text.first(); next != Segment.DONE; next = text.next()) {

          // Check if the current character matches with the next
          // search character.
          char current = text.current();
          if (current == matchUpperCase[nextMatch] || current == matchLowerCase[nextMatch]) {
            nextMatch++;

            // Did we match all search characters?
            if (nextMatch == matchLowerCase.length) {
              int foundIndex =
                  text.getIndex() - text.getBeginIndex() + offset - matchLowerCase.length + 1;
              if (matchType == MatchType.CONTAINS) {
                return foundIndex;
                // break; <- never reached
              } else if (matchType == MatchType.STARTS_WITH) {
                if (!isWordChar(foundIndex - 1)) {
                  return foundIndex;
                }
              } else if (matchType == MatchType.FULL_WORD) {
                if (!isWordChar(foundIndex - 1)
                    && !isWordChar(foundIndex + matchLowerCase.length)) {
                  return foundIndex;
                }
              }
              nextMatch = 0;
            }
          } else {
            nextMatch = 0;
          }
        }

        // Move forward to the next segment
        nleft -= text.count;
        offset += text.count;
      }
      return -1;
    } catch (BadLocationException e) {
      throw new IndexOutOfBoundsException();
    }
  }
コード例 #2
0
 /** This helper method will return the end of the next word in the buffer. */
 private static int getNextWordEnd(Segment text, int startPos) {
   for (char ch = text.setIndex(startPos); ch != Segment.DONE; ch = text.next()) {
     if (!Character.isLetterOrDigit(ch)) {
       return text.getIndex();
     }
   }
   return text.getEndIndex();
 }