diff --git a/platform/util/src/com/intellij/openapi/util/text/StringUtil.java b/platform/util/src/com/intellij/openapi/util/text/StringUtil.java
index c26cb10030c4..c2b7de8ba7ed 100644
--- a/platform/util/src/com/intellij/openapi/util/text/StringUtil.java
+++ b/platform/util/src/com/intellij/openapi/util/text/StringUtil.java
@@ -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
*
* will return the following array: foo\r\n, \n, bar\n, \r\n, baz\r, \r
- *
+ *
*/
@NotNull
@Contract(pure = true)
diff --git a/platform/util/testSrc/com/intellij/util/text/StringUtilTest.java b/platform/util/testSrc/com/intellij/util/text/StringUtilTest.java
index d2f593d13ff1..93ebb038cae1 100644
--- a/platform/util/testSrc/com/intellij/util/text/StringUtilTest.java
+++ b/platform/util/testSrc/com/intellij/util/text/StringUtilTest.java
@@ -111,7 +111,7 @@ public class StringUtilTest extends TestCase {
public void testFormatLinks() {
assertEquals("http://a-b+c", 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");
+ }
}