Unverified 提交 d36834f9 authored 作者: Noel Grandin's avatar Noel Grandin 提交者: GitHub

Merge pull request #837 from katzyn/misc

Use StringUtils.replaceAll() in Function.replace()
......@@ -1947,23 +1947,7 @@ public class Function extends Expression implements FunctionCall {
if (s == null || replace == null || with == null) {
return null;
}
if (replace.length() == 0) {
// avoid out of memory
return s;
}
StringBuilder buff = new StringBuilder(s.length());
int start = 0;
int len = replace.length();
while (true) {
int i = s.indexOf(replace, start);
if (i == -1) {
break;
}
buff.append(s.substring(start, i)).append(with);
start = i + len;
}
buff.append(s.substring(start));
return buff.toString();
return StringUtils.replaceAll(s, replace, with);
}
private static String repeat(String s, int count) {
......
......@@ -707,7 +707,10 @@ public class StringUtils {
}
/**
* Replace all occurrences of the before string with the after string.
* Replace all occurrences of the before string with the after string. Unlike
* {@link String#replaceAll(String, String)} this method reads {@code before}
* and {@code after} arguments as plain strings and if {@code before} argument
* is an empty string this method returns original string {@code s}.
*
* @param s the string
* @param before the old text
......@@ -716,18 +719,18 @@ public class StringUtils {
*/
public static String replaceAll(String s, String before, String after) {
int next = s.indexOf(before);
if (next < 0) {
if (next < 0 || before.isEmpty()) {
return s;
}
StringBuilder buff = new StringBuilder(
s.length() - before.length() + after.length());
int index = 0;
while (true) {
buff.append(s.substring(index, next)).append(after);
buff.append(s, index, next).append(after);
index = next + before.length();
next = s.indexOf(before, index);
if (next < 0) {
buff.append(s.substring(index));
buff.append(s, index, s.length());
break;
}
}
......
......@@ -225,6 +225,8 @@ public class TestStringUtils extends TestBase {
StringUtils.replaceAll("abcabcabc", "abc", ""));
assertEquals("abcabcabc",
StringUtils.replaceAll("abcabcabc", "aBc", ""));
assertEquals("abcabcabc",
StringUtils.replaceAll("abcabcabc", "", "abc"));
}
private void testTrim() {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论