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-lang.git


The following commit(s) were added to refs/heads/master by this push:
     new fbae26f08 Drop trailing empty token from splitByWholeSeparator (#1710)
fbae26f08 is described below

commit fbae26f0819b795b38b4a251021c3086751f69f4
Author: alhuda <[email protected]>
AuthorDate: Thu Jun 18 00:28:41 2026 +0530

    Drop trailing empty token from splitByWholeSeparator (#1710)
    
    * drop trailing empty token from splitByWholeSeparator
    
    * Move trailing-separator assertions to a parameterized test
---
 src/main/java/org/apache/commons/lang3/StringUtils.java   |  6 +++++-
 .../java/org/apache/commons/lang3/StringUtilsTest.java    | 15 +++++++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java 
b/src/main/java/org/apache/commons/lang3/StringUtils.java
index db9b225e6..4cc5ab75d 100644
--- a/src/main/java/org/apache/commons/lang3/StringUtils.java
+++ b/src/main/java/org/apache/commons/lang3/StringUtils.java
@@ -7447,7 +7447,11 @@ private static String[] 
splitByWholeSeparatorWorker(final String str, final Stri
                 }
             } else {
                 // String.substring( beg ) goes from 'beg' to the end of the 
String.
-                substrings.add(str.substring(beg));
+                // beg == len means the String ended on a separator, so the 
trailing
+                // token is empty and must be dropped unless empty tokens are 
preserved.
+                if (preserveAllTokens || beg < len) {
+                    substrings.add(str.substring(beg));
+                }
                 end = len;
             }
         }
diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java 
b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
index df33386b2..e1ce6e1d3 100644
--- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
@@ -49,6 +49,7 @@
 import org.junit.jupiter.api.Disabled;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
 import org.junit.jupiter.params.provider.ValueSource;
 import org.junitpioneer.jupiter.DefaultLocale;
 import org.junitpioneer.jupiter.ReadsDefaultLocale;
@@ -2438,6 +2439,20 @@ void testSplitByWholeString_StringStringBoolean() {
         }
     }
 
+    @ParameterizedTest
+    @CsvSource({
+        "a:b:,       :,   'a,b'",
+        "a:,         :,   a",
+        "ab-!-cd-!-, -!-, 'ab,cd'",
+    })
+    void testSplitByWholeStringDropsTrailingEmpty(final String str, final 
String separator, final String expected) {
+        final String[] expectedTokens = expected.split(",");
+        // a trailing separator must not leak an empty token (it is dropped, 
like leading and adjacent ones)
+        assertArrayEquals(expectedTokens, 
StringUtils.splitByWholeSeparator(str, separator));
+        // the preserve-all-tokens variant still keeps the trailing empty token
+        assertArrayEquals(ArrayUtils.add(expectedTokens, ""), 
StringUtils.splitByWholeSeparatorPreserveAllTokens(str, separator));
+    }
+
     @Test
     void testSplitByWholeString_StringStringBooleanInt() {
         assertArrayEquals(null, StringUtils.splitByWholeSeparator(null, ".", 
3));

Reply via email to