diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/core/compiler/CharOperation.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/core/compiler/CharOperation.java index 5933dbd1958..dbb7deba4f5 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/core/compiler/CharOperation.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/core/compiler/CharOperation.java @@ -3750,7 +3750,7 @@ public static final char[] replace( next : for (int i = 0; i < max;) { int index = indexOf(toBeReplaced, array, true, i); if (index == -1) { - i++; + i = max; // end continue next; } if (occurrenceCount == starts.length) { diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CharOperationTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CharOperationTest.java index a2164eba878..c61288c267d 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CharOperationTest.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CharOperationTest.java @@ -170,4 +170,26 @@ public void test012() { 4, true)); } + +public void testReplacePerformance001() { + // This is the test that takes an excessively long time + // The improvement here is drastic, 99% reduction in time + testReplaceImpl("This is one line with no matches\n", 9, 0.01); +} + +private void testReplaceImpl(String oneLine, int power, double multiplier) { + String total = oneLine; + for( int i = 0; i < power; i++ ) { + total = total + total; // Double the length + } + total = oneLine + total; + + // Now replace + long start = System.currentTimeMillis(); + char[] found = CharOperation.replace(total.toCharArray(), new char[]{'/'}, new char[] {'z'}); + assertNotNull(found); + long end = System.currentTimeMillis(); + long spent = end - start; + assertTrue(spent < 10); +} }