/** * Returns the index of the first 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 forward order calling {@link * #matches} for each character. * * @param sequence the character sequence to examine from the beginning * @return an index, or {@code -1} if no character matches */ public int indexIn(CharSequence sequence) { return indexIn(sequence, 0); }
/** * Returns {@code true} if a character sequence contains no matching BMP characters. Equivalent to * {@code !matchesAnyOf(sequence)}. * * <p>The default implementation iterates over the sequence, invoking {@link #matches} for each * character, until this returns {@code true} or the end is reached. * * @param sequence the character sequence to examine, possibly empty * @return {@code true} if this matcher matches no characters in the sequence, including when the * sequence is empty */ public boolean matchesNoneOf(CharSequence sequence) { return indexIn(sequence) == -1; }
/** * Returns the index of the first 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 forward order calling {@link * #matches} for each character. * * @param sequence the character sequence to examine from the beginning * @return an index, or {@code -1} if no character matches */ public int indexIn(CharSequence sequence) { return indexIn(sequence, 0); }
/** * Returns {@code true} if a character sequence contains no matching BMP characters. Equivalent to * {@code !matchesAnyOf(sequence)}. * * <p>The default implementation iterates over the sequence, invoking {@link #matches} for each * character, until this returns {@code true} or the end is reached. * * @param sequence the character sequence to examine, possibly empty * @return {@code true} if this matcher matches no characters in the sequence, including when the * sequence is empty */ public boolean matchesNoneOf(CharSequence sequence) { return indexIn(sequence) == -1; }
/** * Get right delimiter. * * @param value value * @return right delimiter */ public static String getRightDelimiter(final String value) { int index = CharMatcher.anyOf("]`'\"").indexIn(value); if (-1 == index) { return ""; } return String.valueOf(value.charAt(index)); }
String consumeTokenIfPresent(CharMatcher matcher) { checkState(hasMore()); int startPosition = position; position = matcher.negate().indexIn(input, startPosition); return hasMore() ? input.substring(startPosition, position) : input.substring(startPosition); }
/** * Get left delimiter. * * @param value value * @return left delimiter */ public static String getLeftDelimiter(final String value) { int index = CharMatcher.anyOf("[`'\"").indexIn(value); if (-1 == index) { return ""; } return String.valueOf(value.charAt(index)); }
int pos = indexIn(string); if (pos == -1) { return string;
int pos = indexIn(string); if (pos == -1) { return string;
/** Enum values can override for performance reasons. */ String convert(CaseFormat format, String s) { // deal with camel conversion StringBuilder out = null; int i = 0; int j = -1; while ((j = wordBoundary.indexIn(s, ++j)) != -1) { if (i == 0) { // include some extra space for separators out = new StringBuilder(s.length() + 4 * wordSeparator.length()); out.append(format.normalizeFirstWord(s.substring(i, j))); } else { out.append(format.normalizeWord(s.substring(i, j))); } out.append(format.wordSeparator); i = j + wordSeparator.length(); } return (i == 0) ? format.normalizeFirstWord(s) : out.append(format.normalizeWord(s.substring(i))).toString(); }
String consumeTokenIfPresent(CharMatcher matcher) { checkState(hasMore()); int startPosition = position; position = matcher.negate().indexIn(input, startPosition); return hasMore() ? input.substring(startPosition, position) : input.substring(startPosition); }
String consumeTokenIfPresent(CharMatcher matcher) { checkState(hasMore()); int startPosition = position; position = matcher.negate().indexIn(input, startPosition); return hasMore() ? input.substring(startPosition, position) : input.substring(startPosition); }
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)); }
private void reallyTestMatchThenNoMatch(CharMatcher matcher, String s) { assertEquals(0, matcher.indexIn(s)); assertEquals(0, matcher.indexIn(s, 0)); assertEquals(-1, matcher.indexIn(s, 1)); assertEquals(-1, matcher.indexIn(s, 2)); assertEquals(0, matcher.lastIndexIn(s)); assertTrue(matcher.matchesAnyOf(s)); assertFalse(matcher.matchesAllOf(s)); assertFalse(matcher.matchesNoneOf(s)); assertEquals(s.substring(1), matcher.removeFrom(s)); assertEquals("z" + s.substring(1), matcher.replaceFrom(s, 'z')); assertEquals("ZZ" + s.substring(1), matcher.replaceFrom(s, "ZZ")); assertEquals(s.substring(1), matcher.trimFrom(s)); assertEquals(1, matcher.countIn(s)); }
private void reallyTestNoMatchThenMatch(CharMatcher matcher, String s) { assertEquals(1, matcher.indexIn(s)); assertEquals(1, matcher.indexIn(s, 0)); assertEquals(1, matcher.indexIn(s, 1)); assertEquals(-1, matcher.indexIn(s, 2)); assertEquals(1, matcher.lastIndexIn(s)); assertTrue(matcher.matchesAnyOf(s)); assertFalse(matcher.matchesAllOf(s)); assertFalse(matcher.matchesNoneOf(s)); assertEquals(s.substring(0, 1), matcher.removeFrom(s)); assertEquals(s.substring(0, 1) + "z", matcher.replaceFrom(s, 'z')); assertEquals(s.substring(0, 1) + "ZZ", matcher.replaceFrom(s, "ZZ")); assertEquals(s.substring(0, 1), matcher.trimFrom(s)); assertEquals(1, matcher.countIn(s)); }
private void reallyTestNoMatches(CharMatcher matcher, CharSequence s) { assertFalse(matcher.matches(s.charAt(0))); assertEquals(-1, matcher.indexIn(s)); assertEquals(-1, matcher.indexIn(s, 0)); assertEquals(-1, matcher.indexIn(s, 1)); assertEquals(-1, matcher.indexIn(s, s.length())); try { matcher.indexIn(s, s.length() + 1); fail(); } catch (IndexOutOfBoundsException expected) { } try { matcher.indexIn(s, -1); fail(); } catch (IndexOutOfBoundsException expected) { } assertEquals(-1, matcher.lastIndexIn(s)); assertFalse(matcher.matchesAnyOf(s)); assertFalse(matcher.matchesAllOf(s)); assertTrue(matcher.matchesNoneOf(s)); assertEquals(s.toString(), matcher.removeFrom(s)); assertEquals(s.toString(), matcher.replaceFrom(s, 'z')); assertEquals(s.toString(), matcher.replaceFrom(s, "ZZ")); assertEquals(s.toString(), matcher.trimFrom(s)); assertEquals(0, matcher.countIn(s)); }
private void reallyTestEmpty(CharMatcher matcher) throws Exception { assertEquals(-1, matcher.indexIn("")); assertEquals(-1, matcher.indexIn("", 0)); try { matcher.indexIn("", 1); fail(); } catch (IndexOutOfBoundsException expected) { } try { matcher.indexIn("", -1); fail(); } catch (IndexOutOfBoundsException expected) { } assertEquals(-1, matcher.lastIndexIn("")); assertFalse(matcher.matchesAnyOf("")); assertTrue(matcher.matchesAllOf("")); assertTrue(matcher.matchesNoneOf("")); assertEquals("", matcher.removeFrom("")); assertEquals("", matcher.replaceFrom("", 'z')); assertEquals("", matcher.replaceFrom("", "ZZ")); assertEquals("", matcher.trimFrom("")); assertEquals(0, matcher.countIn("")); }
@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)); }
@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)); }