Purpletech code import: abbreviate, difference, differenceAt
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137258 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
e8a0b41aa3
commit
a50e6215b2
|
@ -74,8 +74,9 @@ import org.apache.commons.lang.exception.NestableRuntimeException;
|
|||
* @author Stephen Colebourne
|
||||
* @author <a href="mailto:fredrik@westermarck.com">Fredrik Westermarck</a>
|
||||
* @author Holger Krauth
|
||||
* @author <a href="mailto:alex@purpletech.com">Alexander Day Chaffee</a>
|
||||
* @since 1.0
|
||||
* @version $Id: StringUtils.java,v 1.32 2003/01/20 22:15:13 dlr Exp $
|
||||
* @version $Id: StringUtils.java,v 1.33 2003/03/17 05:28:36 alex Exp $
|
||||
*/
|
||||
public class StringUtils {
|
||||
|
||||
|
@ -1746,6 +1747,95 @@ public class StringUtils {
|
|||
}
|
||||
}
|
||||
|
||||
// Abbreviating
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Turn "Now is the time for all good men" into "Now is the time for..."
|
||||
* <p>
|
||||
* Specifically:
|
||||
* <p>
|
||||
* If str is less than max characters long, return it.
|
||||
* Else abbreviate it to (substring(str, 0, max-3) + "...").
|
||||
* If maxWidth is less than 3, throw an IllegalArgumentException.
|
||||
* In no case will it return a string of length greater than maxWidth.
|
||||
*
|
||||
* @param maxWidth maximum length of result string
|
||||
**/
|
||||
public static String abbreviate(String s, int maxWidth) {
|
||||
return abbreviate(s, 0, maxWidth);
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn "Now is the time for all good men" into "...is the time for..."
|
||||
* <p>
|
||||
* Works like abbreviate(String, int), but allows you to specify a "left edge"
|
||||
* offset. Note that this left edge is not necessarily going to be the leftmost
|
||||
* character in the result, or the first
|
||||
* character following the ellipses, but it will appear somewhere in the result.
|
||||
* In no case will it return a string of length greater than maxWidth.
|
||||
*
|
||||
* @param offset left edge of source string
|
||||
* @param maxWidth maximum length of result string
|
||||
**/
|
||||
public static String abbreviate(String s, int offset, int maxWidth) {
|
||||
if (maxWidth < 4)
|
||||
throw new IllegalArgumentException("Minimum abbreviation width is 4");
|
||||
if (s.length() <= maxWidth)
|
||||
return s;
|
||||
if (offset > s.length())
|
||||
offset = s.length();
|
||||
if ((s.length() - offset) < (maxWidth-3))
|
||||
offset = s.length() - (maxWidth-3);
|
||||
if (offset <= 4)
|
||||
return s.substring(0, maxWidth-3) + "...";
|
||||
if (maxWidth < 7)
|
||||
throw new IllegalArgumentException("Minimum abbreviation width with offset is 7");
|
||||
if ((offset + (maxWidth-3)) < s.length())
|
||||
return "..." + abbreviate(s.substring(offset), maxWidth-3);
|
||||
return "..." + s.substring(s.length() - (maxWidth-3));
|
||||
}
|
||||
|
||||
// Difference
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Compare two strings, and return the portion where they differ.
|
||||
* (More precisely, return the remainder of the second string,
|
||||
* starting from where it's different from the first.)
|
||||
* <p>
|
||||
* E.g. strdiff("i am a machine", "i am a robot") -> "robot"
|
||||
*
|
||||
* @return the portion of s2 where it differs from s1; returns the empty string ("") if they are equal
|
||||
**/
|
||||
public static String difference(String s1, String s2) {
|
||||
int at = differenceAt(s1, s2);
|
||||
if (at == -1)
|
||||
return "";
|
||||
return s2.substring(at);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare two strings, and return the index at which the strings begin to differ
|
||||
* <p>
|
||||
* E.g. strdiff("i am a machine", "i am a robot") -> 7
|
||||
*
|
||||
* @return the index where s2 and s1 begin to differ; -1 if they are equal
|
||||
**/
|
||||
public static int differenceAt(String s1, String s2)
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<s1.length() && i<s2.length(); ++i) {
|
||||
if (s1.charAt(i) != s2.charAt(i)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i<s2.length() || i<s1.length()) {
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
// Misc
|
||||
//--------------------------------------------------------------------------
|
||||
|
|
|
@ -68,7 +68,7 @@ import junit.textui.TestRunner;
|
|||
* @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
|
||||
* @author <a href="mailto:fredrik@westermarck.com>Fredrik Westermarck</a>
|
||||
* @author Holger Krauth
|
||||
* @version $Id: StringUtilsTest.java,v 1.13 2003/01/20 22:15:13 dlr Exp $
|
||||
* @version $Id: StringUtilsTest.java,v 1.14 2003/03/17 05:28:37 alex Exp $
|
||||
*/
|
||||
public class StringUtilsTest extends TestCase {
|
||||
|
||||
|
@ -377,5 +377,74 @@ public class StringUtilsTest extends TestCase {
|
|||
assertEquals("containsOnly(String3, chars3) success", true, StringUtils.containsOnly(str3, chars3));
|
||||
}
|
||||
|
||||
public void testAbbreviate()
|
||||
{
|
||||
assertEquals("abbreviate(String,int) failed",
|
||||
"short", StringUtils.abbreviate("short", 10));
|
||||
assertEquals("abbreviate(String,int) failed",
|
||||
"Now is ...", StringUtils.abbreviate("Now is the time for all good men to come to the aid of their party.", 10));
|
||||
|
||||
String raspberry = "raspberry peach";
|
||||
assertEquals("abbreviate(String,int) failed (one past limit)",
|
||||
"raspberry p...", StringUtils.abbreviate(raspberry, 14));
|
||||
assertEquals("abbreviate(String,int) (at limit)",
|
||||
"raspberry peach", StringUtils.abbreviate("raspberry peach", 15));
|
||||
assertEquals("abbreviate(String,int) (one below limit)",
|
||||
"raspberry peach", StringUtils.abbreviate("raspberry peach", 16));
|
||||
|
||||
assertEquals("abbreviate(String,int,int) failed",
|
||||
"raspberry peach", StringUtils.abbreviate(raspberry, 11, 15));
|
||||
|
||||
assertAbbreviateWithOffset("abcdefg...", -1, 10);
|
||||
assertAbbreviateWithOffset("abcdefg...", 0, 10);
|
||||
assertAbbreviateWithOffset("abcdefg...", 1, 10);
|
||||
assertAbbreviateWithOffset("abcdefg...", 2, 10);
|
||||
assertAbbreviateWithOffset("abcdefg...", 3, 10);
|
||||
assertAbbreviateWithOffset("abcdefg...", 4, 10);
|
||||
assertAbbreviateWithOffset("...fghi...", 5, 10);
|
||||
assertAbbreviateWithOffset("...ghij...", 6, 10);
|
||||
assertAbbreviateWithOffset("...hijk...", 7, 10);
|
||||
assertAbbreviateWithOffset("...ijklmno", 8, 10);
|
||||
assertAbbreviateWithOffset("...ijklmno", 9, 10);
|
||||
assertAbbreviateWithOffset("...ijklmno", 10, 10);
|
||||
assertAbbreviateWithOffset("...ijklmno", 10, 10);
|
||||
assertAbbreviateWithOffset("...ijklmno", 11, 10);
|
||||
assertAbbreviateWithOffset("...ijklmno", 12, 10);
|
||||
assertAbbreviateWithOffset("...ijklmno", 13, 10);
|
||||
assertAbbreviateWithOffset("...ijklmno", 14, 10);
|
||||
assertAbbreviateWithOffset("...ijklmno", 15, 10);
|
||||
assertAbbreviateWithOffset("...ijklmno", 16, 10);
|
||||
assertAbbreviateWithOffset("...ijklmno", Integer.MAX_VALUE, 10);
|
||||
|
||||
}
|
||||
|
||||
private void assertAbbreviateWithOffset(String expected, int offset, int maxWidth)
|
||||
{
|
||||
String abcdefghijklmno = "abcdefghijklmno";
|
||||
String message = "abbreviate(String,int,int) failed";
|
||||
String actual = StringUtils.abbreviate(abcdefghijklmno, offset, maxWidth);
|
||||
if (offset >= 0 && offset < abcdefghijklmno.length()) {
|
||||
assertTrue(message + " -- should contain offset character",
|
||||
actual.indexOf((char)('a'+offset)) != -1);
|
||||
}
|
||||
assertTrue(message + " -- should not be greater than maxWidth",
|
||||
actual.length() <= maxWidth);
|
||||
assertEquals(message, expected, actual);
|
||||
}
|
||||
|
||||
public void testDifference()
|
||||
{
|
||||
assertEquals("robot", StringUtils.difference("i am a machine", "i am a robot"));
|
||||
assertEquals("", StringUtils.difference("foo", "foo"));
|
||||
assertEquals("you are a robot", StringUtils.difference("i am a robot", "you are a robot"));
|
||||
}
|
||||
|
||||
public void testDifferenceAt()
|
||||
{
|
||||
assertEquals(7, StringUtils.differenceAt("i am a machine", "i am a robot"));
|
||||
assertEquals(-1, StringUtils.differenceAt("foo", "foo"));
|
||||
assertEquals(0, StringUtils.differenceAt("i am a robot", "you are a robot"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue