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