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


The following commit(s) were added to refs/heads/master by this push:
     new 4a5ef8ed2 Fix handling of null marker in 
StringUtils.abbreviate(String, String, int, int)  (#1571)
4a5ef8ed2 is described below

commit 4a5ef8ed29f0c135627f43b58c7ab7b6defe37b7
Author: ThrawnCA <[email protected]>
AuthorDate: Sat Jan 17 22:55:03 2026 +1000

    Fix handling of null marker in StringUtils.abbreviate(String, String, int, 
int)  (#1571)
    
    * add unit tests showing mishandling of null abbrevMarker
    
    * fix handling of null abbrevMarker
    
    - treat null marker as empty string
    - ensure offset and maxWidth are applied as usual (simple 'substring' won't 
cut it)
---
 src/main/java/org/apache/commons/lang3/StringUtils.java        | 10 +++++-----
 .../org/apache/commons/lang3/StringUtilsAbbreviateTest.java    |  8 +++++++-
 2 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java 
b/src/main/java/org/apache/commons/lang3/StringUtils.java
index 22d12e45b..ee6b9eea7 100644
--- a/src/main/java/org/apache/commons/lang3/StringUtils.java
+++ b/src/main/java/org/apache/commons/lang3/StringUtils.java
@@ -352,13 +352,13 @@ public static String abbreviate(final String str, final 
String abbrevMarker, fin
      * @throws IllegalArgumentException if the width is too small.
      * @since 3.6
      */
-    public static String abbreviate(final String str, final String 
abbrevMarker, int offset, final int maxWidth) {
-        if (isNotEmpty(str) && EMPTY.equals(abbrevMarker) && maxWidth > 0) {
-            return substring(str, 0, maxWidth);
-        }
-        if (isAnyEmpty(str, abbrevMarker)) {
+    public static String abbreviate(final String str, String abbrevMarker, int 
offset, final int maxWidth) {
+        if (isEmpty(str)) {
             return str;
         }
+        if (abbrevMarker == null) {
+            abbrevMarker = EMPTY;
+        }
         final int abbrevMarkerLength = abbrevMarker.length();
         final int minAbbrevWidth = abbrevMarkerLength + 1;
         final int minAbbrevWidthOffset = abbrevMarkerLength + 
abbrevMarkerLength + 1;
diff --git 
a/src/test/java/org/apache/commons/lang3/StringUtilsAbbreviateTest.java 
b/src/test/java/org/apache/commons/lang3/StringUtilsAbbreviateTest.java
index 49a188d06..8ef0cfc85 100644
--- a/src/test/java/org/apache/commons/lang3/StringUtilsAbbreviateTest.java
+++ b/src/test/java/org/apache/commons/lang3/StringUtilsAbbreviateTest.java
@@ -107,7 +107,7 @@ void testAbbreviate_StringIntInt() {
     void testAbbreviate_StringStringInt() {
         assertNull(StringUtils.abbreviate(null, null, 10));
         assertNull(StringUtils.abbreviate(null, "...", 10));
-        assertEquals("paranaguacu", StringUtils.abbreviate("paranaguacu", 
null, 10));
+        assertEquals("paranaguac", StringUtils.abbreviate("paranaguacu", null, 
10));
         assertEquals("", StringUtils.abbreviate("", "...", 2));
         assertEquals("wai**", StringUtils.abbreviate("waiheke", "**", 5));
         assertEquals("And af,,,,", StringUtils.abbreviate("And after a long 
time, he finally met his son.", ",,,,", 10));
@@ -130,6 +130,12 @@ void testAbbreviate_StringStringIntInt() {
         assertNull(StringUtils.abbreviate(null, "...", 10, 12));
         assertEquals("", StringUtils.abbreviate("", null, 0, 10));
         assertEquals("", StringUtils.abbreviate("", "...", 2, 10));
+        assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", null, 2, 
10));
+        assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", "", 2, 10));
+        assertEquals("abc", StringUtils.abbreviate("abcdefg", null, 0, 3));
+        assertEquals("cde", StringUtils.abbreviate("abcdefg", null, 2, 3));
+        assertEquals("abc", StringUtils.abbreviate("abcdefg", "", 0, 3));
+        assertEquals("cde", StringUtils.abbreviate("abcdefg", "", 2, 3));
         assertIllegalArgumentException(() -> 
StringUtils.abbreviate("abcdefghij", "::", 0, 2),
                 "StringUtils.abbreviate expecting IllegalArgumentException");
         assertIllegalArgumentException(() -> 
StringUtils.abbreviate("abcdefghij", "!!!", 5, 6),

Reply via email to