commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject cvs commit: jakarta-commons/lang/src/test/org/apache/commons/lang StringUtilsTest.java
Date Mon, 17 Mar 2003 05:28:37 GMT
alex        2003/03/16 21:28:37

  Modified:    lang/src/java/org/apache/commons/lang StringUtils.java
               lang/src/test/org/apache/commons/lang StringUtilsTest.java
  Log:
  Purpletech code import:  abbreviate, difference, differenceAt
  
  Revision  Changes    Path
  1.33      +91 -1     jakarta-commons/lang/src/java/org/apache/commons/lang/StringUtils.java
  
  Index: StringUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/StringUtils.java,v
  retrieving revision 1.32
  retrieving revision 1.33
  diff -u -r1.32 -r1.33
  --- StringUtils.java	20 Jan 2003 22:15:13 -0000	1.32
  +++ StringUtils.java	17 Mar 2003 05:28:36 -0000	1.33
  @@ -74,6 +74,7 @@
    * @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$
    */
  @@ -1744,6 +1745,95 @@
               j--;
               i++;
           }
  +    }
  +
  +    // 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;
       }
   
   
  
  
  
  1.14      +70 -1     jakarta-commons/lang/src/test/org/apache/commons/lang/StringUtilsTest.java
  
  Index: StringUtilsTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/StringUtilsTest.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- StringUtilsTest.java	20 Jan 2003 22:15:13 -0000	1.13
  +++ StringUtilsTest.java	17 Mar 2003 05:28:37 -0000	1.14
  @@ -377,5 +377,74 @@
           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"));
  +    }
  +
   }
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message