Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Stream;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
import javax.tools.DiagnosticListener;
Expand Down Expand Up @@ -316,19 +315,21 @@ private static ImmutableList<String> stringComponents(
}

static int hasEscapedWhitespaceAt(String input, int idx) {
return Stream.of("\\t")
.mapToInt(x -> input.startsWith(x, idx) ? x.length() : -1)
.filter(x -> x != -1)
.findFirst()
.orElse(-1);
if (input.startsWith("\\t", idx)) {
return 2;
}
return -1;
}

static int hasEscapedNewlineAt(String input, int idx) {
return Stream.of("\\r\\n", "\\r", "\\n")
.mapToInt(x -> input.startsWith(x, idx) ? x.length() : -1)
.filter(x -> x != -1)
.findFirst()
.orElse(-1);
int offset = 0;
if (input.startsWith("\\r", idx)) {
offset += 2;
}
if (input.startsWith("\\n", idx)) {
offset += 2;
}
return offset > 0 ? offset : -1;
}

/**
Expand Down Expand Up @@ -360,7 +361,7 @@ private static String reflow(
List<String> line = new ArrayList<>();
// If we know this is going to be the last line, then remove a bit of width to account for the
// trailing characters.
if (input.stream().mapToInt(String::length).sum() <= width) {
if (totalLengthLessThanOrEqual(input, width)) {
// This isn’t quite optimal, but arguably good enough. See b/179561701
width -= trailing;
}
Expand Down Expand Up @@ -391,6 +392,17 @@ private static String reflow(
"\""));
}

private static boolean totalLengthLessThanOrEqual(Iterable<String> input, int length) {
int total = 0;
for (String s : input) {
total += s.length();
if (total > length) {
return false;
}
}
return true;
}

/**
* Flattens the given binary expression tree, and extracts the subset that contains the given path
* and any adjacent nodes that are also string literals.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,71 @@ public static Collection<Object[]> parameters() {
"}"
},
},
{
{
"class T {", //
" String s = \"\\r\\rone\\rlong\\rincredibly\\runbroken\\rsentence\\rmoving\\rfrom\\r"
+ " topic\\rto\\r topic\\rso\\rthat\\rno-one\\rhad\\ra\\rchance\\rto\\rinterrupt\";",
"}"
},
{
"class T {",
" String s =",
" \"\\r\\r\"",
" + \"one\\r\"",
" + \"long\\r\"",
" + \"incredibly\\r\"",
" + \"unbroken\\r\"",
" + \"sentence\\r\"",
" + \"moving\\r\"",
" + \"from\\r\"",
" + \" topic\\r\"",
" + \"to\\r\"",
" + \" topic\\r\"",
" + \"so\\r\"",
" + \"that\\r\"",
" + \"no-one\\r\"",
" + \"had\\r\"",
" + \"a\\r\"",
" + \"chance\\r\"",
" + \"to\\r\"",
" + \"interrupt\";",
"}",
},
},
{
{
"class T {", //
" String s = \"\\r\\n\\r\\none\\r\\nlong\\r\\nincredibly\\r\\nunbroken\\r\\nsentence"
+ "\\r\\nmoving\\r\\nfrom\\r\\n topic\\r\\nto\\r\\n topic\\r\\nso\\r\\nthat\\r\\n"
+ "no-one\\r\\nhad\\r\\na\\r\\nchance\\r\\nto\\r\\ninterrupt\";",
"}"
},
{
"class T {",
" String s =",
" \"\\r\\n\\r\\n\"",
" + \"one\\r\\n\"",
" + \"long\\r\\n\"",
" + \"incredibly\\r\\n\"",
" + \"unbroken\\r\\n\"",
" + \"sentence\\r\\n\"",
" + \"moving\\r\\n\"",
" + \"from\\r\\n\"",
" + \" topic\\r\\n\"",
" + \"to\\r\\n\"",
" + \" topic\\r\\n\"",
" + \"so\\r\\n\"",
" + \"that\\r\\n\"",
" + \"no-one\\r\\n\"",
" + \"had\\r\\n\"",
" + \"a\\r\\n\"",
" + \"chance\\r\\n\"",
" + \"to\\r\\n\"",
" + \"interrupt\";",
"}",
},
},
};
return Arrays.stream(inputsAndOutputs)
.map(
Expand Down
Loading