@Override public boolean matches(char c) { return !original.matches(c); }
/** * Sets bits in {@code table} matched by this matcher. */ void setBits(BitSet table) { for (int c = Character.MAX_VALUE; c >= Character.MIN_VALUE; c--) { if (matches((char) c)) { table.set(c); } } }
/** * Returns the number of matching characters found in a character sequence. */ public int countIn(CharSequence sequence) { int count = 0; for (int i = 0; i < sequence.length(); i++) { if (matches(sequence.charAt(i))) { count++; } } return count; }
/** * Returns {@code true} if a character sequence contains only matching characters. * * <p>The default implementation iterates over the sequence, invoking {@link #matches} for each * character, until this returns {@code false} or the end is reached. * * @param sequence the character sequence to examine, possibly empty * @return {@code true} if this matcher matches every character in the sequence, including when * the sequence is empty */ public boolean matchesAllOf(CharSequence sequence) { for (int i = sequence.length() - 1; i >= 0; i--) { if (!matches(sequence.charAt(i))) { return false; } } return true; }
@Override public CharMatcher and(CharMatcher other) { return other.matches(match) ? super.and(other) : other; }
@Override public CharMatcher or(CharMatcher other) { return other.matches(match) ? any() : this; }
@Override public CharMatcher and(CharMatcher other) { return other.matches(match) ? this : none(); }
@Override public CharMatcher or(CharMatcher other) { return other.matches(match) ? other : super.or(other); }
/** * Returns the index of the first matching character in a character sequence, starting from a * given position, or {@code -1} if no character matches after that position. * * <p>The default implementation iterates over the sequence in forward order, beginning at {@code * start}, calling {@link #matches} for each character. * * @param sequence the character sequence to examine * @param start the first index to examine; must be nonnegative and no greater than {@code * sequence.length()} * @return the index of the first matching character, guaranteed to be no less than {@code start}, * or {@code -1} if no character matches * @throws IndexOutOfBoundsException if start is negative or greater than {@code * sequence.length()} */ public int indexIn(CharSequence sequence, int start) { int length = sequence.length(); Preconditions.checkPositionIndex(start, length); for (int i = start; i < length; i++) { if (matches(sequence.charAt(i))) { return i; } } return -1; }
char consumeCharacter(CharMatcher matcher) { checkState(hasMore(), "No more elements!"); char c = previewChar(); checkState(matcher.matches(c), "Unexpected character matched: " + c); position++; return c; }