From 162b02bf98a19f85dcc592736a8f7863bd6dd148 Mon Sep 17 00:00:00 2001 From: Shekhar Gulati Date: Sat, 21 May 2016 23:56:51 +0530 Subject: [PATCH] Fixed #28. Added support for `safeTruncate` function --- src/main/java/strman/Strman.java | 31 ++++++++++++++++++++++++++++ src/test/java/strman/StrmanTest.java | 15 ++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/src/main/java/strman/Strman.java b/src/main/java/strman/Strman.java index 9fab5ae..c970560 100644 --- a/src/main/java/strman/Strman.java +++ b/src/main/java/strman/Strman.java @@ -812,6 +812,37 @@ public abstract class Strman { return value.replaceAll("\\s+$", ""); } + /** + * Truncate the string securely, not cutting a word in half. It always returns the last full word. + * + * @param value The input String + * @param length Max size of the truncated String + * @param filler String that will be added to the end of the return string. Example: '...' + * @return The truncated String + */ + public static String safeTruncate(final String value, final int length, final String filler) { + validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); + if (length == 0) { + return ""; + } + if (length >= value.length()) { + return value; + } + + String[] words = value.split("\\W+"); + StringJoiner result = new StringJoiner(" "); + int spaceCount = 0; + for (String word : words) { + if (result.length() + word.length() + filler.length() + spaceCount > length) { + break; + } else { + result.add(word); + spaceCount++; + } + } + return append(result.toString(), filler); + } + public static String decode(final String value, final int digits, final int radix) { validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); return Arrays diff --git a/src/test/java/strman/StrmanTest.java b/src/test/java/strman/StrmanTest.java index c808a7f..d389ee3 100644 --- a/src/test/java/strman/StrmanTest.java +++ b/src/test/java/strman/StrmanTest.java @@ -619,4 +619,19 @@ public class StrmanTest { assertThat(rightTrim(" strman"), equalTo(" strman")); assertThat(rightTrim("strman"), equalTo("strman")); } + + @Test + public void safeTruncate_shouldSafelyTruncateStrings() throws Exception { + assertThat(safeTruncate("foo bar", 0, "."), equalTo("")); + assertThat(safeTruncate("foo bar", 4, "."), equalTo("foo.")); + assertThat(safeTruncate("foo bar", 3, "."), equalTo(".")); + assertThat(safeTruncate("foo bar", 2, "."), equalTo(".")); + assertThat(safeTruncate("foo bar", 7, "."), equalTo("foo bar")); + assertThat(safeTruncate("foo bar", 8, "."), equalTo("foo bar")); + assertThat(safeTruncate("A Javascript string manipulation library.", 16, "..."), equalTo("A Javascript...")); + assertThat(safeTruncate("A Javascript string manipulation library.", 15, "..."), equalTo("A Javascript...")); + assertThat(safeTruncate("A Javascript string manipulation library.", 14, "..."), equalTo("A...")); + assertThat(safeTruncate("A Javascript string manipulation library.", 13, "..."), equalTo("A...")); + + } } \ No newline at end of file