@@ -63,6 +63,16 @@ public enum FillMode {
63
63
FORCED
64
64
}
65
65
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
+
66
76
/** State for writing. */
67
77
public static final class State {
68
78
final int lastIndent ;
@@ -103,8 +113,7 @@ public String toString() {
103
113
private static final Range <Integer > EMPTY_RANGE = Range .closedOpen (-1 , -1 );
104
114
private static final DiscreteDomain <Integer > INTEGERS = DiscreteDomain .integers ();
105
115
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 );
108
117
109
118
// Memoized flat; not defined (and never computed) if contains forced breaks.
110
119
private final Supplier <String > flat = Suppliers .memoize (this ::computeFlat );
@@ -113,16 +122,16 @@ public String toString() {
113
122
private final Supplier <Range <Integer >> range = Suppliers .memoize (this ::computeRange );
114
123
115
124
/**
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}.
117
126
*
118
127
* @return the width
119
128
*/
120
- final float getWidth () {
129
+ final int getWidth () {
121
130
return width .get ();
122
131
}
123
132
124
133
/**
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}
126
135
* contains forced breaks.
127
136
*
128
137
* @return the flat-string value
@@ -143,9 +152,9 @@ final Range<Integer> range() {
143
152
/**
144
153
* Compute the {@code Doc}'s width.
145
154
*
146
- * @return the width, or {@code Float.POSITIVE_INFINITY} if it must be broken
155
+ * @return the width
147
156
*/
148
- abstract float computeWidth ();
157
+ abstract int computeWidth ();
149
158
150
159
/**
151
160
* Compute the {@code Doc}'s flat value. Not defined (and never called) if contains forced breaks.
@@ -202,12 +211,8 @@ void add(Doc doc) {
202
211
}
203
212
204
213
@ 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 );
211
216
}
212
217
213
218
@ Override
@@ -246,10 +251,10 @@ Range<Integer> computeRange() {
246
251
247
252
@ Override
248
253
public State computeBreaks (CommentsHelper commentsHelper , int maxWidth , State state ) {
249
- float thisWidth = getWidth ();
254
+ int thisWidth = getWidth ();
250
255
if (state .column + thisWidth <= maxWidth ) {
251
256
oneLine = true ;
252
- return state .withColumn (state .column + ( int ) thisWidth );
257
+ return state .withColumn (state .column + thisWidth );
253
258
}
254
259
State broken =
255
260
computeBroken (
@@ -295,8 +300,8 @@ private static State computeBreakAndSplit(
295
300
State state ,
296
301
Optional <Break > optBreakDoc ,
297
302
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 );
300
305
boolean shouldBreak =
301
306
(optBreakDoc .isPresent () && optBreakDoc .get ().fillMode == FillMode .UNIFIED )
302
307
|| state .mustBreak
@@ -348,12 +353,16 @@ private void writeFilled(Output output) {
348
353
* Get the width of a sequence of {@link Doc}s.
349
354
*
350
355
* @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
352
357
*/
353
- static float getWidth (List <Doc > docs ) {
354
- float width = 0.0F ;
358
+ static int getWidth (List <Doc > docs ) {
359
+ int width = 0 ;
355
360
for (Doc doc : docs ) {
356
361
width += doc .getWidth ();
362
+
363
+ if (width >= MAX_LINE_WIDTH ) {
364
+ return MAX_LINE_WIDTH ; // Paranoid overflow protection
365
+ }
357
366
}
358
367
return width ;
359
368
}
@@ -455,7 +464,7 @@ public void add(DocBuilder builder) {
455
464
}
456
465
457
466
@ Override
458
- float computeWidth () {
467
+ int computeWidth () {
459
468
return token .getTok ().length ();
460
469
}
461
470
@@ -512,8 +521,8 @@ public void add(DocBuilder builder) {
512
521
}
513
522
514
523
@ Override
515
- float computeWidth () {
516
- return 1.0F ;
524
+ int computeWidth () {
525
+ return 1 ;
517
526
}
518
527
519
528
@ Override
@@ -615,8 +624,8 @@ public void add(DocBuilder builder) {
615
624
}
616
625
617
626
@ Override
618
- float computeWidth () {
619
- return isForced () ? Float . POSITIVE_INFINITY : ( float ) flat .length ();
627
+ int computeWidth () {
628
+ return isForced () ? MAX_LINE_WIDTH : flat .length ();
620
629
}
621
630
622
631
@ Override
@@ -705,7 +714,7 @@ public void add(DocBuilder builder) {
705
714
}
706
715
707
716
@ Override
708
- float computeWidth () {
717
+ int computeWidth () {
709
718
int idx = Newlines .firstBreak (tok .getOriginalText ());
710
719
// only count the first line of multi-line block comments
711
720
if (tok .isComment ()) {
@@ -718,7 +727,7 @@ float computeWidth() {
718
727
return reformatParameterComment (tok ).map (String ::length ).orElse (tok .length ());
719
728
}
720
729
}
721
- return idx != -1 ? Float . POSITIVE_INFINITY : ( float ) tok .length ();
730
+ return idx != -1 ? MAX_LINE_WIDTH : tok .length ();
722
731
}
723
732
724
733
@ Override
0 commit comments