scolebourne    2003/07/31 13:38:26

  Modified:    lang/src/java/org/apache/commons/lang CharSetUtils.java
                        StringUtils.java
               lang/src/test/org/apache/commons/lang StringUtilsTest.java
  Log:
  Add  replaceChars()  to StringUtils
  Deprecate  translate()  on CharSetUtils
  
  Revision  Changes    Path
  1.17      +3 -1      
jakarta-commons/lang/src/java/org/apache/commons/lang/CharSetUtils.java
  
  Index: CharSetUtils.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/CharSetUtils.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- CharSetUtils.java 30 Jul 2003 22:17:00 -0000      1.16
  +++ CharSetUtils.java 31 Jul 2003 20:38:26 -0000      1.17
  @@ -345,6 +345,8 @@
        * @throws NullPointerException if <code>with</code> or <code>repl</code> 
        *  is <code>null</code>
        * @throws ArrayIndexOutOfBoundsException if <code>with</code> is empty ("")
  +     * @deprecated Use [EMAIL PROTECTED] StringUtils#replaceChars(String, String, 
String)}.
  +     *             Method will be removed in Commons Lang 3.0.
        */
       public static String translate(String str, String searchChars, String 
replaceChars) {
           if (str == null || str.length() == 0) {
  
  
  
  1.83      +104 -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.82
  retrieving revision 1.83
  diff -u -r1.82 -r1.83
  --- StringUtils.java  30 Jul 2003 22:17:49 -0000      1.82
  +++ StringUtils.java  31 Jul 2003 20:38:26 -0000      1.83
  @@ -2261,6 +2261,109 @@
           buf.append(text.substring(start));
           return buf.toString();
       }
  +    
  +    // Replace, character based
  +    //-----------------------------------------------------------------------
  +
  +    /**
  +     * <p>Replaces all occurrances of a character in a String with another.
  +     * This is a null-safe version of [EMAIL PROTECTED] String#replace(char, 
char)}.</p>
  +     *
  +     * <p>A <code>null</code> string input returns <code>null</code>.
  +     * An empty ("") string input returns an empty string.</p>
  +     * 
  +     * <pre>
  +     * StringUtils.replaceChars(null, *, *)        = null
  +     * StringUtils.replaceChars("", *, *)          = ""
  +     * StringUtils.replaceChars("abcba", 'b', 'y') = "aycya"
  +     * StringUtils.replaceChars("abcba", 'z', 'y') = "abcba"
  +     * </pre>
  +     * 
  +     * @param str  String to replace characters in, may be null
  +     * @param searchChar  the character to search for, may be null
  +     * @param replaceChar  the character to replace, may be null
  +     * @return modified String, <code>null</code> if null string input
  +     */
  +    public static String replaceChars(String str, char searchChar, char 
replaceChar) {
  +        if (str == null) {
  +            return null;
  +        }
  +        return str.replace(searchChar, replaceChar);
  +    }
  +    
  +    /**
  +     * <p>Replaces multiple characters in a String in one go.
  +     * This method can also be used to delete characters.</p>
  +     *
  +     * <p>For example:<br />
  +     * <code>replaceChars(&quot;hello&quot;, &quot;ho&quot;, &quot;jy&quot;) = 
jelly</code>.</p>
  +     * 
  +     * <p>A <code>null</code> string input returns <code>null</code>.
  +     * An empty ("") string input returns an empty string.
  +     * A null or empty set of search characters returns the input string.</p>
  +     * 
  +     * <p>The length of the search characters should normally equal the length
  +     * of the replace characters.
  +     * If the search characters is longer, then the extra search characters
  +     * are deleted.
  +     * If the search characters is shorter, then the extra replace characters
  +     * are ignored.</p>
  +     * 
  +     * <pre>
  +     * StringUtils.replaceChars(null, *, *)           = null
  +     * StringUtils.replaceChars("", *, *)             = ""
  +     * StringUtils.replaceChars("abc", null, *)       = "abc"
  +     * StringUtils.replaceChars("abc", "", *)         = "abc"
  +     * StringUtils.replaceChars("abc", "b", null)     = "ac"
  +     * StringUtils.replaceChars("abc", "b", "")       = "ac"
  +     * StringUtils.replaceChars("abcba", "bc", "yz")  = "ayzya"
  +     * StringUtils.replaceChars("abcba", "bc", "y")   = "ayya"
  +     * StringUtils.replaceChars("abcba", "bc", "yzx") = "ayzya"
  +     * </pre>
  +     * 
  +     * @param str  String to replace characters in, may be null
  +     * @param searchChars  a set of characters to search for, may be null
  +     * @param replaceChars  a set of characters to replace, may be null
  +     * @return modified String, <code>null</code> if null string input
  +     */
  +    public static String replaceChars(String str, String searchChars, String 
replaceChars) {
  +        if (str == null || str.length() == 0 || searchChars == null || 
searchChars.length()== 0) {
  +            return str;
  +        }
  +        char[] chars = str.toCharArray();
  +        int len = chars.length;
  +        boolean modified = false;
  +        for (int i = 0, isize = searchChars.length(); i < isize; i++) {
  +            char searchChar = searchChars.charAt(i);
  +            if (replaceChars == null || i >= replaceChars.length()) {
  +                // delete
  +                int pos = 0;
  +                for (int j = 0; j < len; j++) {
  +                    if (chars[j] != searchChar) {
  +                        chars[pos++] = chars[j];
  +                    } else {
  +                        modified = true;
  +                    }
  +                }
  +                len = pos;
  +            } else {
  +                // replace
  +                for (int j = 0; j < len; j++) {
  +                    if (chars[j] == searchChar) {
  +                        chars[j] = replaceChars.charAt(i);
  +                        modified = true;
  +                    }
  +                }
  +            }
  +        }
  +        if (modified == false) {
  +            return str;
  +        }
  +        return new String(chars, 0, len);
  +    }
  +
  +    // Overlay
  +    //-----------------------------------------------------------------------
   
       /**
        * <p>Overlays part of a String with another String.</p>
  
  
  
  1.41      +42 -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.40
  retrieving revision 1.41
  diff -u -r1.40 -r1.41
  --- StringUtilsTest.java      30 Jul 2003 22:21:39 -0000      1.40
  +++ StringUtilsTest.java      31 Jul 2003 20:38:26 -0000      1.41
  @@ -439,6 +439,47 @@
           assertEquals("foofoo", StringUtils.replaceOnce("foofoofoo", "foo", ""));
       }
   
  +    public void testReplaceChars_StringCharChar() {
  +        assertEquals(null, StringUtils.replaceChars(null, 'b', 'z'));
  +        assertEquals("", StringUtils.replaceChars("", 'b', 'z'));
  +        assertEquals("azcza", StringUtils.replaceChars("abcba", 'b', 'z'));
  +        assertEquals("abcba", StringUtils.replaceChars("abcba", 'x', 'z'));
  +    }
  +    
  +    public void testReplaceChars_StringStringString() {
  +        assertEquals("jelly", StringUtils.replaceChars("hello", "ho", "jy"));
  +
  +        assertEquals(null, StringUtils.replaceChars(null, null, null));
  +        assertEquals(null, StringUtils.replaceChars(null, "", null));
  +        assertEquals(null, StringUtils.replaceChars(null, "a", null));
  +        assertEquals(null, StringUtils.replaceChars(null, null, ""));
  +        assertEquals(null, StringUtils.replaceChars(null, null, "x"));
  +        
  +        assertEquals("", StringUtils.replaceChars("", null, null));
  +        assertEquals("", StringUtils.replaceChars("", "", null));
  +        assertEquals("", StringUtils.replaceChars("", "a", null));
  +        assertEquals("", StringUtils.replaceChars("", null, ""));
  +        assertEquals("", StringUtils.replaceChars("", null, "x"));
  +
  +        assertEquals("abc", StringUtils.replaceChars("abc", null, null));
  +        assertEquals("abc", StringUtils.replaceChars("abc", null, ""));
  +        assertEquals("abc", StringUtils.replaceChars("abc", null, "x"));
  +        
  +        assertEquals("abc", StringUtils.replaceChars("abc", "", null));
  +        assertEquals("abc", StringUtils.replaceChars("abc", "", ""));
  +        assertEquals("abc", StringUtils.replaceChars("abc", "", "x"));
  +        
  +        assertEquals("ac", StringUtils.replaceChars("abc", "b", null));
  +        assertEquals("ac", StringUtils.replaceChars("abc", "b", ""));
  +        assertEquals("axc", StringUtils.replaceChars("abc", "b", "x"));
  +        
  +        assertEquals("ayzya", StringUtils.replaceChars("abcba", "bc", "yz"));
  +        assertEquals("ayya", StringUtils.replaceChars("abcba", "bc", "y"));
  +        assertEquals("ayzya", StringUtils.replaceChars("abcba", "bc", "yzx"));
  +        
  +        assertSame("abcba", StringUtils.replaceChars("abcba", "z", "w"));
  +    }
  +    
       public void testOverlayString() {
           assertEquals("overlayString(String, String, int, int) failed",
                        "foo foor baz", StringUtils.overlayString(SENTENCE, FOO, 4, 6) 
);
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to