Beispiel #1
0
 /**
  * Removes any spaces or tabs from the end of the segment.
  *
  * @param segment The segment from which to remove tailing whitespace.
  * @return <code>segment</code> with trailing whitespace removed.
  */
 private static Segment removeEndingWhitespace(Segment segment) {
   int toTrim = 0;
   char currentChar = segment.setIndex(segment.getEndIndex() - 1);
   while ((currentChar == ' ' || currentChar == '\t') && currentChar != CharacterIterator.DONE) {
     toTrim++;
     currentChar = segment.previous();
   }
   String stringVal = segment.toString();
   String newStringVal = stringVal.substring(0, stringVal.length() - toTrim);
   return new Segment(newStringVal.toCharArray(), 0, newStringVal.length());
 }
Beispiel #2
0
  /**
   * Attempts to find the previous 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 findPrevious() {
    // Don't match empty strings and don't match if we are at the beginning of the document.
    if (findString.length() == 0 || startIndex < findString.length() - 1) {
      // System.out.println("too close to start");
      return -1;
    }

    try {
      int nextMatch = matchLowerCase.length - 1; // index of next matching character

      // For simplicity, we request all text of the document in a single
      // segment.
      Segment text = new Segment();
      text.setPartialReturn(false);
      document.getText(0, startIndex + 1, text);

      // Iterate through the characters in the current segment
      char previous = text.last();
      // System.out.println("previus isch "+previous);
      for (text.last(); previous != Segment.DONE; previous = text.previous()) {

        // Check if the current character matches with the next
        // search character.
        char current = text.current();
        if (current == matchUpperCase[nextMatch] || current == matchLowerCase[nextMatch]) {
          nextMatch--;
          // System.out.println("matched "+nextMatch);
          // Did we match all search characters?
          if (nextMatch == -1) {
            int foundIndex = text.getIndex() - text.getBeginIndex();
            // System.out.println("found index:"+foundIndex);
            if (matchType == MatchType.CONTAINS) {
              return foundIndex;
            } 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 = matchLowerCase.length - 1;
          }
        } else {
          nextMatch = matchLowerCase.length - 1;
        }
      }

      return -1;
    } catch (BadLocationException e) {
      throw new IndexOutOfBoundsException();
    }
  }