Optimize the performance of StringUtils.replace
bug 36583, from Chris Hyzer git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@279977 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
7240ef6dec
commit
7c818a9f22
|
@ -226,6 +226,9 @@ limitations under the License.
|
||||||
<contributor>
|
<contributor>
|
||||||
<name>Oliver Heger</name>
|
<name>Oliver Heger</name>
|
||||||
</contributor>
|
</contributor>
|
||||||
|
<contributor>
|
||||||
|
<name>Chris Hyzer</name>
|
||||||
|
</contributor>
|
||||||
<contributor>
|
<contributor>
|
||||||
<name>Marc Johnson</name>
|
<name>Marc Johnson</name>
|
||||||
</contributor>
|
</contributor>
|
||||||
|
|
|
@ -111,6 +111,7 @@
|
||||||
* @author Al Chou
|
* @author Al Chou
|
||||||
* @author Michael Davey
|
* @author Michael Davey
|
||||||
* @author Reuben Sivan
|
* @author Reuben Sivan
|
||||||
|
* @author Chris Hyzer
|
||||||
* @since 1.0
|
* @since 1.0
|
||||||
* @version $Id$
|
* @version $Id$
|
||||||
*/
|
*/
|
||||||
|
@ -2956,19 +2957,25 @@ public static String replace(String text, String repl, String with) {
|
||||||
* <code>null</code> if null String input
|
* <code>null</code> if null String input
|
||||||
*/
|
*/
|
||||||
public static String replace(String text, String repl, String with, int max) {
|
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;
|
return text;
|
||||||
}
|
}
|
||||||
|
int start = 0;
|
||||||
StringBuffer buf = new StringBuffer(text.length());
|
int end = text.indexOf(repl, start);
|
||||||
int start = 0, end = 0;
|
if (end == -1) {
|
||||||
while ((end = text.indexOf(repl, start)) != -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);
|
buf.append(text.substring(start, end)).append(with);
|
||||||
start = end + repl.length();
|
start = end + repl.length();
|
||||||
|
|
||||||
if (--max == 0) {
|
if (--max == 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
end = text.indexOf(repl, start);
|
||||||
}
|
}
|
||||||
buf.append(text.substring(start));
|
buf.append(text.substring(start));
|
||||||
return buf.toString();
|
return buf.toString();
|
||||||
|
|
|
@ -804,12 +804,23 @@ public void testReplace_StringStringStringInt() {
|
||||||
assertEquals(null, StringUtils.replace(null, "any", null, 2));
|
assertEquals(null, StringUtils.replace(null, "any", null, 2));
|
||||||
assertEquals(null, StringUtils.replace(null, "any", "any", 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("f", StringUtils.replace("oofoo", "o", "", -1));
|
||||||
assertEquals("oofoo", StringUtils.replace("oofoo", "o", "", 0));
|
assertEquals("oofoo", StringUtils.replace("oofoo", "o", "", 0));
|
||||||
assertEquals("ofoo", StringUtils.replace("oofoo", "o", "", 1));
|
assertEquals("ofoo", StringUtils.replace("oofoo", "o", "", 1));
|
||||||
assertEquals("foo", StringUtils.replace("oofoo", "o", "", 2));
|
assertEquals("foo", StringUtils.replace("oofoo", "o", "", 2));
|
||||||
assertEquals("fo", StringUtils.replace("oofoo", "o", "", 3));
|
assertEquals("fo", StringUtils.replace("oofoo", "o", "", 3));
|
||||||
assertEquals("f", StringUtils.replace("oofoo", "o", "", 4));
|
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() {
|
public void testReplaceOnce_StringStringString() {
|
||||||
|
|
Loading…
Reference in New Issue