replace return replacement if text equals to replaced text (avoid String creation)

This commit is contained in:
Vladimir Krivosheev
2014-09-05 10:06:23 +02:00
parent 45595ef17b
commit 7a403dd8a5
2 changed files with 29 additions and 11 deletions
@@ -153,17 +153,26 @@ public class StringUtil extends StringUtilRt {
int i = 0;
while (i < text.length()) {
final int i1 = ignoreCase? indexOfIgnoreCase(text, oldS, i) : text.indexOf(oldS, i);
if (i1 < 0) {
if (i == 0) return text;
final int index = ignoreCase? indexOfIgnoreCase(text, oldS, i) : text.indexOf(oldS, i);
if (index < 0) {
if (i == 0) {
return text;
}
newText.append(text, i, text.length());
break;
}
else {
if (newText == null) newText = new StringBuilder(text.length() - i);
newText.append(text, i, i1);
if (newText == null) {
if (text.length() == oldS.length()) {
return newS;
}
newText = new StringBuilder(text.length() - i);
}
newText.append(text, i, index);
newText.append(newS);
i = i1 + oldS.length();
i = index + oldS.length();
}
}
return newText != null ? newText.toString() : "";
@@ -1786,7 +1795,7 @@ public class StringUtil extends StringUtilRt {
public static boolean contains(@NotNull CharSequence sequence, @NotNull CharSequence infix) {
return indexOf(sequence, infix) >= 0;
}
@Contract(pure = true)
public static int indexOf(@NotNull CharSequence sequence, @NotNull CharSequence infix) {
for (int i = 0; i < sequence.length() - infix.length(); i++) {
@@ -2448,7 +2457,7 @@ public class StringUtil extends StringUtilRt {
* \r<br>
* </blockquote>
* will return the following array: foo\r\n, \n, bar\n, \r\n, baz\r, \r
*
*
*/
@NotNull
@Contract(pure = true)
@@ -111,7 +111,7 @@ public class StringUtilTest extends TestCase {
public void testFormatLinks() {
assertEquals("<a href=\"http://a-b+c\">http://a-b+c</a>", StringUtil.formatLinks("http://a-b+c"));
}
public void testCopyHeapCharBuffer() {
String s = "abcde";
CharBuffer buffer = CharBuffer.allocate(s.length());
@@ -124,7 +124,7 @@ public class StringUtilTest extends TestCase {
assertNull(CharArrayUtil.fromSequenceWithoutCopying(buffer.subSequence(1, 5)));
assertNull(CharArrayUtil.fromSequenceWithoutCopying(buffer.subSequence(1, 2)));
}
public void testTitleCase() {
assertEquals("Couldn't Connect to Debugger", StringUtil.wordsToBeginFromUpperCase("Couldn't connect to debugger"));
}
@@ -198,7 +198,7 @@ public class StringUtilTest extends TestCase {
assertEquals(Arrays.asList("aa"), Arrays.asList(StringUtil.splitByLinesKeepSeparators("aa")));
assertEquals(Arrays.asList("\n", "\n", "aa\n", "\n", "bb\n", "cc\n", "\n"),
Arrays.asList(StringUtil.splitByLinesKeepSeparators("\n\naa\n\nbb\ncc\n\n")));
assertEquals(Arrays.asList("\r", "\r\n", "\r"), Arrays.asList(StringUtil.splitByLinesKeepSeparators("\r\r\n\r")));
assertEquals(Arrays.asList("\r\n", "\r", "\r\n"), Arrays.asList(StringUtil.splitByLinesKeepSeparators("\r\n\r\r\n")));
@@ -224,4 +224,13 @@ public class StringUtilTest extends TestCase {
}
}
}
public void testReplaceReturnReplacementIfTextEqualsToReplacedText() {
String newS = "/tmp";
assertSame(StringUtil.replace("$PROJECT_FILE$", "$PROJECT_FILE$".toLowerCase().toUpperCase() /* ensure new String instance */, newS), newS);
}
public void testReplace() {
assertEquals(StringUtil.replace("$PROJECT_FILE$/filename", "$PROJECT_FILE$", "/tmp"), "/tmp/filename");
}
}