mirror of
https://github.com/apache/commons-lang.git
synced 2025-02-09 19:45:01 +00:00
LANG-1619 - refine StringUtils.abbreviate (#663)
This commit is contained in:
parent
76443d7f1d
commit
dd7f02feac
@ -349,14 +349,15 @@ public static String abbreviate(final String str, final String abbrevMarker, int
|
|||||||
if (maxWidth < minAbbrevWidth) {
|
if (maxWidth < minAbbrevWidth) {
|
||||||
throw new IllegalArgumentException(String.format("Minimum abbreviation width is %d", minAbbrevWidth));
|
throw new IllegalArgumentException(String.format("Minimum abbreviation width is %d", minAbbrevWidth));
|
||||||
}
|
}
|
||||||
if (str.length() <= maxWidth) {
|
final int strLen = str.length();
|
||||||
|
if (strLen <= maxWidth) {
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
if (offset > str.length()) {
|
if (offset > strLen) {
|
||||||
offset = str.length();
|
offset = strLen;
|
||||||
}
|
}
|
||||||
if (str.length() - offset < maxWidth - abbrevMarkerLength) {
|
if (strLen - offset < maxWidth - abbrevMarkerLength) {
|
||||||
offset = str.length() - (maxWidth - abbrevMarkerLength);
|
offset = strLen - (maxWidth - abbrevMarkerLength);
|
||||||
}
|
}
|
||||||
if (offset <= abbrevMarkerLength+1) {
|
if (offset <= abbrevMarkerLength+1) {
|
||||||
return str.substring(0, maxWidth - abbrevMarkerLength) + abbrevMarker;
|
return str.substring(0, maxWidth - abbrevMarkerLength) + abbrevMarker;
|
||||||
@ -364,10 +365,10 @@ public static String abbreviate(final String str, final String abbrevMarker, int
|
|||||||
if (maxWidth < minAbbrevWidthOffset) {
|
if (maxWidth < minAbbrevWidthOffset) {
|
||||||
throw new IllegalArgumentException(String.format("Minimum abbreviation width with offset is %d", minAbbrevWidthOffset));
|
throw new IllegalArgumentException(String.format("Minimum abbreviation width with offset is %d", minAbbrevWidthOffset));
|
||||||
}
|
}
|
||||||
if (offset + maxWidth - abbrevMarkerLength < str.length()) {
|
if (offset + maxWidth - abbrevMarkerLength < strLen) {
|
||||||
return abbrevMarker + abbreviate(str.substring(offset), abbrevMarker, maxWidth - abbrevMarkerLength);
|
return abbrevMarker + abbreviate(str.substring(offset), abbrevMarker, maxWidth - abbrevMarkerLength);
|
||||||
}
|
}
|
||||||
return abbrevMarker + str.substring(str.length() - (maxWidth - abbrevMarkerLength));
|
return abbrevMarker + str.substring(strLen - (maxWidth - abbrevMarkerLength));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user