A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/google/google-java-format/commit/b9fd8d2242869ea7c9efd10cbbe7278c4b6611b1 below:

Fix an off-by-one issue on the reflowing of string literals. · google/google-java-format@b9fd8d2 · GitHub

File tree Expand file treeCollapse file tree 7 files changed

+130

-8

lines changed

Filter options

Expand file treeCollapse file tree 7 files changed

+130

-8

lines changed Original file line number Diff line number Diff line change

@@ -239,9 +239,10 @@ static int hasEscapedNewlineAt(String input, int idx) {

239 239

* @param separator the line separator

240 240

* @param columnLimit the number of columns to wrap at

241 241

* @param startColumn the column position of the beginning of the original text

242 -

* @param trailing extra space to leave after the last line

243 -

* @param components the text to reflow

244 -

* @param first0 true if the text includes the beginning of its enclosing concat chain, i.e. a

242 +

* @param trailing extra space to leave after the last line, to accommodate a ; or )

243 +

* @param components the text to reflow. This is a list of “words” of a single literal. Its first

244 +

* and last quotes have been stripped

245 +

* @param first0 true if the text includes the beginning of its enclosing concat chain

245 246

*/

246 247

private static String reflow(

247 248

String separator,

@@ -251,18 +252,21 @@ private static String reflow(

251 252

ImmutableList<String> components,

252 253

boolean first0) {

253 254

// We have space between the start column and the limit to output the first line.

254 -

// Reserve two spaces for the quotes.

255 +

// Reserve two spaces for the start and end quotes.

255 256

int width = columnLimit - startColumn - 2;

256 257

Deque<String> input = new ArrayDeque<>(components);

257 258

List<String> lines = new ArrayList<>();

258 259

boolean first = first0;

259 260

while (!input.isEmpty()) {

260 261

int length = 0;

261 262

List<String> line = new ArrayList<>();

263 +

// If we know this is going to be the last line, then remove a bit of width to account for the

264 +

// trailing characters.

262 265

if (input.stream().mapToInt(String::length).sum() <= width) {

266 +

// This isn’t quite optimal, but arguably good enough. See b/179561701

263 267

width -= trailing;

264 268

}

265 -

while (!input.isEmpty() && (length <= 4 || (length + input.peekFirst().length()) < width)) {

269 +

while (!input.isEmpty() && (length <= 4 || (length + input.peekFirst().length()) <= width)) {

266 270

String text = input.removeFirst();

267 271

line.add(text);

268 272

length += text.length();

Original file line number Diff line number Diff line change

@@ -125,7 +125,9 @@ public FormatterIntegrationTest(String name, String input, String expected) {

125 125

@Test

126 126

public void format() {

127 127

try {

128 -

String output = new Formatter().formatSource(input);

128 +

Formatter formatter = new Formatter();

129 +

String output = formatter.formatSource(input);

130 +

output = StringWrapper.wrap(output, formatter);

129 131

assertEquals("bad output for " + name, expected, output);

130 132

} catch (FormatterException e) {

131 133

fail(String.format("Formatter crashed on %s: %s", name, e.getMessage()));

Original file line number Diff line number Diff line change

@@ -525,8 +525,8 @@ public void reflowLongStrings() throws Exception {

525 525

"class T {",

526 526

" String s =",

527 527

" \"one long incredibly unbroken sentence moving from topic to topic so that no one had"

528 -

+ " a\"",

529 -

" + \" chance to interrupt\";",

528 +

+ " a chance\"",

529 +

" + \" to interrupt\";",

530 530

"}",

531 531

"",

532 532

};

Original file line number Diff line number Diff line change

@@ -0,0 +1,8 @@

1 +

class B173808510 {

2 +

// b/173808510

3 +

@FlagSpec(

4 +

name = "myFlag",

5 +

help =

6 +

"areallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyloongword word1 word2")

7 +

Flag<Integer> dummy = null;

8 +

}

Original file line number Diff line number Diff line change

@@ -0,0 +1,9 @@

1 +

class B173808510 {

2 +

// b/173808510

3 +

@FlagSpec(

4 +

name = "myFlag",

5 +

help =

6 +

"areallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyloongword word1"

7 +

+ " word2")

8 +

Flag<Integer> dummy = null;

9 +

}

Original file line number Diff line number Diff line change

@@ -0,0 +1,44 @@

1 +

class LiteralReflow {

2 +

static class TestLineBreak {

3 +

String doesNotBreakAt100 = "A very long long long long long long long long long loong sentence";

4 +

String breaksAt101 = "A very long long long long long long long long long long loooong sentence";

5 +

}

6 + 7 +

static class TestReflowLimit {

8 +

String doesNotReflowAt100 =

9 +

"A very long long long long long long long long long long long long long looooong sentence";

10 +

String reflowsWhenLongerThan100 =

11 +

"A very long long long long long long long long long long long long long long long sentence";

12 +

}

13 + 14 +

static class TestReflowLocation {

15 +

String accommodatesWordsUpTo100 =

16 +

"A very long long long long long long long long long long long long long long long looooong sentence";

17 +

String breaksBeforeWordsReach101 =

18 +

"A very long long long long long long long long long long long long long long long loooooong sentence";

19 +

}

20 + 21 +

static class Test2LineReflowLimit {

22 +

String doesNotReflowEitherLinesAt100 =

23 +

"A very long long long long long long long long long long long long long looooong sentence. And a second very long long long long long long long long long long loong sentence";

24 +

String reflowsLastLineAt101 =

25 +

"A very long long long long long long long long long long long long long looooong sentence. And a second very long long long long long long long long long long looong sentence";

26 +

}

27 + 28 +

static class TestWithTrailingCharacters {

29 +

String fitsLastLineUpTo100WithTrailingCharacters =

30 +

f(

31 +

f(

32 +

"A very long long long long long long long long long long long long loong sentence. And a second very long long long long long long long long loong sentence"));

33 +

String reflowsLastLineToAccommodateTrailingCharacters =

34 +

f(

35 +

f(

36 +

"A very long long long long long long long long long long long long loong sentence. And a second very long long long long long long long long looong sentence"));

37 +

// Tests an off-by-one issue, but see b/179561701 for a similar issue that is not yet fixed

38 +

String doesNotOverTriggerLastLineReflow =

39 +

f(

40 +

f(

41 +

"A very long long long long long long long long long long long long loong sentence."

42 +

+ " And a second very loong sentence with trailing a a a a a a a a a a a a a a a"));

43 +

}

44 +

}

Original file line number Diff line number Diff line change

@@ -0,0 +1,55 @@

1 +

class LiteralReflow {

2 +

static class TestLineBreak {

3 +

String doesNotBreakAt100 = "A very long long long long long long long long long loong sentence";

4 +

String breaksAt101 =

5 +

"A very long long long long long long long long long long loooong sentence";

6 +

}

7 + 8 +

static class TestReflowLimit {

9 +

String doesNotReflowAt100 =

10 +

"A very long long long long long long long long long long long long long looooong sentence";

11 +

String reflowsWhenLongerThan100 =

12 +

"A very long long long long long long long long long long long long long long long"

13 +

+ " sentence";

14 +

}

15 + 16 +

static class TestReflowLocation {

17 +

String accommodatesWordsUpTo100 =

18 +

"A very long long long long long long long long long long long long long long long looooong"

19 +

+ " sentence";

20 +

String breaksBeforeWordsReach101 =

21 +

"A very long long long long long long long long long long long long long long long"

22 +

+ " loooooong sentence";

23 +

}

24 + 25 +

static class Test2LineReflowLimit {

26 +

String doesNotReflowEitherLinesAt100 =

27 +

"A very long long long long long long long long long long long long long looooong sentence."

28 +

+ " And a second very long long long long long long long long long long loong sentence";

29 +

String reflowsLastLineAt101 =

30 +

"A very long long long long long long long long long long long long long looooong sentence."

31 +

+ " And a second very long long long long long long long long long long looong"

32 +

+ " sentence";

33 +

}

34 + 35 +

static class TestWithTrailingCharacters {

36 +

String fitsLastLineUpTo100WithTrailingCharacters =

37 +

f(

38 +

f(

39 +

"A very long long long long long long long long long long long long loong sentence."

40 +

+ " And a second very long long long long long long long long loong sentence"));

41 +

String reflowsLastLineToAccommodateTrailingCharacters =

42 +

f(

43 +

f(

44 +

"A very long long long long long long long long long long long long loong sentence."

45 +

+ " And a second very long long long long long long long long looong"

46 +

+ " sentence"));

47 +

// Tests an off-by-one issue, but see b/179561701 for a similar issue that is not yet fixed

48 +

String doesNotOverTriggerLastLineReflow =

49 +

f(

50 +

f(

51 +

"A very long long long long long long long long long long long long loong sentence."

52 +

+ " And a second very loong sentence with trailing a a a a a a a a a a a a a a"

53 +

+ " a"));

54 +

}

55 +

}

You can’t perform that action at this time.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4