@@ -119,7 +119,7 @@ private void truncate(
119119 for (; ; ) {
120120
121121 // Find the next label start, if present.
122- final int labeledLineStartIndex = findLabeledLineStartIndex (srcWriter , startIndex , srcWriter . length () );
122+ final int labeledLineStartIndex = findLabeledLineStartIndex (srcWriter , startIndex );
123123 final int endIndex = labeledLineStartIndex >= 0 ? labeledLineStartIndex : srcWriter .length ();
124124
125125 // Copy up to the truncation point, if it matches.
@@ -195,26 +195,27 @@ private int findTruncationPointIndex(
195195 return -1 ;
196196 }
197197
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 ) {
199199 // Note that the index arithmetic in this method is not guarded.
200200 // That is, there are no `Math.addExact()` or `Math.subtractExact()` usages.
201201 // 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 ; ) {
203204
204205 // Find the next line start, if exists.
205- final int lineStartIndex = findLineStartIndex (buffer , bufferIndex , endIndex );
206+ final int lineStartIndex = findLineStartIndex (buffer , bufferIndex );
206207 if (lineStartIndex < 0 ) {
207208 break ;
208209 }
209210 bufferIndex = lineStartIndex ;
210211
211212 // Skip tabs.
212- while (bufferIndex < endIndex && '\t' == buffer .charAt (bufferIndex )) {
213+ while (bufferIndex < bufferLength && '\t' == buffer .charAt (bufferIndex )) {
213214 bufferIndex ++;
214215 }
215216
216217 // Search for the `Caused by: ` occurrence.
217- if (bufferIndex < (endIndex - 11 )
218+ if (bufferIndex < (bufferLength - 11 )
218219 && buffer .charAt (bufferIndex ) == 'C'
219220 && buffer .charAt (bufferIndex + 1 ) == 'a'
220221 && buffer .charAt (bufferIndex + 2 ) == 'u'
@@ -230,7 +231,7 @@ private static int findLabeledLineStartIndex(final CharSequence buffer, final in
230231 }
231232
232233 // Search for the `Suppressed: ` occurrence.
233- else if (bufferIndex < (endIndex - 12 )
234+ else if (bufferIndex < (bufferLength - 12 )
234235 && buffer .charAt (bufferIndex ) == 'S'
235236 && buffer .charAt (bufferIndex + 1 ) == 'u'
236237 && buffer .charAt (bufferIndex + 2 ) == 'p'
@@ -249,13 +250,11 @@ else if (bufferIndex < (endIndex - 12)
249250 return -1 ;
250251 }
251252
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 ;
257257 }
258- prevChar = buffer .charAt (i );
259258 }
260259 return -1 ;
261260 }
0 commit comments