Skip to content

Commit 3167818

Browse files
nreid260google-java-format Team
authored and
google-java-format Team
committed
Stop using floats to represent line widths, which must be integers.
Float was used so that forced breaks could be modeled with POSITIVE_INFINISTY, but an arbitrary large int works just as well. PiperOrigin-RevId: 620395732
1 parent 3ee6e2a commit 3167818

File tree

1 file changed

+36
-27
lines changed
  • core/src/main/java/com/google/googlejavaformat

1 file changed

+36
-27
lines changed

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

+36-27
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ public enum FillMode {
6363
FORCED
6464
}
6565

66+
/**
67+
* The maximum supported line width.
68+
*
69+
* <p>This can be used as a sentinel/threshold for {@code Doc}s that break unconditionally.
70+
*
71+
* <p>The value was selected to be obviously too large for any practical line, but small enough to
72+
* prevent accidental overflow.
73+
*/
74+
public static final int MAX_LINE_WIDTH = 1000;
75+
6676
/** State for writing. */
6777
public static final class State {
6878
final int lastIndent;
@@ -103,8 +113,7 @@ public String toString() {
103113
private static final Range<Integer> EMPTY_RANGE = Range.closedOpen(-1, -1);
104114
private static final DiscreteDomain<Integer> INTEGERS = DiscreteDomain.integers();
105115

106-
// Memoized width; Float.POSITIVE_INFINITY if contains forced breaks.
107-
private final Supplier<Float> width = Suppliers.memoize(this::computeWidth);
116+
private final Supplier<Integer> width = Suppliers.memoize(this::computeWidth);
108117

109118
// Memoized flat; not defined (and never computed) if contains forced breaks.
110119
private final Supplier<String> flat = Suppliers.memoize(this::computeFlat);
@@ -113,16 +122,16 @@ public String toString() {
113122
private final Supplier<Range<Integer>> range = Suppliers.memoize(this::computeRange);
114123

115124
/**
116-
* Return the width of a {@code Doc}, or {@code Float.POSITIVE_INFINITY} if it must be broken.
125+
* Return the width of a {@code Doc}.
117126
*
118127
* @return the width
119128
*/
120-
final float getWidth() {
129+
final int getWidth() {
121130
return width.get();
122131
}
123132

124133
/**
125-
* Return a {@code Doc}'s flat-string value; not defined (and never called) if the (@code Doc}
134+
* Return a {@code Doc}'s flat-string value; not defined (and never called) if the {@code Doc}
126135
* contains forced breaks.
127136
*
128137
* @return the flat-string value
@@ -143,9 +152,9 @@ final Range<Integer> range() {
143152
/**
144153
* Compute the {@code Doc}'s width.
145154
*
146-
* @return the width, or {@code Float.POSITIVE_INFINITY} if it must be broken
155+
* @return the width
147156
*/
148-
abstract float computeWidth();
157+
abstract int computeWidth();
149158

150159
/**
151160
* Compute the {@code Doc}'s flat value. Not defined (and never called) if contains forced breaks.
@@ -202,12 +211,8 @@ void add(Doc doc) {
202211
}
203212

204213
@Override
205-
float computeWidth() {
206-
float thisWidth = 0.0F;
207-
for (Doc doc : docs) {
208-
thisWidth += doc.getWidth();
209-
}
210-
return thisWidth;
214+
int computeWidth() {
215+
return getWidth(docs);
211216
}
212217

213218
@Override
@@ -246,10 +251,10 @@ Range<Integer> computeRange() {
246251

247252
@Override
248253
public State computeBreaks(CommentsHelper commentsHelper, int maxWidth, State state) {
249-
float thisWidth = getWidth();
254+
int thisWidth = getWidth();
250255
if (state.column + thisWidth <= maxWidth) {
251256
oneLine = true;
252-
return state.withColumn(state.column + (int) thisWidth);
257+
return state.withColumn(state.column + thisWidth);
253258
}
254259
State broken =
255260
computeBroken(
@@ -295,8 +300,8 @@ private static State computeBreakAndSplit(
295300
State state,
296301
Optional<Break> optBreakDoc,
297302
List<Doc> split) {
298-
float breakWidth = optBreakDoc.isPresent() ? optBreakDoc.get().getWidth() : 0.0F;
299-
float splitWidth = getWidth(split);
303+
int breakWidth = optBreakDoc.isPresent() ? optBreakDoc.get().getWidth() : 0;
304+
int splitWidth = getWidth(split);
300305
boolean shouldBreak =
301306
(optBreakDoc.isPresent() && optBreakDoc.get().fillMode == FillMode.UNIFIED)
302307
|| state.mustBreak
@@ -348,12 +353,16 @@ private void writeFilled(Output output) {
348353
* Get the width of a sequence of {@link Doc}s.
349354
*
350355
* @param docs the {@link Doc}s
351-
* @return the width, or {@code Float.POSITIVE_INFINITY} if any {@link Doc} must be broken
356+
* @return the width
352357
*/
353-
static float getWidth(List<Doc> docs) {
354-
float width = 0.0F;
358+
static int getWidth(List<Doc> docs) {
359+
int width = 0;
355360
for (Doc doc : docs) {
356361
width += doc.getWidth();
362+
363+
if (width >= MAX_LINE_WIDTH) {
364+
return MAX_LINE_WIDTH; // Paranoid overflow protection
365+
}
357366
}
358367
return width;
359368
}
@@ -455,7 +464,7 @@ public void add(DocBuilder builder) {
455464
}
456465

457466
@Override
458-
float computeWidth() {
467+
int computeWidth() {
459468
return token.getTok().length();
460469
}
461470

@@ -512,8 +521,8 @@ public void add(DocBuilder builder) {
512521
}
513522

514523
@Override
515-
float computeWidth() {
516-
return 1.0F;
524+
int computeWidth() {
525+
return 1;
517526
}
518527

519528
@Override
@@ -615,8 +624,8 @@ public void add(DocBuilder builder) {
615624
}
616625

617626
@Override
618-
float computeWidth() {
619-
return isForced() ? Float.POSITIVE_INFINITY : (float) flat.length();
627+
int computeWidth() {
628+
return isForced() ? MAX_LINE_WIDTH : flat.length();
620629
}
621630

622631
@Override
@@ -705,7 +714,7 @@ public void add(DocBuilder builder) {
705714
}
706715

707716
@Override
708-
float computeWidth() {
717+
int computeWidth() {
709718
int idx = Newlines.firstBreak(tok.getOriginalText());
710719
// only count the first line of multi-line block comments
711720
if (tok.isComment()) {
@@ -718,7 +727,7 @@ float computeWidth() {
718727
return reformatParameterComment(tok).map(String::length).orElse(tok.length());
719728
}
720729
}
721-
return idx != -1 ? Float.POSITIVE_INFINITY : (float) tok.length();
730+
return idx != -1 ? MAX_LINE_WIDTH : tok.length();
722731
}
723732

724733
@Override

0 commit comments

Comments
 (0)