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