Skip to content

Commit 8633826

Browse files
cushongoogle-java-format Team
authored and
google-java-format Team
committed
Optimize string wrapping
PiperOrigin-RevId: 744065507
1 parent aee16ed commit 8633826

File tree

2 files changed

+89
-12
lines changed

2 files changed

+89
-12
lines changed

core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
import java.util.List;
5454
import java.util.Map;
5555
import java.util.concurrent.atomic.AtomicBoolean;
56-
import java.util.stream.Stream;
5756
import javax.tools.Diagnostic;
5857
import javax.tools.DiagnosticCollector;
5958
import javax.tools.DiagnosticListener;
@@ -316,19 +315,21 @@ private static ImmutableList<String> stringComponents(
316315
}
317316

318317
static int hasEscapedWhitespaceAt(String input, int idx) {
319-
return Stream.of("\\t")
320-
.mapToInt(x -> input.startsWith(x, idx) ? x.length() : -1)
321-
.filter(x -> x != -1)
322-
.findFirst()
323-
.orElse(-1);
318+
if (input.startsWith("\\t", idx)) {
319+
return 2;
320+
}
321+
return -1;
324322
}
325323

326324
static int hasEscapedNewlineAt(String input, int idx) {
327-
return Stream.of("\\r\\n", "\\r", "\\n")
328-
.mapToInt(x -> input.startsWith(x, idx) ? x.length() : -1)
329-
.filter(x -> x != -1)
330-
.findFirst()
331-
.orElse(-1);
325+
int offset = 0;
326+
if (input.startsWith("\\r", idx)) {
327+
offset += 2;
328+
}
329+
if (input.startsWith("\\n", idx)) {
330+
offset += 2;
331+
}
332+
return offset > 0 ? offset : -1;
332333
}
333334

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

395+
private static boolean totalLengthLessThanOrEqual(Iterable<String> input, int length) {
396+
int total = 0;
397+
for (String s : input) {
398+
total += s.length();
399+
if (total > length) {
400+
return false;
401+
}
402+
}
403+
return true;
404+
}
405+
394406
/**
395407
* Flattens the given binary expression tree, and extracts the subset that contains the given path
396408
* and any adjacent nodes that are also string literals.

core/src/test/java/com/google/googlejavaformat/java/StringWrapperIntegrationTest.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,71 @@ public static Collection<Object[]> parameters() {
365365
"}"
366366
},
367367
},
368+
{
369+
{
370+
"class T {", //
371+
" String s = \"\\r\\rone\\rlong\\rincredibly\\runbroken\\rsentence\\rmoving\\rfrom\\r"
372+
+ " topic\\rto\\r topic\\rso\\rthat\\rno-one\\rhad\\ra\\rchance\\rto\\rinterrupt\";",
373+
"}"
374+
},
375+
{
376+
"class T {",
377+
" String s =",
378+
" \"\\r\\r\"",
379+
" + \"one\\r\"",
380+
" + \"long\\r\"",
381+
" + \"incredibly\\r\"",
382+
" + \"unbroken\\r\"",
383+
" + \"sentence\\r\"",
384+
" + \"moving\\r\"",
385+
" + \"from\\r\"",
386+
" + \" topic\\r\"",
387+
" + \"to\\r\"",
388+
" + \" topic\\r\"",
389+
" + \"so\\r\"",
390+
" + \"that\\r\"",
391+
" + \"no-one\\r\"",
392+
" + \"had\\r\"",
393+
" + \"a\\r\"",
394+
" + \"chance\\r\"",
395+
" + \"to\\r\"",
396+
" + \"interrupt\";",
397+
"}",
398+
},
399+
},
400+
{
401+
{
402+
"class T {", //
403+
" String s = \"\\r\\n\\r\\none\\r\\nlong\\r\\nincredibly\\r\\nunbroken\\r\\nsentence"
404+
+ "\\r\\nmoving\\r\\nfrom\\r\\n topic\\r\\nto\\r\\n topic\\r\\nso\\r\\nthat\\r\\n"
405+
+ "no-one\\r\\nhad\\r\\na\\r\\nchance\\r\\nto\\r\\ninterrupt\";",
406+
"}"
407+
},
408+
{
409+
"class T {",
410+
" String s =",
411+
" \"\\r\\n\\r\\n\"",
412+
" + \"one\\r\\n\"",
413+
" + \"long\\r\\n\"",
414+
" + \"incredibly\\r\\n\"",
415+
" + \"unbroken\\r\\n\"",
416+
" + \"sentence\\r\\n\"",
417+
" + \"moving\\r\\n\"",
418+
" + \"from\\r\\n\"",
419+
" + \" topic\\r\\n\"",
420+
" + \"to\\r\\n\"",
421+
" + \" topic\\r\\n\"",
422+
" + \"so\\r\\n\"",
423+
" + \"that\\r\\n\"",
424+
" + \"no-one\\r\\n\"",
425+
" + \"had\\r\\n\"",
426+
" + \"a\\r\\n\"",
427+
" + \"chance\\r\\n\"",
428+
" + \"to\\r\\n\"",
429+
" + \"interrupt\";",
430+
"}",
431+
},
432+
},
368433
};
369434
return Arrays.stream(inputsAndOutputs)
370435
.map(

0 commit comments

Comments
 (0)