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