< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 1994, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 178      *
 179      *    if (coder() == LATIN1) { ... }
 180      *
 181      * or:
 182      *
 183      *    if (COMPACT_STRINGS && coder == LATIN1) { ... }
 184      *
 185      * An optimizing JIT compiler can fold the above conditional as:
 186      *
 187      *    COMPACT_STRINGS == true  => if (coder == LATIN1) { ... }
 188      *    COMPACT_STRINGS == false => if (false)           { ... }
 189      *
 190      * @implNote
 191      * The actual value for this field is injected by JVM. The static
 192      * initialization block is used to set the value here to communicate
 193      * that this static final field is not statically foldable, and to
 194      * avoid any possible circular dependency during vm initialization.
 195      */
 196     static final boolean COMPACT_STRINGS;
 197 


 198     static {
 199         COMPACT_STRINGS = true;

 200     }
 201 
 202     /**
 203      * Class String is special cased within the Serialization Stream Protocol.
 204      *
 205      * A String instance is written into an ObjectOutputStream according to
 206      * <a href="{@docRoot}/../platform/serialization/spec/output.html">
 207      * Object Serialization Specification, Section 6.2, "Stream Elements"</a>
 208      */
 209     private static final ObjectStreamField[] serialPersistentFields =
 210         new ObjectStreamField[0];
 211 
 212     /**
 213      * Initializes a newly created {@code String} object so that it represents
 214      * an empty character sequence.  Note that use of this constructor is
 215      * unnecessary since Strings are immutable.
 216      */
 217     public String() {
 218         this.value = "".value;
 219         this.coder = "".coder;


 364      * @see  #String(byte[], int, int)
 365      * @see  #String(byte[], java.lang.String)
 366      * @see  #String(byte[], java.nio.charset.Charset)
 367      * @see  #String(byte[])
 368      */
 369     @Deprecated(since="1.1")
 370     public String(byte ascii[], int hibyte, int offset, int count) {
 371         checkBoundsOffCount(offset, count, ascii.length);
 372         if (count == 0) {
 373             this.value = "".value;
 374             this.coder = "".coder;
 375             return;
 376         }
 377         if (COMPACT_STRINGS && (byte)hibyte == 0) {
 378             this.value = Arrays.copyOfRange(ascii, offset, offset + count);
 379             this.coder = LATIN1;
 380         } else {
 381             hibyte <<= 8;
 382             byte[] val = StringUTF16.newBytesFor(count);
 383             for (int i = 0; i < count; i++) {
 384                 StringUTF16.putChar(val, i, hibyte | (ascii[offset++] & 0xff));
 385             }
 386             this.value = val;
 387             this.coder = UTF16;
 388         }
 389     }
 390 
 391     /**
 392      * Allocates a new {@code String} containing characters constructed from
 393      * an array of 8-bit integer values. Each character <i>c</i>in the
 394      * resulting string is constructed from the corresponding component
 395      * <i>b</i> in the byte array such that:
 396      *
 397      * <blockquote><pre>
 398      *     <b><i>c</i></b> == (char)(((hibyte &amp; 0xff) &lt;&lt; 8)
 399      *                         | (<b><i>b</i></b> &amp; 0xff))
 400      * </pre></blockquote>
 401      *
 402      * @deprecated  This method does not properly convert bytes into
 403      * characters.  As of JDK&nbsp;1.1, the preferred way to do this is via the
 404      * {@code String} constructors that take a {@link


1047     }
1048 
1049     private boolean nonSyncContentEquals(AbstractStringBuilder sb) {
1050         int len = length();
1051         if (len != sb.length()) {
1052             return false;
1053         }
1054         byte v1[] = value;
1055         byte v2[] = sb.getValue();
1056         if (coder() == sb.getCoder()) {
1057             int n = v1.length;
1058             for (int i = 0; i < n; i++) {
1059                 if (v1[i] != v2[i]) {
1060                     return false;
1061                 }
1062             }
1063         } else {
1064             if (!isLatin1()) {  // utf16 str and latin1 abs can never be "equal"
1065                 return false;
1066             }
1067             for (int i = 0; i < len; i++) {
1068                 if ((char)(v1[i] & 0xff) != StringUTF16.getChar(v2, i)) {
1069                     return false;
1070                 }
1071             }
1072         }
1073         return true;
1074     }
1075 
1076     /**
1077      * Compares this string to the specified {@code CharSequence}.  The
1078      * result is {@code true} if and only if this {@code String} represents the
1079      * same sequence of char values as the specified sequence. Note that if the
1080      * {@code CharSequence} is a {@code StringBuffer} then the method
1081      * synchronizes on it.
1082      *
1083      * <p>For finer-grained String comparison, refer to
1084      * {@link java.text.Collator}.
1085      *
1086      * @param  cs
1087      *         The sequence to compare this {@code String} against
1088      *
1089      * @return  {@code true} if this {@code String} represents the same
1090      *          sequence of char values as the specified sequence, {@code
1091      *          false} otherwise


1103                 return nonSyncContentEquals((AbstractStringBuilder)cs);
1104             }
1105         }
1106         // Argument is a String
1107         if (cs instanceof String) {
1108             return equals(cs);
1109         }
1110         // Argument is a generic CharSequence
1111         int n = cs.length();
1112         if (n != length()) {
1113             return false;
1114         }
1115         byte[] val = this.value;
1116         if (isLatin1()) {
1117             for (int i = 0; i < n; i++) {
1118                 if ((val[i] & 0xff) != cs.charAt(i)) {
1119                     return false;
1120                 }
1121             }
1122         } else {
1123             for (int i = 0; i < n; i++) {
1124                 if (StringUTF16.getChar(val, i) != cs.charAt(i)) {
1125                     return false;
1126                 }
1127             }
1128         }
1129         return true;
1130     }
1131 
1132     /**
1133      * Compares this {@code String} to another {@code String}, ignoring case
1134      * considerations.  Two strings are considered equal ignoring case if they
1135      * are of the same length and corresponding characters in the two strings
1136      * are equal ignoring case.
1137      *
1138      * <p> Two characters {@code c1} and {@code c2} are considered the same
1139      * ignoring case if at least one of the following is true:
1140      * <ul>
1141      *   <li> The two characters are the same (as compared by the
1142      *        {@code ==} operator)
1143      *   <li> Calling {@code Character.toLowerCase(Character.toUpperCase(char))}
1144      *        on each character produces the same result
1145      * </ul>
1146      *
1147      * <p>Note that this method does <em>not</em> take locale into account, and
1148      * will result in unsatisfactory results for certain locales.  The


1317         if ((ooffset < 0) || (toffset < 0) ||
1318              (toffset > (long)length() - len) ||
1319              (ooffset > (long)other.length() - len)) {
1320             return false;
1321         }
1322         if (coder() == other.coder()) {
1323             if (!isLatin1() && (len > 0)) {
1324                 toffset = toffset << 1;
1325                 ooffset = ooffset << 1;
1326                 len = len << 1;
1327             }
1328             while (len-- > 0) {
1329                 if (tv[toffset++] != ov[ooffset++]) {
1330                     return false;
1331                 }
1332             }
1333         } else {
1334             if (coder() == LATIN1) {
1335                 while (len-- > 0) {
1336                     if (StringLatin1.getChar(tv, toffset++) !=
1337                         StringUTF16.getChar(ov, ooffset++)) {
1338                         return false;
1339                     }
1340                 }
1341             } else {
1342                 while (len-- > 0) {
1343                     if (StringUTF16.getChar(tv, toffset++) !=
1344                         StringLatin1.getChar(ov, ooffset++)) {
1345                         return false;
1346                     }
1347                 }
1348             }
1349         }
1350         return true;
1351     }
1352 
1353     /**
1354      * Tests if two string regions are equal.
1355      * <p>
1356      * A substring of this {@code String} object is compared to a substring
1357      * of the argument {@code other}. The result is {@code true} if these
1358      * substrings represent character sequences that are the same, ignoring
1359      * case if and only if {@code ignoreCase} is true. The substring of
1360      * this {@code String} object to be compared begins at index
1361      * {@code toffset} and has length {@code len}. The substring of
1362      * {@code other} to be compared begins at index {@code ooffset} and
1363      * has length {@code len}. The result is {@code false} if and only if


1445         if (toffset < 0 || toffset > length() - prefix.length()) {
1446             return false;
1447         }
1448         byte ta[] = value;
1449         byte pa[] = prefix.value;
1450         int po = 0;
1451         int pc = pa.length;
1452         if (coder() == prefix.coder()) {
1453             int to = isLatin1() ? toffset : toffset << 1;
1454             while (po < pc) {
1455                 if (ta[to++] != pa[po++]) {
1456                     return false;
1457                 }
1458             }
1459         } else {
1460             if (isLatin1()) {  // && pcoder == UTF16
1461                 return false;
1462             }
1463             // coder == UTF16 && pcoder == LATIN1)
1464             while (po < pc) {
1465                 if (StringUTF16.getChar(ta, toffset++) != (pa[po++] & 0xff)) {
1466                     return false;
1467                }
1468             }
1469         }
1470         return true;
1471     }
1472 
1473     /**
1474      * Tests if this string starts with the specified prefix.
1475      *
1476      * @param   prefix   the prefix.
1477      * @return  {@code true} if the character sequence represented by the
1478      *          argument is a prefix of the character sequence represented by
1479      *          this string; {@code false} otherwise.
1480      *          Note also that {@code true} will be returned if the
1481      *          argument is an empty string or is equal to this
1482      *          {@code String} object as determined by the
1483      *          {@link #equals(Object)} method.
1484      * @since   1.0
1485      */


1717      * @param   srcCoder  the coder of the source string.
1718      * @param   srcCount  length of the source string.
1719      * @param   tgtStr    the characters being searched for.
1720      * @param   fromIndex the index to begin searching from.
1721      */
1722     static int indexOf(byte[] src, byte srcCoder, int srcCount,
1723                        String tgtStr, int fromIndex) {
1724         byte[] tgt    = tgtStr.value;
1725         byte tgtCoder = tgtStr.coder();
1726         int tgtCount  = tgtStr.length();
1727 
1728         if (fromIndex >= srcCount) {
1729             return (tgtCount == 0 ? srcCount : -1);
1730         }
1731         if (fromIndex < 0) {
1732             fromIndex = 0;
1733         }
1734         if (tgtCount == 0) {
1735             return fromIndex;
1736         }



1737         if (srcCoder == tgtCoder) {
1738             return srcCoder == LATIN1
1739                 ? StringLatin1.indexOf(src, srcCount, tgt, tgtCount, fromIndex)
1740                 : StringUTF16.indexOf(src, srcCount, tgt, tgtCount, fromIndex);
1741         }
1742         if (srcCoder == LATIN1) {    //  && tgtCoder == UTF16
1743             return -1;
1744         }
1745         // srcCoder == UTF16 && tgtCoder == LATIN1) {
1746         return StringUTF16.indexOfLatin1(src, srcCount, tgt, tgtCount, fromIndex);
1747     }
1748 
1749     /**
1750      * Returns the index within this string of the last occurrence of the
1751      * specified substring.  The last occurrence of the empty string ""
1752      * is considered to occur at the index value {@code this.length()}.
1753      *
1754      * <p>The returned index is the largest value {@code k} for which:
1755      * <pre>{@code
1756      * this.startsWith(str, k)


1790      * Code shared by String and AbstractStringBuilder to do searches. The
1791      * source is the character array being searched, and the target
1792      * is the string being searched for.
1793      *
1794      * @param   src         the characters being searched.
1795      * @param   srcCoder  coder handles the mapping between bytes/chars
1796      * @param   srcCount    count of the source string.
1797      * @param   tgt         the characters being searched for.
1798      * @param   fromIndex   the index to begin searching from.
1799      */
1800     static int lastIndexOf(byte[] src, byte srcCoder, int srcCount,
1801                            String tgtStr, int fromIndex) {
1802         byte[] tgt = tgtStr.value;
1803         byte tgtCoder = tgtStr.coder();
1804         int tgtCount = tgtStr.length();
1805         /*
1806          * Check arguments; return immediately where possible. For
1807          * consistency, don't check for null str.
1808          */
1809         int rightIndex = srcCount - tgtCount;
1810         if (fromIndex < 0) {
1811             return -1;
1812         }
1813         if (fromIndex > rightIndex) {
1814             fromIndex = rightIndex;
1815         }



1816         /* Empty string always matches. */
1817         if (tgtCount == 0) {
1818             return fromIndex;
1819         }
1820         if (srcCoder == tgtCoder) {
1821             return srcCoder == LATIN1
1822                 ? StringLatin1.lastIndexOf(src, srcCount, tgt, tgtCount, fromIndex)
1823                 : StringUTF16.lastIndexOf(src, srcCount, tgt, tgtCount, fromIndex);
1824         }
1825         if (srcCoder == LATIN1) {    // && tgtCoder == UTF16
1826             return -1;
1827         }
1828                                      // srcCoder == UTF16 && tgtCoder == LATIN1
1829         int min = tgtCount - 1;
1830         int i = min + fromIndex;
1831         int strLastIndex = tgtCount - 1;
1832 
1833         char strLastChar = (char)(tgt[strLastIndex] & 0xff);
1834     startSearchForLastChar:
1835         while (true) {
1836             while (i >= min && StringUTF16.getChar(src, i) != strLastChar) {
1837                 i--;
1838             }
1839             if (i < min) {
1840                 return -1;
1841             }
1842             int j = i - 1;
1843             int start = j - strLastIndex;
1844             int k = strLastIndex - 1;
1845             while (j > start) {
1846                 if (StringUTF16.getChar(src, j--) != (tgt[k--] & 0xff)) {
1847                     i--;
1848                     continue startSearchForLastChar;
1849                 }
1850             }
1851             return start + 1;
1852         }
1853     }
1854 
1855     /**
1856      * Returns a string that is a substring of this string. The
1857      * substring begins with the character at the specified index and
1858      * extends to the end of this string. <p>
1859      * Examples:
1860      * <blockquote><pre>
1861      * "unhappy".substring(2) returns "happy"
1862      * "Harbison".substring(3) returns "bison"
1863      * "emptiness".substring(9) returns "" (an empty string)
1864      * </pre></blockquote>
1865      *
1866      * @param      beginIndex   the beginning index, inclusive.
1867      * @return     the specified substring.
1868      * @exception  IndexOutOfBoundsException  if
1869      *             {@code beginIndex} is negative or larger than the
1870      *             length of this {@code String} object.
1871      */
1872     public String substring(int beginIndex) {


3063         this.coder = coder;
3064     }
3065 
3066     byte coder() {
3067         return COMPACT_STRINGS ? coder : UTF16;
3068     }
3069 
3070     private boolean isLatin1() {
3071         return COMPACT_STRINGS && coder == LATIN1;
3072     }
3073 
3074     static final byte LATIN1 = 0;
3075     static final byte UTF16  = 1;
3076 
3077     /*
3078      * StringIndexOutOfBoundsException  if {@code index} is
3079      * negative or greater than or equal to {@code length}.
3080      */
3081     static void checkIndex(int index, int length) {
3082         if (index < 0 || index >= length) {
3083             throw new StringIndexOutOfBoundsException("index " + index);

3084         }
3085     }
3086 
3087     /*
3088      * StringIndexOutOfBoundsException  if {@code offset}
3089      * is negative or greater than {@code length}.
3090      */
3091     static void checkOffset(int offset, int length) {
3092         if (offset < 0 || offset > length) {
3093             throw new StringIndexOutOfBoundsException("offset " + offset +
3094                                                       ",length " + length);
3095         }
3096     }
3097 
3098     /*
3099      * Check {@code offset}, {@code count} against {@code 0} and {@code length}
3100      * bounds.
3101      *
3102      * @throws  StringIndexOutOfBoundsException
3103      *          If {@code offset} is negative, {@code count} is negative,
3104      *          or {@code offset} is greater than {@code length - count}
3105      */
3106     static void checkBoundsOffCount(int offset, int count, int length) {
3107         if (offset < 0 || count < 0 || offset > length - count) {
3108             throw new StringIndexOutOfBoundsException(
3109                 "offset " + offset + ", count " + count + ", length " + length);
3110         }
3111     }
3112 
3113     /*
3114      * Check {@code begin}, {@code end} against {@code 0} and {@code length}
3115      * bounds.
3116      *
3117      * @throws  StringIndexOutOfBoundsException
3118      *          If {@code begin} is negative, {@code begin} is greater than
3119      *          {@code end}, or {@code end} is greater than {@code length}.
3120      */
3121     private static void checkBoundsBeginEnd(int begin, int end, int length) {
3122         if (begin < 0 || begin > end || end > length) {
3123             throw new StringIndexOutOfBoundsException(
3124                 "begin " + begin + ", end " + end + ", length " + length);
3125         }
3126     }
3127 }
   1 /*
   2  * Copyright (c) 1994, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 178      *
 179      *    if (coder() == LATIN1) { ... }
 180      *
 181      * or:
 182      *
 183      *    if (COMPACT_STRINGS && coder == LATIN1) { ... }
 184      *
 185      * An optimizing JIT compiler can fold the above conditional as:
 186      *
 187      *    COMPACT_STRINGS == true  => if (coder == LATIN1) { ... }
 188      *    COMPACT_STRINGS == false => if (false)           { ... }
 189      *
 190      * @implNote
 191      * The actual value for this field is injected by JVM. The static
 192      * initialization block is used to set the value here to communicate
 193      * that this static final field is not statically foldable, and to
 194      * avoid any possible circular dependency during vm initialization.
 195      */
 196     static final boolean COMPACT_STRINGS;
 197 
 198     static final boolean DEBUG_INTRINSICS;
 199 
 200     static {
 201         COMPACT_STRINGS = true;
 202         DEBUG_INTRINSICS = false;
 203     }
 204 
 205     /**
 206      * Class String is special cased within the Serialization Stream Protocol.
 207      *
 208      * A String instance is written into an ObjectOutputStream according to
 209      * <a href="{@docRoot}/../platform/serialization/spec/output.html">
 210      * Object Serialization Specification, Section 6.2, "Stream Elements"</a>
 211      */
 212     private static final ObjectStreamField[] serialPersistentFields =
 213         new ObjectStreamField[0];
 214 
 215     /**
 216      * Initializes a newly created {@code String} object so that it represents
 217      * an empty character sequence.  Note that use of this constructor is
 218      * unnecessary since Strings are immutable.
 219      */
 220     public String() {
 221         this.value = "".value;
 222         this.coder = "".coder;


 367      * @see  #String(byte[], int, int)
 368      * @see  #String(byte[], java.lang.String)
 369      * @see  #String(byte[], java.nio.charset.Charset)
 370      * @see  #String(byte[])
 371      */
 372     @Deprecated(since="1.1")
 373     public String(byte ascii[], int hibyte, int offset, int count) {
 374         checkBoundsOffCount(offset, count, ascii.length);
 375         if (count == 0) {
 376             this.value = "".value;
 377             this.coder = "".coder;
 378             return;
 379         }
 380         if (COMPACT_STRINGS && (byte)hibyte == 0) {
 381             this.value = Arrays.copyOfRange(ascii, offset, offset + count);
 382             this.coder = LATIN1;
 383         } else {
 384             hibyte <<= 8;
 385             byte[] val = StringUTF16.newBytesFor(count);
 386             for (int i = 0; i < count; i++) {
 387                 StringUTF16.Trusted.putChar(val, i, hibyte | (ascii[offset++] & 0xff));
 388             }
 389             this.value = val;
 390             this.coder = UTF16;
 391         }
 392     }
 393 
 394     /**
 395      * Allocates a new {@code String} containing characters constructed from
 396      * an array of 8-bit integer values. Each character <i>c</i>in the
 397      * resulting string is constructed from the corresponding component
 398      * <i>b</i> in the byte array such that:
 399      *
 400      * <blockquote><pre>
 401      *     <b><i>c</i></b> == (char)(((hibyte &amp; 0xff) &lt;&lt; 8)
 402      *                         | (<b><i>b</i></b> &amp; 0xff))
 403      * </pre></blockquote>
 404      *
 405      * @deprecated  This method does not properly convert bytes into
 406      * characters.  As of JDK&nbsp;1.1, the preferred way to do this is via the
 407      * {@code String} constructors that take a {@link


1050     }
1051 
1052     private boolean nonSyncContentEquals(AbstractStringBuilder sb) {
1053         int len = length();
1054         if (len != sb.length()) {
1055             return false;
1056         }
1057         byte v1[] = value;
1058         byte v2[] = sb.getValue();
1059         if (coder() == sb.getCoder()) {
1060             int n = v1.length;
1061             for (int i = 0; i < n; i++) {
1062                 if (v1[i] != v2[i]) {
1063                     return false;
1064                 }
1065             }
1066         } else {
1067             if (!isLatin1()) {  // utf16 str and latin1 abs can never be "equal"
1068                 return false;
1069             }
1070             return StringUTF16.contentEquals(v1, v2, len);




1071         }
1072         return true;
1073     }
1074 
1075     /**
1076      * Compares this string to the specified {@code CharSequence}.  The
1077      * result is {@code true} if and only if this {@code String} represents the
1078      * same sequence of char values as the specified sequence. Note that if the
1079      * {@code CharSequence} is a {@code StringBuffer} then the method
1080      * synchronizes on it.
1081      *
1082      * <p>For finer-grained String comparison, refer to
1083      * {@link java.text.Collator}.
1084      *
1085      * @param  cs
1086      *         The sequence to compare this {@code String} against
1087      *
1088      * @return  {@code true} if this {@code String} represents the same
1089      *          sequence of char values as the specified sequence, {@code
1090      *          false} otherwise


1102                 return nonSyncContentEquals((AbstractStringBuilder)cs);
1103             }
1104         }
1105         // Argument is a String
1106         if (cs instanceof String) {
1107             return equals(cs);
1108         }
1109         // Argument is a generic CharSequence
1110         int n = cs.length();
1111         if (n != length()) {
1112             return false;
1113         }
1114         byte[] val = this.value;
1115         if (isLatin1()) {
1116             for (int i = 0; i < n; i++) {
1117                 if ((val[i] & 0xff) != cs.charAt(i)) {
1118                     return false;
1119                 }
1120             }
1121         } else {
1122             if (!StringUTF16.contentEquals(val, cs, n)) {

1123                 return false;
1124             }
1125         }

1126         return true;
1127     }
1128 
1129     /**
1130      * Compares this {@code String} to another {@code String}, ignoring case
1131      * considerations.  Two strings are considered equal ignoring case if they
1132      * are of the same length and corresponding characters in the two strings
1133      * are equal ignoring case.
1134      *
1135      * <p> Two characters {@code c1} and {@code c2} are considered the same
1136      * ignoring case if at least one of the following is true:
1137      * <ul>
1138      *   <li> The two characters are the same (as compared by the
1139      *        {@code ==} operator)
1140      *   <li> Calling {@code Character.toLowerCase(Character.toUpperCase(char))}
1141      *        on each character produces the same result
1142      * </ul>
1143      *
1144      * <p>Note that this method does <em>not</em> take locale into account, and
1145      * will result in unsatisfactory results for certain locales.  The


1314         if ((ooffset < 0) || (toffset < 0) ||
1315              (toffset > (long)length() - len) ||
1316              (ooffset > (long)other.length() - len)) {
1317             return false;
1318         }
1319         if (coder() == other.coder()) {
1320             if (!isLatin1() && (len > 0)) {
1321                 toffset = toffset << 1;
1322                 ooffset = ooffset << 1;
1323                 len = len << 1;
1324             }
1325             while (len-- > 0) {
1326                 if (tv[toffset++] != ov[ooffset++]) {
1327                     return false;
1328                 }
1329             }
1330         } else {
1331             if (coder() == LATIN1) {
1332                 while (len-- > 0) {
1333                     if (StringLatin1.getChar(tv, toffset++) !=
1334                         StringUTF16.Trusted.getChar(ov, ooffset++)) {
1335                         return false;
1336                     }
1337                 }
1338             } else {
1339                 while (len-- > 0) {
1340                     if (StringUTF16.Trusted.getChar(tv, toffset++) !=
1341                         StringLatin1.getChar(ov, ooffset++)) {
1342                         return false;
1343                     }
1344                 }
1345             }
1346         }
1347         return true;
1348     }
1349 
1350     /**
1351      * Tests if two string regions are equal.
1352      * <p>
1353      * A substring of this {@code String} object is compared to a substring
1354      * of the argument {@code other}. The result is {@code true} if these
1355      * substrings represent character sequences that are the same, ignoring
1356      * case if and only if {@code ignoreCase} is true. The substring of
1357      * this {@code String} object to be compared begins at index
1358      * {@code toffset} and has length {@code len}. The substring of
1359      * {@code other} to be compared begins at index {@code ooffset} and
1360      * has length {@code len}. The result is {@code false} if and only if


1442         if (toffset < 0 || toffset > length() - prefix.length()) {
1443             return false;
1444         }
1445         byte ta[] = value;
1446         byte pa[] = prefix.value;
1447         int po = 0;
1448         int pc = pa.length;
1449         if (coder() == prefix.coder()) {
1450             int to = isLatin1() ? toffset : toffset << 1;
1451             while (po < pc) {
1452                 if (ta[to++] != pa[po++]) {
1453                     return false;
1454                 }
1455             }
1456         } else {
1457             if (isLatin1()) {  // && pcoder == UTF16
1458                 return false;
1459             }
1460             // coder == UTF16 && pcoder == LATIN1)
1461             while (po < pc) {
1462                 if (StringUTF16.Trusted.getChar(ta, toffset++) != (pa[po++] & 0xff)) {
1463                     return false;
1464                }
1465             }
1466         }
1467         return true;
1468     }
1469 
1470     /**
1471      * Tests if this string starts with the specified prefix.
1472      *
1473      * @param   prefix   the prefix.
1474      * @return  {@code true} if the character sequence represented by the
1475      *          argument is a prefix of the character sequence represented by
1476      *          this string; {@code false} otherwise.
1477      *          Note also that {@code true} will be returned if the
1478      *          argument is an empty string or is equal to this
1479      *          {@code String} object as determined by the
1480      *          {@link #equals(Object)} method.
1481      * @since   1.0
1482      */


1714      * @param   srcCoder  the coder of the source string.
1715      * @param   srcCount  length of the source string.
1716      * @param   tgtStr    the characters being searched for.
1717      * @param   fromIndex the index to begin searching from.
1718      */
1719     static int indexOf(byte[] src, byte srcCoder, int srcCount,
1720                        String tgtStr, int fromIndex) {
1721         byte[] tgt    = tgtStr.value;
1722         byte tgtCoder = tgtStr.coder();
1723         int tgtCount  = tgtStr.length();
1724 
1725         if (fromIndex >= srcCount) {
1726             return (tgtCount == 0 ? srcCount : -1);
1727         }
1728         if (fromIndex < 0) {
1729             fromIndex = 0;
1730         }
1731         if (tgtCount == 0) {
1732             return fromIndex;
1733         }
1734         if (tgtCount > srcCount) {
1735             return -1;
1736         }
1737         if (srcCoder == tgtCoder) {
1738             return srcCoder == LATIN1
1739                 ? StringLatin1.indexOf(src, srcCount, tgt, tgtCount, fromIndex)
1740                 : StringUTF16.indexOf(src, srcCount, tgt, tgtCount, fromIndex);
1741         }
1742         if (srcCoder == LATIN1) {    //  && tgtCoder == UTF16
1743             return -1;
1744         }
1745         // srcCoder == UTF16 && tgtCoder == LATIN1) {
1746         return StringUTF16.indexOfLatin1(src, srcCount, tgt, tgtCount, fromIndex);
1747     }
1748 
1749     /**
1750      * Returns the index within this string of the last occurrence of the
1751      * specified substring.  The last occurrence of the empty string ""
1752      * is considered to occur at the index value {@code this.length()}.
1753      *
1754      * <p>The returned index is the largest value {@code k} for which:
1755      * <pre>{@code
1756      * this.startsWith(str, k)


1790      * Code shared by String and AbstractStringBuilder to do searches. The
1791      * source is the character array being searched, and the target
1792      * is the string being searched for.
1793      *
1794      * @param   src         the characters being searched.
1795      * @param   srcCoder    coder handles the mapping between bytes/chars
1796      * @param   srcCount    count of the source string.
1797      * @param   tgt         the characters being searched for.
1798      * @param   fromIndex   the index to begin searching from.
1799      */
1800     static int lastIndexOf(byte[] src, byte srcCoder, int srcCount,
1801                            String tgtStr, int fromIndex) {
1802         byte[] tgt = tgtStr.value;
1803         byte tgtCoder = tgtStr.coder();
1804         int tgtCount = tgtStr.length();
1805         /*
1806          * Check arguments; return immediately where possible. For
1807          * consistency, don't check for null str.
1808          */
1809         int rightIndex = srcCount - tgtCount;



1810         if (fromIndex > rightIndex) {
1811             fromIndex = rightIndex;
1812         }
1813         if (fromIndex < 0) {
1814             return -1;
1815         }
1816         /* Empty string always matches. */
1817         if (tgtCount == 0) {
1818             return fromIndex;
1819         }
1820         if (srcCoder == tgtCoder) {
1821             return srcCoder == LATIN1
1822                 ? StringLatin1.lastIndexOf(src, srcCount, tgt, tgtCount, fromIndex)
1823                 : StringUTF16.lastIndexOf(src, srcCount, tgt, tgtCount, fromIndex);
1824         }
1825         if (srcCoder == LATIN1) {    // && tgtCoder == UTF16
1826             return -1;
1827         }
1828         // srcCoder == UTF16 && tgtCoder == LATIN1
1829         return StringUTF16.lastIndexOfLatin1(src, srcCount, tgt, tgtCount, fromIndex);























1830     }
1831 
1832     /**
1833      * Returns a string that is a substring of this string. The
1834      * substring begins with the character at the specified index and
1835      * extends to the end of this string. <p>
1836      * Examples:
1837      * <blockquote><pre>
1838      * "unhappy".substring(2) returns "happy"
1839      * "Harbison".substring(3) returns "bison"
1840      * "emptiness".substring(9) returns "" (an empty string)
1841      * </pre></blockquote>
1842      *
1843      * @param      beginIndex   the beginning index, inclusive.
1844      * @return     the specified substring.
1845      * @exception  IndexOutOfBoundsException  if
1846      *             {@code beginIndex} is negative or larger than the
1847      *             length of this {@code String} object.
1848      */
1849     public String substring(int beginIndex) {


3040         this.coder = coder;
3041     }
3042 
3043     byte coder() {
3044         return COMPACT_STRINGS ? coder : UTF16;
3045     }
3046 
3047     private boolean isLatin1() {
3048         return COMPACT_STRINGS && coder == LATIN1;
3049     }
3050 
3051     static final byte LATIN1 = 0;
3052     static final byte UTF16  = 1;
3053 
3054     /*
3055      * StringIndexOutOfBoundsException  if {@code index} is
3056      * negative or greater than or equal to {@code length}.
3057      */
3058     static void checkIndex(int index, int length) {
3059         if (index < 0 || index >= length) {
3060             throw new StringIndexOutOfBoundsException("index " + index +
3061                                                       ",length " + length);
3062         }
3063     }
3064 
3065     /*
3066      * StringIndexOutOfBoundsException  if {@code offset}
3067      * is negative or greater than {@code length}.
3068      */
3069     static void checkOffset(int offset, int length) {
3070         if (offset < 0 || offset > length) {
3071             throw new StringIndexOutOfBoundsException("offset " + offset +
3072                                                       ",length " + length);
3073         }
3074     }
3075 
3076     /*
3077      * Check {@code offset}, {@code count} against {@code 0} and {@code length}
3078      * bounds.
3079      *
3080      * @throws  StringIndexOutOfBoundsException
3081      *          If {@code offset} is negative, {@code count} is negative,
3082      *          or {@code offset} is greater than {@code length - count}
3083      */
3084     static void checkBoundsOffCount(int offset, int count, int length) {
3085         if (offset < 0 || count < 0 || offset > length - count) {
3086             throw new StringIndexOutOfBoundsException(
3087                 "offset " + offset + ", count " + count + ", length " + length);
3088         }
3089     }
3090 
3091     /*
3092      * Check {@code begin}, {@code end} against {@code 0} and {@code length}
3093      * bounds.
3094      *
3095      * @throws  StringIndexOutOfBoundsException
3096      *          If {@code begin} is negative, {@code begin} is greater than
3097      *          {@code end}, or {@code end} is greater than {@code length}.
3098      */
3099     static void checkBoundsBeginEnd(int begin, int end, int length) {
3100         if (begin < 0 || begin > end || end > length) {
3101             throw new StringIndexOutOfBoundsException(
3102                 "begin " + begin + ", end " + end + ", length " + length);
3103         }
3104     }
3105 }
< prev index next >