commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject cvs commit: jakarta-commons/lang/src/java/org/apache/commons/lang StringUtils.java
Date Fri, 01 Aug 2003 23:54:41 GMT
scolebourne    2003/08/01 16:54:41

  Modified:    lang/src/test/org/apache/commons/lang
                        StringUtilsEqualsIndexOfTest.java
               lang/src/java/org/apache/commons/lang StringUtils.java
  Log:
  Complete the indexOfAny() method set
  
  Revision  Changes    Path
  1.7       +64 -37    jakarta-commons/lang/src/test/org/apache/commons/lang/StringUtilsEqualsIndexOfTest.java
  
  Index: StringUtilsEqualsIndexOfTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/StringUtilsEqualsIndexOfTest.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- StringUtilsEqualsIndexOfTest.java	30 Jul 2003 22:21:39 -0000	1.6
  +++ StringUtilsEqualsIndexOfTest.java	1 Aug 2003 23:54:41 -0000	1.7
  @@ -251,10 +251,10 @@
       }
   
       //-----------------------------------------------------------------------
  -    public void testIndexOfAny() {
  -        assertEquals(-1, StringUtils.indexOfAny(null, null));
  +    public void testIndexOfAny_StringStringarray() {
  +        assertEquals(-1, StringUtils.indexOfAny(null, (String[]) null));
           assertEquals(-1, StringUtils.indexOfAny(null, FOOBAR_SUB_ARRAY));
  -        assertEquals(-1, StringUtils.indexOfAny(FOOBAR, null));
  +        assertEquals(-1, StringUtils.indexOfAny(FOOBAR, (String[]) null));
           assertEquals(2, StringUtils.indexOfAny(FOOBAR, FOOBAR_SUB_ARRAY));
           assertEquals(-1, StringUtils.indexOfAny(FOOBAR, new String[0]));
           assertEquals(-1, StringUtils.indexOfAny(null, new String[0]));
  @@ -268,7 +268,7 @@
           assertEquals(-1, StringUtils.indexOfAny(null, new String[] {null}));
       }
   
  -    public void testLastIndexOfAny() {
  +    public void testLastIndexOfAny_StringStringarray() {
           assertEquals(-1, StringUtils.lastIndexOfAny(null, null));
           assertEquals(-1, StringUtils.lastIndexOfAny(null, FOOBAR_SUB_ARRAY));
           assertEquals(-1, StringUtils.lastIndexOfAny(FOOBAR, null));
  @@ -286,44 +286,71 @@
       }
   
       //-----------------------------------------------------------------------
  -    public void testIndexOfAnyBut() {
  -        String str1 = "a";
  -        String str2 = "b";
  -        String str3 = "ab";
  -        String chars1= "b";
  -        String chars2= "a";
  -        String chars3= "ab";
  -        assertEquals(-1, StringUtils.indexOfAnyBut(null, (String) null));
  -        assertEquals(-1, StringUtils.indexOfAnyBut("", (String) null));
  -        assertEquals(-1, StringUtils.indexOfAnyBut(null, ""));
  -        assertEquals(0, StringUtils.indexOfAnyBut(str1, ""));
  -        assertEquals(-1, StringUtils.indexOfAnyBut("", ""));
  -        assertEquals(-1, StringUtils.indexOfAnyBut("", chars1));
  -        assertEquals(0, StringUtils.indexOfAnyBut(str1, chars1));
  -        assertEquals(-1, StringUtils.indexOfAnyBut(str1, chars2));
  -        assertEquals(-1, StringUtils.indexOfAnyBut(str1, chars3));
  -        assertEquals(-1, StringUtils.indexOfAnyBut(str2, chars1));
  -        assertEquals(0, StringUtils.indexOfAnyBut(str2, chars2));
  -        assertEquals(-1, StringUtils.indexOfAnyBut(str2, chars3));
  -        assertEquals(0, StringUtils.indexOfAnyBut(str3, chars1));
  -        assertEquals(1, StringUtils.indexOfAnyBut(str3, chars2));
  -        assertEquals(-1, StringUtils.indexOfAnyBut(str3, chars3));
  -        assertEquals(3, StringUtils.indexOfAnyBut("zzabyycdxx", "za"));
  -        assertEquals(0, StringUtils.indexOfAnyBut("zzabyycdxx", ""));
  -        assertEquals(-1, StringUtils.indexOfAnyBut("aba","ab"));
  +    public void testIndexOfAny_StringChararray() {
  +        assertEquals(-1, StringUtils.indexOfAny(null, (char[]) null));
  +        assertEquals(-1, StringUtils.indexOfAny(null, new char[0]));
  +        assertEquals(-1, StringUtils.indexOfAny(null, new char[] {'a','b'}));
  +        
  +        assertEquals(-1, StringUtils.indexOfAny("", (char[]) null));
  +        assertEquals(-1, StringUtils.indexOfAny("", new char[0]));
  +        assertEquals(-1, StringUtils.indexOfAny("", new char[] {'a','b'}));
  +        
  +        assertEquals(-1, StringUtils.indexOfAny("zzabyycdxx", (char[]) null)); 
  +        assertEquals(-1, StringUtils.indexOfAny("zzabyycdxx", new char[0])); 
  +        assertEquals(0, StringUtils.indexOfAny("zzabyycdxx", new char[] {'z','a'})); 
  +        assertEquals(3, StringUtils.indexOfAny("zzabyycdxx", new char[] {'b','y'}));
  +        assertEquals(-1, StringUtils.indexOfAny("ab", new char[] {'z'}));
  +    }
  +
  +    public void testIndexOfAny_StringString() {
  +        assertEquals(-1, StringUtils.indexOfAny(null, (String) null));
  +        assertEquals(-1, StringUtils.indexOfAny(null, ""));
  +        assertEquals(-1, StringUtils.indexOfAny(null, "ab"));
  +        
  +        assertEquals(-1, StringUtils.indexOfAny("", (String) null));
  +        assertEquals(-1, StringUtils.indexOfAny("", ""));
  +        assertEquals(-1, StringUtils.indexOfAny("", "ab"));
  +        
  +        assertEquals(-1, StringUtils.indexOfAny("zzabyycdxx", (String) null)); 
  +        assertEquals(-1, StringUtils.indexOfAny("zzabyycdxx", "")); 
  +        assertEquals(0, StringUtils.indexOfAny("zzabyycdxx", "za")); 
  +        assertEquals(3, StringUtils.indexOfAny("zzabyycdxx", "by"));
  +        assertEquals(-1, StringUtils.indexOfAny("ab", "z"));
       }
  -    
  +
       //-----------------------------------------------------------------------
  -    public void testIndexOfAnyButChar() {
  +    public void testIndexOfAnyBut_StringChararray() {
           assertEquals(-1, StringUtils.indexOfAnyBut(null, (char[]) null));
  -        assertEquals(-1, StringUtils.indexOfAnyBut("", (char[]) null));
           assertEquals(-1, StringUtils.indexOfAnyBut(null, new char[0]));
  +        assertEquals(-1, StringUtils.indexOfAnyBut(null, new char[] {'a','b'}));
  +        
  +        assertEquals(-1, StringUtils.indexOfAnyBut("", (char[]) null));
           assertEquals(-1, StringUtils.indexOfAnyBut("", new char[0]));
  +        assertEquals(-1, StringUtils.indexOfAnyBut("", new char[] {'a','b'}));
  +        
  +        assertEquals(-1, StringUtils.indexOfAnyBut("zzabyycdxx", (char[]) null));
  +        assertEquals(-1, StringUtils.indexOfAnyBut("zzabyycdxx", new char[0]));
           assertEquals(3, StringUtils.indexOfAnyBut("zzabyycdxx", new char[] {'z','a'}));

  -        assertEquals(0, StringUtils.indexOfAnyBut("zzabyycdxx", new char[0]));
  -        assertEquals(-1, StringUtils.indexOfAnyBut("ab", new char[] {'a','b'}));
  +        assertEquals(0, StringUtils.indexOfAnyBut("zzabyycdxx", new char[] {'b','y'}));

  +        assertEquals(0, StringUtils.indexOfAnyBut("ab", new char[] {'z'}));
       }
   
  +    public void testIndexOfAnyBut_StringString() {
  +        assertEquals(-1, StringUtils.indexOfAnyBut(null, (String) null));
  +        assertEquals(-1, StringUtils.indexOfAnyBut(null, ""));
  +        assertEquals(-1, StringUtils.indexOfAnyBut(null, "ab"));
  +        
  +        assertEquals(-1, StringUtils.indexOfAnyBut("", (String) null));
  +        assertEquals(-1, StringUtils.indexOfAnyBut("", ""));
  +        assertEquals(-1, StringUtils.indexOfAnyBut("", "ab"));
  +        
  +        assertEquals(-1, StringUtils.indexOfAnyBut("zzabyycdxx", (String) null)); 
  +        assertEquals(-1, StringUtils.indexOfAnyBut("zzabyycdxx", "")); 
  +        assertEquals(3, StringUtils.indexOfAnyBut("zzabyycdxx", "za")); 
  +        assertEquals(0, StringUtils.indexOfAnyBut("zzabyycdxx", "by"));
  +        assertEquals(0, StringUtils.indexOfAnyBut("ab", "z"));
  +    }
  +    
       //-----------------------------------------------------------------------
       public void testContainsOnly_String() {
           String str1 = "a";
  @@ -349,7 +376,7 @@
           assertEquals(true, StringUtils.containsOnly(str3, chars3));
       }
   
  -    public void testContainsOnly_CharArray() {
  +    public void testContainsOnly_Chararray() {
           String str1 = "a";
           String str2 = "b";
           String str3 = "ab";
  @@ -398,7 +425,7 @@
           assertEquals(true, StringUtils.containsNone(str3, chars3));
       }
   
  -    public void testContainsNone_CharArray() {
  +    public void testContainsNone_Chararray() {
           String str1 = "a";
           String str2 = "b";
           String str3 = "ab.";
  
  
  
  1.90      +89 -12    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.89
  retrieving revision 1.90
  diff -u -r1.89 -r1.90
  --- StringUtils.java	1 Aug 2003 23:20:06 -0000	1.89
  +++ StringUtils.java	1 Aug 2003 23:54:41 -0000	1.90
  @@ -1011,7 +1011,7 @@
           return (str.indexOf(searchStr) >= 0);
       }
       
  -    // IndexOfAny
  +    // IndexOfAny strings
       //-----------------------------------------------------------------------
       /**
        * <p>Find the first index of any of a set of potential substrings.</p>
  @@ -1112,7 +1112,73 @@
           return ret;
       }
   
  -    // IndexOfAnyBut
  +    // IndexOfAny chars
  +    //-----------------------------------------------------------------------
  +    /**
  +     * <p>Search a String to find the first index of any
  +     * character in the given set of characters.</p>
  +     *
  +     * <p>A <code>null</code> String will return <code>-1</code>.
  +     * A <code>null</code> or zero length search array will return <code>-1</code>.</p>
  +     * 
  +     * <pre>
  +     * StringUtils.indexOfAny(null, *)                = -1
  +     * StringUtils.indexOfAny("", *)                  = -1
  +     * StringUtils.indexOfAny(*, null)                = -1
  +     * StringUtils.indexOfAny(*, [])                  = -1
  +     * StringUtils.indexOfAny("zzabyycdxx",['z','a']) = 0
  +     * StringUtils.indexOfAny("zzabyycdxx",['b','y']) = 3
  +     * StringUtils.indexOfAny("aba", ['z'])           = -1
  +     * </pre>
  +     * 
  +     * @param str  the String to check, may be null
  +     * @param searchChars  the chars to search for, may be null
  +     * @return the index of any of the chars, -1 if no match or null input
  +     */
  +     public static int indexOfAny(String str, char[] searchChars) {
  +         if (str == null || str.length() == 0 || searchChars == null || searchChars.length
== 0) {
  +             return -1;
  +         }
  +         for (int i = 0; i < str.length(); i ++) {
  +             char ch = str.charAt(i);
  +             for (int j = 0; j < searchChars.length; j++) {
  +                 if (searchChars[j] == ch) {
  +                     return i;
  +                 }
  +             }
  +         }
  +         return -1;
  +     }
  +
  +    /**
  +     * <p>Search a String to find the first index of any
  +     * character in the given set of characters.</p>
  +     *
  +     * <p>A <code>null</code> String will return <code>-1</code>.
  +     * A <code>null</code> search string will return <code>-1</code>.</p>
  +     * 
  +     * <pre>
  +     * StringUtils.indexOfAny(null, *)            = -1
  +     * StringUtils.indexOfAny("", *)              = -1
  +     * StringUtils.indexOfAny(*, null)            = -1
  +     * StringUtils.indexOfAny(*, "")              = -1
  +     * StringUtils.indexOfAny("zzabyycdxx", "za") = 0
  +     * StringUtils.indexOfAny("zzabyycdxx", "by") = 3
  +     * StringUtils.indexOfAny("aba","z")          = -1
  +     * </pre>
  +     *  
  +     * @param str  the String to check, may be null
  +     * @param searchChars  the chars to search for, may be null
  +     * @return the index of any of the chars, -1 if no match or null input
  +     */
  +    public static int indexOfAny(String str, String searchChars) {
  +        if (str == null || str.length() == 0 || searchChars == null || searchChars.length()
== 0) {
  +            return -1;
  +        }
  +        return indexOfAny(str, searchChars.toCharArray());
  +    }
  +
  +    // IndexOfAnyBut chars
       //-----------------------------------------------------------------------
       /**
        * <p>Search a String to find the first index of any
  @@ -1123,7 +1189,9 @@
        * 
        * <pre>
        * StringUtils.indexOfAnyBut(null, *)           = -1
  +     * StringUtils.indexOfAnyBut("", *)             = -1
        * StringUtils.indexOfAnyBut(*, null)           = -1
  +     * StringUtils.indexOfAnyBut(*, [])             = -1
        * StringUtils.indexOfAnyBut("zzabyycdxx",'za') = 3
        * StringUtils.indexOfAnyBut("zzabyycdxx", '')  = 0
        * StringUtils.indexOfAnyBut("aba", 'ab')       = -1
  @@ -1134,10 +1202,19 @@
        * @return the index of any of the chars, -1 if no match or null input
        */
        public static int indexOfAnyBut(String str, char[] searchChars) {
  -         if (searchChars == null) {
  +         if (str == null || str.length() == 0 || searchChars == null || searchChars.length
== 0) {
                return -1;
            }
  -         return indexOfAnyBut(str, new String(searchChars));
  +         outer: for (int i = 0; i < str.length(); i ++) {
  +             char ch = str.charAt(i);
  +             for (int j = 0; j < searchChars.length; j++) {
  +                 if (searchChars[j] == ch) {
  +                     continue outer;
  +                 }
  +             }
  +             return i;
  +         }
  +         return -1;
        }
   
       /**
  @@ -1149,7 +1226,9 @@
        * 
        * <pre>
        * StringUtils.indexOfAnyBut(null, *)            = -1
  +     * StringUtils.indexOfAnyBut("", *)              = -1
        * StringUtils.indexOfAnyBut(*, null)            = -1
  +     * StringUtils.indexOfAnyBut(*, "")              = -1
        * StringUtils.indexOfAnyBut("zzabyycdxx", "za") = 3
        * StringUtils.indexOfAnyBut("zzabyycdxx", "")   = 0
        * StringUtils.indexOfAnyBut("aba","ab")         = -1
  @@ -1160,16 +1239,14 @@
        * @return the index of any of the chars, -1 if no match or null input
        */
       public static int indexOfAnyBut(String str, String searchChars) {
  -        if (str == null || searchChars == null) {
  +        if (str == null || str.length() == 0 || searchChars == null || searchChars.length()
== 0) {
               return -1;
           }
  -
  -        for (int i = 0; i < str.length(); i ++) {
  -           if (searchChars.indexOf(str.charAt(i)) < 0) {
  -               return i;
  -           }
  +        for (int i = 0; i < str.length(); i++) {
  +            if (searchChars.indexOf(str.charAt(i)) < 0) {
  +                return i;
  +            }
           }
  -
           return -1;
       }
   
  
  
  

Mime
View raw message