< prev index next >

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

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * 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
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -193,12 +193,15 @@
      * that this static final field is not statically foldable, and to
      * avoid any possible circular dependency during vm initialization.
      */
     static final boolean COMPACT_STRINGS;
 
+    static final boolean DEBUG_INTRINSICS;
+
     static {
         COMPACT_STRINGS = true;
+        DEBUG_INTRINSICS = false;
     }
 
     /**
      * Class String is special cased within the Serialization Stream Protocol.
      *

@@ -379,11 +382,11 @@
             this.coder = LATIN1;
         } else {
             hibyte <<= 8;
             byte[] val = StringUTF16.newBytesFor(count);
             for (int i = 0; i < count; i++) {
-                StringUTF16.putChar(val, i, hibyte | (ascii[offset++] & 0xff));
+                StringUTF16.Trusted.putChar(val, i, hibyte | (ascii[offset++] & 0xff));
             }
             this.value = val;
             this.coder = UTF16;
         }
     }

@@ -1062,15 +1065,11 @@
             }
         } else {
             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;
     }
 
     /**

@@ -1118,16 +1117,14 @@
                 if ((val[i] & 0xff) != cs.charAt(i)) {
                     return false;
                 }
             }
         } else {
-            for (int i = 0; i < n; i++) {
-                if (StringUTF16.getChar(val, i) != cs.charAt(i)) {
+            if (!StringUTF16.contentEquals(val, cs, n)) {
                     return false;
                 }
             }
-        }
         return true;
     }
 
     /**
      * Compares this {@code String} to another {@code String}, ignoring case

@@ -1332,17 +1329,17 @@
             }
         } else {
             if (coder() == LATIN1) {
                 while (len-- > 0) {
                     if (StringLatin1.getChar(tv, toffset++) !=
-                        StringUTF16.getChar(ov, ooffset++)) {
+                        StringUTF16.Trusted.getChar(ov, ooffset++)) {
                         return false;
                     }
                 }
             } else {
                 while (len-- > 0) {
-                    if (StringUTF16.getChar(tv, toffset++) !=
+                    if (StringUTF16.Trusted.getChar(tv, toffset++) !=
                         StringLatin1.getChar(ov, ooffset++)) {
                         return false;
                     }
                 }
             }

@@ -1460,11 +1457,11 @@
             if (isLatin1()) {  // && pcoder == UTF16
                 return false;
             }
             // coder == UTF16 && pcoder == LATIN1)
             while (po < pc) {
-                if (StringUTF16.getChar(ta, toffset++) != (pa[po++] & 0xff)) {
+                if (StringUTF16.Trusted.getChar(ta, toffset++) != (pa[po++] & 0xff)) {
                     return false;
                }
             }
         }
         return true;

@@ -1732,10 +1729,13 @@
             fromIndex = 0;
         }
         if (tgtCount == 0) {
             return fromIndex;
         }
+        if (tgtCount > srcCount) {
+            return -1;
+        }
         if (srcCoder == tgtCoder) {
             return srcCoder == LATIN1
                 ? StringLatin1.indexOf(src, srcCount, tgt, tgtCount, fromIndex)
                 : StringUTF16.indexOf(src, srcCount, tgt, tgtCount, fromIndex);
         }

@@ -1805,16 +1805,16 @@
         /*
          * Check arguments; return immediately where possible. For
          * consistency, don't check for null str.
          */
         int rightIndex = srcCount - tgtCount;
-        if (fromIndex < 0) {
-            return -1;
-        }
         if (fromIndex > rightIndex) {
             fromIndex = rightIndex;
         }
+        if (fromIndex < 0) {
+            return -1;
+        }
         /* Empty string always matches. */
         if (tgtCount == 0) {
             return fromIndex;
         }
         if (srcCoder == tgtCoder) {

@@ -1824,34 +1824,11 @@
         }
         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;
-        }
+        return StringUTF16.lastIndexOfLatin1(src, srcCount, tgt, tgtCount, fromIndex);
     }
 
     /**
      * Returns a string that is a substring of this string. The
      * substring begins with the character at the specified index and

@@ -3078,11 +3055,12 @@
      * StringIndexOutOfBoundsException  if {@code index} is
      * negative or greater than or equal to {@code length}.
      */
     static void checkIndex(int index, int length) {
         if (index < 0 || index >= length) {
-            throw new StringIndexOutOfBoundsException("index " + index);
+            throw new StringIndexOutOfBoundsException("index " + index +
+                                                      ",length " + length);
         }
     }
 
     /*
      * StringIndexOutOfBoundsException  if {@code offset}

@@ -3116,11 +3094,11 @@
      *
      * @throws  StringIndexOutOfBoundsException
      *          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);
         }
     }
< prev index next >