From 694d1de4a912dcb1baade3edfecbe6e152c12e24 Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Wed, 10 Mar 2004 23:54:48 +0000 Subject: [PATCH] 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 --- .../org/apache/commons/lang/StringUtils.java | 16 +++++++++++----- .../org/apache/commons/lang/StringUtilsTest.java | 10 +++++----- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/java/org/apache/commons/lang/StringUtils.java b/src/java/org/apache/commons/lang/StringUtils.java index 7e2e5d15d..22a8188e1 100644 --- a/src/java/org/apache/commons/lang/StringUtils.java +++ b/src/java/org/apache/commons/lang/StringUtils.java @@ -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 diff --git a/src/test/org/apache/commons/lang/StringUtilsTest.java b/src/test/org/apache/commons/lang/StringUtilsTest.java index 6da51d24e..d1ab8d986 100644 --- a/src/test/org/apache/commons/lang/StringUtilsTest.java +++ b/src/test/org/apache/commons/lang/StringUtilsTest.java @@ -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() {