Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/main/java/org/apache/commons/lang3/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -350,13 +350,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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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),
Expand Down