Skip to content

Commit 74f18e7

Browse files
committed
Fix PMD AvoidBranchingStatementAsLastInLoop in StrBuilder
1 parent a84093d commit 74f18e7

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ The <action> type attribute can be add,update,fix,remove.
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>
5757
<action type="fix" dev="ggregory" due-to="Gary Gregory">Fix PMD AvoidBranchingStatementAsLastInLoop in TextStringBuilder.</action>
58+
<action type="fix" dev="ggregory" due-to="Gary Gregory">Fix PMD AvoidBranchingStatementAsLastInLoop in StrBuilder.</action>
5859
<!-- ADD -->
5960
<action type="add" dev="ggregory" due-to="Gary Gregory">Interface StringLookup now extends UnaryOperator&lt;String&gt;.</action>
6061
<action type="add" dev="ggregory" due-to="Gary Gregory">Interface TextRandomProvider extends IntUnaryOperator.</action>

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

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1946,9 +1946,9 @@ public int indexOf(final String str) {
19461946
* @return The first index of the string, or -1 if not found
19471947
*/
19481948
public int indexOf(final String str, int startIndex) {
1949-
startIndex = Math.max(startIndex, 0);
1949+
startIndex = Math.max(0, startIndex);
19501950
if (str == null || startIndex >= size) {
1951-
return -1;
1951+
return StringUtils.INDEX_NOT_FOUND;
19521952
}
19531953
final int strLen = str.length();
19541954
if (strLen == 1) {
@@ -1958,19 +1958,20 @@ public int indexOf(final String str, int startIndex) {
19581958
return startIndex;
19591959
}
19601960
if (strLen > size) {
1961-
return -1;
1961+
return StringUtils.INDEX_NOT_FOUND;
19621962
}
19631963
final char[] thisBuf = buffer;
1964-
final int len = size - strLen + 1;
1965-
outer: for (int i = startIndex; i < len; i++) {
1966-
for (int j = 0; j < strLen; j++) {
1967-
if (str.charAt(j) != thisBuf[i + j]) {
1968-
continue outer;
1969-
}
1964+
final int searchLen = size - strLen + 1;
1965+
for (int i = startIndex; i < searchLen; i++) {
1966+
boolean found = true;
1967+
for (int j = 0; j < strLen && found; j++) {
1968+
found = str.charAt(j) == thisBuf[i + j];
1969+
}
1970+
if (found) {
1971+
return i;
19701972
}
1971-
return i;
19721973
}
1973-
return -1;
1974+
return StringUtils.INDEX_NOT_FOUND;
19741975
}
19751976

19761977
/**
@@ -2282,27 +2283,28 @@ public int lastIndexOf(final String str) {
22822283
public int lastIndexOf(final String str, int startIndex) {
22832284
startIndex = startIndex >= size ? size - 1 : startIndex;
22842285
if (str == null || startIndex < 0) {
2285-
return -1;
2286+
return StringUtils.INDEX_NOT_FOUND;
22862287
}
22872288
final int strLen = str.length();
2288-
if (strLen > 0 && strLen <= size) {
2289-
if (strLen == 1) {
2290-
return lastIndexOf(str.charAt(0), startIndex);
2289+
if (strLen == 0) {
2290+
return startIndex;
2291+
}
2292+
if (strLen > size) {
2293+
return StringUtils.INDEX_NOT_FOUND;
2294+
}
2295+
if (strLen == 1) {
2296+
return lastIndexOf(str.charAt(0), startIndex);
2297+
}
2298+
for (int i = startIndex - strLen + 1; i >= 0; i--) {
2299+
boolean found = true;
2300+
for (int j = 0; j < strLen && found; j++) {
2301+
found = str.charAt(j) == buffer[i + j];
22912302
}
2292-
2293-
outer: for (int i = startIndex - strLen + 1; i >= 0; i--) {
2294-
for (int j = 0; j < strLen; j++) {
2295-
if (str.charAt(j) != buffer[i + j]) {
2296-
continue outer;
2297-
}
2298-
}
2303+
if (found) {
22992304
return i;
23002305
}
2301-
2302-
} else if (strLen == 0) {
2303-
return startIndex;
23042306
}
2305-
return -1;
2307+
return StringUtils.INDEX_NOT_FOUND;
23062308
}
23072309

23082310
/**

0 commit comments

Comments
 (0)