LANG-1619 - refine StringUtils.abbreviate (#663)

This commit is contained in:
Arturo Bernal 2020-12-05 18:20:00 +01:00 committed by GitHub
parent 76443d7f1d
commit dd7f02feac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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));
} }
/** /**