jdk/src/java.base/share/classes/java/lang/String.java
changeset 44642 331e669007f7
parent 44369 b5c4e28a7521
child 44846 b3f9f5bf40b2
--- a/jdk/src/java.base/share/classes/java/lang/String.java	Wed Apr 12 12:57:49 2017 -0700
+++ b/jdk/src/java.base/share/classes/java/lang/String.java	Wed Apr 12 16:37:33 2017 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1994, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1994, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -1064,11 +1064,7 @@
             if (!isLatin1()) {  // utf16 str and latin1 abs can never be "equal"
                 return false;
             }
-            for (int i = 0; i < len; i++) {
-                if ((char)(v1[i] & 0xff) != StringUTF16.getChar(v2, i)) {
-                    return false;
-                }
-            }
+            return StringUTF16.contentEquals(v1, v2, len);
         }
         return true;
     }
@@ -1120,10 +1116,8 @@
                 }
             }
         } else {
-            for (int i = 0; i < n; i++) {
-                if (StringUTF16.getChar(val, i) != cs.charAt(i)) {
-                    return false;
-                }
+            if (!StringUTF16.contentEquals(val, cs, n)) {
+                return false;
             }
         }
         return true;
@@ -1734,6 +1728,9 @@
         if (tgtCount == 0) {
             return fromIndex;
         }
+        if (tgtCount > srcCount) {
+            return -1;
+        }
         if (srcCoder == tgtCoder) {
             return srcCoder == LATIN1
                 ? StringLatin1.indexOf(src, srcCount, tgt, tgtCount, fromIndex)
@@ -1792,7 +1789,7 @@
      * is the string being searched for.
      *
      * @param   src         the characters being searched.
-     * @param   srcCoder  coder handles the mapping between bytes/chars
+     * @param   srcCoder    coder handles the mapping between bytes/chars
      * @param   srcCount    count of the source string.
      * @param   tgt         the characters being searched for.
      * @param   fromIndex   the index to begin searching from.
@@ -1807,12 +1804,12 @@
          * consistency, don't check for null str.
          */
         int rightIndex = srcCount - tgtCount;
+        if (fromIndex > rightIndex) {
+            fromIndex = rightIndex;
+        }
         if (fromIndex < 0) {
             return -1;
         }
-        if (fromIndex > rightIndex) {
-            fromIndex = rightIndex;
-        }
         /* Empty string always matches. */
         if (tgtCount == 0) {
             return fromIndex;
@@ -1825,31 +1822,8 @@
         if (srcCoder == LATIN1) {    // && tgtCoder == UTF16
             return -1;
         }
-                                     // srcCoder == UTF16 && tgtCoder == LATIN1
-        int min = tgtCount - 1;
-        int i = min + fromIndex;
-        int strLastIndex = tgtCount - 1;
-
-        char strLastChar = (char)(tgt[strLastIndex] & 0xff);
-    startSearchForLastChar:
-        while (true) {
-            while (i >= min && StringUTF16.getChar(src, i) != strLastChar) {
-                i--;
-            }
-            if (i < min) {
-                return -1;
-            }
-            int j = i - 1;
-            int start = j - strLastIndex;
-            int k = strLastIndex - 1;
-            while (j > start) {
-                if (StringUTF16.getChar(src, j--) != (tgt[k--] & 0xff)) {
-                    i--;
-                    continue startSearchForLastChar;
-                }
-            }
-            return start + 1;
-        }
+        // srcCoder == UTF16 && tgtCoder == LATIN1
+        return StringUTF16.lastIndexOfLatin1(src, srcCount, tgt, tgtCount, fromIndex);
     }
 
     /**
@@ -3078,7 +3052,8 @@
      */
     static void checkIndex(int index, int length) {
         if (index < 0 || index >= length) {
-            throw new StringIndexOutOfBoundsException("index " + index);
+            throw new StringIndexOutOfBoundsException("index " + index +
+                                                      ",length " + length);
         }
     }
 
@@ -3116,7 +3091,7 @@
      *          If {@code begin} is negative, {@code begin} is greater than
      *          {@code end}, or {@code end} is greater than {@code length}.
      */
-    private static void checkBoundsBeginEnd(int begin, int end, int length) {
+    static void checkBoundsBeginEnd(int begin, int end, int length) {
         if (begin < 0 || begin > end || end > length) {
             throw new StringIndexOutOfBoundsException(
                 "begin " + begin + ", end " + end + ", length " + length);