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 a84093d8b9746753ddc1e1f3f4e0fb012212d251 Author: Gary D. Gregory <garydgreg...@gmail.com> AuthorDate: Sat Jul 19 14:10:46 2025 -0400 Fix PMD AvoidBranchingStatementAsLastInLoop in TextStringBuilder --- src/changes/changes.xml | 1 + .../org/apache/commons/text/TextStringBuilder.java | 46 +++++++++++----------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 9fdb798d..e550ad8a 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -54,6 +54,7 @@ The <action> type attribute can be add,update,fix,remove. <action type="fix" dev="ggregory" due-to="Gary Gregory">Fix PMD UnnecessaryFullyQualifiedName in StringSubstitutor.</action> <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> <!-- ADD --> <action type="add" dev="ggregory" due-to="Gary Gregory">Interface StringLookup now extends UnaryOperator<String>.</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/TextStringBuilder.java b/src/main/java/org/apache/commons/text/TextStringBuilder.java index 95616cf0..939ce1ea 100644 --- a/src/main/java/org/apache/commons/text/TextStringBuilder.java +++ b/src/main/java/org/apache/commons/text/TextStringBuilder.java @@ -635,8 +635,8 @@ public class TextStringBuilder implements CharSequence, Appendable, Serializable /** * Appends an object to this string builder. Appending null will call {@link #appendNull()}. * - * @param obj the object to append - * @return this, to enable chaining + * @param obj the object to append. + * @return this, to enable chaining. */ public TextStringBuilder append(final Object obj) { if (obj == null) { @@ -2128,14 +2128,15 @@ public class TextStringBuilder implements CharSequence, Appendable, Serializable 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 StringUtils.INDEX_NOT_FOUND; } @@ -2457,22 +2458,23 @@ public class TextStringBuilder implements CharSequence, Appendable, Serializable 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 StringUtils.INDEX_NOT_FOUND; }