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