src/share/classes/java/lang/String.java

Print this page

        

@@ -1750,23 +1750,29 @@
         if (targetCount == 0) {
             return fromIndex;
         }
 
         char first = target[targetOffset];
+        char second = 0;
+        if (targetCount != 1) {
+            second = target[targetOffset + 1];
+        }
         int max = sourceOffset + (sourceCount - targetCount);
 
         for (int i = sourceOffset + fromIndex; i <= max; i++) {
             /* Look for first character. */
             if (source[i] != first) {
                 while (++i <= max && source[i] != first);
             }
-
+            if (targetCount == 1) {
+                return (i <= max) ? i - sourceOffset : -1;
+            }
             /* Found first character, now look at the rest of v2 */
-            if (i <= max) {
-                int j = i + 1;
-                int end = j + targetCount - 1;
-                for (int k = targetOffset + 1; j < end && source[j]
+            if (i <= max && source[i + 1] == second) {
+                int j = i + 2;
+                int end = i + targetCount;
+                for (int k = targetOffset + 2; j < end && source[j]
                         == target[k]; j++, k++);
 
                 if (j == end) {
                     /* Found whole string. */
                     return i - sourceOffset;