Fix replaceChars() to complete bug 25454

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137826 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2004-03-10 23:54:48 +00:00
parent 70ecaae8b1
commit 694d1de4a9
2 changed files with 16 additions and 10 deletions

View File

@ -111,7 +111,7 @@ import java.util.List;
* @author Al Chou
* @author Michael Davey
* @since 1.0
* @version $Id: StringUtils.java,v 1.128 2004/02/24 22:31:42 fredrik Exp $
* @version $Id: StringUtils.java,v 1.129 2004/03/10 23:54:48 scolebourne Exp $
*/
public class StringUtils {
// Performance testing notes (JDK 1.4, Jul03, scolebourne)
@ -2675,19 +2675,25 @@ public class StringUtils {
if (replaceChars == null) {
replaceChars = "";
}
StringBuffer buffer = new StringBuffer(str.length());
boolean modified = false;
StringBuffer buf = new StringBuffer(str.length());
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
int index = searchChars.indexOf(ch);
if (index >= 0) {
modified = true;
if (index < replaceChars.length()) {
buffer.append(replaceChars.charAt(index));
buf.append(replaceChars.charAt(index));
}
} else {
buffer.append(ch);
buf.append(ch);
}
}
return buffer.toString();
if (modified) {
return buf.toString();
} else {
return str;
}
}
// Overlay

View File

@ -38,7 +38,7 @@ import junit.textui.TestRunner;
* @author Phil Steitz
* @author Gary D. Gregory
* @author Al Chou
* @version $Id: StringUtilsTest.java,v 1.58 2004/02/18 23:06:19 ggregory Exp $
* @version $Id: StringUtilsTest.java,v 1.59 2004/03/10 23:54:48 scolebourne Exp $
*/
public class StringUtilsTest extends TestCase {
@ -471,8 +471,7 @@ public class StringUtilsTest extends TestCase {
assertEquals("ayzya", StringUtils.replaceChars("abcba", "bc", "yzx"));
assertEquals("abcba", StringUtils.replaceChars("abcba", "z", "w"));
// Comment out for now, delete later when discussion completes [Gary Gregory, Dec 14 2003]
//assertSame("abcba", StringUtils.replaceChars("abcba", "z", "w"));
assertSame("abcba", StringUtils.replaceChars("abcba", "z", "w"));
// Javadoc examples:
assertEquals("jelly", StringUtils.replaceChars("hello", "ho", "jy"));
@ -482,8 +481,9 @@ public class StringUtilsTest extends TestCase {
// From http://nagoya.apache.org/bugzilla/show_bug.cgi?id=25454
assertEquals("bcc", StringUtils.replaceChars("abc", "ab", "bc"));
assertEquals("q651.506bera", StringUtils.replaceChars("d216.102oren", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789",
"nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM567891234"));
assertEquals("q651.506bera", StringUtils.replaceChars("d216.102oren",
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789",
"nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM567891234"));
}
public void testOverlayString_StringStringIntInt() {