private void reallyTestAllMatches(CharMatcher matcher, CharSequence s) { assertTrue(matcher.matches(s.charAt(0))); assertEquals(0, matcher.indexIn(s)); assertEquals(0, matcher.indexIn(s, 0)); assertEquals(1, matcher.indexIn(s, 1)); assertEquals(-1, matcher.indexIn(s, s.length())); assertEquals(s.length() - 1, matcher.lastIndexIn(s)); assertTrue(matcher.matchesAnyOf(s)); assertTrue(matcher.matchesAllOf(s)); assertFalse(matcher.matchesNoneOf(s)); assertEquals("", matcher.removeFrom(s)); assertEquals(Strings.repeat("z", s.length()), matcher.replaceFrom(s, 'z')); assertEquals(Strings.repeat("ZZ", s.length()), matcher.replaceFrom(s, "ZZ")); assertEquals("", matcher.trimFrom(s)); assertEquals(s.length(), matcher.countIn(s)); }
@SuppressWarnings("deprecation") // intentionally testing apply() method private void reallyTestOneCharMatch(CharMatcher matcher, String s) { assertTrue(matcher.matches(s.charAt(0))); assertTrue(matcher.apply(s.charAt(0))); assertEquals(0, matcher.indexIn(s)); assertEquals(0, matcher.indexIn(s, 0)); assertEquals(-1, matcher.indexIn(s, 1)); assertEquals(0, matcher.lastIndexIn(s)); assertTrue(matcher.matchesAnyOf(s)); assertTrue(matcher.matchesAllOf(s)); assertFalse(matcher.matchesNoneOf(s)); assertEquals("", matcher.removeFrom(s)); assertEquals("z", matcher.replaceFrom(s, 'z')); assertEquals("ZZ", matcher.replaceFrom(s, "ZZ")); assertEquals("", matcher.trimFrom(s)); assertEquals(1, matcher.countIn(s)); }
@Override public boolean matches(char c) { return !original.matches(c); }
@SuppressWarnings("deprecation") // intentionally testing apply() method private void reallyTestOneCharNoMatch(CharMatcher matcher, String s) { assertFalse(matcher.matches(s.charAt(0))); assertFalse(matcher.apply(s.charAt(0))); assertEquals(-1, matcher.indexIn(s)); assertEquals(-1, matcher.indexIn(s, 0)); assertEquals(-1, matcher.indexIn(s, 1)); assertEquals(-1, matcher.lastIndexIn(s)); assertFalse(matcher.matchesAnyOf(s)); assertFalse(matcher.matchesAllOf(s)); assertTrue(matcher.matchesNoneOf(s)); assertSame(s, matcher.removeFrom(s)); assertSame(s, matcher.replaceFrom(s, 'z')); assertSame(s, matcher.replaceFrom(s, "ZZ")); assertSame(s, matcher.trimFrom(s)); assertSame(0, matcher.countIn(s)); }
/** * @deprecated Provided only to satisfy the {@link Predicate} interface; use {@link #matches} * instead. */ @Deprecated @Override public boolean apply(Character character) { return matches(character); }
/** * Returns the number of matching {@code char}s found in a character sequence. * * <p>Counts 2 per supplementary character, such as for {@link #whitespace}().{@link #negate}(). */ 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 BMP 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; }
/** * Returns the index of the last matching BMP character in a character sequence, or {@code -1} if * no matching character is present. * * <p>The default implementation iterates over the sequence in reverse order calling {@link * #matches} for each character. * * @param sequence the character sequence to examine from the end * @return an index, or {@code -1} if no character matches */ public int lastIndexIn(CharSequence sequence) { for (int i = sequence.length() - 1; i >= 0; i--) { if (matches(sequence.charAt(i))) { return i; } } return -1; }
/** Sets bits in {@code table} matched by this matcher. */ @GwtIncompatible // used only from other GwtIncompatible code void setBits(BitSet table) { for (int c = Character.MAX_VALUE; c >= Character.MIN_VALUE; c--) { if (matches((char) c)) { table.set(c); } } }
@Override public CharMatcher and(CharMatcher other) { return other.matches(match) ? this : none(); }
@Override public CharMatcher or(CharMatcher other) { return other.matches(match) ? any() : this; }
@Override public CharMatcher and(CharMatcher other) { return other.matches(match) ? super.and(other) : other; }
@Override public CharMatcher or(CharMatcher other) { return other.matches(match) ? other : super.or(other); }
char consumeCharacter(CharMatcher matcher) { checkState(hasMore()); char c = previewChar(); checkState(matcher.matches(c)); position++; return c; }
static void checkExactMatches(CharMatcher m, char[] chars) { Set<Character> positive = Sets.newHashSetWithExpectedSize(chars.length); for (char c : chars) { positive.add(c); } for (int c = 0; c <= Character.MAX_VALUE; c++) { assertFalse(positive.contains(new Character((char) c)) ^ m.matches((char) c)); } }
@GwtIncompatible // java.util.BitSet private void doTestSetBits(CharMatcher matcher) { BitSet bitset = new BitSet(); matcher.setBits(bitset); for (int i = Character.MIN_VALUE; i <= Character.MAX_VALUE; i++) { assertEquals(matcher.matches((char) i), bitset.get(i)); } }
@GwtIncompatible // Character.isISOControl public void testJavaIsoControl() { for (int c = 0; c <= Character.MAX_VALUE; c++) { assertEquals( "" + c, Character.isISOControl(c), CharMatcher.javaIsoControl().matches((char) c)); } }