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/86338266807cb8d78082179733b31a8fd0be972c below:

Optimize string wrapping · google/google-java-format@8633826 · GitHub

File tree Expand file treeCollapse file tree 2 files changed

+89

-12

lines changed

Filter options

Expand file treeCollapse file tree 2 files changed

+89

-12

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

@@ -53,7 +53,6 @@

53 53

import java.util.List;

54 54

import java.util.Map;

55 55

import java.util.concurrent.atomic.AtomicBoolean;

56 -

import java.util.stream.Stream;

57 56

import javax.tools.Diagnostic;

58 57

import javax.tools.DiagnosticCollector;

59 58

import javax.tools.DiagnosticListener;

@@ -316,19 +315,21 @@ private static ImmutableList<String> stringComponents(

316 315

}

317 316 318 317

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;

324 322

}

325 323 326 324

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;

332 333

}

333 334 334 335

/**

@@ -360,7 +361,7 @@ private static String reflow(

360 361

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

361 362

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

362 363

// trailing characters.

363 -

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

364 +

if (totalLengthLessThanOrEqual(input, width)) {

364 365

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

365 366

width -= trailing;

366 367

}

@@ -391,6 +392,17 @@ private static String reflow(

391 392

"\""));

392 393

}

393 394 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 + 394 406

/**

395 407

* Flattens the given binary expression tree, and extracts the subset that contains the given path

396 408

* and any adjacent nodes that are also string literals.

Original file line number Diff line number Diff line change

@@ -365,6 +365,71 @@ public static Collection<Object[]> parameters() {

365 365

"}"

366 366

},

367 367

},

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 +

},

368 433

};

369 434

return Arrays.stream(inputsAndOutputs)

370 435

.map(

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