diff --git a/project.xml b/project.xml
index eb0f803fd..90f2b2576 100644
--- a/project.xml
+++ b/project.xml
@@ -226,6 +226,9 @@ limitations under the License.
Oliver Heger
+
+ Chris Hyzer
+
Marc Johnson
diff --git a/src/java/org/apache/commons/lang/StringUtils.java b/src/java/org/apache/commons/lang/StringUtils.java
index 4a8f7e177..1db4ca381 100644
--- a/src/java/org/apache/commons/lang/StringUtils.java
+++ b/src/java/org/apache/commons/lang/StringUtils.java
@@ -111,6 +111,7 @@
* @author Al Chou
* @author Michael Davey
* @author Reuben Sivan
+ * @author Chris Hyzer
* @since 1.0
* @version $Id$
*/
@@ -2956,19 +2957,25 @@ public static String replace(String text, String repl, String with) {
* null
if null String input
*/
public static String replace(String text, String repl, String with, int max) {
- if (text == null || isEmpty(repl) || with == null || max == 0) {
+ if (isEmpty(text) || isEmpty(repl) || with == null || max == 0) {
return text;
}
-
- StringBuffer buf = new StringBuffer(text.length());
- int start = 0, end = 0;
- while ((end = text.indexOf(repl, start)) != -1) {
+ int start = 0;
+ int end = text.indexOf(repl, start);
+ if (end == -1) {
+ return text;
+ }
+ int increase = with.length() - repl.length();
+ increase = (increase < 0 ? 0 : increase);
+ increase *= (max < 0 ? 16 : (max > 64 ? 64 : max));
+ StringBuffer buf = new StringBuffer(text.length() + increase);
+ while (end != -1) {
buf.append(text.substring(start, end)).append(with);
start = end + repl.length();
-
if (--max == 0) {
break;
}
+ end = text.indexOf(repl, start);
}
buf.append(text.substring(start));
return buf.toString();
diff --git a/src/test/org/apache/commons/lang/StringUtilsTest.java b/src/test/org/apache/commons/lang/StringUtilsTest.java
index 9d067d52d..59a995338 100644
--- a/src/test/org/apache/commons/lang/StringUtilsTest.java
+++ b/src/test/org/apache/commons/lang/StringUtilsTest.java
@@ -804,12 +804,23 @@ public void testReplace_StringStringStringInt() {
assertEquals(null, StringUtils.replace(null, "any", null, 2));
assertEquals(null, StringUtils.replace(null, "any", "any", 2));
+ assertEquals("", StringUtils.replace("", null, null, 2));
+ assertEquals("", StringUtils.replace("", null, "any", 2));
+ assertEquals("", StringUtils.replace("", "any", null, 2));
+ assertEquals("", StringUtils.replace("", "any", "any", 2));
+
+ String str = new String(new char[] {'o', 'o', 'f', 'o', 'o'});
+ assertSame(str, StringUtils.replace(str, "x", "", -1));
+
assertEquals("f", StringUtils.replace("oofoo", "o", "", -1));
assertEquals("oofoo", StringUtils.replace("oofoo", "o", "", 0));
assertEquals("ofoo", StringUtils.replace("oofoo", "o", "", 1));
assertEquals("foo", StringUtils.replace("oofoo", "o", "", 2));
assertEquals("fo", StringUtils.replace("oofoo", "o", "", 3));
assertEquals("f", StringUtils.replace("oofoo", "o", "", 4));
+
+ assertEquals("f", StringUtils.replace("oofoo", "o", "", -5));
+ assertEquals("f", StringUtils.replace("oofoo", "o", "", 1000));
}
public void testReplaceOnce_StringStringString() {