emul/src/main/java/java/lang/String.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 29 Sep 2012 08:26:30 +0200
branchemul
changeset 64 55745bf6696c
parent 61 4b334950499d
child 65 f99a92839285
permissions -rw-r--r--
Commenting out extra-adventurous methods in String
     1 /*
     2  * Copyright (c) 1994, 2010, 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
    23  * questions.
    24  */
    25 
    26 package java.lang;
    27 
    28 import java.io.ObjectStreamField;
    29 import java.io.UnsupportedEncodingException;
    30 import java.util.Comparator;
    31 
    32 /**
    33  * The <code>String</code> class represents character strings. All
    34  * string literals in Java programs, such as <code>"abc"</code>, are
    35  * implemented as instances of this class.
    36  * <p>
    37  * Strings are constant; their values cannot be changed after they
    38  * are created. String buffers support mutable strings.
    39  * Because String objects are immutable they can be shared. For example:
    40  * <p><blockquote><pre>
    41  *     String str = "abc";
    42  * </pre></blockquote><p>
    43  * is equivalent to:
    44  * <p><blockquote><pre>
    45  *     char data[] = {'a', 'b', 'c'};
    46  *     String str = new String(data);
    47  * </pre></blockquote><p>
    48  * Here are some more examples of how strings can be used:
    49  * <p><blockquote><pre>
    50  *     System.out.println("abc");
    51  *     String cde = "cde";
    52  *     System.out.println("abc" + cde);
    53  *     String c = "abc".substring(2,3);
    54  *     String d = cde.substring(1, 2);
    55  * </pre></blockquote>
    56  * <p>
    57  * The class <code>String</code> includes methods for examining
    58  * individual characters of the sequence, for comparing strings, for
    59  * searching strings, for extracting substrings, and for creating a
    60  * copy of a string with all characters translated to uppercase or to
    61  * lowercase. Case mapping is based on the Unicode Standard version
    62  * specified by the {@link java.lang.Character Character} class.
    63  * <p>
    64  * The Java language provides special support for the string
    65  * concatenation operator (&nbsp;+&nbsp;), and for conversion of
    66  * other objects to strings. String concatenation is implemented
    67  * through the <code>StringBuilder</code>(or <code>StringBuffer</code>)
    68  * class and its <code>append</code> method.
    69  * String conversions are implemented through the method
    70  * <code>toString</code>, defined by <code>Object</code> and
    71  * inherited by all classes in Java. For additional information on
    72  * string concatenation and conversion, see Gosling, Joy, and Steele,
    73  * <i>The Java Language Specification</i>.
    74  *
    75  * <p> Unless otherwise noted, passing a <tt>null</tt> argument to a constructor
    76  * or method in this class will cause a {@link NullPointerException} to be
    77  * thrown.
    78  *
    79  * <p>A <code>String</code> represents a string in the UTF-16 format
    80  * in which <em>supplementary characters</em> are represented by <em>surrogate
    81  * pairs</em> (see the section <a href="Character.html#unicode">Unicode
    82  * Character Representations</a> in the <code>Character</code> class for
    83  * more information).
    84  * Index values refer to <code>char</code> code units, so a supplementary
    85  * character uses two positions in a <code>String</code>.
    86  * <p>The <code>String</code> class provides methods for dealing with
    87  * Unicode code points (i.e., characters), in addition to those for
    88  * dealing with Unicode code units (i.e., <code>char</code> values).
    89  *
    90  * @author  Lee Boynton
    91  * @author  Arthur van Hoff
    92  * @author  Martin Buchholz
    93  * @author  Ulf Zibis
    94  * @see     java.lang.Object#toString()
    95  * @see     java.lang.StringBuffer
    96  * @see     java.lang.StringBuilder
    97  * @see     java.nio.charset.Charset
    98  * @since   JDK1.0
    99  */
   100 
   101 public final class String
   102     implements java.io.Serializable, Comparable<String>, CharSequence
   103 {
   104     /** The value is used for character storage. */
   105     private final char value[];
   106 
   107     /** The offset is the first index of the storage that is used. */
   108     private final int offset;
   109 
   110     /** The count is the number of characters in the String. */
   111     private final int count;
   112 
   113     /** Cache the hash code for the string */
   114     private int hash; // Default to 0
   115 
   116     /** use serialVersionUID from JDK 1.0.2 for interoperability */
   117     private static final long serialVersionUID = -6849794470754667710L;
   118 
   119     /**
   120      * Class String is special cased within the Serialization Stream Protocol.
   121      *
   122      * A String instance is written initially into an ObjectOutputStream in the
   123      * following format:
   124      * <pre>
   125      *      <code>TC_STRING</code> (utf String)
   126      * </pre>
   127      * The String is written by method <code>DataOutput.writeUTF</code>.
   128      * A new handle is generated to  refer to all future references to the
   129      * string instance within the stream.
   130      */
   131     private static final ObjectStreamField[] serialPersistentFields =
   132         new ObjectStreamField[0];
   133 
   134     /**
   135      * Initializes a newly created {@code String} object so that it represents
   136      * an empty character sequence.  Note that use of this constructor is
   137      * unnecessary since Strings are immutable.
   138      */
   139     public String() {
   140         this.offset = 0;
   141         this.count = 0;
   142         this.value = new char[0];
   143     }
   144 
   145     /**
   146      * Initializes a newly created {@code String} object so that it represents
   147      * the same sequence of characters as the argument; in other words, the
   148      * newly created string is a copy of the argument string. Unless an
   149      * explicit copy of {@code original} is needed, use of this constructor is
   150      * unnecessary since Strings are immutable.
   151      *
   152      * @param  original
   153      *         A {@code String}
   154      */
   155     public String(String original) {
   156         int size = original.count;
   157         char[] originalValue = original.value;
   158         char[] v;
   159         if (originalValue.length > size) {
   160             // The array representing the String is bigger than the new
   161             // String itself.  Perhaps this constructor is being called
   162             // in order to trim the baggage, so make a copy of the array.
   163             int off = original.offset;
   164             v = copyOfRange(originalValue, off, off+size);
   165         } else {
   166             // The array representing the String is the same
   167             // size as the String, so no point in making a copy.
   168             v = originalValue;
   169         }
   170         this.offset = 0;
   171         this.count = size;
   172         this.value = v;
   173     }
   174 
   175     /**
   176      * Allocates a new {@code String} so that it represents the sequence of
   177      * characters currently contained in the character array argument. The
   178      * contents of the character array are copied; subsequent modification of
   179      * the character array does not affect the newly created string.
   180      *
   181      * @param  value
   182      *         The initial value of the string
   183      */
   184     public String(char value[]) {
   185         int size = value.length;
   186         this.offset = 0;
   187         this.count = size;
   188         this.value = copyOf(value, size);
   189     }
   190 
   191     /**
   192      * Allocates a new {@code String} that contains characters from a subarray
   193      * of the character array argument. The {@code offset} argument is the
   194      * index of the first character of the subarray and the {@code count}
   195      * argument specifies the length of the subarray. The contents of the
   196      * subarray are copied; subsequent modification of the character array does
   197      * not affect the newly created string.
   198      *
   199      * @param  value
   200      *         Array that is the source of characters
   201      *
   202      * @param  offset
   203      *         The initial offset
   204      *
   205      * @param  count
   206      *         The length
   207      *
   208      * @throws  IndexOutOfBoundsException
   209      *          If the {@code offset} and {@code count} arguments index
   210      *          characters outside the bounds of the {@code value} array
   211      */
   212     public String(char value[], int offset, int count) {
   213         if (offset < 0) {
   214             throw new StringIndexOutOfBoundsException(offset);
   215         }
   216         if (count < 0) {
   217             throw new StringIndexOutOfBoundsException(count);
   218         }
   219         // Note: offset or count might be near -1>>>1.
   220         if (offset > value.length - count) {
   221             throw new StringIndexOutOfBoundsException(offset + count);
   222         }
   223         this.offset = 0;
   224         this.count = count;
   225         this.value = copyOfRange(value, offset, offset+count);
   226     }
   227 
   228     /**
   229      * Allocates a new {@code String} that contains characters from a subarray
   230      * of the <a href="Character.html#unicode">Unicode code point</a> array
   231      * argument.  The {@code offset} argument is the index of the first code
   232      * point of the subarray and the {@code count} argument specifies the
   233      * length of the subarray.  The contents of the subarray are converted to
   234      * {@code char}s; subsequent modification of the {@code int} array does not
   235      * affect the newly created string.
   236      *
   237      * @param  codePoints
   238      *         Array that is the source of Unicode code points
   239      *
   240      * @param  offset
   241      *         The initial offset
   242      *
   243      * @param  count
   244      *         The length
   245      *
   246      * @throws  IllegalArgumentException
   247      *          If any invalid Unicode code point is found in {@code
   248      *          codePoints}
   249      *
   250      * @throws  IndexOutOfBoundsException
   251      *          If the {@code offset} and {@code count} arguments index
   252      *          characters outside the bounds of the {@code codePoints} array
   253      *
   254      * @since  1.5
   255      */
   256     public String(int[] codePoints, int offset, int count) {
   257         if (offset < 0) {
   258             throw new StringIndexOutOfBoundsException(offset);
   259         }
   260         if (count < 0) {
   261             throw new StringIndexOutOfBoundsException(count);
   262         }
   263         // Note: offset or count might be near -1>>>1.
   264         if (offset > codePoints.length - count) {
   265             throw new StringIndexOutOfBoundsException(offset + count);
   266         }
   267 
   268         final int end = offset + count;
   269 
   270         // Pass 1: Compute precise size of char[]
   271         int n = count;
   272         for (int i = offset; i < end; i++) {
   273             int c = codePoints[i];
   274             if (Character.isBmpCodePoint(c))
   275                 continue;
   276             else if (Character.isValidCodePoint(c))
   277                 n++;
   278             else throw new IllegalArgumentException(Integer.toString(c));
   279         }
   280 
   281         // Pass 2: Allocate and fill in char[]
   282         final char[] v = new char[n];
   283 
   284         for (int i = offset, j = 0; i < end; i++, j++) {
   285             int c = codePoints[i];
   286             if (Character.isBmpCodePoint(c))
   287                 v[j] = (char) c;
   288             else
   289                 Character.toSurrogates(c, v, j++);
   290         }
   291 
   292         this.value  = v;
   293         this.count  = n;
   294         this.offset = 0;
   295     }
   296 
   297     /**
   298      * Allocates a new {@code String} constructed from a subarray of an array
   299      * of 8-bit integer values.
   300      *
   301      * <p> The {@code offset} argument is the index of the first byte of the
   302      * subarray, and the {@code count} argument specifies the length of the
   303      * subarray.
   304      *
   305      * <p> Each {@code byte} in the subarray is converted to a {@code char} as
   306      * specified in the method above.
   307      *
   308      * @deprecated This method does not properly convert bytes into characters.
   309      * As of JDK&nbsp;1.1, the preferred way to do this is via the
   310      * {@code String} constructors that take a {@link
   311      * java.nio.charset.Charset}, charset name, or that use the platform's
   312      * default charset.
   313      *
   314      * @param  ascii
   315      *         The bytes to be converted to characters
   316      *
   317      * @param  hibyte
   318      *         The top 8 bits of each 16-bit Unicode code unit
   319      *
   320      * @param  offset
   321      *         The initial offset
   322      * @param  count
   323      *         The length
   324      *
   325      * @throws  IndexOutOfBoundsException
   326      *          If the {@code offset} or {@code count} argument is invalid
   327      *
   328      * @see  #String(byte[], int)
   329      * @see  #String(byte[], int, int, java.lang.String)
   330      * @see  #String(byte[], int, int, java.nio.charset.Charset)
   331      * @see  #String(byte[], int, int)
   332      * @see  #String(byte[], java.lang.String)
   333      * @see  #String(byte[], java.nio.charset.Charset)
   334      * @see  #String(byte[])
   335      */
   336     @Deprecated
   337     public String(byte ascii[], int hibyte, int offset, int count) {
   338         checkBounds(ascii, offset, count);
   339         char value[] = new char[count];
   340 
   341         if (hibyte == 0) {
   342             for (int i = count ; i-- > 0 ;) {
   343                 value[i] = (char) (ascii[i + offset] & 0xff);
   344             }
   345         } else {
   346             hibyte <<= 8;
   347             for (int i = count ; i-- > 0 ;) {
   348                 value[i] = (char) (hibyte | (ascii[i + offset] & 0xff));
   349             }
   350         }
   351         this.offset = 0;
   352         this.count = count;
   353         this.value = value;
   354     }
   355 
   356     /**
   357      * Allocates a new {@code String} containing characters constructed from
   358      * an array of 8-bit integer values. Each character <i>c</i>in the
   359      * resulting string is constructed from the corresponding component
   360      * <i>b</i> in the byte array such that:
   361      *
   362      * <blockquote><pre>
   363      *     <b><i>c</i></b> == (char)(((hibyte &amp; 0xff) &lt;&lt; 8)
   364      *                         | (<b><i>b</i></b> &amp; 0xff))
   365      * </pre></blockquote>
   366      *
   367      * @deprecated  This method does not properly convert bytes into
   368      * characters.  As of JDK&nbsp;1.1, the preferred way to do this is via the
   369      * {@code String} constructors that take a {@link
   370      * java.nio.charset.Charset}, charset name, or that use the platform's
   371      * default charset.
   372      *
   373      * @param  ascii
   374      *         The bytes to be converted to characters
   375      *
   376      * @param  hibyte
   377      *         The top 8 bits of each 16-bit Unicode code unit
   378      *
   379      * @see  #String(byte[], int, int, java.lang.String)
   380      * @see  #String(byte[], int, int, java.nio.charset.Charset)
   381      * @see  #String(byte[], int, int)
   382      * @see  #String(byte[], java.lang.String)
   383      * @see  #String(byte[], java.nio.charset.Charset)
   384      * @see  #String(byte[])
   385      */
   386     @Deprecated
   387     public String(byte ascii[], int hibyte) {
   388         this(ascii, hibyte, 0, ascii.length);
   389     }
   390 
   391     /* Common private utility method used to bounds check the byte array
   392      * and requested offset & length values used by the String(byte[],..)
   393      * constructors.
   394      */
   395     private static void checkBounds(byte[] bytes, int offset, int length) {
   396         if (length < 0)
   397             throw new StringIndexOutOfBoundsException(length);
   398         if (offset < 0)
   399             throw new StringIndexOutOfBoundsException(offset);
   400         if (offset > bytes.length - length)
   401             throw new StringIndexOutOfBoundsException(offset + length);
   402     }
   403 
   404     /**
   405      * Constructs a new {@code String} by decoding the specified subarray of
   406      * bytes using the specified charset.  The length of the new {@code String}
   407      * is a function of the charset, and hence may not be equal to the length
   408      * of the subarray.
   409      *
   410      * <p> The behavior of this constructor when the given bytes are not valid
   411      * in the given charset is unspecified.  The {@link
   412      * java.nio.charset.CharsetDecoder} class should be used when more control
   413      * over the decoding process is required.
   414      *
   415      * @param  bytes
   416      *         The bytes to be decoded into characters
   417      *
   418      * @param  offset
   419      *         The index of the first byte to decode
   420      *
   421      * @param  length
   422      *         The number of bytes to decode
   423 
   424      * @param  charsetName
   425      *         The name of a supported {@linkplain java.nio.charset.Charset
   426      *         charset}
   427      *
   428      * @throws  UnsupportedEncodingException
   429      *          If the named charset is not supported
   430      *
   431      * @throws  IndexOutOfBoundsException
   432      *          If the {@code offset} and {@code length} arguments index
   433      *          characters outside the bounds of the {@code bytes} array
   434      *
   435      * @since  JDK1.1
   436      */
   437     public String(byte bytes[], int offset, int length, String charsetName)
   438         throws UnsupportedEncodingException
   439     {
   440         if (charsetName == null)
   441             throw new NullPointerException("charsetName");
   442         checkBounds(bytes, offset, length);
   443         char[] v = StringCoding.decode(charsetName, bytes, offset, length);
   444         this.offset = 0;
   445         this.count = v.length;
   446         this.value = v;
   447     }
   448 
   449     /**
   450      * Constructs a new {@code String} by decoding the specified subarray of
   451      * bytes using the specified {@linkplain java.nio.charset.Charset charset}.
   452      * The length of the new {@code String} is a function of the charset, and
   453      * hence may not be equal to the length of the subarray.
   454      *
   455      * <p> This method always replaces malformed-input and unmappable-character
   456      * sequences with this charset's default replacement string.  The {@link
   457      * java.nio.charset.CharsetDecoder} class should be used when more control
   458      * over the decoding process is required.
   459      *
   460      * @param  bytes
   461      *         The bytes to be decoded into characters
   462      *
   463      * @param  offset
   464      *         The index of the first byte to decode
   465      *
   466      * @param  length
   467      *         The number of bytes to decode
   468      *
   469      * @param  charset
   470      *         The {@linkplain java.nio.charset.Charset charset} to be used to
   471      *         decode the {@code bytes}
   472      *
   473      * @throws  IndexOutOfBoundsException
   474      *          If the {@code offset} and {@code length} arguments index
   475      *          characters outside the bounds of the {@code bytes} array
   476      *
   477      * @since  1.6
   478      */
   479     /* don't want dependnecy on Charset
   480     public String(byte bytes[], int offset, int length, Charset charset) {
   481         if (charset == null)
   482             throw new NullPointerException("charset");
   483         checkBounds(bytes, offset, length);
   484         char[] v = StringCoding.decode(charset, bytes, offset, length);
   485         this.offset = 0;
   486         this.count = v.length;
   487         this.value = v;
   488     }
   489     */
   490 
   491     /**
   492      * Constructs a new {@code String} by decoding the specified array of bytes
   493      * using the specified {@linkplain java.nio.charset.Charset charset}.  The
   494      * length of the new {@code String} is a function of the charset, and hence
   495      * may not be equal to the length of the byte array.
   496      *
   497      * <p> The behavior of this constructor when the given bytes are not valid
   498      * in the given charset is unspecified.  The {@link
   499      * java.nio.charset.CharsetDecoder} class should be used when more control
   500      * over the decoding process is required.
   501      *
   502      * @param  bytes
   503      *         The bytes to be decoded into characters
   504      *
   505      * @param  charsetName
   506      *         The name of a supported {@linkplain java.nio.charset.Charset
   507      *         charset}
   508      *
   509      * @throws  UnsupportedEncodingException
   510      *          If the named charset is not supported
   511      *
   512      * @since  JDK1.1
   513      */
   514     public String(byte bytes[], String charsetName)
   515         throws UnsupportedEncodingException
   516     {
   517         this(bytes, 0, bytes.length, charsetName);
   518     }
   519 
   520     /**
   521      * Constructs a new {@code String} by decoding the specified array of
   522      * bytes using the specified {@linkplain java.nio.charset.Charset charset}.
   523      * The length of the new {@code String} is a function of the charset, and
   524      * hence may not be equal to the length of the byte array.
   525      *
   526      * <p> This method always replaces malformed-input and unmappable-character
   527      * sequences with this charset's default replacement string.  The {@link
   528      * java.nio.charset.CharsetDecoder} class should be used when more control
   529      * over the decoding process is required.
   530      *
   531      * @param  bytes
   532      *         The bytes to be decoded into characters
   533      *
   534      * @param  charset
   535      *         The {@linkplain java.nio.charset.Charset charset} to be used to
   536      *         decode the {@code bytes}
   537      *
   538      * @since  1.6
   539      */
   540     /* don't want dep on Charset
   541     public String(byte bytes[], Charset charset) {
   542         this(bytes, 0, bytes.length, charset);
   543     }
   544     */
   545 
   546     /**
   547      * Constructs a new {@code String} by decoding the specified subarray of
   548      * bytes using the platform's default charset.  The length of the new
   549      * {@code String} is a function of the charset, and hence may not be equal
   550      * to the length of the subarray.
   551      *
   552      * <p> The behavior of this constructor when the given bytes are not valid
   553      * in the default charset is unspecified.  The {@link
   554      * java.nio.charset.CharsetDecoder} class should be used when more control
   555      * over the decoding process is required.
   556      *
   557      * @param  bytes
   558      *         The bytes to be decoded into characters
   559      *
   560      * @param  offset
   561      *         The index of the first byte to decode
   562      *
   563      * @param  length
   564      *         The number of bytes to decode
   565      *
   566      * @throws  IndexOutOfBoundsException
   567      *          If the {@code offset} and the {@code length} arguments index
   568      *          characters outside the bounds of the {@code bytes} array
   569      *
   570      * @since  JDK1.1
   571      */
   572     public String(byte bytes[], int offset, int length) {
   573         checkBounds(bytes, offset, length);
   574         char[] v  = StringCoding.decode(bytes, offset, length);
   575         this.offset = 0;
   576         this.count = v.length;
   577         this.value = v;
   578     }
   579 
   580     /**
   581      * Constructs a new {@code String} by decoding the specified array of bytes
   582      * using the platform's default charset.  The length of the new {@code
   583      * String} is a function of the charset, and hence may not be equal to the
   584      * length of the byte array.
   585      *
   586      * <p> The behavior of this constructor when the given bytes are not valid
   587      * in the default charset is unspecified.  The {@link
   588      * java.nio.charset.CharsetDecoder} class should be used when more control
   589      * over the decoding process is required.
   590      *
   591      * @param  bytes
   592      *         The bytes to be decoded into characters
   593      *
   594      * @since  JDK1.1
   595      */
   596     public String(byte bytes[]) {
   597         this(bytes, 0, bytes.length);
   598     }
   599 
   600     /**
   601      * Allocates a new string that contains the sequence of characters
   602      * currently contained in the string buffer argument. The contents of the
   603      * string buffer are copied; subsequent modification of the string buffer
   604      * does not affect the newly created string.
   605      *
   606      * @param  buffer
   607      *         A {@code StringBuffer}
   608      */
   609     public String(StringBuffer buffer) {
   610         String result = buffer.toString();
   611         this.value = result.value;
   612         this.count = result.count;
   613         this.offset = result.offset;
   614     }
   615 
   616     /**
   617      * Allocates a new string that contains the sequence of characters
   618      * currently contained in the string builder argument. The contents of the
   619      * string builder are copied; subsequent modification of the string builder
   620      * does not affect the newly created string.
   621      *
   622      * <p> This constructor is provided to ease migration to {@code
   623      * StringBuilder}. Obtaining a string from a string builder via the {@code
   624      * toString} method is likely to run faster and is generally preferred.
   625      *
   626      * @param   builder
   627      *          A {@code StringBuilder}
   628      *
   629      * @since  1.5
   630      */
   631     public String(StringBuilder builder) {
   632         String result = builder.toString();
   633         this.value = result.value;
   634         this.count = result.count;
   635         this.offset = result.offset;
   636     }
   637 
   638 
   639     // Package private constructor which shares value array for speed.
   640     String(int offset, int count, char value[]) {
   641         this.value = value;
   642         this.offset = offset;
   643         this.count = count;
   644     }
   645 
   646     /**
   647      * Returns the length of this string.
   648      * The length is equal to the number of <a href="Character.html#unicode">Unicode
   649      * code units</a> in the string.
   650      *
   651      * @return  the length of the sequence of characters represented by this
   652      *          object.
   653      */
   654     public int length() {
   655         return count;
   656     }
   657 
   658     /**
   659      * Returns <tt>true</tt> if, and only if, {@link #length()} is <tt>0</tt>.
   660      *
   661      * @return <tt>true</tt> if {@link #length()} is <tt>0</tt>, otherwise
   662      * <tt>false</tt>
   663      *
   664      * @since 1.6
   665      */
   666     public boolean isEmpty() {
   667         return count == 0;
   668     }
   669 
   670     /**
   671      * Returns the <code>char</code> value at the
   672      * specified index. An index ranges from <code>0</code> to
   673      * <code>length() - 1</code>. The first <code>char</code> value of the sequence
   674      * is at index <code>0</code>, the next at index <code>1</code>,
   675      * and so on, as for array indexing.
   676      *
   677      * <p>If the <code>char</code> value specified by the index is a
   678      * <a href="Character.html#unicode">surrogate</a>, the surrogate
   679      * value is returned.
   680      *
   681      * @param      index   the index of the <code>char</code> value.
   682      * @return     the <code>char</code> value at the specified index of this string.
   683      *             The first <code>char</code> value is at index <code>0</code>.
   684      * @exception  IndexOutOfBoundsException  if the <code>index</code>
   685      *             argument is negative or not less than the length of this
   686      *             string.
   687      */
   688     public char charAt(int index) {
   689         if ((index < 0) || (index >= count)) {
   690             throw new StringIndexOutOfBoundsException(index);
   691         }
   692         return value[index + offset];
   693     }
   694 
   695     /**
   696      * Returns the character (Unicode code point) at the specified
   697      * index. The index refers to <code>char</code> values
   698      * (Unicode code units) and ranges from <code>0</code> to
   699      * {@link #length()}<code> - 1</code>.
   700      *
   701      * <p> If the <code>char</code> value specified at the given index
   702      * is in the high-surrogate range, the following index is less
   703      * than the length of this <code>String</code>, and the
   704      * <code>char</code> value at the following index is in the
   705      * low-surrogate range, then the supplementary code point
   706      * corresponding to this surrogate pair is returned. Otherwise,
   707      * the <code>char</code> value at the given index is returned.
   708      *
   709      * @param      index the index to the <code>char</code> values
   710      * @return     the code point value of the character at the
   711      *             <code>index</code>
   712      * @exception  IndexOutOfBoundsException  if the <code>index</code>
   713      *             argument is negative or not less than the length of this
   714      *             string.
   715      * @since      1.5
   716      */
   717     public int codePointAt(int index) {
   718         if ((index < 0) || (index >= count)) {
   719             throw new StringIndexOutOfBoundsException(index);
   720         }
   721         return Character.codePointAtImpl(value, offset + index, offset + count);
   722     }
   723 
   724     /**
   725      * Returns the character (Unicode code point) before the specified
   726      * index. The index refers to <code>char</code> values
   727      * (Unicode code units) and ranges from <code>1</code> to {@link
   728      * CharSequence#length() length}.
   729      *
   730      * <p> If the <code>char</code> value at <code>(index - 1)</code>
   731      * is in the low-surrogate range, <code>(index - 2)</code> is not
   732      * negative, and the <code>char</code> value at <code>(index -
   733      * 2)</code> is in the high-surrogate range, then the
   734      * supplementary code point value of the surrogate pair is
   735      * returned. If the <code>char</code> value at <code>index -
   736      * 1</code> is an unpaired low-surrogate or a high-surrogate, the
   737      * surrogate value is returned.
   738      *
   739      * @param     index the index following the code point that should be returned
   740      * @return    the Unicode code point value before the given index.
   741      * @exception IndexOutOfBoundsException if the <code>index</code>
   742      *            argument is less than 1 or greater than the length
   743      *            of this string.
   744      * @since     1.5
   745      */
   746     public int codePointBefore(int index) {
   747         int i = index - 1;
   748         if ((i < 0) || (i >= count)) {
   749             throw new StringIndexOutOfBoundsException(index);
   750         }
   751         return Character.codePointBeforeImpl(value, offset + index, offset);
   752     }
   753 
   754     /**
   755      * Returns the number of Unicode code points in the specified text
   756      * range of this <code>String</code>. The text range begins at the
   757      * specified <code>beginIndex</code> and extends to the
   758      * <code>char</code> at index <code>endIndex - 1</code>. Thus the
   759      * length (in <code>char</code>s) of the text range is
   760      * <code>endIndex-beginIndex</code>. Unpaired surrogates within
   761      * the text range count as one code point each.
   762      *
   763      * @param beginIndex the index to the first <code>char</code> of
   764      * the text range.
   765      * @param endIndex the index after the last <code>char</code> of
   766      * the text range.
   767      * @return the number of Unicode code points in the specified text
   768      * range
   769      * @exception IndexOutOfBoundsException if the
   770      * <code>beginIndex</code> is negative, or <code>endIndex</code>
   771      * is larger than the length of this <code>String</code>, or
   772      * <code>beginIndex</code> is larger than <code>endIndex</code>.
   773      * @since  1.5
   774      */
   775     public int codePointCount(int beginIndex, int endIndex) {
   776         if (beginIndex < 0 || endIndex > count || beginIndex > endIndex) {
   777             throw new IndexOutOfBoundsException();
   778         }
   779         return Character.codePointCountImpl(value, offset+beginIndex, endIndex-beginIndex);
   780     }
   781 
   782     /**
   783      * Returns the index within this <code>String</code> that is
   784      * offset from the given <code>index</code> by
   785      * <code>codePointOffset</code> code points. Unpaired surrogates
   786      * within the text range given by <code>index</code> and
   787      * <code>codePointOffset</code> count as one code point each.
   788      *
   789      * @param index the index to be offset
   790      * @param codePointOffset the offset in code points
   791      * @return the index within this <code>String</code>
   792      * @exception IndexOutOfBoundsException if <code>index</code>
   793      *   is negative or larger then the length of this
   794      *   <code>String</code>, or if <code>codePointOffset</code> is positive
   795      *   and the substring starting with <code>index</code> has fewer
   796      *   than <code>codePointOffset</code> code points,
   797      *   or if <code>codePointOffset</code> is negative and the substring
   798      *   before <code>index</code> has fewer than the absolute value
   799      *   of <code>codePointOffset</code> code points.
   800      * @since 1.5
   801      */
   802     public int offsetByCodePoints(int index, int codePointOffset) {
   803         if (index < 0 || index > count) {
   804             throw new IndexOutOfBoundsException();
   805         }
   806         return Character.offsetByCodePointsImpl(value, offset, count,
   807                                                 offset+index, codePointOffset) - offset;
   808     }
   809 
   810     /**
   811      * Copy characters from this string into dst starting at dstBegin.
   812      * This method doesn't perform any range checking.
   813      */
   814     void getChars(char dst[], int dstBegin) {
   815         System.arraycopy(value, offset, dst, dstBegin, count);
   816     }
   817 
   818     /**
   819      * Copies characters from this string into the destination character
   820      * array.
   821      * <p>
   822      * The first character to be copied is at index <code>srcBegin</code>;
   823      * the last character to be copied is at index <code>srcEnd-1</code>
   824      * (thus the total number of characters to be copied is
   825      * <code>srcEnd-srcBegin</code>). The characters are copied into the
   826      * subarray of <code>dst</code> starting at index <code>dstBegin</code>
   827      * and ending at index:
   828      * <p><blockquote><pre>
   829      *     dstbegin + (srcEnd-srcBegin) - 1
   830      * </pre></blockquote>
   831      *
   832      * @param      srcBegin   index of the first character in the string
   833      *                        to copy.
   834      * @param      srcEnd     index after the last character in the string
   835      *                        to copy.
   836      * @param      dst        the destination array.
   837      * @param      dstBegin   the start offset in the destination array.
   838      * @exception IndexOutOfBoundsException If any of the following
   839      *            is true:
   840      *            <ul><li><code>srcBegin</code> is negative.
   841      *            <li><code>srcBegin</code> is greater than <code>srcEnd</code>
   842      *            <li><code>srcEnd</code> is greater than the length of this
   843      *                string
   844      *            <li><code>dstBegin</code> is negative
   845      *            <li><code>dstBegin+(srcEnd-srcBegin)</code> is larger than
   846      *                <code>dst.length</code></ul>
   847      */
   848     public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
   849         if (srcBegin < 0) {
   850             throw new StringIndexOutOfBoundsException(srcBegin);
   851         }
   852         if (srcEnd > count) {
   853             throw new StringIndexOutOfBoundsException(srcEnd);
   854         }
   855         if (srcBegin > srcEnd) {
   856             throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
   857         }
   858         System.arraycopy(value, offset + srcBegin, dst, dstBegin,
   859              srcEnd - srcBegin);
   860     }
   861 
   862     /**
   863      * Copies characters from this string into the destination byte array. Each
   864      * byte receives the 8 low-order bits of the corresponding character. The
   865      * eight high-order bits of each character are not copied and do not
   866      * participate in the transfer in any way.
   867      *
   868      * <p> The first character to be copied is at index {@code srcBegin}; the
   869      * last character to be copied is at index {@code srcEnd-1}.  The total
   870      * number of characters to be copied is {@code srcEnd-srcBegin}. The
   871      * characters, converted to bytes, are copied into the subarray of {@code
   872      * dst} starting at index {@code dstBegin} and ending at index:
   873      *
   874      * <blockquote><pre>
   875      *     dstbegin + (srcEnd-srcBegin) - 1
   876      * </pre></blockquote>
   877      *
   878      * @deprecated  This method does not properly convert characters into
   879      * bytes.  As of JDK&nbsp;1.1, the preferred way to do this is via the
   880      * {@link #getBytes()} method, which uses the platform's default charset.
   881      *
   882      * @param  srcBegin
   883      *         Index of the first character in the string to copy
   884      *
   885      * @param  srcEnd
   886      *         Index after the last character in the string to copy
   887      *
   888      * @param  dst
   889      *         The destination array
   890      *
   891      * @param  dstBegin
   892      *         The start offset in the destination array
   893      *
   894      * @throws  IndexOutOfBoundsException
   895      *          If any of the following is true:
   896      *          <ul>
   897      *            <li> {@code srcBegin} is negative
   898      *            <li> {@code srcBegin} is greater than {@code srcEnd}
   899      *            <li> {@code srcEnd} is greater than the length of this String
   900      *            <li> {@code dstBegin} is negative
   901      *            <li> {@code dstBegin+(srcEnd-srcBegin)} is larger than {@code
   902      *                 dst.length}
   903      *          </ul>
   904      */
   905     @Deprecated
   906     public void getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin) {
   907         if (srcBegin < 0) {
   908             throw new StringIndexOutOfBoundsException(srcBegin);
   909         }
   910         if (srcEnd > count) {
   911             throw new StringIndexOutOfBoundsException(srcEnd);
   912         }
   913         if (srcBegin > srcEnd) {
   914             throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
   915         }
   916         int j = dstBegin;
   917         int n = offset + srcEnd;
   918         int i = offset + srcBegin;
   919         char[] val = value;   /* avoid getfield opcode */
   920 
   921         while (i < n) {
   922             dst[j++] = (byte)val[i++];
   923         }
   924     }
   925 
   926     /**
   927      * Encodes this {@code String} into a sequence of bytes using the named
   928      * charset, storing the result into a new byte array.
   929      *
   930      * <p> The behavior of this method when this string cannot be encoded in
   931      * the given charset is unspecified.  The {@link
   932      * java.nio.charset.CharsetEncoder} class should be used when more control
   933      * over the encoding process is required.
   934      *
   935      * @param  charsetName
   936      *         The name of a supported {@linkplain java.nio.charset.Charset
   937      *         charset}
   938      *
   939      * @return  The resultant byte array
   940      *
   941      * @throws  UnsupportedEncodingException
   942      *          If the named charset is not supported
   943      *
   944      * @since  JDK1.1
   945      */
   946     public byte[] getBytes(String charsetName)
   947         throws UnsupportedEncodingException
   948     {
   949         if (charsetName == null) throw new NullPointerException();
   950         return StringCoding.encode(charsetName, value, offset, count);
   951     }
   952 
   953     /**
   954      * Encodes this {@code String} into a sequence of bytes using the given
   955      * {@linkplain java.nio.charset.Charset charset}, storing the result into a
   956      * new byte array.
   957      *
   958      * <p> This method always replaces malformed-input and unmappable-character
   959      * sequences with this charset's default replacement byte array.  The
   960      * {@link java.nio.charset.CharsetEncoder} class should be used when more
   961      * control over the encoding process is required.
   962      *
   963      * @param  charset
   964      *         The {@linkplain java.nio.charset.Charset} to be used to encode
   965      *         the {@code String}
   966      *
   967      * @return  The resultant byte array
   968      *
   969      * @since  1.6
   970      */
   971     /* don't want dep on Charset
   972     public byte[] getBytes(Charset charset) {
   973         if (charset == null) throw new NullPointerException();
   974         return StringCoding.encode(charset, value, offset, count);
   975     }
   976     */
   977 
   978     /**
   979      * Encodes this {@code String} into a sequence of bytes using the
   980      * platform's default charset, storing the result into a new byte array.
   981      *
   982      * <p> The behavior of this method when this string cannot be encoded in
   983      * the default charset is unspecified.  The {@link
   984      * java.nio.charset.CharsetEncoder} class should be used when more control
   985      * over the encoding process is required.
   986      *
   987      * @return  The resultant byte array
   988      *
   989      * @since      JDK1.1
   990      */
   991     public byte[] getBytes() {
   992         return StringCoding.encode(value, offset, count);
   993     }
   994 
   995     /**
   996      * Compares this string to the specified object.  The result is {@code
   997      * true} if and only if the argument is not {@code null} and is a {@code
   998      * String} object that represents the same sequence of characters as this
   999      * object.
  1000      *
  1001      * @param  anObject
  1002      *         The object to compare this {@code String} against
  1003      *
  1004      * @return  {@code true} if the given object represents a {@code String}
  1005      *          equivalent to this string, {@code false} otherwise
  1006      *
  1007      * @see  #compareTo(String)
  1008      * @see  #equalsIgnoreCase(String)
  1009      */
  1010     public boolean equals(Object anObject) {
  1011         if (this == anObject) {
  1012             return true;
  1013         }
  1014         if (anObject instanceof String) {
  1015             String anotherString = (String)anObject;
  1016             int n = count;
  1017             if (n == anotherString.count) {
  1018                 char v1[] = value;
  1019                 char v2[] = anotherString.value;
  1020                 int i = offset;
  1021                 int j = anotherString.offset;
  1022                 while (n-- != 0) {
  1023                     if (v1[i++] != v2[j++])
  1024                         return false;
  1025                 }
  1026                 return true;
  1027             }
  1028         }
  1029         return false;
  1030     }
  1031 
  1032     /**
  1033      * Compares this string to the specified {@code StringBuffer}.  The result
  1034      * is {@code true} if and only if this {@code String} represents the same
  1035      * sequence of characters as the specified {@code StringBuffer}.
  1036      *
  1037      * @param  sb
  1038      *         The {@code StringBuffer} to compare this {@code String} against
  1039      *
  1040      * @return  {@code true} if this {@code String} represents the same
  1041      *          sequence of characters as the specified {@code StringBuffer},
  1042      *          {@code false} otherwise
  1043      *
  1044      * @since  1.4
  1045      */
  1046     public boolean contentEquals(StringBuffer sb) {
  1047         synchronized(sb) {
  1048             return contentEquals((CharSequence)sb);
  1049         }
  1050     }
  1051 
  1052     /**
  1053      * Compares this string to the specified {@code CharSequence}.  The result
  1054      * is {@code true} if and only if this {@code String} represents the same
  1055      * sequence of char values as the specified sequence.
  1056      *
  1057      * @param  cs
  1058      *         The sequence to compare this {@code String} against
  1059      *
  1060      * @return  {@code true} if this {@code String} represents the same
  1061      *          sequence of char values as the specified sequence, {@code
  1062      *          false} otherwise
  1063      *
  1064      * @since  1.5
  1065      */
  1066     public boolean contentEquals(CharSequence cs) {
  1067         if (count != cs.length())
  1068             return false;
  1069         // Argument is a StringBuffer, StringBuilder
  1070         if (cs instanceof AbstractStringBuilder) {
  1071             char v1[] = value;
  1072             char v2[] = ((AbstractStringBuilder)cs).getValue();
  1073             int i = offset;
  1074             int j = 0;
  1075             int n = count;
  1076             while (n-- != 0) {
  1077                 if (v1[i++] != v2[j++])
  1078                     return false;
  1079             }
  1080             return true;
  1081         }
  1082         // Argument is a String
  1083         if (cs.equals(this))
  1084             return true;
  1085         // Argument is a generic CharSequence
  1086         char v1[] = value;
  1087         int i = offset;
  1088         int j = 0;
  1089         int n = count;
  1090         while (n-- != 0) {
  1091             if (v1[i++] != cs.charAt(j++))
  1092                 return false;
  1093         }
  1094         return true;
  1095     }
  1096 
  1097     /**
  1098      * Compares this {@code String} to another {@code String}, ignoring case
  1099      * considerations.  Two strings are considered equal ignoring case if they
  1100      * are of the same length and corresponding characters in the two strings
  1101      * are equal ignoring case.
  1102      *
  1103      * <p> Two characters {@code c1} and {@code c2} are considered the same
  1104      * ignoring case if at least one of the following is true:
  1105      * <ul>
  1106      *   <li> The two characters are the same (as compared by the
  1107      *        {@code ==} operator)
  1108      *   <li> Applying the method {@link
  1109      *        java.lang.Character#toUpperCase(char)} to each character
  1110      *        produces the same result
  1111      *   <li> Applying the method {@link
  1112      *        java.lang.Character#toLowerCase(char)} to each character
  1113      *        produces the same result
  1114      * </ul>
  1115      *
  1116      * @param  anotherString
  1117      *         The {@code String} to compare this {@code String} against
  1118      *
  1119      * @return  {@code true} if the argument is not {@code null} and it
  1120      *          represents an equivalent {@code String} ignoring case; {@code
  1121      *          false} otherwise
  1122      *
  1123      * @see  #equals(Object)
  1124      */
  1125     public boolean equalsIgnoreCase(String anotherString) {
  1126         return (this == anotherString) ? true :
  1127                (anotherString != null) && (anotherString.count == count) &&
  1128                regionMatches(true, 0, anotherString, 0, count);
  1129     }
  1130 
  1131     /**
  1132      * Compares two strings lexicographically.
  1133      * The comparison is based on the Unicode value of each character in
  1134      * the strings. The character sequence represented by this
  1135      * <code>String</code> object is compared lexicographically to the
  1136      * character sequence represented by the argument string. The result is
  1137      * a negative integer if this <code>String</code> object
  1138      * lexicographically precedes the argument string. The result is a
  1139      * positive integer if this <code>String</code> object lexicographically
  1140      * follows the argument string. The result is zero if the strings
  1141      * are equal; <code>compareTo</code> returns <code>0</code> exactly when
  1142      * the {@link #equals(Object)} method would return <code>true</code>.
  1143      * <p>
  1144      * This is the definition of lexicographic ordering. If two strings are
  1145      * different, then either they have different characters at some index
  1146      * that is a valid index for both strings, or their lengths are different,
  1147      * or both. If they have different characters at one or more index
  1148      * positions, let <i>k</i> be the smallest such index; then the string
  1149      * whose character at position <i>k</i> has the smaller value, as
  1150      * determined by using the &lt; operator, lexicographically precedes the
  1151      * other string. In this case, <code>compareTo</code> returns the
  1152      * difference of the two character values at position <code>k</code> in
  1153      * the two string -- that is, the value:
  1154      * <blockquote><pre>
  1155      * this.charAt(k)-anotherString.charAt(k)
  1156      * </pre></blockquote>
  1157      * If there is no index position at which they differ, then the shorter
  1158      * string lexicographically precedes the longer string. In this case,
  1159      * <code>compareTo</code> returns the difference of the lengths of the
  1160      * strings -- that is, the value:
  1161      * <blockquote><pre>
  1162      * this.length()-anotherString.length()
  1163      * </pre></blockquote>
  1164      *
  1165      * @param   anotherString   the <code>String</code> to be compared.
  1166      * @return  the value <code>0</code> if the argument string is equal to
  1167      *          this string; a value less than <code>0</code> if this string
  1168      *          is lexicographically less than the string argument; and a
  1169      *          value greater than <code>0</code> if this string is
  1170      *          lexicographically greater than the string argument.
  1171      */
  1172     public int compareTo(String anotherString) {
  1173         int len1 = count;
  1174         int len2 = anotherString.count;
  1175         int n = Math.min(len1, len2);
  1176         char v1[] = value;
  1177         char v2[] = anotherString.value;
  1178         int i = offset;
  1179         int j = anotherString.offset;
  1180 
  1181         if (i == j) {
  1182             int k = i;
  1183             int lim = n + i;
  1184             while (k < lim) {
  1185                 char c1 = v1[k];
  1186                 char c2 = v2[k];
  1187                 if (c1 != c2) {
  1188                     return c1 - c2;
  1189                 }
  1190                 k++;
  1191             }
  1192         } else {
  1193             while (n-- != 0) {
  1194                 char c1 = v1[i++];
  1195                 char c2 = v2[j++];
  1196                 if (c1 != c2) {
  1197                     return c1 - c2;
  1198                 }
  1199             }
  1200         }
  1201         return len1 - len2;
  1202     }
  1203 
  1204     /**
  1205      * A Comparator that orders <code>String</code> objects as by
  1206      * <code>compareToIgnoreCase</code>. This comparator is serializable.
  1207      * <p>
  1208      * Note that this Comparator does <em>not</em> take locale into account,
  1209      * and will result in an unsatisfactory ordering for certain locales.
  1210      * The java.text package provides <em>Collators</em> to allow
  1211      * locale-sensitive ordering.
  1212      *
  1213      * @see     java.text.Collator#compare(String, String)
  1214      * @since   1.2
  1215      */
  1216     public static final Comparator<String> CASE_INSENSITIVE_ORDER
  1217                                          = new CaseInsensitiveComparator();
  1218     private static class CaseInsensitiveComparator
  1219                          implements Comparator<String>, java.io.Serializable {
  1220         // use serialVersionUID from JDK 1.2.2 for interoperability
  1221         private static final long serialVersionUID = 8575799808933029326L;
  1222 
  1223         public int compare(String s1, String s2) {
  1224             int n1 = s1.length();
  1225             int n2 = s2.length();
  1226             int min = Math.min(n1, n2);
  1227             for (int i = 0; i < min; i++) {
  1228                 char c1 = s1.charAt(i);
  1229                 char c2 = s2.charAt(i);
  1230                 if (c1 != c2) {
  1231                     c1 = Character.toUpperCase(c1);
  1232                     c2 = Character.toUpperCase(c2);
  1233                     if (c1 != c2) {
  1234                         c1 = Character.toLowerCase(c1);
  1235                         c2 = Character.toLowerCase(c2);
  1236                         if (c1 != c2) {
  1237                             // No overflow because of numeric promotion
  1238                             return c1 - c2;
  1239                         }
  1240                     }
  1241                 }
  1242             }
  1243             return n1 - n2;
  1244         }
  1245     }
  1246 
  1247     /**
  1248      * Compares two strings lexicographically, ignoring case
  1249      * differences. This method returns an integer whose sign is that of
  1250      * calling <code>compareTo</code> with normalized versions of the strings
  1251      * where case differences have been eliminated by calling
  1252      * <code>Character.toLowerCase(Character.toUpperCase(character))</code> on
  1253      * each character.
  1254      * <p>
  1255      * Note that this method does <em>not</em> take locale into account,
  1256      * and will result in an unsatisfactory ordering for certain locales.
  1257      * The java.text package provides <em>collators</em> to allow
  1258      * locale-sensitive ordering.
  1259      *
  1260      * @param   str   the <code>String</code> to be compared.
  1261      * @return  a negative integer, zero, or a positive integer as the
  1262      *          specified String is greater than, equal to, or less
  1263      *          than this String, ignoring case considerations.
  1264      * @see     java.text.Collator#compare(String, String)
  1265      * @since   1.2
  1266      */
  1267     public int compareToIgnoreCase(String str) {
  1268         return CASE_INSENSITIVE_ORDER.compare(this, str);
  1269     }
  1270 
  1271     /**
  1272      * Tests if two string regions are equal.
  1273      * <p>
  1274      * A substring of this <tt>String</tt> object is compared to a substring
  1275      * of the argument other. The result is true if these substrings
  1276      * represent identical character sequences. The substring of this
  1277      * <tt>String</tt> object to be compared begins at index <tt>toffset</tt>
  1278      * and has length <tt>len</tt>. The substring of other to be compared
  1279      * begins at index <tt>ooffset</tt> and has length <tt>len</tt>. The
  1280      * result is <tt>false</tt> if and only if at least one of the following
  1281      * is true:
  1282      * <ul><li><tt>toffset</tt> is negative.
  1283      * <li><tt>ooffset</tt> is negative.
  1284      * <li><tt>toffset+len</tt> is greater than the length of this
  1285      * <tt>String</tt> object.
  1286      * <li><tt>ooffset+len</tt> is greater than the length of the other
  1287      * argument.
  1288      * <li>There is some nonnegative integer <i>k</i> less than <tt>len</tt>
  1289      * such that:
  1290      * <tt>this.charAt(toffset+<i>k</i>)&nbsp;!=&nbsp;other.charAt(ooffset+<i>k</i>)</tt>
  1291      * </ul>
  1292      *
  1293      * @param   toffset   the starting offset of the subregion in this string.
  1294      * @param   other     the string argument.
  1295      * @param   ooffset   the starting offset of the subregion in the string
  1296      *                    argument.
  1297      * @param   len       the number of characters to compare.
  1298      * @return  <code>true</code> if the specified subregion of this string
  1299      *          exactly matches the specified subregion of the string argument;
  1300      *          <code>false</code> otherwise.
  1301      */
  1302     public boolean regionMatches(int toffset, String other, int ooffset,
  1303                                  int len) {
  1304         char ta[] = value;
  1305         int to = offset + toffset;
  1306         char pa[] = other.value;
  1307         int po = other.offset + ooffset;
  1308         // Note: toffset, ooffset, or len might be near -1>>>1.
  1309         if ((ooffset < 0) || (toffset < 0) || (toffset > (long)count - len)
  1310             || (ooffset > (long)other.count - len)) {
  1311             return false;
  1312         }
  1313         while (len-- > 0) {
  1314             if (ta[to++] != pa[po++]) {
  1315                 return false;
  1316             }
  1317         }
  1318         return true;
  1319     }
  1320 
  1321     /**
  1322      * Tests if two string regions are equal.
  1323      * <p>
  1324      * A substring of this <tt>String</tt> object is compared to a substring
  1325      * of the argument <tt>other</tt>. The result is <tt>true</tt> if these
  1326      * substrings represent character sequences that are the same, ignoring
  1327      * case if and only if <tt>ignoreCase</tt> is true. The substring of
  1328      * this <tt>String</tt> object to be compared begins at index
  1329      * <tt>toffset</tt> and has length <tt>len</tt>. The substring of
  1330      * <tt>other</tt> to be compared begins at index <tt>ooffset</tt> and
  1331      * has length <tt>len</tt>. The result is <tt>false</tt> if and only if
  1332      * at least one of the following is true:
  1333      * <ul><li><tt>toffset</tt> is negative.
  1334      * <li><tt>ooffset</tt> is negative.
  1335      * <li><tt>toffset+len</tt> is greater than the length of this
  1336      * <tt>String</tt> object.
  1337      * <li><tt>ooffset+len</tt> is greater than the length of the other
  1338      * argument.
  1339      * <li><tt>ignoreCase</tt> is <tt>false</tt> and there is some nonnegative
  1340      * integer <i>k</i> less than <tt>len</tt> such that:
  1341      * <blockquote><pre>
  1342      * this.charAt(toffset+k) != other.charAt(ooffset+k)
  1343      * </pre></blockquote>
  1344      * <li><tt>ignoreCase</tt> is <tt>true</tt> and there is some nonnegative
  1345      * integer <i>k</i> less than <tt>len</tt> such that:
  1346      * <blockquote><pre>
  1347      * Character.toLowerCase(this.charAt(toffset+k)) !=
  1348                Character.toLowerCase(other.charAt(ooffset+k))
  1349      * </pre></blockquote>
  1350      * and:
  1351      * <blockquote><pre>
  1352      * Character.toUpperCase(this.charAt(toffset+k)) !=
  1353      *         Character.toUpperCase(other.charAt(ooffset+k))
  1354      * </pre></blockquote>
  1355      * </ul>
  1356      *
  1357      * @param   ignoreCase   if <code>true</code>, ignore case when comparing
  1358      *                       characters.
  1359      * @param   toffset      the starting offset of the subregion in this
  1360      *                       string.
  1361      * @param   other        the string argument.
  1362      * @param   ooffset      the starting offset of the subregion in the string
  1363      *                       argument.
  1364      * @param   len          the number of characters to compare.
  1365      * @return  <code>true</code> if the specified subregion of this string
  1366      *          matches the specified subregion of the string argument;
  1367      *          <code>false</code> otherwise. Whether the matching is exact
  1368      *          or case insensitive depends on the <code>ignoreCase</code>
  1369      *          argument.
  1370      */
  1371     public boolean regionMatches(boolean ignoreCase, int toffset,
  1372                            String other, int ooffset, int len) {
  1373         char ta[] = value;
  1374         int to = offset + toffset;
  1375         char pa[] = other.value;
  1376         int po = other.offset + ooffset;
  1377         // Note: toffset, ooffset, or len might be near -1>>>1.
  1378         if ((ooffset < 0) || (toffset < 0) || (toffset > (long)count - len) ||
  1379                 (ooffset > (long)other.count - len)) {
  1380             return false;
  1381         }
  1382         while (len-- > 0) {
  1383             char c1 = ta[to++];
  1384             char c2 = pa[po++];
  1385             if (c1 == c2) {
  1386                 continue;
  1387             }
  1388             if (ignoreCase) {
  1389                 // If characters don't match but case may be ignored,
  1390                 // try converting both characters to uppercase.
  1391                 // If the results match, then the comparison scan should
  1392                 // continue.
  1393                 char u1 = Character.toUpperCase(c1);
  1394                 char u2 = Character.toUpperCase(c2);
  1395                 if (u1 == u2) {
  1396                     continue;
  1397                 }
  1398                 // Unfortunately, conversion to uppercase does not work properly
  1399                 // for the Georgian alphabet, which has strange rules about case
  1400                 // conversion.  So we need to make one last check before
  1401                 // exiting.
  1402                 if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) {
  1403                     continue;
  1404                 }
  1405             }
  1406             return false;
  1407         }
  1408         return true;
  1409     }
  1410 
  1411     /**
  1412      * Tests if the substring of this string beginning at the
  1413      * specified index starts with the specified prefix.
  1414      *
  1415      * @param   prefix    the prefix.
  1416      * @param   toffset   where to begin looking in this string.
  1417      * @return  <code>true</code> if the character sequence represented by the
  1418      *          argument is a prefix of the substring of this object starting
  1419      *          at index <code>toffset</code>; <code>false</code> otherwise.
  1420      *          The result is <code>false</code> if <code>toffset</code> is
  1421      *          negative or greater than the length of this
  1422      *          <code>String</code> object; otherwise the result is the same
  1423      *          as the result of the expression
  1424      *          <pre>
  1425      *          this.substring(toffset).startsWith(prefix)
  1426      *          </pre>
  1427      */
  1428     public boolean startsWith(String prefix, int toffset) {
  1429         char ta[] = value;
  1430         int to = offset + toffset;
  1431         char pa[] = prefix.value;
  1432         int po = prefix.offset;
  1433         int pc = prefix.count;
  1434         // Note: toffset might be near -1>>>1.
  1435         if ((toffset < 0) || (toffset > count - pc)) {
  1436             return false;
  1437         }
  1438         while (--pc >= 0) {
  1439             if (ta[to++] != pa[po++]) {
  1440                 return false;
  1441             }
  1442         }
  1443         return true;
  1444     }
  1445 
  1446     /**
  1447      * Tests if this string starts with the specified prefix.
  1448      *
  1449      * @param   prefix   the prefix.
  1450      * @return  <code>true</code> if the character sequence represented by the
  1451      *          argument is a prefix of the character sequence represented by
  1452      *          this string; <code>false</code> otherwise.
  1453      *          Note also that <code>true</code> will be returned if the
  1454      *          argument is an empty string or is equal to this
  1455      *          <code>String</code> object as determined by the
  1456      *          {@link #equals(Object)} method.
  1457      * @since   1. 0
  1458      */
  1459     public boolean startsWith(String prefix) {
  1460         return startsWith(prefix, 0);
  1461     }
  1462 
  1463     /**
  1464      * Tests if this string ends with the specified suffix.
  1465      *
  1466      * @param   suffix   the suffix.
  1467      * @return  <code>true</code> if the character sequence represented by the
  1468      *          argument is a suffix of the character sequence represented by
  1469      *          this object; <code>false</code> otherwise. Note that the
  1470      *          result will be <code>true</code> if the argument is the
  1471      *          empty string or is equal to this <code>String</code> object
  1472      *          as determined by the {@link #equals(Object)} method.
  1473      */
  1474     public boolean endsWith(String suffix) {
  1475         return startsWith(suffix, count - suffix.count);
  1476     }
  1477 
  1478     /**
  1479      * Returns a hash code for this string. The hash code for a
  1480      * <code>String</code> object is computed as
  1481      * <blockquote><pre>
  1482      * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
  1483      * </pre></blockquote>
  1484      * using <code>int</code> arithmetic, where <code>s[i]</code> is the
  1485      * <i>i</i>th character of the string, <code>n</code> is the length of
  1486      * the string, and <code>^</code> indicates exponentiation.
  1487      * (The hash value of the empty string is zero.)
  1488      *
  1489      * @return  a hash code value for this object.
  1490      */
  1491     public int hashCode() {
  1492         int h = hash;
  1493         if (h == 0 && count > 0) {
  1494             int off = offset;
  1495             char val[] = value;
  1496             int len = count;
  1497 
  1498             for (int i = 0; i < len; i++) {
  1499                 h = 31*h + val[off++];
  1500             }
  1501             hash = h;
  1502         }
  1503         return h;
  1504     }
  1505 
  1506     /**
  1507      * Returns the index within this string of the first occurrence of
  1508      * the specified character. If a character with value
  1509      * <code>ch</code> occurs in the character sequence represented by
  1510      * this <code>String</code> object, then the index (in Unicode
  1511      * code units) of the first such occurrence is returned. For
  1512      * values of <code>ch</code> in the range from 0 to 0xFFFF
  1513      * (inclusive), this is the smallest value <i>k</i> such that:
  1514      * <blockquote><pre>
  1515      * this.charAt(<i>k</i>) == ch
  1516      * </pre></blockquote>
  1517      * is true. For other values of <code>ch</code>, it is the
  1518      * smallest value <i>k</i> such that:
  1519      * <blockquote><pre>
  1520      * this.codePointAt(<i>k</i>) == ch
  1521      * </pre></blockquote>
  1522      * is true. In either case, if no such character occurs in this
  1523      * string, then <code>-1</code> is returned.
  1524      *
  1525      * @param   ch   a character (Unicode code point).
  1526      * @return  the index of the first occurrence of the character in the
  1527      *          character sequence represented by this object, or
  1528      *          <code>-1</code> if the character does not occur.
  1529      */
  1530     public int indexOf(int ch) {
  1531         return indexOf(ch, 0);
  1532     }
  1533 
  1534     /**
  1535      * Returns the index within this string of the first occurrence of the
  1536      * specified character, starting the search at the specified index.
  1537      * <p>
  1538      * If a character with value <code>ch</code> occurs in the
  1539      * character sequence represented by this <code>String</code>
  1540      * object at an index no smaller than <code>fromIndex</code>, then
  1541      * the index of the first such occurrence is returned. For values
  1542      * of <code>ch</code> in the range from 0 to 0xFFFF (inclusive),
  1543      * this is the smallest value <i>k</i> such that:
  1544      * <blockquote><pre>
  1545      * (this.charAt(<i>k</i>) == ch) && (<i>k</i> &gt;= fromIndex)
  1546      * </pre></blockquote>
  1547      * is true. For other values of <code>ch</code>, it is the
  1548      * smallest value <i>k</i> such that:
  1549      * <blockquote><pre>
  1550      * (this.codePointAt(<i>k</i>) == ch) && (<i>k</i> &gt;= fromIndex)
  1551      * </pre></blockquote>
  1552      * is true. In either case, if no such character occurs in this
  1553      * string at or after position <code>fromIndex</code>, then
  1554      * <code>-1</code> is returned.
  1555      *
  1556      * <p>
  1557      * There is no restriction on the value of <code>fromIndex</code>. If it
  1558      * is negative, it has the same effect as if it were zero: this entire
  1559      * string may be searched. If it is greater than the length of this
  1560      * string, it has the same effect as if it were equal to the length of
  1561      * this string: <code>-1</code> is returned.
  1562      *
  1563      * <p>All indices are specified in <code>char</code> values
  1564      * (Unicode code units).
  1565      *
  1566      * @param   ch          a character (Unicode code point).
  1567      * @param   fromIndex   the index to start the search from.
  1568      * @return  the index of the first occurrence of the character in the
  1569      *          character sequence represented by this object that is greater
  1570      *          than or equal to <code>fromIndex</code>, or <code>-1</code>
  1571      *          if the character does not occur.
  1572      */
  1573     public int indexOf(int ch, int fromIndex) {
  1574         if (fromIndex < 0) {
  1575             fromIndex = 0;
  1576         } else if (fromIndex >= count) {
  1577             // Note: fromIndex might be near -1>>>1.
  1578             return -1;
  1579         }
  1580 
  1581         if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
  1582             // handle most cases here (ch is a BMP code point or a
  1583             // negative value (invalid code point))
  1584             final char[] value = this.value;
  1585             final int offset = this.offset;
  1586             final int max = offset + count;
  1587             for (int i = offset + fromIndex; i < max ; i++) {
  1588                 if (value[i] == ch) {
  1589                     return i - offset;
  1590                 }
  1591             }
  1592             return -1;
  1593         } else {
  1594             return indexOfSupplementary(ch, fromIndex);
  1595         }
  1596     }
  1597 
  1598     /**
  1599      * Handles (rare) calls of indexOf with a supplementary character.
  1600      */
  1601     private int indexOfSupplementary(int ch, int fromIndex) {
  1602         if (Character.isValidCodePoint(ch)) {
  1603             final char[] value = this.value;
  1604             final int offset = this.offset;
  1605             final char hi = Character.highSurrogate(ch);
  1606             final char lo = Character.lowSurrogate(ch);
  1607             final int max = offset + count - 1;
  1608             for (int i = offset + fromIndex; i < max; i++) {
  1609                 if (value[i] == hi && value[i+1] == lo) {
  1610                     return i - offset;
  1611                 }
  1612             }
  1613         }
  1614         return -1;
  1615     }
  1616 
  1617     /**
  1618      * Returns the index within this string of the last occurrence of
  1619      * the specified character. For values of <code>ch</code> in the
  1620      * range from 0 to 0xFFFF (inclusive), the index (in Unicode code
  1621      * units) returned is the largest value <i>k</i> such that:
  1622      * <blockquote><pre>
  1623      * this.charAt(<i>k</i>) == ch
  1624      * </pre></blockquote>
  1625      * is true. For other values of <code>ch</code>, it is the
  1626      * largest value <i>k</i> such that:
  1627      * <blockquote><pre>
  1628      * this.codePointAt(<i>k</i>) == ch
  1629      * </pre></blockquote>
  1630      * is true.  In either case, if no such character occurs in this
  1631      * string, then <code>-1</code> is returned.  The
  1632      * <code>String</code> is searched backwards starting at the last
  1633      * character.
  1634      *
  1635      * @param   ch   a character (Unicode code point).
  1636      * @return  the index of the last occurrence of the character in the
  1637      *          character sequence represented by this object, or
  1638      *          <code>-1</code> if the character does not occur.
  1639      */
  1640     public int lastIndexOf(int ch) {
  1641         return lastIndexOf(ch, count - 1);
  1642     }
  1643 
  1644     /**
  1645      * Returns the index within this string of the last occurrence of
  1646      * the specified character, searching backward starting at the
  1647      * specified index. For values of <code>ch</code> in the range
  1648      * from 0 to 0xFFFF (inclusive), the index returned is the largest
  1649      * value <i>k</i> such that:
  1650      * <blockquote><pre>
  1651      * (this.charAt(<i>k</i>) == ch) && (<i>k</i> &lt;= fromIndex)
  1652      * </pre></blockquote>
  1653      * is true. For other values of <code>ch</code>, it is the
  1654      * largest value <i>k</i> such that:
  1655      * <blockquote><pre>
  1656      * (this.codePointAt(<i>k</i>) == ch) && (<i>k</i> &lt;= fromIndex)
  1657      * </pre></blockquote>
  1658      * is true. In either case, if no such character occurs in this
  1659      * string at or before position <code>fromIndex</code>, then
  1660      * <code>-1</code> is returned.
  1661      *
  1662      * <p>All indices are specified in <code>char</code> values
  1663      * (Unicode code units).
  1664      *
  1665      * @param   ch          a character (Unicode code point).
  1666      * @param   fromIndex   the index to start the search from. There is no
  1667      *          restriction on the value of <code>fromIndex</code>. If it is
  1668      *          greater than or equal to the length of this string, it has
  1669      *          the same effect as if it were equal to one less than the
  1670      *          length of this string: this entire string may be searched.
  1671      *          If it is negative, it has the same effect as if it were -1:
  1672      *          -1 is returned.
  1673      * @return  the index of the last occurrence of the character in the
  1674      *          character sequence represented by this object that is less
  1675      *          than or equal to <code>fromIndex</code>, or <code>-1</code>
  1676      *          if the character does not occur before that point.
  1677      */
  1678     public int lastIndexOf(int ch, int fromIndex) {
  1679         if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
  1680             // handle most cases here (ch is a BMP code point or a
  1681             // negative value (invalid code point))
  1682             final char[] value = this.value;
  1683             final int offset = this.offset;
  1684             int i = offset + Math.min(fromIndex, count - 1);
  1685             for (; i >= offset ; i--) {
  1686                 if (value[i] == ch) {
  1687                     return i - offset;
  1688                 }
  1689             }
  1690             return -1;
  1691         } else {
  1692             return lastIndexOfSupplementary(ch, fromIndex);
  1693         }
  1694     }
  1695 
  1696     /**
  1697      * Handles (rare) calls of lastIndexOf with a supplementary character.
  1698      */
  1699     private int lastIndexOfSupplementary(int ch, int fromIndex) {
  1700         if (Character.isValidCodePoint(ch)) {
  1701             final char[] value = this.value;
  1702             final int offset = this.offset;
  1703             char hi = Character.highSurrogate(ch);
  1704             char lo = Character.lowSurrogate(ch);
  1705             int i = offset + Math.min(fromIndex, count - 2);
  1706             for (; i >= offset; i--) {
  1707                 if (value[i] == hi && value[i+1] == lo) {
  1708                     return i - offset;
  1709                 }
  1710             }
  1711         }
  1712         return -1;
  1713     }
  1714 
  1715     /**
  1716      * Returns the index within this string of the first occurrence of the
  1717      * specified substring.
  1718      *
  1719      * <p>The returned index is the smallest value <i>k</i> for which:
  1720      * <blockquote><pre>
  1721      * this.startsWith(str, <i>k</i>)
  1722      * </pre></blockquote>
  1723      * If no such value of <i>k</i> exists, then {@code -1} is returned.
  1724      *
  1725      * @param   str   the substring to search for.
  1726      * @return  the index of the first occurrence of the specified substring,
  1727      *          or {@code -1} if there is no such occurrence.
  1728      */
  1729     public int indexOf(String str) {
  1730         return indexOf(str, 0);
  1731     }
  1732 
  1733     /**
  1734      * Returns the index within this string of the first occurrence of the
  1735      * specified substring, starting at the specified index.
  1736      *
  1737      * <p>The returned index is the smallest value <i>k</i> for which:
  1738      * <blockquote><pre>
  1739      * <i>k</i> &gt;= fromIndex && this.startsWith(str, <i>k</i>)
  1740      * </pre></blockquote>
  1741      * If no such value of <i>k</i> exists, then {@code -1} is returned.
  1742      *
  1743      * @param   str         the substring to search for.
  1744      * @param   fromIndex   the index from which to start the search.
  1745      * @return  the index of the first occurrence of the specified substring,
  1746      *          starting at the specified index,
  1747      *          or {@code -1} if there is no such occurrence.
  1748      */
  1749     public int indexOf(String str, int fromIndex) {
  1750         return indexOf(value, offset, count,
  1751                        str.value, str.offset, str.count, fromIndex);
  1752     }
  1753 
  1754     /**
  1755      * Code shared by String and StringBuffer to do searches. The
  1756      * source is the character array being searched, and the target
  1757      * is the string being searched for.
  1758      *
  1759      * @param   source       the characters being searched.
  1760      * @param   sourceOffset offset of the source string.
  1761      * @param   sourceCount  count of the source string.
  1762      * @param   target       the characters being searched for.
  1763      * @param   targetOffset offset of the target string.
  1764      * @param   targetCount  count of the target string.
  1765      * @param   fromIndex    the index to begin searching from.
  1766      */
  1767     static int indexOf(char[] source, int sourceOffset, int sourceCount,
  1768                        char[] target, int targetOffset, int targetCount,
  1769                        int fromIndex) {
  1770         if (fromIndex >= sourceCount) {
  1771             return (targetCount == 0 ? sourceCount : -1);
  1772         }
  1773         if (fromIndex < 0) {
  1774             fromIndex = 0;
  1775         }
  1776         if (targetCount == 0) {
  1777             return fromIndex;
  1778         }
  1779 
  1780         char first  = target[targetOffset];
  1781         int max = sourceOffset + (sourceCount - targetCount);
  1782 
  1783         for (int i = sourceOffset + fromIndex; i <= max; i++) {
  1784             /* Look for first character. */
  1785             if (source[i] != first) {
  1786                 while (++i <= max && source[i] != first);
  1787             }
  1788 
  1789             /* Found first character, now look at the rest of v2 */
  1790             if (i <= max) {
  1791                 int j = i + 1;
  1792                 int end = j + targetCount - 1;
  1793                 for (int k = targetOffset + 1; j < end && source[j] ==
  1794                          target[k]; j++, k++);
  1795 
  1796                 if (j == end) {
  1797                     /* Found whole string. */
  1798                     return i - sourceOffset;
  1799                 }
  1800             }
  1801         }
  1802         return -1;
  1803     }
  1804 
  1805     /**
  1806      * Returns the index within this string of the last occurrence of the
  1807      * specified substring.  The last occurrence of the empty string ""
  1808      * is considered to occur at the index value {@code this.length()}.
  1809      *
  1810      * <p>The returned index is the largest value <i>k</i> for which:
  1811      * <blockquote><pre>
  1812      * this.startsWith(str, <i>k</i>)
  1813      * </pre></blockquote>
  1814      * If no such value of <i>k</i> exists, then {@code -1} is returned.
  1815      *
  1816      * @param   str   the substring to search for.
  1817      * @return  the index of the last occurrence of the specified substring,
  1818      *          or {@code -1} if there is no such occurrence.
  1819      */
  1820     public int lastIndexOf(String str) {
  1821         return lastIndexOf(str, count);
  1822     }
  1823 
  1824     /**
  1825      * Returns the index within this string of the last occurrence of the
  1826      * specified substring, searching backward starting at the specified index.
  1827      *
  1828      * <p>The returned index is the largest value <i>k</i> for which:
  1829      * <blockquote><pre>
  1830      * <i>k</i> &lt;= fromIndex && this.startsWith(str, <i>k</i>)
  1831      * </pre></blockquote>
  1832      * If no such value of <i>k</i> exists, then {@code -1} is returned.
  1833      *
  1834      * @param   str         the substring to search for.
  1835      * @param   fromIndex   the index to start the search from.
  1836      * @return  the index of the last occurrence of the specified substring,
  1837      *          searching backward from the specified index,
  1838      *          or {@code -1} if there is no such occurrence.
  1839      */
  1840     public int lastIndexOf(String str, int fromIndex) {
  1841         return lastIndexOf(value, offset, count,
  1842                            str.value, str.offset, str.count, fromIndex);
  1843     }
  1844 
  1845     /**
  1846      * Code shared by String and StringBuffer to do searches. The
  1847      * source is the character array being searched, and the target
  1848      * is the string being searched for.
  1849      *
  1850      * @param   source       the characters being searched.
  1851      * @param   sourceOffset offset of the source string.
  1852      * @param   sourceCount  count of the source string.
  1853      * @param   target       the characters being searched for.
  1854      * @param   targetOffset offset of the target string.
  1855      * @param   targetCount  count of the target string.
  1856      * @param   fromIndex    the index to begin searching from.
  1857      */
  1858     static int lastIndexOf(char[] source, int sourceOffset, int sourceCount,
  1859                            char[] target, int targetOffset, int targetCount,
  1860                            int fromIndex) {
  1861         /*
  1862          * Check arguments; return immediately where possible. For
  1863          * consistency, don't check for null str.
  1864          */
  1865         int rightIndex = sourceCount - targetCount;
  1866         if (fromIndex < 0) {
  1867             return -1;
  1868         }
  1869         if (fromIndex > rightIndex) {
  1870             fromIndex = rightIndex;
  1871         }
  1872         /* Empty string always matches. */
  1873         if (targetCount == 0) {
  1874             return fromIndex;
  1875         }
  1876 
  1877         int strLastIndex = targetOffset + targetCount - 1;
  1878         char strLastChar = target[strLastIndex];
  1879         int min = sourceOffset + targetCount - 1;
  1880         int i = min + fromIndex;
  1881 
  1882     startSearchForLastChar:
  1883         while (true) {
  1884             while (i >= min && source[i] != strLastChar) {
  1885                 i--;
  1886             }
  1887             if (i < min) {
  1888                 return -1;
  1889             }
  1890             int j = i - 1;
  1891             int start = j - (targetCount - 1);
  1892             int k = strLastIndex - 1;
  1893 
  1894             while (j > start) {
  1895                 if (source[j--] != target[k--]) {
  1896                     i--;
  1897                     continue startSearchForLastChar;
  1898                 }
  1899             }
  1900             return start - sourceOffset + 1;
  1901         }
  1902     }
  1903 
  1904     /**
  1905      * Returns a new string that is a substring of this string. The
  1906      * substring begins with the character at the specified index and
  1907      * extends to the end of this string. <p>
  1908      * Examples:
  1909      * <blockquote><pre>
  1910      * "unhappy".substring(2) returns "happy"
  1911      * "Harbison".substring(3) returns "bison"
  1912      * "emptiness".substring(9) returns "" (an empty string)
  1913      * </pre></blockquote>
  1914      *
  1915      * @param      beginIndex   the beginning index, inclusive.
  1916      * @return     the specified substring.
  1917      * @exception  IndexOutOfBoundsException  if
  1918      *             <code>beginIndex</code> is negative or larger than the
  1919      *             length of this <code>String</code> object.
  1920      */
  1921     public String substring(int beginIndex) {
  1922         return substring(beginIndex, count);
  1923     }
  1924 
  1925     /**
  1926      * Returns a new string that is a substring of this string. The
  1927      * substring begins at the specified <code>beginIndex</code> and
  1928      * extends to the character at index <code>endIndex - 1</code>.
  1929      * Thus the length of the substring is <code>endIndex-beginIndex</code>.
  1930      * <p>
  1931      * Examples:
  1932      * <blockquote><pre>
  1933      * "hamburger".substring(4, 8) returns "urge"
  1934      * "smiles".substring(1, 5) returns "mile"
  1935      * </pre></blockquote>
  1936      *
  1937      * @param      beginIndex   the beginning index, inclusive.
  1938      * @param      endIndex     the ending index, exclusive.
  1939      * @return     the specified substring.
  1940      * @exception  IndexOutOfBoundsException  if the
  1941      *             <code>beginIndex</code> is negative, or
  1942      *             <code>endIndex</code> is larger than the length of
  1943      *             this <code>String</code> object, or
  1944      *             <code>beginIndex</code> is larger than
  1945      *             <code>endIndex</code>.
  1946      */
  1947     public String substring(int beginIndex, int endIndex) {
  1948         if (beginIndex < 0) {
  1949             throw new StringIndexOutOfBoundsException(beginIndex);
  1950         }
  1951         if (endIndex > count) {
  1952             throw new StringIndexOutOfBoundsException(endIndex);
  1953         }
  1954         if (beginIndex > endIndex) {
  1955             throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
  1956         }
  1957         return ((beginIndex == 0) && (endIndex == count)) ? this :
  1958             new String(offset + beginIndex, endIndex - beginIndex, value);
  1959     }
  1960 
  1961     /**
  1962      * Returns a new character sequence that is a subsequence of this sequence.
  1963      *
  1964      * <p> An invocation of this method of the form
  1965      *
  1966      * <blockquote><pre>
  1967      * str.subSequence(begin,&nbsp;end)</pre></blockquote>
  1968      *
  1969      * behaves in exactly the same way as the invocation
  1970      *
  1971      * <blockquote><pre>
  1972      * str.substring(begin,&nbsp;end)</pre></blockquote>
  1973      *
  1974      * This method is defined so that the <tt>String</tt> class can implement
  1975      * the {@link CharSequence} interface. </p>
  1976      *
  1977      * @param      beginIndex   the begin index, inclusive.
  1978      * @param      endIndex     the end index, exclusive.
  1979      * @return     the specified subsequence.
  1980      *
  1981      * @throws  IndexOutOfBoundsException
  1982      *          if <tt>beginIndex</tt> or <tt>endIndex</tt> are negative,
  1983      *          if <tt>endIndex</tt> is greater than <tt>length()</tt>,
  1984      *          or if <tt>beginIndex</tt> is greater than <tt>startIndex</tt>
  1985      *
  1986      * @since 1.4
  1987      * @spec JSR-51
  1988      */
  1989     public CharSequence subSequence(int beginIndex, int endIndex) {
  1990         return this.substring(beginIndex, endIndex);
  1991     }
  1992 
  1993     /**
  1994      * Concatenates the specified string to the end of this string.
  1995      * <p>
  1996      * If the length of the argument string is <code>0</code>, then this
  1997      * <code>String</code> object is returned. Otherwise, a new
  1998      * <code>String</code> object is created, representing a character
  1999      * sequence that is the concatenation of the character sequence
  2000      * represented by this <code>String</code> object and the character
  2001      * sequence represented by the argument string.<p>
  2002      * Examples:
  2003      * <blockquote><pre>
  2004      * "cares".concat("s") returns "caress"
  2005      * "to".concat("get").concat("her") returns "together"
  2006      * </pre></blockquote>
  2007      *
  2008      * @param   str   the <code>String</code> that is concatenated to the end
  2009      *                of this <code>String</code>.
  2010      * @return  a string that represents the concatenation of this object's
  2011      *          characters followed by the string argument's characters.
  2012      */
  2013     public String concat(String str) {
  2014         int otherLen = str.length();
  2015         if (otherLen == 0) {
  2016             return this;
  2017         }
  2018         char buf[] = new char[count + otherLen];
  2019         getChars(0, count, buf, 0);
  2020         str.getChars(0, otherLen, buf, count);
  2021         return new String(0, count + otherLen, buf);
  2022     }
  2023 
  2024     /**
  2025      * Returns a new string resulting from replacing all occurrences of
  2026      * <code>oldChar</code> in this string with <code>newChar</code>.
  2027      * <p>
  2028      * If the character <code>oldChar</code> does not occur in the
  2029      * character sequence represented by this <code>String</code> object,
  2030      * then a reference to this <code>String</code> object is returned.
  2031      * Otherwise, a new <code>String</code> object is created that
  2032      * represents a character sequence identical to the character sequence
  2033      * represented by this <code>String</code> object, except that every
  2034      * occurrence of <code>oldChar</code> is replaced by an occurrence
  2035      * of <code>newChar</code>.
  2036      * <p>
  2037      * Examples:
  2038      * <blockquote><pre>
  2039      * "mesquite in your cellar".replace('e', 'o')
  2040      *         returns "mosquito in your collar"
  2041      * "the war of baronets".replace('r', 'y')
  2042      *         returns "the way of bayonets"
  2043      * "sparring with a purple porpoise".replace('p', 't')
  2044      *         returns "starring with a turtle tortoise"
  2045      * "JonL".replace('q', 'x') returns "JonL" (no change)
  2046      * </pre></blockquote>
  2047      *
  2048      * @param   oldChar   the old character.
  2049      * @param   newChar   the new character.
  2050      * @return  a string derived from this string by replacing every
  2051      *          occurrence of <code>oldChar</code> with <code>newChar</code>.
  2052      */
  2053     public String replace(char oldChar, char newChar) {
  2054         if (oldChar != newChar) {
  2055             int len = count;
  2056             int i = -1;
  2057             char[] val = value; /* avoid getfield opcode */
  2058             int off = offset;   /* avoid getfield opcode */
  2059 
  2060             while (++i < len) {
  2061                 if (val[off + i] == oldChar) {
  2062                     break;
  2063                 }
  2064             }
  2065             if (i < len) {
  2066                 char buf[] = new char[len];
  2067                 for (int j = 0 ; j < i ; j++) {
  2068                     buf[j] = val[off+j];
  2069                 }
  2070                 while (i < len) {
  2071                     char c = val[off + i];
  2072                     buf[i] = (c == oldChar) ? newChar : c;
  2073                     i++;
  2074                 }
  2075                 return new String(0, len, buf);
  2076             }
  2077         }
  2078         return this;
  2079     }
  2080 
  2081     /**
  2082      * Tells whether or not this string matches the given <a
  2083      * href="../util/regex/Pattern.html#sum">regular expression</a>.
  2084      *
  2085      * <p> An invocation of this method of the form
  2086      * <i>str</i><tt>.matches(</tt><i>regex</i><tt>)</tt> yields exactly the
  2087      * same result as the expression
  2088      *
  2089      * <blockquote><tt> {@link java.util.regex.Pattern}.{@link
  2090      * java.util.regex.Pattern#matches(String,CharSequence)
  2091      * matches}(</tt><i>regex</i><tt>,</tt> <i>str</i><tt>)</tt></blockquote>
  2092      *
  2093      * @param   regex
  2094      *          the regular expression to which this string is to be matched
  2095      *
  2096      * @return  <tt>true</tt> if, and only if, this string matches the
  2097      *          given regular expression
  2098      *
  2099      * @throws  PatternSyntaxException
  2100      *          if the regular expression's syntax is invalid
  2101      *
  2102      * @see java.util.regex.Pattern
  2103      *
  2104      * @since 1.4
  2105      * @spec JSR-51
  2106      */
  2107     public boolean matches(String regex) {
  2108         throw new UnsupportedOperationException();
  2109     }
  2110 
  2111     /**
  2112      * Returns true if and only if this string contains the specified
  2113      * sequence of char values.
  2114      *
  2115      * @param s the sequence to search for
  2116      * @return true if this string contains <code>s</code>, false otherwise
  2117      * @throws NullPointerException if <code>s</code> is <code>null</code>
  2118      * @since 1.5
  2119      */
  2120     public boolean contains(CharSequence s) {
  2121         return indexOf(s.toString()) > -1;
  2122     }
  2123 
  2124     /**
  2125      * Replaces the first substring of this string that matches the given <a
  2126      * href="../util/regex/Pattern.html#sum">regular expression</a> with the
  2127      * given replacement.
  2128      *
  2129      * <p> An invocation of this method of the form
  2130      * <i>str</i><tt>.replaceFirst(</tt><i>regex</i><tt>,</tt> <i>repl</i><tt>)</tt>
  2131      * yields exactly the same result as the expression
  2132      *
  2133      * <blockquote><tt>
  2134      * {@link java.util.regex.Pattern}.{@link java.util.regex.Pattern#compile
  2135      * compile}(</tt><i>regex</i><tt>).{@link
  2136      * java.util.regex.Pattern#matcher(java.lang.CharSequence)
  2137      * matcher}(</tt><i>str</i><tt>).{@link java.util.regex.Matcher#replaceFirst
  2138      * replaceFirst}(</tt><i>repl</i><tt>)</tt></blockquote>
  2139      *
  2140      *<p>
  2141      * Note that backslashes (<tt>\</tt>) and dollar signs (<tt>$</tt>) in the
  2142      * replacement string may cause the results to be different than if it were
  2143      * being treated as a literal replacement string; see
  2144      * {@link java.util.regex.Matcher#replaceFirst}.
  2145      * Use {@link java.util.regex.Matcher#quoteReplacement} to suppress the special
  2146      * meaning of these characters, if desired.
  2147      *
  2148      * @param   regex
  2149      *          the regular expression to which this string is to be matched
  2150      * @param   replacement
  2151      *          the string to be substituted for the first match
  2152      *
  2153      * @return  The resulting <tt>String</tt>
  2154      *
  2155      * @throws  PatternSyntaxException
  2156      *          if the regular expression's syntax is invalid
  2157      *
  2158      * @see java.util.regex.Pattern
  2159      *
  2160      * @since 1.4
  2161      * @spec JSR-51
  2162      */
  2163     public String replaceFirst(String regex, String replacement) {
  2164         throw new UnsupportedOperationException();
  2165     }
  2166 
  2167     /**
  2168      * Replaces each substring of this string that matches the given <a
  2169      * href="../util/regex/Pattern.html#sum">regular expression</a> with the
  2170      * given replacement.
  2171      *
  2172      * <p> An invocation of this method of the form
  2173      * <i>str</i><tt>.replaceAll(</tt><i>regex</i><tt>,</tt> <i>repl</i><tt>)</tt>
  2174      * yields exactly the same result as the expression
  2175      *
  2176      * <blockquote><tt>
  2177      * {@link java.util.regex.Pattern}.{@link java.util.regex.Pattern#compile
  2178      * compile}(</tt><i>regex</i><tt>).{@link
  2179      * java.util.regex.Pattern#matcher(java.lang.CharSequence)
  2180      * matcher}(</tt><i>str</i><tt>).{@link java.util.regex.Matcher#replaceAll
  2181      * replaceAll}(</tt><i>repl</i><tt>)</tt></blockquote>
  2182      *
  2183      *<p>
  2184      * Note that backslashes (<tt>\</tt>) and dollar signs (<tt>$</tt>) in the
  2185      * replacement string may cause the results to be different than if it were
  2186      * being treated as a literal replacement string; see
  2187      * {@link java.util.regex.Matcher#replaceAll Matcher.replaceAll}.
  2188      * Use {@link java.util.regex.Matcher#quoteReplacement} to suppress the special
  2189      * meaning of these characters, if desired.
  2190      *
  2191      * @param   regex
  2192      *          the regular expression to which this string is to be matched
  2193      * @param   replacement
  2194      *          the string to be substituted for each match
  2195      *
  2196      * @return  The resulting <tt>String</tt>
  2197      *
  2198      * @throws  PatternSyntaxException
  2199      *          if the regular expression's syntax is invalid
  2200      *
  2201      * @see java.util.regex.Pattern
  2202      *
  2203      * @since 1.4
  2204      * @spec JSR-51
  2205      */
  2206     public String replaceAll(String regex, String replacement) {
  2207         throw new UnsupportedOperationException();
  2208     }
  2209 
  2210     /**
  2211      * Replaces each substring of this string that matches the literal target
  2212      * sequence with the specified literal replacement sequence. The
  2213      * replacement proceeds from the beginning of the string to the end, for
  2214      * example, replacing "aa" with "b" in the string "aaa" will result in
  2215      * "ba" rather than "ab".
  2216      *
  2217      * @param  target The sequence of char values to be replaced
  2218      * @param  replacement The replacement sequence of char values
  2219      * @return  The resulting string
  2220      * @throws NullPointerException if <code>target</code> or
  2221      *         <code>replacement</code> is <code>null</code>.
  2222      * @since 1.5
  2223      */
  2224     public String replace(CharSequence target, CharSequence replacement) {
  2225         throw new UnsupportedOperationException("This one should be supported, but without dep on rest of regexp");
  2226     }
  2227 
  2228     /**
  2229      * Splits this string around matches of the given
  2230      * <a href="../util/regex/Pattern.html#sum">regular expression</a>.
  2231      *
  2232      * <p> The array returned by this method contains each substring of this
  2233      * string that is terminated by another substring that matches the given
  2234      * expression or is terminated by the end of the string.  The substrings in
  2235      * the array are in the order in which they occur in this string.  If the
  2236      * expression does not match any part of the input then the resulting array
  2237      * has just one element, namely this string.
  2238      *
  2239      * <p> The <tt>limit</tt> parameter controls the number of times the
  2240      * pattern is applied and therefore affects the length of the resulting
  2241      * array.  If the limit <i>n</i> is greater than zero then the pattern
  2242      * will be applied at most <i>n</i>&nbsp;-&nbsp;1 times, the array's
  2243      * length will be no greater than <i>n</i>, and the array's last entry
  2244      * will contain all input beyond the last matched delimiter.  If <i>n</i>
  2245      * is non-positive then the pattern will be applied as many times as
  2246      * possible and the array can have any length.  If <i>n</i> is zero then
  2247      * the pattern will be applied as many times as possible, the array can
  2248      * have any length, and trailing empty strings will be discarded.
  2249      *
  2250      * <p> The string <tt>"boo:and:foo"</tt>, for example, yields the
  2251      * following results with these parameters:
  2252      *
  2253      * <blockquote><table cellpadding=1 cellspacing=0 summary="Split example showing regex, limit, and result">
  2254      * <tr>
  2255      *     <th>Regex</th>
  2256      *     <th>Limit</th>
  2257      *     <th>Result</th>
  2258      * </tr>
  2259      * <tr><td align=center>:</td>
  2260      *     <td align=center>2</td>
  2261      *     <td><tt>{ "boo", "and:foo" }</tt></td></tr>
  2262      * <tr><td align=center>:</td>
  2263      *     <td align=center>5</td>
  2264      *     <td><tt>{ "boo", "and", "foo" }</tt></td></tr>
  2265      * <tr><td align=center>:</td>
  2266      *     <td align=center>-2</td>
  2267      *     <td><tt>{ "boo", "and", "foo" }</tt></td></tr>
  2268      * <tr><td align=center>o</td>
  2269      *     <td align=center>5</td>
  2270      *     <td><tt>{ "b", "", ":and:f", "", "" }</tt></td></tr>
  2271      * <tr><td align=center>o</td>
  2272      *     <td align=center>-2</td>
  2273      *     <td><tt>{ "b", "", ":and:f", "", "" }</tt></td></tr>
  2274      * <tr><td align=center>o</td>
  2275      *     <td align=center>0</td>
  2276      *     <td><tt>{ "b", "", ":and:f" }</tt></td></tr>
  2277      * </table></blockquote>
  2278      *
  2279      * <p> An invocation of this method of the form
  2280      * <i>str.</i><tt>split(</tt><i>regex</i><tt>,</tt>&nbsp;<i>n</i><tt>)</tt>
  2281      * yields the same result as the expression
  2282      *
  2283      * <blockquote>
  2284      * {@link java.util.regex.Pattern}.{@link java.util.regex.Pattern#compile
  2285      * compile}<tt>(</tt><i>regex</i><tt>)</tt>.{@link
  2286      * java.util.regex.Pattern#split(java.lang.CharSequence,int)
  2287      * split}<tt>(</tt><i>str</i><tt>,</tt>&nbsp;<i>n</i><tt>)</tt>
  2288      * </blockquote>
  2289      *
  2290      *
  2291      * @param  regex
  2292      *         the delimiting regular expression
  2293      *
  2294      * @param  limit
  2295      *         the result threshold, as described above
  2296      *
  2297      * @return  the array of strings computed by splitting this string
  2298      *          around matches of the given regular expression
  2299      *
  2300      * @throws  PatternSyntaxException
  2301      *          if the regular expression's syntax is invalid
  2302      *
  2303      * @see java.util.regex.Pattern
  2304      *
  2305      * @since 1.4
  2306      * @spec JSR-51
  2307      */
  2308     public String[] split(String regex, int limit) {
  2309         throw new UnsupportedOperationException("Needs regexp");
  2310     }
  2311 
  2312     /**
  2313      * Splits this string around matches of the given <a
  2314      * href="../util/regex/Pattern.html#sum">regular expression</a>.
  2315      *
  2316      * <p> This method works as if by invoking the two-argument {@link
  2317      * #split(String, int) split} method with the given expression and a limit
  2318      * argument of zero.  Trailing empty strings are therefore not included in
  2319      * the resulting array.
  2320      *
  2321      * <p> The string <tt>"boo:and:foo"</tt>, for example, yields the following
  2322      * results with these expressions:
  2323      *
  2324      * <blockquote><table cellpadding=1 cellspacing=0 summary="Split examples showing regex and result">
  2325      * <tr>
  2326      *  <th>Regex</th>
  2327      *  <th>Result</th>
  2328      * </tr>
  2329      * <tr><td align=center>:</td>
  2330      *     <td><tt>{ "boo", "and", "foo" }</tt></td></tr>
  2331      * <tr><td align=center>o</td>
  2332      *     <td><tt>{ "b", "", ":and:f" }</tt></td></tr>
  2333      * </table></blockquote>
  2334      *
  2335      *
  2336      * @param  regex
  2337      *         the delimiting regular expression
  2338      *
  2339      * @return  the array of strings computed by splitting this string
  2340      *          around matches of the given regular expression
  2341      *
  2342      * @throws  PatternSyntaxException
  2343      *          if the regular expression's syntax is invalid
  2344      *
  2345      * @see java.util.regex.Pattern
  2346      *
  2347      * @since 1.4
  2348      * @spec JSR-51
  2349      */
  2350     public String[] split(String regex) {
  2351         return split(regex, 0);
  2352     }
  2353 
  2354     /**
  2355      * Converts all of the characters in this <code>String</code> to lower
  2356      * case using the rules of the given <code>Locale</code>.  Case mapping is based
  2357      * on the Unicode Standard version specified by the {@link java.lang.Character Character}
  2358      * class. Since case mappings are not always 1:1 char mappings, the resulting
  2359      * <code>String</code> may be a different length than the original <code>String</code>.
  2360      * <p>
  2361      * Examples of lowercase  mappings are in the following table:
  2362      * <table border="1" summary="Lowercase mapping examples showing language code of locale, upper case, lower case, and description">
  2363      * <tr>
  2364      *   <th>Language Code of Locale</th>
  2365      *   <th>Upper Case</th>
  2366      *   <th>Lower Case</th>
  2367      *   <th>Description</th>
  2368      * </tr>
  2369      * <tr>
  2370      *   <td>tr (Turkish)</td>
  2371      *   <td>&#92;u0130</td>
  2372      *   <td>&#92;u0069</td>
  2373      *   <td>capital letter I with dot above -&gt; small letter i</td>
  2374      * </tr>
  2375      * <tr>
  2376      *   <td>tr (Turkish)</td>
  2377      *   <td>&#92;u0049</td>
  2378      *   <td>&#92;u0131</td>
  2379      *   <td>capital letter I -&gt; small letter dotless i </td>
  2380      * </tr>
  2381      * <tr>
  2382      *   <td>(all)</td>
  2383      *   <td>French Fries</td>
  2384      *   <td>french fries</td>
  2385      *   <td>lowercased all chars in String</td>
  2386      * </tr>
  2387      * <tr>
  2388      *   <td>(all)</td>
  2389      *   <td><img src="doc-files/capiota.gif" alt="capiota"><img src="doc-files/capchi.gif" alt="capchi">
  2390      *       <img src="doc-files/captheta.gif" alt="captheta"><img src="doc-files/capupsil.gif" alt="capupsil">
  2391      *       <img src="doc-files/capsigma.gif" alt="capsigma"></td>
  2392      *   <td><img src="doc-files/iota.gif" alt="iota"><img src="doc-files/chi.gif" alt="chi">
  2393      *       <img src="doc-files/theta.gif" alt="theta"><img src="doc-files/upsilon.gif" alt="upsilon">
  2394      *       <img src="doc-files/sigma1.gif" alt="sigma"></td>
  2395      *   <td>lowercased all chars in String</td>
  2396      * </tr>
  2397      * </table>
  2398      *
  2399      * @param locale use the case transformation rules for this locale
  2400      * @return the <code>String</code>, converted to lowercase.
  2401      * @see     java.lang.String#toLowerCase()
  2402      * @see     java.lang.String#toUpperCase()
  2403      * @see     java.lang.String#toUpperCase(Locale)
  2404      * @since   1.1
  2405      */
  2406 //    public String toLowerCase(Locale locale) {
  2407 //        if (locale == null) {
  2408 //            throw new NullPointerException();
  2409 //        }
  2410 //
  2411 //        int     firstUpper;
  2412 //
  2413 //        /* Now check if there are any characters that need to be changed. */
  2414 //        scan: {
  2415 //            for (firstUpper = 0 ; firstUpper < count; ) {
  2416 //                char c = value[offset+firstUpper];
  2417 //                if ((c >= Character.MIN_HIGH_SURROGATE) &&
  2418 //                    (c <= Character.MAX_HIGH_SURROGATE)) {
  2419 //                    int supplChar = codePointAt(firstUpper);
  2420 //                    if (supplChar != Character.toLowerCase(supplChar)) {
  2421 //                        break scan;
  2422 //                    }
  2423 //                    firstUpper += Character.charCount(supplChar);
  2424 //                } else {
  2425 //                    if (c != Character.toLowerCase(c)) {
  2426 //                        break scan;
  2427 //                    }
  2428 //                    firstUpper++;
  2429 //                }
  2430 //            }
  2431 //            return this;
  2432 //        }
  2433 //
  2434 //        char[]  result = new char[count];
  2435 //        int     resultOffset = 0;  /* result may grow, so i+resultOffset
  2436 //                                    * is the write location in result */
  2437 //
  2438 //        /* Just copy the first few lowerCase characters. */
  2439 //        System.arraycopy(value, offset, result, 0, firstUpper);
  2440 //
  2441 //        String lang = locale.getLanguage();
  2442 //        boolean localeDependent =
  2443 //            (lang == "tr" || lang == "az" || lang == "lt");
  2444 //        char[] lowerCharArray;
  2445 //        int lowerChar;
  2446 //        int srcChar;
  2447 //        int srcCount;
  2448 //        for (int i = firstUpper; i < count; i += srcCount) {
  2449 //            srcChar = (int)value[offset+i];
  2450 //            if ((char)srcChar >= Character.MIN_HIGH_SURROGATE &&
  2451 //                (char)srcChar <= Character.MAX_HIGH_SURROGATE) {
  2452 //                srcChar = codePointAt(i);
  2453 //                srcCount = Character.charCount(srcChar);
  2454 //            } else {
  2455 //                srcCount = 1;
  2456 //            }
  2457 //            if (localeDependent || srcChar == '\u03A3') { // GREEK CAPITAL LETTER SIGMA
  2458 //                lowerChar = ConditionalSpecialCasing.toLowerCaseEx(this, i, locale);
  2459 //            } else if (srcChar == '\u0130') { // LATIN CAPITAL LETTER I DOT
  2460 //                lowerChar = Character.ERROR;
  2461 //            } else {
  2462 //                lowerChar = Character.toLowerCase(srcChar);
  2463 //            }
  2464 //            if ((lowerChar == Character.ERROR) ||
  2465 //                (lowerChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {
  2466 //                if (lowerChar == Character.ERROR) {
  2467 //                     if (!localeDependent && srcChar == '\u0130') {
  2468 //                         lowerCharArray =
  2469 //                             ConditionalSpecialCasing.toLowerCaseCharArray(this, i, Locale.ENGLISH);
  2470 //                     } else {
  2471 //                        lowerCharArray =
  2472 //                            ConditionalSpecialCasing.toLowerCaseCharArray(this, i, locale);
  2473 //                     }
  2474 //                } else if (srcCount == 2) {
  2475 //                    resultOffset += Character.toChars(lowerChar, result, i + resultOffset) - srcCount;
  2476 //                    continue;
  2477 //                } else {
  2478 //                    lowerCharArray = Character.toChars(lowerChar);
  2479 //                }
  2480 //
  2481 //                /* Grow result if needed */
  2482 //                int mapLen = lowerCharArray.length;
  2483 //                if (mapLen > srcCount) {
  2484 //                    char[] result2 = new char[result.length + mapLen - srcCount];
  2485 //                    System.arraycopy(result, 0, result2, 0,
  2486 //                        i + resultOffset);
  2487 //                    result = result2;
  2488 //                }
  2489 //                for (int x=0; x<mapLen; ++x) {
  2490 //                    result[i+resultOffset+x] = lowerCharArray[x];
  2491 //                }
  2492 //                resultOffset += (mapLen - srcCount);
  2493 //            } else {
  2494 //                result[i+resultOffset] = (char)lowerChar;
  2495 //            }
  2496 //        }
  2497 //        return new String(0, count+resultOffset, result);
  2498 //    }
  2499 
  2500     /**
  2501      * Converts all of the characters in this <code>String</code> to lower
  2502      * case using the rules of the default locale. This is equivalent to calling
  2503      * <code>toLowerCase(Locale.getDefault())</code>.
  2504      * <p>
  2505      * <b>Note:</b> This method is locale sensitive, and may produce unexpected
  2506      * results if used for strings that are intended to be interpreted locale
  2507      * independently.
  2508      * Examples are programming language identifiers, protocol keys, and HTML
  2509      * tags.
  2510      * For instance, <code>"TITLE".toLowerCase()</code> in a Turkish locale
  2511      * returns <code>"t\u005Cu0131tle"</code>, where '\u005Cu0131' is the
  2512      * LATIN SMALL LETTER DOTLESS I character.
  2513      * To obtain correct results for locale insensitive strings, use
  2514      * <code>toLowerCase(Locale.ENGLISH)</code>.
  2515      * <p>
  2516      * @return  the <code>String</code>, converted to lowercase.
  2517      * @see     java.lang.String#toLowerCase(Locale)
  2518      */
  2519     public String toLowerCase() {
  2520         throw new UnsupportedOperationException("Should be supported but without connection to locale");
  2521     }
  2522 
  2523     /**
  2524      * Converts all of the characters in this <code>String</code> to upper
  2525      * case using the rules of the given <code>Locale</code>. Case mapping is based
  2526      * on the Unicode Standard version specified by the {@link java.lang.Character Character}
  2527      * class. Since case mappings are not always 1:1 char mappings, the resulting
  2528      * <code>String</code> may be a different length than the original <code>String</code>.
  2529      * <p>
  2530      * Examples of locale-sensitive and 1:M case mappings are in the following table.
  2531      * <p>
  2532      * <table border="1" summary="Examples of locale-sensitive and 1:M case mappings. Shows Language code of locale, lower case, upper case, and description.">
  2533      * <tr>
  2534      *   <th>Language Code of Locale</th>
  2535      *   <th>Lower Case</th>
  2536      *   <th>Upper Case</th>
  2537      *   <th>Description</th>
  2538      * </tr>
  2539      * <tr>
  2540      *   <td>tr (Turkish)</td>
  2541      *   <td>&#92;u0069</td>
  2542      *   <td>&#92;u0130</td>
  2543      *   <td>small letter i -&gt; capital letter I with dot above</td>
  2544      * </tr>
  2545      * <tr>
  2546      *   <td>tr (Turkish)</td>
  2547      *   <td>&#92;u0131</td>
  2548      *   <td>&#92;u0049</td>
  2549      *   <td>small letter dotless i -&gt; capital letter I</td>
  2550      * </tr>
  2551      * <tr>
  2552      *   <td>(all)</td>
  2553      *   <td>&#92;u00df</td>
  2554      *   <td>&#92;u0053 &#92;u0053</td>
  2555      *   <td>small letter sharp s -&gt; two letters: SS</td>
  2556      * </tr>
  2557      * <tr>
  2558      *   <td>(all)</td>
  2559      *   <td>Fahrvergn&uuml;gen</td>
  2560      *   <td>FAHRVERGN&Uuml;GEN</td>
  2561      *   <td></td>
  2562      * </tr>
  2563      * </table>
  2564      * @param locale use the case transformation rules for this locale
  2565      * @return the <code>String</code>, converted to uppercase.
  2566      * @see     java.lang.String#toUpperCase()
  2567      * @see     java.lang.String#toLowerCase()
  2568      * @see     java.lang.String#toLowerCase(Locale)
  2569      * @since   1.1
  2570      */
  2571     /* not for javascript 
  2572     public String toUpperCase(Locale locale) {
  2573         if (locale == null) {
  2574             throw new NullPointerException();
  2575         }
  2576 
  2577         int     firstLower;
  2578 
  2579         // Now check if there are any characters that need to be changed. 
  2580         scan: {
  2581             for (firstLower = 0 ; firstLower < count; ) {
  2582                 int c = (int)value[offset+firstLower];
  2583                 int srcCount;
  2584                 if ((c >= Character.MIN_HIGH_SURROGATE) &&
  2585                     (c <= Character.MAX_HIGH_SURROGATE)) {
  2586                     c = codePointAt(firstLower);
  2587                     srcCount = Character.charCount(c);
  2588                 } else {
  2589                     srcCount = 1;
  2590                 }
  2591                 int upperCaseChar = Character.toUpperCaseEx(c);
  2592                 if ((upperCaseChar == Character.ERROR) ||
  2593                     (c != upperCaseChar)) {
  2594                     break scan;
  2595                 }
  2596                 firstLower += srcCount;
  2597             }
  2598             return this;
  2599         }
  2600 
  2601         char[]  result       = new char[count]; /* may grow *
  2602         int     resultOffset = 0;  /* result may grow, so i+resultOffset
  2603                                     * is the write location in result *
  2604 
  2605         /* Just copy the first few upperCase characters. *
  2606         System.arraycopy(value, offset, result, 0, firstLower);
  2607 
  2608         String lang = locale.getLanguage();
  2609         boolean localeDependent =
  2610             (lang == "tr" || lang == "az" || lang == "lt");
  2611         char[] upperCharArray;
  2612         int upperChar;
  2613         int srcChar;
  2614         int srcCount;
  2615         for (int i = firstLower; i < count; i += srcCount) {
  2616             srcChar = (int)value[offset+i];
  2617             if ((char)srcChar >= Character.MIN_HIGH_SURROGATE &&
  2618                 (char)srcChar <= Character.MAX_HIGH_SURROGATE) {
  2619                 srcChar = codePointAt(i);
  2620                 srcCount = Character.charCount(srcChar);
  2621             } else {
  2622                 srcCount = 1;
  2623             }
  2624             if (localeDependent) {
  2625                 upperChar = ConditionalSpecialCasing.toUpperCaseEx(this, i, locale);
  2626             } else {
  2627                 upperChar = Character.toUpperCaseEx(srcChar);
  2628             }
  2629             if ((upperChar == Character.ERROR) ||
  2630                 (upperChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {
  2631                 if (upperChar == Character.ERROR) {
  2632                     if (localeDependent) {
  2633                         upperCharArray =
  2634                             ConditionalSpecialCasing.toUpperCaseCharArray(this, i, locale);
  2635                     } else {
  2636                         upperCharArray = Character.toUpperCaseCharArray(srcChar);
  2637                     }
  2638                 } else if (srcCount == 2) {
  2639                     resultOffset += Character.toChars(upperChar, result, i + resultOffset) - srcCount;
  2640                     continue;
  2641                 } else {
  2642                     upperCharArray = Character.toChars(upperChar);
  2643                 }
  2644 
  2645                 /* Grow result if needed *
  2646                 int mapLen = upperCharArray.length;
  2647                 if (mapLen > srcCount) {
  2648                     char[] result2 = new char[result.length + mapLen - srcCount];
  2649                     System.arraycopy(result, 0, result2, 0,
  2650                         i + resultOffset);
  2651                     result = result2;
  2652                 }
  2653                 for (int x=0; x<mapLen; ++x) {
  2654                     result[i+resultOffset+x] = upperCharArray[x];
  2655                 }
  2656                 resultOffset += (mapLen - srcCount);
  2657             } else {
  2658                 result[i+resultOffset] = (char)upperChar;
  2659             }
  2660         }
  2661         return new String(0, count+resultOffset, result);
  2662     }
  2663     */
  2664 
  2665     /**
  2666      * Converts all of the characters in this <code>String</code> to upper
  2667      * case using the rules of the default locale. This method is equivalent to
  2668      * <code>toUpperCase(Locale.getDefault())</code>.
  2669      * <p>
  2670      * <b>Note:</b> This method is locale sensitive, and may produce unexpected
  2671      * results if used for strings that are intended to be interpreted locale
  2672      * independently.
  2673      * Examples are programming language identifiers, protocol keys, and HTML
  2674      * tags.
  2675      * For instance, <code>"title".toUpperCase()</code> in a Turkish locale
  2676      * returns <code>"T\u005Cu0130TLE"</code>, where '\u005Cu0130' is the
  2677      * LATIN CAPITAL LETTER I WITH DOT ABOVE character.
  2678      * To obtain correct results for locale insensitive strings, use
  2679      * <code>toUpperCase(Locale.ENGLISH)</code>.
  2680      * <p>
  2681      * @return  the <code>String</code>, converted to uppercase.
  2682      * @see     java.lang.String#toUpperCase(Locale)
  2683      */
  2684     public String toUpperCase() {
  2685         throw new UnsupportedOperationException();
  2686     }
  2687 
  2688     /**
  2689      * Returns a copy of the string, with leading and trailing whitespace
  2690      * omitted.
  2691      * <p>
  2692      * If this <code>String</code> object represents an empty character
  2693      * sequence, or the first and last characters of character sequence
  2694      * represented by this <code>String</code> object both have codes
  2695      * greater than <code>'&#92;u0020'</code> (the space character), then a
  2696      * reference to this <code>String</code> object is returned.
  2697      * <p>
  2698      * Otherwise, if there is no character with a code greater than
  2699      * <code>'&#92;u0020'</code> in the string, then a new
  2700      * <code>String</code> object representing an empty string is created
  2701      * and returned.
  2702      * <p>
  2703      * Otherwise, let <i>k</i> be the index of the first character in the
  2704      * string whose code is greater than <code>'&#92;u0020'</code>, and let
  2705      * <i>m</i> be the index of the last character in the string whose code
  2706      * is greater than <code>'&#92;u0020'</code>. A new <code>String</code>
  2707      * object is created, representing the substring of this string that
  2708      * begins with the character at index <i>k</i> and ends with the
  2709      * character at index <i>m</i>-that is, the result of
  2710      * <code>this.substring(<i>k</i>,&nbsp;<i>m</i>+1)</code>.
  2711      * <p>
  2712      * This method may be used to trim whitespace (as defined above) from
  2713      * the beginning and end of a string.
  2714      *
  2715      * @return  A copy of this string with leading and trailing white
  2716      *          space removed, or this string if it has no leading or
  2717      *          trailing white space.
  2718      */
  2719     public String trim() {
  2720         int len = count;
  2721         int st = 0;
  2722         int off = offset;      /* avoid getfield opcode */
  2723         char[] val = value;    /* avoid getfield opcode */
  2724 
  2725         while ((st < len) && (val[off + st] <= ' ')) {
  2726             st++;
  2727         }
  2728         while ((st < len) && (val[off + len - 1] <= ' ')) {
  2729             len--;
  2730         }
  2731         return ((st > 0) || (len < count)) ? substring(st, len) : this;
  2732     }
  2733 
  2734     /**
  2735      * This object (which is already a string!) is itself returned.
  2736      *
  2737      * @return  the string itself.
  2738      */
  2739     public String toString() {
  2740         return this;
  2741     }
  2742 
  2743     /**
  2744      * Converts this string to a new character array.
  2745      *
  2746      * @return  a newly allocated character array whose length is the length
  2747      *          of this string and whose contents are initialized to contain
  2748      *          the character sequence represented by this string.
  2749      */
  2750     public char[] toCharArray() {
  2751         char result[] = new char[count];
  2752         getChars(0, count, result, 0);
  2753         return result;
  2754     }
  2755 
  2756     /**
  2757      * Returns a formatted string using the specified format string and
  2758      * arguments.
  2759      *
  2760      * <p> The locale always used is the one returned by {@link
  2761      * java.util.Locale#getDefault() Locale.getDefault()}.
  2762      *
  2763      * @param  format
  2764      *         A <a href="../util/Formatter.html#syntax">format string</a>
  2765      *
  2766      * @param  args
  2767      *         Arguments referenced by the format specifiers in the format
  2768      *         string.  If there are more arguments than format specifiers, the
  2769      *         extra arguments are ignored.  The number of arguments is
  2770      *         variable and may be zero.  The maximum number of arguments is
  2771      *         limited by the maximum dimension of a Java array as defined by
  2772      *         <cite>The Java&trade; Virtual Machine Specification</cite>.
  2773      *         The behaviour on a
  2774      *         <tt>null</tt> argument depends on the <a
  2775      *         href="../util/Formatter.html#syntax">conversion</a>.
  2776      *
  2777      * @throws  IllegalFormatException
  2778      *          If a format string contains an illegal syntax, a format
  2779      *          specifier that is incompatible with the given arguments,
  2780      *          insufficient arguments given the format string, or other
  2781      *          illegal conditions.  For specification of all possible
  2782      *          formatting errors, see the <a
  2783      *          href="../util/Formatter.html#detail">Details</a> section of the
  2784      *          formatter class specification.
  2785      *
  2786      * @throws  NullPointerException
  2787      *          If the <tt>format</tt> is <tt>null</tt>
  2788      *
  2789      * @return  A formatted string
  2790      *
  2791      * @see  java.util.Formatter
  2792      * @since  1.5
  2793      */
  2794     public static String format(String format, Object ... args) {
  2795         throw new UnsupportedOperationException();
  2796     }
  2797 
  2798     /**
  2799      * Returns a formatted string using the specified locale, format string,
  2800      * and arguments.
  2801      *
  2802      * @param  l
  2803      *         The {@linkplain java.util.Locale locale} to apply during
  2804      *         formatting.  If <tt>l</tt> is <tt>null</tt> then no localization
  2805      *         is applied.
  2806      *
  2807      * @param  format
  2808      *         A <a href="../util/Formatter.html#syntax">format string</a>
  2809      *
  2810      * @param  args
  2811      *         Arguments referenced by the format specifiers in the format
  2812      *         string.  If there are more arguments than format specifiers, the
  2813      *         extra arguments are ignored.  The number of arguments is
  2814      *         variable and may be zero.  The maximum number of arguments is
  2815      *         limited by the maximum dimension of a Java array as defined by
  2816      *         <cite>The Java&trade; Virtual Machine Specification</cite>.
  2817      *         The behaviour on a
  2818      *         <tt>null</tt> argument depends on the <a
  2819      *         href="../util/Formatter.html#syntax">conversion</a>.
  2820      *
  2821      * @throws  IllegalFormatException
  2822      *          If a format string contains an illegal syntax, a format
  2823      *          specifier that is incompatible with the given arguments,
  2824      *          insufficient arguments given the format string, or other
  2825      *          illegal conditions.  For specification of all possible
  2826      *          formatting errors, see the <a
  2827      *          href="../util/Formatter.html#detail">Details</a> section of the
  2828      *          formatter class specification
  2829      *
  2830      * @throws  NullPointerException
  2831      *          If the <tt>format</tt> is <tt>null</tt>
  2832      *
  2833      * @return  A formatted string
  2834      *
  2835      * @see  java.util.Formatter
  2836      * @since  1.5
  2837      */
  2838 //    public static String format(Locale l, String format, Object ... args) {
  2839 //        return new Formatter(l).format(format, args).toString();
  2840 //    }
  2841 
  2842     /**
  2843      * Returns the string representation of the <code>Object</code> argument.
  2844      *
  2845      * @param   obj   an <code>Object</code>.
  2846      * @return  if the argument is <code>null</code>, then a string equal to
  2847      *          <code>"null"</code>; otherwise, the value of
  2848      *          <code>obj.toString()</code> is returned.
  2849      * @see     java.lang.Object#toString()
  2850      */
  2851     public static String valueOf(Object obj) {
  2852         return (obj == null) ? "null" : obj.toString();
  2853     }
  2854 
  2855     /**
  2856      * Returns the string representation of the <code>char</code> array
  2857      * argument. The contents of the character array are copied; subsequent
  2858      * modification of the character array does not affect the newly
  2859      * created string.
  2860      *
  2861      * @param   data   a <code>char</code> array.
  2862      * @return  a newly allocated string representing the same sequence of
  2863      *          characters contained in the character array argument.
  2864      */
  2865     public static String valueOf(char data[]) {
  2866         return new String(data);
  2867     }
  2868 
  2869     /**
  2870      * Returns the string representation of a specific subarray of the
  2871      * <code>char</code> array argument.
  2872      * <p>
  2873      * The <code>offset</code> argument is the index of the first
  2874      * character of the subarray. The <code>count</code> argument
  2875      * specifies the length of the subarray. The contents of the subarray
  2876      * are copied; subsequent modification of the character array does not
  2877      * affect the newly created string.
  2878      *
  2879      * @param   data     the character array.
  2880      * @param   offset   the initial offset into the value of the
  2881      *                  <code>String</code>.
  2882      * @param   count    the length of the value of the <code>String</code>.
  2883      * @return  a string representing the sequence of characters contained
  2884      *          in the subarray of the character array argument.
  2885      * @exception IndexOutOfBoundsException if <code>offset</code> is
  2886      *          negative, or <code>count</code> is negative, or
  2887      *          <code>offset+count</code> is larger than
  2888      *          <code>data.length</code>.
  2889      */
  2890     public static String valueOf(char data[], int offset, int count) {
  2891         return new String(data, offset, count);
  2892     }
  2893 
  2894     /**
  2895      * Returns a String that represents the character sequence in the
  2896      * array specified.
  2897      *
  2898      * @param   data     the character array.
  2899      * @param   offset   initial offset of the subarray.
  2900      * @param   count    length of the subarray.
  2901      * @return  a <code>String</code> that contains the characters of the
  2902      *          specified subarray of the character array.
  2903      */
  2904     public static String copyValueOf(char data[], int offset, int count) {
  2905         // All public String constructors now copy the data.
  2906         return new String(data, offset, count);
  2907     }
  2908 
  2909     /**
  2910      * Returns a String that represents the character sequence in the
  2911      * array specified.
  2912      *
  2913      * @param   data   the character array.
  2914      * @return  a <code>String</code> that contains the characters of the
  2915      *          character array.
  2916      */
  2917     public static String copyValueOf(char data[]) {
  2918         return copyValueOf(data, 0, data.length);
  2919     }
  2920 
  2921     /**
  2922      * Returns the string representation of the <code>boolean</code> argument.
  2923      *
  2924      * @param   b   a <code>boolean</code>.
  2925      * @return  if the argument is <code>true</code>, a string equal to
  2926      *          <code>"true"</code> is returned; otherwise, a string equal to
  2927      *          <code>"false"</code> is returned.
  2928      */
  2929     public static String valueOf(boolean b) {
  2930         return b ? "true" : "false";
  2931     }
  2932 
  2933     /**
  2934      * Returns the string representation of the <code>char</code>
  2935      * argument.
  2936      *
  2937      * @param   c   a <code>char</code>.
  2938      * @return  a string of length <code>1</code> containing
  2939      *          as its single character the argument <code>c</code>.
  2940      */
  2941     public static String valueOf(char c) {
  2942         char data[] = {c};
  2943         return new String(0, 1, data);
  2944     }
  2945 
  2946     /**
  2947      * Returns the string representation of the <code>int</code> argument.
  2948      * <p>
  2949      * The representation is exactly the one returned by the
  2950      * <code>Integer.toString</code> method of one argument.
  2951      *
  2952      * @param   i   an <code>int</code>.
  2953      * @return  a string representation of the <code>int</code> argument.
  2954      * @see     java.lang.Integer#toString(int, int)
  2955      */
  2956     public static String valueOf(int i) {
  2957         return Integer.toString(i);
  2958     }
  2959 
  2960     /**
  2961      * Returns the string representation of the <code>long</code> argument.
  2962      * <p>
  2963      * The representation is exactly the one returned by the
  2964      * <code>Long.toString</code> method of one argument.
  2965      *
  2966      * @param   l   a <code>long</code>.
  2967      * @return  a string representation of the <code>long</code> argument.
  2968      * @see     java.lang.Long#toString(long)
  2969      */
  2970     public static String valueOf(long l) {
  2971         return Long.toString(l);
  2972     }
  2973 
  2974     /**
  2975      * Returns the string representation of the <code>float</code> argument.
  2976      * <p>
  2977      * The representation is exactly the one returned by the
  2978      * <code>Float.toString</code> method of one argument.
  2979      *
  2980      * @param   f   a <code>float</code>.
  2981      * @return  a string representation of the <code>float</code> argument.
  2982      * @see     java.lang.Float#toString(float)
  2983      */
  2984     public static String valueOf(float f) {
  2985         return Float.toString(f);
  2986     }
  2987 
  2988     /**
  2989      * Returns the string representation of the <code>double</code> argument.
  2990      * <p>
  2991      * The representation is exactly the one returned by the
  2992      * <code>Double.toString</code> method of one argument.
  2993      *
  2994      * @param   d   a <code>double</code>.
  2995      * @return  a  string representation of the <code>double</code> argument.
  2996      * @see     java.lang.Double#toString(double)
  2997      */
  2998     public static String valueOf(double d) {
  2999         return Double.toString(d);
  3000     }
  3001 
  3002     /**
  3003      * Returns a canonical representation for the string object.
  3004      * <p>
  3005      * A pool of strings, initially empty, is maintained privately by the
  3006      * class <code>String</code>.
  3007      * <p>
  3008      * When the intern method is invoked, if the pool already contains a
  3009      * string equal to this <code>String</code> object as determined by
  3010      * the {@link #equals(Object)} method, then the string from the pool is
  3011      * returned. Otherwise, this <code>String</code> object is added to the
  3012      * pool and a reference to this <code>String</code> object is returned.
  3013      * <p>
  3014      * It follows that for any two strings <code>s</code> and <code>t</code>,
  3015      * <code>s.intern()&nbsp;==&nbsp;t.intern()</code> is <code>true</code>
  3016      * if and only if <code>s.equals(t)</code> is <code>true</code>.
  3017      * <p>
  3018      * All literal strings and string-valued constant expressions are
  3019      * interned. String literals are defined in section 3.10.5 of the
  3020      * <cite>The Java&trade; Language Specification</cite>.
  3021      *
  3022      * @return  a string that has the same contents as this string, but is
  3023      *          guaranteed to be from a pool of unique strings.
  3024      */
  3025     public native String intern();
  3026 
  3027     static char[] copyOfRange(char[] original, int from, int to) {
  3028         int newLength = to - from;
  3029         if (newLength < 0) {
  3030             throw new IllegalArgumentException(from + " > " + to);
  3031         }
  3032         char[] copy = new char[newLength];
  3033         System.arraycopy(original, from, copy, 0,
  3034             Math.min(original.length - from, newLength));
  3035         return copy;
  3036     }
  3037     static char[] copyOf(char[] original, int newLength) {
  3038         char[] copy = new char[newLength];
  3039         System.arraycopy(original, 0, copy, 0,
  3040             Math.min(original.length, newLength));
  3041         return copy;
  3042     }
  3043 
  3044 }