private void assertResultAndMessages(BuildResult result, TaskOutcome outcome, String... messages) { String expectedToStartWith = StringPrinter.buildStringFromLines(messages).trim(); int numNewlines = CharMatcher.is('\n').countIn(expectedToStartWith); List<String> actualLines = Splitter.on('\n').splitToList(LineEnding.toUnix(result.getOutput())); String actualStart = String.join("\n", actualLines.subList(0, numNewlines + 1)); Assertions.assertThat(actualStart).isEqualTo(expectedToStartWith); Assertions.assertThat(result.tasks(outcome).size() + result.tasks(TaskOutcome.UP_TO_DATE).size()) .isEqualTo(result.getTasks().size()); } }
/** * Returns a git-style diff between the two unix strings. * * Output has no trailing newlines. * * Boolean args determine whether whitespace or line endings will be visible. */ private static String diffWhitespaceLineEndings(String dirty, String clean, boolean whitespace, boolean lineEndings) throws IOException { dirty = visibleWhitespaceLineEndings(dirty, whitespace, lineEndings); clean = visibleWhitespaceLineEndings(clean, whitespace, lineEndings); RawText a = new RawText(dirty.getBytes(StandardCharsets.UTF_8)); RawText b = new RawText(clean.getBytes(StandardCharsets.UTF_8)); EditList edits = new EditList(); edits.addAll(MyersDiff.INSTANCE.diff(RawTextComparator.DEFAULT, a, b)); ByteArrayOutputStream out = new ByteArrayOutputStream(); try (DiffFormatter formatter = new DiffFormatter(out)) { formatter.format(edits, a, b); } String formatted = out.toString(StandardCharsets.UTF_8.name()); // we don't need the diff to show this, since we display newlines ourselves formatted = formatted.replace("\\ No newline at end of file\n", ""); return NEWLINE_MATCHER.trimTrailingFrom(formatted); }
/** * 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"}. */ @CheckReturnValue public String retainFrom(CharSequence sequence) { return negate().removeFrom(sequence); }
@Override public boolean matches(char c) { return !original.matches(c); }
/** * Helper method for {@link #precomputedInternal} that doesn't test if the negation is cheaper. */ @GwtIncompatible("java.util.BitSet") private static CharMatcher precomputedPositive( int totalCharacters, BitSet table, String description) { switch (totalCharacters) { case 0: return none(); case 1: return is((char) table.nextSetBit(0)); case 2: char c1 = (char) table.nextSetBit(0); char c2 = (char) table.nextSetBit(c1 + 1); return isEither(c1, c2); default: return isSmall(totalCharacters, table.length()) ? SmallCharMatcher.from(table, description) : new BitSetMatcher(table, description); } }
/** * Returns a {@code char} matcher that matches any character present in the given character * sequence. */ public static CharMatcher anyOf(final CharSequence sequence) { switch (sequence.length()) { case 0: return none(); case 1: return is(sequence.charAt(0)); case 2: return isEither(sequence.charAt(0), sequence.charAt(1)); default: // TODO(lowasser): is it potentially worth just going ahead and building a precomputed // matcher? return new AnyOf(sequence); } }
/** * 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 = 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); }
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 CharMatcher padding() { return (paddingChar == null) ? CharMatcher.NONE : CharMatcher.is(paddingChar.charValue()); }
SeparatedBaseEncoding(BaseEncoding delegate, String separator, int afterEveryChars) { this.delegate = checkNotNull(delegate); this.separator = checkNotNull(separator); this.afterEveryChars = afterEveryChars; checkArgument( afterEveryChars > 0, "Cannot add a separator after every %s chars", afterEveryChars); this.separatorChars = CharMatcher.anyOf(separator).precomputed(); }
for (int i = 0; i < len; i++) { char c = sequence.charAt(i); if (matches(c)) { if (c == replacement && (i == len - 1 || !matches(sequence.charAt(i + 1)))) { return finishCollapseFrom(sequence, i + 1, len, replacement, builder, true);
@Override public BaseEncoding withSeparator(String separator, int afterEveryChars) { checkArgument(padding().or(alphabet).matchesNoneOf(separator), "Separator (%s) cannot contain alphabet or padding characters", separator); return new SeparatedBaseEncoding(this, separator, afterEveryChars); }
@Override public boolean matchesAllOf(CharSequence sequence) { return original.matchesNoneOf(sequence); }
@Override public int countIn(CharSequence sequence) { return sequence.length() - original.countIn(sequence); }
@Override int decodeTo(byte[] target, CharSequence chars) throws DecodingException { return delegate.decodeTo(target, separatorChars.removeFrom(chars)); }
@Override public boolean matches(char c) { return CharMatcher.ASCII.matches(c) && decodabet[c] != -1; }
/** * Returns a splitter that uses the given single-character separator. For * example, {@code Splitter.on(',').split("foo,,bar")} returns an iterable * containing {@code ["foo", "", "bar"]}. * * @param separator the character to recognize as a separator * @return a splitter, with default settings, that recognizes that separator */ @CheckReturnValue public static Splitter on(char separator) { return on(CharMatcher.is(separator)); }