Skip to content

Commit

Permalink
Lang 1463: StringUtils abbreviate returns String of length greater th…
Browse files Browse the repository at this point in the history
…an 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
  • Loading branch information
bbeckercscc authored and garydgregory committed Nov 4, 2019
1 parent 24e6468 commit d0b95be
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/main/java/org/apache/commons/lang3/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ public static String abbreviate(final String str, final int offset, final int ma
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
Expand Down Expand Up @@ -333,10 +332,13 @@ public static String abbreviate(final String str, final String abbrevMarker, fin
* @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;
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/org/apache/commons/lang3/StringUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,13 @@ private void innerTestSplitPreserveAllTokens(final char separator, final String
}

//-----------------------------------------------------------------------
//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));
Expand Down

0 comments on commit d0b95be

Please sign in to comment.