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