You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2019/11/04 22:56:59 UTC

[commons-lang] branch master updated: Lang 1463: StringUtils abbreviate returns String of length greater than maxWidth (#477)

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 d0b95be  Lang 1463: StringUtils abbreviate returns String of length greater than maxWidth (#477)
d0b95be is described below

commit d0b95be2819469371c0276df80e54b088a9b7cad
Author: bbeckercscc <56...@users.noreply.github.com>
AuthorDate: Mon Nov 4 17:56:50 2019 -0500

    Lang 1463: StringUtils abbreviate returns String of length greater than maxWidth (#477)
    
    * fixed LANG-1463 StringUtils abbreviate returns String of length greater than maxWidth
    
    * fixed LANG-1463 StringUtils abbreviate returns String of length greater than maxWidth
    
    * fixed LANG-1463 StringUtils abbreviate returns String of length greater than maxWidth
    
    * formatting fix
    
    * removed magic string
---
 src/main/java/org/apache/commons/lang3/StringUtils.java     | 8 +++++---
 src/test/java/org/apache/commons/lang3/StringUtilsTest.java | 7 +++++++
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java
index ea99127..ee94635 100644
--- a/src/main/java/org/apache/commons/lang3/StringUtils.java
+++ b/src/main/java/org/apache/commons/lang3/StringUtils.java
@@ -294,7 +294,6 @@ public class StringUtils {
     public static String abbreviate(final String str, final String abbrevMarker, final int maxWidth) {
         return abbreviate(str, abbrevMarker, 0, maxWidth);
     }
-
     /**
      * <p>Abbreviates a String using a given replacement marker. This will turn
      * "Now is the time for all good men" into "...is the time for..." if "..." was defined
@@ -333,10 +332,13 @@ public class StringUtils {
      * @since 3.6
      */
     public static String abbreviate(final String str, final String abbrevMarker, int offset, final int maxWidth) {
-        if (isEmpty(str) || isEmpty(abbrevMarker)) {
+        if (isEmpty(str) && isEmpty(abbrevMarker)) {
+            return str;
+        } else if (isNotEmpty(str) && EMPTY.equals(abbrevMarker) && maxWidth > 0) {
+            return str.substring(0, maxWidth);
+        } else if (isEmpty(str) || isEmpty(abbrevMarker)) {
             return str;
         }
-
         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/StringUtilsTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
index 95e98af..a21c7e1 100644
--- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
@@ -236,6 +236,13 @@ public class StringUtilsTest {
     }
 
     //-----------------------------------------------------------------------
+    //Fixed LANG-1463
+    @Test
+    public void testAbbreviateMarkerWithEmptyString() {
+        String greaterThanMaxTest = "much too long text";
+        assertEquals("much too long", StringUtils.abbreviate(greaterThanMaxTest, "", 13));
+    }
+
     @Test
     public void testAbbreviate_StringInt() {
         assertNull(StringUtils.abbreviate(null, 10));