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

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


The following commit(s) were added to refs/heads/master by this push:
     new a8a2466d Refactor DamerauLevenshteinDistance and TextStringBuilder 
internals for duplication (#739).
a8a2466d is described below

commit a8a2466dcec8a310f2246d189f4cc79ad67fb6f9
Author: Gary Gregory <[email protected]>
AuthorDate: Fri Jun 12 11:15:20 2026 +0000

    Refactor DamerauLevenshteinDistance and TextStringBuilder internals for
    duplication (#739).
    
    - Sort members
    - Reduce vertical whitespace
---
 src/changes/changes.xml                            |  1 +
 .../org/apache/commons/text/TextStringBuilder.java | 18 ++++++-------
 .../similarity/DamerauLevenshteinDistance.java     | 30 ++++++++++------------
 3 files changed, 23 insertions(+), 26 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 5f288595..74bb8464 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -58,6 +58,7 @@ The <action> type attribute can be add,update,fix,remove.
       <action type="fix" dev="ggregory" due-to="Javid Khan, Gary Gregory">Fix 
StringIndexOutOfBoundsException in CsvUnescaper on lone quote (#748).</action>
       <action type="fix" dev="ggregory" issue="TEXT-179" due-to="Gary Gregory, 
LeandroAntunes39">[TEXT-179] StringSubstitutor incorrectly removes the escape 
character in "$${${a}".</action>
       <action type="fix" dev="ggregory" due-to="Javid Khan">Throw 
IllegalArgumentException for trailing whitespace in ExtendedMessageFormat 
argument index (#750).</action>
+      <action type="fix" dev="ggregory" due-to="aaaZayne, Gary 
Gregory">Refactor DamerauLevenshteinDistance and TextStringBuilder internals 
for duplication (#739).</action>
       <!-- ADD -->
       <!-- UPDATE -->
       <action type="update" dev="ggregory" due-to="Gary Gregory">Bump 
org.apache.commons:commons-parent from 93 to 102.</action>
diff --git a/src/main/java/org/apache/commons/text/TextStringBuilder.java 
b/src/main/java/org/apache/commons/text/TextStringBuilder.java
index a489857b..d2adf157 100644
--- a/src/main/java/org/apache/commons/text/TextStringBuilder.java
+++ b/src/main/java/org/apache/commons/text/TextStringBuilder.java
@@ -652,15 +652,6 @@ public class TextStringBuilder implements CharSequence, 
Appendable, Serializable
         return append(str, 0, StringUtils.length(str));
     }
 
-    private void validateRange(final int startIndex, final int length, final 
int strLength) {
-        if (startIndex < 0 || startIndex > strLength) {
-            throw new StringIndexOutOfBoundsException("startIndex must be 
valid");
-        }
-        if (length < 0 || startIndex + length > strLength) {
-            throw new StringIndexOutOfBoundsException("length must be valid");
-        }
-    }
-
     /**
      * Appends part of a string to this string builder. Appending null will 
call {@link #appendNull()}.
      *
@@ -3192,4 +3183,13 @@ public class TextStringBuilder implements CharSequence, 
Appendable, Serializable
         return endIndex;
     }
 
+    private void validateRange(final int startIndex, final int length, final 
int strLength) {
+        if (startIndex < 0 || startIndex > strLength) {
+            throw new StringIndexOutOfBoundsException("startIndex must be 
valid");
+        }
+        if (length < 0 || startIndex + length > strLength) {
+            throw new StringIndexOutOfBoundsException("length must be valid");
+        }
+    }
+
 }
diff --git 
a/src/main/java/org/apache/commons/text/similarity/DamerauLevenshteinDistance.java
 
b/src/main/java/org/apache/commons/text/similarity/DamerauLevenshteinDistance.java
index ab963579..0afce35f 100644
--- 
a/src/main/java/org/apache/commons/text/similarity/DamerauLevenshteinDistance.java
+++ 
b/src/main/java/org/apache/commons/text/similarity/DamerauLevenshteinDistance.java
@@ -30,22 +30,9 @@ package org.apache.commons.text.similarity;
  */
 public class DamerauLevenshteinDistance implements EditDistance<Integer> {
 
-    /**
-     * Utility function to ensure distance is valid according to threshold.
-     *
-     * @param distance  The distance value.
-     * @param threshold The threshold value.
-     * @return The distance value, or {@code -1} if distance is greater than 
threshold.
-     */
-    private static int clampDistance(final int distance, final int threshold) {
-        return distance > threshold ? -1 : distance;
-    }
-
-    private static <E> int calculateCost(final SimilarityInput<E> left, final 
SimilarityInput<E> right,
-                                         final int leftIndex, final int 
rightIndex,
-                                         final int[] curr, final int[] prev, 
final int[] prevPrev) {
+    private static <E> int calculateCost(final SimilarityInput<E> left, final 
SimilarityInput<E> right, final int leftIndex, final int rightIndex,
+            final int[] curr, final int[] prev, final int[] prevPrev) {
         final int cost = left.at(leftIndex - 1) == right.at(rightIndex - 1) ? 
0 : 1;
-
         // Select cheapest operation
         int value = Math.min(
                 Math.min(
@@ -54,7 +41,6 @@ public class DamerauLevenshteinDistance implements 
EditDistance<Integer> {
                 ),
                 prev[rightIndex - 1] + cost // Replace (or no cost if same 
character)
         );
-
         // Check if adjacent characters are the same -> transpose if cheaper
         if (leftIndex > 1
                 && rightIndex > 1
@@ -63,10 +49,20 @@ public class DamerauLevenshteinDistance implements 
EditDistance<Integer> {
             // Use cost here, to properly handle two subsequent equal letters
             value = Math.min(value, prevPrev[rightIndex - 2] + cost);
         }
-
         return value;
     }
 
+    /**
+     * Utility function to ensure distance is valid according to threshold.
+     *
+     * @param distance  The distance value.
+     * @param threshold The threshold value.
+     * @return The distance value, or {@code -1} if distance is greater than 
threshold.
+     */
+    private static int clampDistance(final int distance, final int threshold) {
+        return distance > threshold ? -1 : distance;
+    }
+
     /**
      * Finds the Damerau-Levenshtein distance between two CharSequences if 
it's less than or equal to a given threshold.
      *

Reply via email to