You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by nd...@apache.org on 2006/08/11 18:50:47 UTC

svn commit: r430824 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/lang/String.java test/java/org/apache/harmony/luni/tests/java/lang/String2Test.java

Author: ndbeyer
Date: Fri Aug 11 09:50:46 2006
New Revision: 430824

URL: http://svn.apache.org/viewvc?rev=430824&view=rev
Log:
Apply patch for HARMONY-1057:  [classlib] missing method j.l.String.replace(CharSequence,CharSequence)

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/String.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/String2Test.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/String.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/String.java?rev=430824&r1=430823&r2=430824&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/String.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/String.java Fri Aug 11 09:50:46 2006
@@ -1334,6 +1334,48 @@
         } while ((index = indexOf(oldChar, index)) != -1);
         return new String(0, count, buffer);
     }
+    
+    /**
+     * Copies this String replacing occurrences of the specified 
+     * target sequence with another sequence. 
+     * The string is processed from the beginning to the end. 
+     * 
+     * @param target
+     *            the sequence to replace
+     * @param replacement
+     *            the replacement sequence
+     * @return the resulting String
+     * 
+     * @throws NullPointerException if either of the arguments 
+     * is <code>null</code>
+     */
+    public String replace(CharSequence target, CharSequence replacement) {
+        if (target == null) {
+            throw new NullPointerException("target should not be null");
+        }
+        if (replacement == null) {
+            throw new NullPointerException("replacement should not be null");
+        }
+        String ts = target.toString();
+        int index = indexOf(ts, 0);
+
+        if (index == -1)
+            return this;
+
+        String rs = replacement.toString();
+        StringBuilder buffer = new StringBuilder(count);
+        int tl = target.length();
+        int tail = 0;
+        do {
+            buffer.append(value, offset + tail, index - tail);
+            buffer.append(rs);
+            tail = index + tl;
+        } while ((index = indexOf(ts, tail)) != -1);
+        //append trailing chars 
+        buffer.append(value, offset + tail, count - tail);
+
+        return buffer.toString();
+    }
 
     /**
      * Compares the specified string to this String to determine if the

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/String2Test.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/String2Test.java?rev=430824&r1=430823&r2=430824&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/String2Test.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/String2Test.java Fri Aug 11 09:50:46 2006
@@ -641,6 +641,18 @@
 		// Test for method java.lang.String java.lang.String.replace(char, char)
 		assertEquals("Failed replace", "HezzoWorzd", hw1.replace('l', 'z'));
 	}
+	
+	/**
+	 * @tests java.lang.String#replace(CharSequence, CharSequence)
+	 */
+	public void test_replaceLjava_langCharSequenceLjava_langCharSequence() {	        
+		assertEquals("Failed replace", "aaccdd", "aabbdd".replace(
+			new StringBuffer("bb"), "cc"));
+		assertEquals("Failed replace by bigger seq", "cccbccc", "aba".replace(
+			"a", "ccc"));
+		assertEquals("Failed replace by smaller seq", "$bba^", 
+			"$aaaaa^".replace(new StringBuilder("aa"), "b"));
+	}
 
 	/**
 	 * @tests java.lang.String#startsWith(java.lang.String)