Skip to content

Commit a84093d

Browse files
committed
Fix PMD AvoidBranchingStatementAsLastInLoop in TextStringBuilder
1 parent 477a74f commit a84093d

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ The <action> type attribute can be add,update,fix,remove.
5454
<action type="fix" dev="ggregory" due-to="Gary Gregory">Fix PMD UnnecessaryFullyQualifiedName in StringSubstitutor.</action>
5555
<action type="fix" dev="ggregory" due-to="Gary Gregory">Fix PMD UnnecessaryFullyQualifiedName in StrSubstitutor.</action>
5656
<action type="fix" dev="ggregory" due-to="Gary Gregory">Fix PMD UnnecessaryFullyQualifiedName in AlphabetConverter.</action>
57+
<action type="fix" dev="ggregory" due-to="Gary Gregory">Fix PMD AvoidBranchingStatementAsLastInLoop in TextStringBuilder.</action>
5758
<!-- ADD -->
5859
<action type="add" dev="ggregory" due-to="Gary Gregory">Interface StringLookup now extends UnaryOperator&lt;String&gt;.</action>
5960
<action type="add" dev="ggregory" due-to="Gary Gregory">Interface TextRandomProvider extends IntUnaryOperator.</action>

src/main/java/org/apache/commons/text/TextStringBuilder.java

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -635,8 +635,8 @@ public TextStringBuilder append(final long value) {
635635
/**
636636
* Appends an object to this string builder. Appending null will call {@link #appendNull()}.
637637
*
638-
* @param obj the object to append
639-
* @return this, to enable chaining
638+
* @param obj the object to append.
639+
* @return this, to enable chaining.
640640
*/
641641
public TextStringBuilder append(final Object obj) {
642642
if (obj == null) {
@@ -2128,14 +2128,15 @@ public int indexOf(final String str, int startIndex) {
21282128
return StringUtils.INDEX_NOT_FOUND;
21292129
}
21302130
final char[] thisBuf = buffer;
2131-
final int len = size - strLen + 1;
2132-
outer: for (int i = startIndex; i < len; i++) {
2133-
for (int j = 0; j < strLen; j++) {
2134-
if (str.charAt(j) != thisBuf[i + j]) {
2135-
continue outer;
2136-
}
2131+
final int searchLen = size - strLen + 1;
2132+
for (int i = startIndex; i < searchLen; i++) {
2133+
boolean found = true;
2134+
for (int j = 0; j < strLen && found; j++) {
2135+
found = str.charAt(j) == thisBuf[i + j];
2136+
}
2137+
if (found) {
2138+
return i;
21372139
}
2138-
return i;
21392140
}
21402141
return StringUtils.INDEX_NOT_FOUND;
21412142
}
@@ -2457,22 +2458,23 @@ public int lastIndexOf(final String str, int startIndex) {
24572458
return StringUtils.INDEX_NOT_FOUND;
24582459
}
24592460
final int strLen = str.length();
2460-
if (strLen > 0 && strLen <= size) {
2461-
if (strLen == 1) {
2462-
return lastIndexOf(str.charAt(0), startIndex);
2461+
if (strLen == 0) {
2462+
return startIndex;
2463+
}
2464+
if (strLen > size) {
2465+
return StringUtils.INDEX_NOT_FOUND;
2466+
}
2467+
if (strLen == 1) {
2468+
return lastIndexOf(str.charAt(0), startIndex);
2469+
}
2470+
for (int i = startIndex - strLen + 1; i >= 0; i--) {
2471+
boolean found = true;
2472+
for (int j = 0; j < strLen && found; j++) {
2473+
found = str.charAt(j) == buffer[i + j];
24632474
}
2464-
2465-
outer: for (int i = startIndex - strLen + 1; i >= 0; i--) {
2466-
for (int j = 0; j < strLen; j++) {
2467-
if (str.charAt(j) != buffer[i + j]) {
2468-
continue outer;
2469-
}
2470-
}
2475+
if (found) {
24712476
return i;
24722477
}
2473-
2474-
} else if (strLen == 0) {
2475-
return startIndex;
24762478
}
24772479
return StringUtils.INDEX_NOT_FOUND;
24782480
}

0 commit comments

Comments
 (0)