@Override public int countIn(CharSequence sequence) { return sequence.length() - original.countIn(sequence); } @Override public CharMatcher negate() {
/** * Returns a string containing all matching characters of a character * sequence, in order. For example: <pre> {@code * * CharMatcher.is('a').retainFrom("bazaar")}</pre> * * ... returns {@code "aaa"}. */ public String retainFrom(CharSequence sequence) { return negate().removeFrom(sequence); }
@Override public boolean matches(char c) { for (CharMatcher matcher : components) { if (!matcher.matches(c)) { return false; } } return true; }
/** * Collapses groups of matching characters exactly as {@link #collapseFrom} * does, except that groups of matching characters at the start or end of the * sequence are removed without replacement. */ public String trimAndCollapseFrom(CharSequence sequence, char replacement) { int first = negate().indexIn(sequence); if (first == -1) { return ""; // everything matches. nothing's left. } StringBuilder builder = new StringBuilder(sequence.length()); boolean inMatchingGroup = false; for (int i = first; i < sequence.length(); i++) { char c = sequence.charAt(i); if (apply(c)) { inMatchingGroup = true; } else { if (inMatchingGroup) { builder.append(replacement); inMatchingGroup = false; } builder.append(c); } } return builder.toString(); }
int replacementLen = replacement.length(); if (replacementLen == 0) { return removeFrom(sequence); return replaceFrom(sequence, replacement.charAt(0)); int pos = indexIn(string); if (pos == -1) { return string; buf.append(replacement); oldpos = pos + 1; pos = indexIn(string, oldpos); } while (pos != -1);
@Override public boolean matchesNoneOf(CharSequence sequence) { return original.matchesAllOf(sequence); } @Override public int countIn(CharSequence sequence) {
@Override public boolean matchesAllOf(CharSequence sequence) { return original.matchesNoneOf(sequence); } @Override public boolean matchesNoneOf(CharSequence sequence) {
/** * Returns {@code true} if a character sequence contains no 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 matchesNoneOf(CharSequence sequence) { return indexIn(sequence) == -1; }
return NONE; case 1: return is(sequence.charAt(0)); case 2: final char match1 = sequence.charAt(0);
@Override public boolean matches(char c) { for (CharMatcher matcher : components) { if (!matcher.matches(c)) { return false; } } return true; }
/** * Collapses groups of matching characters exactly as {@link #collapseFrom} * does, except that groups of matching characters at the start or end of the * sequence are removed without replacement. */ public String trimAndCollapseFrom(CharSequence sequence, char replacement) { int first = negate().indexIn(sequence); if (first == -1) { return ""; // everything matches. nothing's left. } StringBuilder builder = new StringBuilder(sequence.length()); boolean inMatchingGroup = false; for (int i = first; i < sequence.length(); i++) { char c = sequence.charAt(i); if (apply(c)) { inMatchingGroup = true; } else { if (inMatchingGroup) { builder.append(replacement); inMatchingGroup = false; } builder.append(c); } } return builder.toString(); }
int replacementLen = replacement.length(); if (replacementLen == 0) { return removeFrom(sequence); return replaceFrom(sequence, replacement.charAt(0)); int pos = indexIn(string); if (pos == -1) { return string; buf.append(replacement); oldpos = pos + 1; pos = indexIn(string, oldpos); } while (pos != -1);
/** * Returns a string containing all matching characters of a character * sequence, in order. For example: <pre> {@code * * CharMatcher.is('a').retainFrom("bazaar")}</pre> * * ... returns {@code "aaa"}. */ public String retainFrom(CharSequence sequence) { return negate().removeFrom(sequence); }