mirror of
https://github.com/apache/commons-lang.git
synced 2025-02-06 10:08:32 +00:00
Fix infinite recursion in replace() when blank string used
from Holger Krauth git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137231 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
7ce43857cd
commit
c2d07cc3f0
@ -73,8 +73,9 @@
|
||||
* @author <a href="mailto:rand_mcneely@yahoo.com">Rand McNeely</a>
|
||||
* @author Stephen Colebourne
|
||||
* @author <a href="mailto:fredrik@westermarck.com">Fredrik Westermarck</a>
|
||||
* @author Holger Krauth
|
||||
* @since 1.0
|
||||
* @version $Id: StringUtils.java,v 1.30 2002/12/23 00:32:24 scolebourne Exp $
|
||||
* @version $Id: StringUtils.java,v 1.31 2003/01/19 18:15:38 scolebourne Exp $
|
||||
*/
|
||||
public class StringUtils {
|
||||
|
||||
@ -584,7 +585,9 @@ public static String join(Iterator iterator, String separator) {
|
||||
|
||||
/**
|
||||
* <p>Replace a String with another String inside a larger String, once.</p>
|
||||
*
|
||||
*
|
||||
* <p>A <code>null</code> reference passed to this method is a no-op.</p>
|
||||
*
|
||||
* @see #replace(String text, String repl, String with, int max)
|
||||
* @param text text to search and replace in
|
||||
* @param repl String to search for
|
||||
@ -598,6 +601,8 @@ public static String replaceOnce(String text, String repl, String with) {
|
||||
/**
|
||||
* <p>Replace all occurances of a String within another String.</p>
|
||||
*
|
||||
* <p>A <code>null</code> reference passed to this method is a no-op.</p>
|
||||
*
|
||||
* @see #replace(String text, String repl, String with, int max)
|
||||
* @param text text to search and replace in
|
||||
* @param repl String to search for
|
||||
@ -612,21 +617,17 @@ public static String replace(String text, String repl, String with) {
|
||||
* <p>Replace a String with another String inside a larger String,
|
||||
* for the first <code>max</code> values of the search String.</p>
|
||||
*
|
||||
* <p>A <code>null</code> reference is passed to this method is a
|
||||
* no-op.</p>
|
||||
* <p>A <code>null</code> reference passed to this method is a no-op.</p>
|
||||
*
|
||||
* @param text text to search and replace in
|
||||
* @param repl String to search for
|
||||
* @param with String to replace with
|
||||
* @param max maximum number of values to replace, or
|
||||
* <code>-1</code> if no maximum
|
||||
* @param max maximum number of values to replace, or <code>-1</code> if no maximum
|
||||
* @return the text with any replacements processed
|
||||
* @throws NullPointerException if repl is <code>null</code>
|
||||
*/
|
||||
public static String replace(String text, String repl, String with,
|
||||
int max) {
|
||||
if (text == null) {
|
||||
return null;
|
||||
public static String replace(String text, String repl, String with, int max) {
|
||||
if (text == null || repl == null || with == null || repl.length() == 0) {
|
||||
return text;
|
||||
}
|
||||
|
||||
StringBuffer buf = new StringBuffer(text.length());
|
||||
|
@ -1,5 +1,3 @@
|
||||
package org.apache.commons.lang;
|
||||
|
||||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
@ -53,6 +51,7 @@
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.lang;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@ -65,13 +64,14 @@
|
||||
*
|
||||
* @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
|
||||
* @author <a href="mailto:bayard@generationjava.com">Henri Yandell</a>
|
||||
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
|
||||
* @author Stephen Colebourne
|
||||
* @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
|
||||
* @author <a href="mailto:fredrik@westermarck.com>Fredrik Westermarck</a>
|
||||
* @version $Id: StringUtilsTest.java,v 1.11 2002/12/07 21:50:30 bayard Exp $
|
||||
* @author Holger Krauth
|
||||
* @version $Id: StringUtilsTest.java,v 1.12 2003/01/19 18:15:38 scolebourne Exp $
|
||||
*/
|
||||
public class StringUtilsTest extends TestCase
|
||||
{
|
||||
public class StringUtilsTest extends TestCase {
|
||||
|
||||
private static final String[] ARRAY_LIST = { "foo", "bar", "baz" };
|
||||
|
||||
private static final String SEPARATOR = ",";
|
||||
@ -108,8 +108,7 @@ protected void tearDown() throws Exception {
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
public void testCaseFunctions()
|
||||
{
|
||||
public void testCaseFunctions() {
|
||||
assertEquals("capitalise(String) failed",
|
||||
CAP_FOO, StringUtils.capitalise(FOO) );
|
||||
assertEquals("capitalise(empty-string) failed",
|
||||
@ -144,8 +143,7 @@ public void testCaseFunctions()
|
||||
"Hello aPACHE", StringUtils.swapCase("hELLO Apache") );
|
||||
}
|
||||
|
||||
public void testJoin()
|
||||
{
|
||||
public void testJoin() {
|
||||
assertEquals("concatenate(Object[]) failed",
|
||||
"foobarbaz", StringUtils.concatenate(ARRAY_LIST));
|
||||
assertEquals("join(Object[], String) failed", TEXT_LIST,
|
||||
@ -155,8 +153,7 @@ public void testJoin()
|
||||
SEPARATOR));
|
||||
}
|
||||
|
||||
public void testSplit()
|
||||
{
|
||||
public void testSplit() {
|
||||
String[] result = StringUtils.split(TEXT_LIST, SEPARATOR, 2);
|
||||
String[] expected = { "foo", "bar,baz" };
|
||||
assertEquals("split(Object[], String, int) yielded unexpected length",
|
||||
@ -193,8 +190,7 @@ public void testSplit()
|
||||
assertEquals("split(Object[], null, int)[2] failed", "three four five six", result[2]);
|
||||
}
|
||||
|
||||
public void testReplaceFunctions()
|
||||
{
|
||||
public void testReplaceFunctions() {
|
||||
assertEquals("replace(String, String, String, int) failed",
|
||||
FOO, StringUtils.replace("oo" + FOO, "o", "", 2));
|
||||
assertEquals("replace(String, String, String) failed",
|
||||
@ -203,29 +199,35 @@ public void testReplaceFunctions()
|
||||
FOO, StringUtils.replaceOnce(FOO + FOO, FOO, ""));
|
||||
assertEquals("carriage-return replace(String,String,String) failed",
|
||||
"test123", StringUtils.replace("test\r1\r2\r3", "\r", ""));
|
||||
|
||||
assertEquals("replace(String, String, String) failed",
|
||||
"FOO", StringUtils.replace("FOO", "", "any"));
|
||||
assertEquals("replace(String, String, String) failed",
|
||||
"FOO", StringUtils.replace("FOO", null, "any"));
|
||||
assertEquals("replace(String, String, String) failed",
|
||||
"FOO", StringUtils.replace("FOO", "F", null));
|
||||
assertEquals("replace(String, String, String) failed",
|
||||
"FOO", StringUtils.replace("FOO", null, null));
|
||||
assertEquals("replace(String, String, String) failed",
|
||||
null, StringUtils.replace(null, "", "any"));
|
||||
}
|
||||
|
||||
public void testOverlayString()
|
||||
{
|
||||
public void testOverlayString() {
|
||||
assertEquals("overlayString(String, String, int, int) failed",
|
||||
"foo foor baz", StringUtils.overlayString(SENTENCE, FOO, 4, 6) );
|
||||
}
|
||||
|
||||
public void testRepeat()
|
||||
{
|
||||
public void testRepeat() {
|
||||
assertEquals("repeat(String, int) failed",
|
||||
FOO + FOO + FOO, StringUtils.repeat(FOO, 3) );
|
||||
}
|
||||
|
||||
public void testCenter()
|
||||
{
|
||||
public void testCenter() {
|
||||
assertEquals("center(String, int) failed",
|
||||
" "+FOO+" ", StringUtils.center(FOO, 9) );
|
||||
}
|
||||
|
||||
public void testChompFunctions()
|
||||
{
|
||||
|
||||
public void testChompFunctions() {
|
||||
assertEquals("chomp(String) failed",
|
||||
FOO, StringUtils.chomp(FOO + "\n" + FOO) );
|
||||
|
||||
@ -248,8 +250,7 @@ public void testChompFunctions()
|
||||
FOO, StringUtils.chopNewline(FOO + "\r\n") );
|
||||
}
|
||||
|
||||
public void testPadFunctions()
|
||||
{
|
||||
public void testPadFunctions() {
|
||||
assertEquals("rightPad(String, int) failed",
|
||||
"1234 ", StringUtils.rightPad ("1234", 8) );
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user