/** * Collapses groups of matching characters exactly as {@link #collapseFrom} does, except that * groups of matching BMP characters at the start or end of the sequence are removed without * replacement. */ public String trimAndCollapseFrom(CharSequence sequence, char replacement) { // This implementation avoids unnecessary allocation. int len = sequence.length(); int first = 0; int last = len - 1; while (first < len && matches(sequence.charAt(first))) { first++; } while (last > first && matches(sequence.charAt(last))) { last--; } return (first == 0 && last == len - 1) ? collapseFrom(sequence, replacement) : finishCollapseFrom( sequence, first, last + 1, replacement, new StringBuilder(last + 1 - first), false); }
/** * Processes String resource values in the same way real Android does, namely:- * 1) Trim leading and trailing whitespace. * 2) Converts code points. * 3) Escapes */ public static String processStringResources(String inputValue) { return escape(whitespace().collapseFrom(inputValue.trim(), ' ')); }
/** * Collapses groups of matching characters exactly as {@link #collapseFrom} does, except that * groups of matching BMP characters at the start or end of the sequence are removed without * replacement. */ public String trimAndCollapseFrom(CharSequence sequence, char replacement) { // This implementation avoids unnecessary allocation. int len = sequence.length(); int first = 0; int last = len - 1; while (first < len && matches(sequence.charAt(first))) { first++; } while (last > first && matches(sequence.charAt(last))) { last--; } return (first == 0 && last == len - 1) ? collapseFrom(sequence, replacement) : finishCollapseFrom( sequence, first, last + 1, replacement, new StringBuilder(last + 1 - first), false); }
/** * Collapses groups of matching characters exactly as {@link #collapseFrom} does, except that * groups of matching BMP characters at the start or end of the sequence are removed without * replacement. */ public String trimAndCollapseFrom(CharSequence sequence, char replacement) { // This implementation avoids unnecessary allocation. int len = sequence.length(); int first = 0; int last = len - 1; while (first < len && matches(sequence.charAt(first))) { first++; } while (last > first && matches(sequence.charAt(last))) { last--; } return (first == 0 && last == len - 1) ? collapseFrom(sequence, replacement) : finishCollapseFrom( sequence, first, last + 1, replacement, new StringBuilder(last + 1 - first), false); }
public void testCollapse_any() { assertEquals("", CharMatcher.any().collapseFrom("", '_')); assertEquals("_", CharMatcher.any().collapseFrom("a", '_')); assertEquals("_", CharMatcher.any().collapseFrom("ab", '_')); assertEquals("_", CharMatcher.any().collapseFrom("abcd", '_')); }
private void doTestCollapseWithNoChange(String inout) { assertSame(inout, is('-').collapseFrom(inout, '_')); assertSame(inout, is('-').or(is('#')).collapseFrom(inout, '_')); assertSame(inout, isNot('x').collapseFrom(inout, '_')); assertSame(inout, is('x').negate().collapseFrom(inout, '_')); assertSame(inout, anyOf("-").collapseFrom(inout, '_')); assertSame(inout, anyOf("-#").collapseFrom(inout, '_')); assertSame(inout, anyOf("-#123").collapseFrom(inout, '_')); assertSame(inout, CharMatcher.none().collapseFrom(inout, '_')); }
private void doTestCollapse(String in, String out) { // Try a few different matchers which all match '-' and not 'x' // Try replacement chars that both do and do not change the value. for (char replacement : new char[] {'_', '-'}) { String expected = out.replace('_', replacement); assertEqualsSame(expected, in, is('-').collapseFrom(in, replacement)); assertEqualsSame(expected, in, is('-').collapseFrom(in, replacement)); assertEqualsSame(expected, in, is('-').or(is('#')).collapseFrom(in, replacement)); assertEqualsSame(expected, in, isNot('x').collapseFrom(in, replacement)); assertEqualsSame(expected, in, is('x').negate().collapseFrom(in, replacement)); assertEqualsSame(expected, in, anyOf("-").collapseFrom(in, replacement)); assertEqualsSame(expected, in, anyOf("-#").collapseFrom(in, replacement)); assertEqualsSame(expected, in, anyOf("-#123").collapseFrom(in, replacement)); } }
@Override public String apply(@Nullable String input) { if (input == null) return null; return DOCKERFILE_INVALID_CHARACTERS.collapseFrom(input.toLowerCase(Locale.ENGLISH), '_'); } };
/** * Removes extra slashes from a path. Leading slash is preserved, trailing * slash is stripped, and any runs of more than one slash in the middle is * replaced by a single slash. */ static String removeExtraneousSlashes(String s) { int lastNonSlash = NON_SLASH_MATCHER.lastIndexIn(s); if (lastNonSlash != -1) { s = s.substring(0, lastNonSlash + 1); } return SLASH_MATCHER.collapseFrom(s, '/'); }
/** * Removes extra slashes from a path. Leading slash is preserved, trailing * slash is stripped, and any runs of more than one slash in the middle is * replaced by a single slash. */ static String removeExtraneousSlashes(String s) { int lastNonSlash = NON_SLASH_MATCHER.lastIndexIn(s); if (lastNonSlash != -1) { s = s.substring(0, lastNonSlash + 1); } return SLASH_MATCHER.collapseFrom(s, '/'); }
/** * Reduces multiple adjacent slashes to a single slash and removes any trailing slash. */ public static String clean(String name) { CharMatcher slashMatcher = CharMatcher.is('/'); name = slashMatcher.collapseFrom(name, '/'); if ("/".equals(name)) { return name; } return slashMatcher.trimTrailingFrom(name); }
/** * Processes String resource values in the same way real Android does, namely:- * 1) Trim leading and trailing whitespace. * 2) Converts code points. * 3) Escapes */ public static String processStringResources(String inputValue) { return escape(whitespace().collapseFrom(inputValue.trim(), ' ')); }
/** * 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. */ @CheckReturnValue public String trimAndCollapseFrom(CharSequence sequence, char replacement) { // This implementation avoids unnecessary allocation. int len = sequence.length(); int first; int last; for (first = 0; first < len && matches(sequence.charAt(first)); first++) {} for (last = len - 1; last > first && matches(sequence.charAt(last)); last--) {} return (first == 0 && last == len - 1) ? collapseFrom(sequence, replacement) : finishCollapseFrom( sequence, first, last + 1, replacement, new StringBuilder(last + 1 - first), false); }
/** * 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. */ @CheckReturnValue public String trimAndCollapseFrom(CharSequence sequence, char replacement) { // This implementation avoids unnecessary allocation. int len = sequence.length(); int first; int last; for (first = 0; first < len && matches(sequence.charAt(first)); first++) {} for (last = len - 1; last > first && matches(sequence.charAt(last)); last--) {} return (first == 0 && last == len - 1) ? collapseFrom(sequence, replacement) : finishCollapseFrom( sequence, first, last + 1, replacement, new StringBuilder(last + 1 - first), false); }
/** * 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. */ @CheckReturnValue public String trimAndCollapseFrom(CharSequence sequence, char replacement) { // This implementation avoids unnecessary allocation. int len = sequence.length(); int first; int last; for (first = 0; first < len && matches(sequence.charAt(first)); first++) {} for (last = len - 1; last > first && matches(sequence.charAt(last)); last--) {} return (first == 0 && last == len - 1) ? collapseFrom(sequence, replacement) : finishCollapseFrom( sequence, first, last + 1, replacement, new StringBuilder(last + 1 - first), false); }
/** * Returns a valid Java identifier name based on the input. * * Removes certain characterss (like apostrophe), replaces one or more invalid * characterss with {@literal _}, and prepends {@literal _} if the first character * is only valid as an identifier part (not start). * <p> * The result is usually unique to s, though this isn't guaranteed, for example if * all characters are invalid. For a unique identifier use {@link #makeValidUniqueJavaName(String)}. * * @see #makeValidUniqueJavaName(String) */ public static String makeValidJavaName(String s) { if (s==null) return "__null"; if (s.length()==0) return "__empty"; String name = IS_JAVA_IDENTIFIER_PART.negate().collapseFrom(CharMatcher.is('\'').removeFrom(s), '_'); if (!Character.isJavaIdentifierStart(s.charAt(0))) return "_" + name; return name; }
@Benchmark public int collapseFrom(int reps) { int result = 0; CharMatcher matcher = this.matcher; String teststring = this.teststring; for (int i = 0; i < reps; i++) { result += System.identityHashCode(matcher.collapseFrom(teststring, ' ')); } return result; }
public void testCollapse_any() { assertEquals("", CharMatcher.any().collapseFrom("", '_')); assertEquals("_", CharMatcher.any().collapseFrom("a", '_')); assertEquals("_", CharMatcher.any().collapseFrom("ab", '_')); assertEquals("_", CharMatcher.any().collapseFrom("abcd", '_')); }
private void doTestCollapseWithNoChange(String inout) { assertSame(inout, is('-').collapseFrom(inout, '_')); assertSame(inout, is('-').or(is('#')).collapseFrom(inout, '_')); assertSame(inout, isNot('x').collapseFrom(inout, '_')); assertSame(inout, is('x').negate().collapseFrom(inout, '_')); assertSame(inout, anyOf("-").collapseFrom(inout, '_')); assertSame(inout, anyOf("-#").collapseFrom(inout, '_')); assertSame(inout, anyOf("-#123").collapseFrom(inout, '_')); assertSame(inout, CharMatcher.none().collapseFrom(inout, '_')); }
private void doTestCollapse(String in, String out) { // Try a few different matchers which all match '-' and not 'x' // Try replacement chars that both do and do not change the value. for (char replacement : new char[] { '_', '-' }) { String expected = out.replace('_', replacement); assertEqualsSame(expected, in, is('-').collapseFrom(in, replacement)); assertEqualsSame(expected, in, is('-').collapseFrom(in, replacement)); assertEqualsSame(expected, in, is('-').or(is('#')).collapseFrom(in, replacement)); assertEqualsSame(expected, in, isNot('x').collapseFrom(in, replacement)); assertEqualsSame(expected, in, is('x').negate().collapseFrom(in, replacement)); assertEqualsSame(expected, in, anyOf("-").collapseFrom(in, replacement)); assertEqualsSame(expected, in, anyOf("-#").collapseFrom(in, replacement)); assertEqualsSame(expected, in, anyOf("-#123").collapseFrom(in, replacement)); } }