@@ -119,7 +119,7 @@ private void truncate(
119
119
for (; ; ) {
120
120
121
121
// Find the next label start, if present.
122
- final int labeledLineStartIndex = findLabeledLineStartIndex (srcWriter , startIndex , srcWriter . length () );
122
+ final int labeledLineStartIndex = findLabeledLineStartIndex (srcWriter , startIndex );
123
123
final int endIndex = labeledLineStartIndex >= 0 ? labeledLineStartIndex : srcWriter .length ();
124
124
125
125
// Copy up to the truncation point, if it matches.
@@ -195,26 +195,27 @@ private int findTruncationPointIndex(
195
195
return -1 ;
196
196
}
197
197
198
- private static int findLabeledLineStartIndex (final CharSequence buffer , final int startIndex , final int endIndex ) {
198
+ private static int findLabeledLineStartIndex (final CharSequence buffer , final int startIndex ) {
199
199
// Note that the index arithmetic in this method is not guarded.
200
200
// That is, there are no `Math.addExact()` or `Math.subtractExact()` usages.
201
201
// Since we know a priori that we are already operating within buffer limits.
202
- for (int bufferIndex = startIndex ; bufferIndex < endIndex ; ) {
202
+ final int bufferLength = buffer .length ();
203
+ for (int bufferIndex = startIndex ; bufferIndex < bufferLength ; ) {
203
204
204
205
// Find the next line start, if exists.
205
- final int lineStartIndex = findLineStartIndex (buffer , bufferIndex , endIndex );
206
+ final int lineStartIndex = findLineStartIndex (buffer , bufferIndex );
206
207
if (lineStartIndex < 0 ) {
207
208
break ;
208
209
}
209
210
bufferIndex = lineStartIndex ;
210
211
211
212
// Skip tabs.
212
- while (bufferIndex < endIndex && '\t' == buffer .charAt (bufferIndex )) {
213
+ while (bufferIndex < bufferLength && '\t' == buffer .charAt (bufferIndex )) {
213
214
bufferIndex ++;
214
215
}
215
216
216
217
// Search for the `Caused by: ` occurrence.
217
- if (bufferIndex < (endIndex - 11 )
218
+ if (bufferIndex < (bufferLength - 11 )
218
219
&& buffer .charAt (bufferIndex ) == 'C'
219
220
&& buffer .charAt (bufferIndex + 1 ) == 'a'
220
221
&& buffer .charAt (bufferIndex + 2 ) == 'u'
@@ -230,7 +231,7 @@ private static int findLabeledLineStartIndex(final CharSequence buffer, final in
230
231
}
231
232
232
233
// Search for the `Suppressed: ` occurrence.
233
- else if (bufferIndex < (endIndex - 12 )
234
+ else if (bufferIndex < (bufferLength - 12 )
234
235
&& buffer .charAt (bufferIndex ) == 'S'
235
236
&& buffer .charAt (bufferIndex + 1 ) == 'u'
236
237
&& buffer .charAt (bufferIndex + 2 ) == 'p'
@@ -249,13 +250,11 @@ else if (bufferIndex < (endIndex - 12)
249
250
return -1 ;
250
251
}
251
252
252
- private static int findLineStartIndex (final CharSequence buffer , final int startIndex , final int endIndex ) {
253
- char prevChar = '-' ;
254
- for (int i = startIndex ; i <= endIndex ; i ++) {
255
- if (prevChar == '\n' ) {
256
- return i ;
253
+ private static int findLineStartIndex (final CharSequence buffer , final int startIndex ) {
254
+ for (int bufferIndex = startIndex ; bufferIndex < buffer .length (); bufferIndex ++) {
255
+ if (buffer .charAt (bufferIndex ) == '\n' && (bufferIndex + 1 ) < buffer .length ()) {
256
+ return bufferIndex + 1 ;
257
257
}
258
- prevChar = buffer .charAt (i );
259
258
}
260
259
return -1 ;
261
260
}
0 commit comments