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