Skip to content

Commit fd55d26

Browse files
committed
Simplify line start matching
1 parent c6f43ad commit fd55d26

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/StackTraceStringResolver.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ private void truncate(
121121
for (; ; ) {
122122

123123
// Find the next label start, if present.
124-
final int labeledLineStartIndex = findLabeledLineStartIndex(srcWriter, startIndex, srcWriter.length());
124+
final int labeledLineStartIndex = findLabeledLineStartIndex(srcWriter, startIndex);
125125
final int endIndex = labeledLineStartIndex >= 0 ? labeledLineStartIndex : srcWriter.length();
126126

127127
// Copy up to the truncation point, if it matches.
@@ -197,26 +197,27 @@ private int findTruncationPointIndex(
197197
return -1;
198198
}
199199

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) {
201201
// Note that the index arithmetic in this method is not guarded.
202202
// That is, there are no `Math.addExact()` or `Math.subtractExact()` usages.
203203
// 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; ) {
205206

206207
// Find the next line start, if exists.
207-
final int lineStartIndex = findLineStartIndex(buffer, bufferIndex, endIndex);
208+
final int lineStartIndex = findLineStartIndex(buffer, bufferIndex);
208209
if (lineStartIndex < 0) {
209210
break;
210211
}
211212
bufferIndex = lineStartIndex;
212213

213214
// Skip tabs.
214-
while (bufferIndex < endIndex && '\t' == buffer.charAt(bufferIndex)) {
215+
while (bufferIndex < bufferLength && '\t' == buffer.charAt(bufferIndex)) {
215216
bufferIndex++;
216217
}
217218

218219
// Search for the `Caused by: ` occurrence.
219-
if (bufferIndex < (endIndex - 11)
220+
if (bufferIndex < (bufferLength - 11)
220221
&& buffer.charAt(bufferIndex) == 'C'
221222
&& buffer.charAt(bufferIndex + 1) == 'a'
222223
&& buffer.charAt(bufferIndex + 2) == 'u'
@@ -232,7 +233,7 @@ private static int findLabeledLineStartIndex(final CharSequence buffer, final in
232233
}
233234

234235
// Search for the `Suppressed: ` occurrence.
235-
else if (bufferIndex < (endIndex - 12)
236+
else if (bufferIndex < (bufferLength - 12)
236237
&& buffer.charAt(bufferIndex) == 'S'
237238
&& buffer.charAt(bufferIndex + 1) == 'u'
238239
&& buffer.charAt(bufferIndex + 2) == 'p'
@@ -251,16 +252,13 @@ else if (bufferIndex < (endIndex - 12)
251252
return -1;
252253
}
253254

254-
private static int findLineStartIndex(final CharSequence buffer, final int startIndex, final int endIndex) {
255-
char prevChar = '-';
256-
int i = startIndex;
257-
for (; i < endIndex; i++) {
258-
if (prevChar == '\n') {
259-
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;
260259
}
261-
prevChar = buffer.charAt(i);
262260
}
263-
return prevChar == '\n' ? i : -1;
261+
return -1;
264262
}
265263

266264
private static int findMatchingIndex(

0 commit comments

Comments
 (0)