This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-text.git

commit 74f18e737332f7e5343fe7ce57694d89b54a954c
Author: Gary D. Gregory <garydgreg...@gmail.com>
AuthorDate: Sat Jul 19 14:14:04 2025 -0400

    Fix PMD AvoidBranchingStatementAsLastInLoop in StrBuilder
---
 src/changes/changes.xml                            |  1 +
 .../java/org/apache/commons/text/StrBuilder.java   | 54 +++++++++++-----------
 2 files changed, 29 insertions(+), 26 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index e550ad8a..0d1aa51f 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -55,6 +55,7 @@ The <action> type attribute can be add,update,fix,remove.
     <action type="fix" dev="ggregory" due-to="Gary Gregory">Fix PMD 
UnnecessaryFullyQualifiedName in StrSubstitutor.</action>
     <action type="fix" dev="ggregory" due-to="Gary Gregory">Fix PMD 
UnnecessaryFullyQualifiedName in AlphabetConverter.</action>
     <action type="fix" dev="ggregory" due-to="Gary Gregory">Fix PMD 
AvoidBranchingStatementAsLastInLoop in TextStringBuilder.</action>
+    <action type="fix" dev="ggregory" due-to="Gary Gregory">Fix PMD 
AvoidBranchingStatementAsLastInLoop in StrBuilder.</action>
     <!-- ADD -->
     <action type="add" dev="ggregory" due-to="Gary Gregory">Interface 
StringLookup now extends UnaryOperator&lt;String&gt;.</action>
     <action type="add" dev="ggregory" due-to="Gary Gregory">Interface 
TextRandomProvider extends IntUnaryOperator.</action>
diff --git a/src/main/java/org/apache/commons/text/StrBuilder.java 
b/src/main/java/org/apache/commons/text/StrBuilder.java
index 25759a7e..a89ebec3 100644
--- a/src/main/java/org/apache/commons/text/StrBuilder.java
+++ b/src/main/java/org/apache/commons/text/StrBuilder.java
@@ -1946,9 +1946,9 @@ public class StrBuilder implements CharSequence, 
Appendable, Serializable, Build
      * @return The first index of the string, or -1 if not found
      */
     public int indexOf(final String str, int startIndex) {
-        startIndex = Math.max(startIndex, 0);
+        startIndex = Math.max(0, startIndex);
         if (str == null || startIndex >= size) {
-            return -1;
+            return StringUtils.INDEX_NOT_FOUND;
         }
         final int strLen = str.length();
         if (strLen == 1) {
@@ -1958,19 +1958,20 @@ public class StrBuilder implements CharSequence, 
Appendable, Serializable, Build
             return startIndex;
         }
         if (strLen > size) {
-            return -1;
+            return StringUtils.INDEX_NOT_FOUND;
         }
         final char[] thisBuf = buffer;
-        final int len = size - strLen + 1;
-        outer: for (int i = startIndex; i < len; i++) {
-            for (int j = 0; j < strLen; j++) {
-                if (str.charAt(j) != thisBuf[i + j]) {
-                    continue outer;
-                }
+        final int searchLen = size - strLen + 1;
+        for (int i = startIndex; i < searchLen; i++) {
+            boolean found = true;
+            for (int j = 0; j < strLen && found; j++) {
+                found = str.charAt(j) == thisBuf[i + j];
+            }
+            if (found) {
+                return i;
             }
-            return i;
         }
-        return -1;
+        return StringUtils.INDEX_NOT_FOUND;
     }
 
     /**
@@ -2282,27 +2283,28 @@ public class StrBuilder implements CharSequence, 
Appendable, Serializable, Build
     public int lastIndexOf(final String str, int startIndex) {
         startIndex = startIndex >= size ? size - 1 : startIndex;
         if (str == null || startIndex < 0) {
-            return -1;
+            return StringUtils.INDEX_NOT_FOUND;
         }
         final int strLen = str.length();
-        if (strLen > 0 && strLen <= size) {
-            if (strLen == 1) {
-                return lastIndexOf(str.charAt(0), startIndex);
+        if (strLen == 0) {
+            return startIndex;
+        }
+        if (strLen > size) {
+            return StringUtils.INDEX_NOT_FOUND;
+        }
+        if (strLen == 1) {
+            return lastIndexOf(str.charAt(0), startIndex);
+        }
+        for (int i = startIndex - strLen + 1; i >= 0; i--) {
+            boolean found = true;
+            for (int j = 0; j < strLen && found; j++) {
+                found = str.charAt(j) == buffer[i + j];
             }
-
-            outer: for (int i = startIndex - strLen + 1; i >= 0; i--) {
-                for (int j = 0; j < strLen; j++) {
-                    if (str.charAt(j) != buffer[i + j]) {
-                        continue outer;
-                    }
-                }
+            if (found) {
                 return i;
             }
-
-        } else if (strLen == 0) {
-            return startIndex;
         }
-        return -1;
+        return StringUtils.INDEX_NOT_FOUND;
     }
 
     /**

Reply via email to