Merge "Avoid NPE in trimToLengthWithEllipsis()."

This commit is contained in:
Sudheer Shanka
2020-10-08 20:15:47 +00:00
committed by Android (Google) Code Review
2 changed files with 2 additions and 1 deletions

View File

@@ -2173,7 +2173,7 @@ public class TextUtils {
public static <T extends CharSequence> T trimToLengthWithEllipsis(@Nullable T text,
@IntRange(from = 1) int size) {
T trimmed = trimToSize(text, size);
if (trimmed.length() < text.length()) {
if (text != null && trimmed.length() < text.length()) {
trimmed = (T) (trimmed.toString() + "...");
}
return trimmed;

View File

@@ -792,5 +792,6 @@ public class TextUtilsTest {
assertEquals("ABC...", TextUtils.trimToLengthWithEllipsis("ABCDEF", 3));
assertEquals("ABC", TextUtils.trimToLengthWithEllipsis("ABC", 3));
assertEquals("", TextUtils.trimToLengthWithEllipsis("", 3));
assertNull(TextUtils.trimToLengthWithEllipsis(null, 3));
}
}