jdk/src/share/classes/java/lang/AbstractStringBuilder.java
author bchristi
Wed, 26 Feb 2014 13:02:05 -0800
changeset 23024 7d5ecb31115f
parent 21334 c60dfce46a77
permissions -rw-r--r--
8027640: String.indexOf(String,int) for the empty string case not specified Summary: Spec & wording clean up of indexOf(), lastIndexOf() Reviewed-by: psandoz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 16742
diff changeset
     2
 * Copyright (c) 2003, 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: 5466
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: 5466
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: 5466
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5466
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5466
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 sun.misc.FloatingDecimal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * A mutable sequence of characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Implements a modifiable string. At any point in time it contains some
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * particular sequence of characters, but the length and content of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * sequence can be changed through certain method calls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
15312
4b57f9da8192 4247235: (spec str) StringBuffer.insert(int, char[]) specification is inconsistent
jgish
parents: 14997
diff changeset
    38
 * <p>Unless otherwise noted, passing a {@code null} argument to a constructor
4b57f9da8192 4247235: (spec str) StringBuffer.insert(int, char[]) specification is inconsistent
jgish
parents: 14997
diff changeset
    39
 * or method in this class will cause a {@link NullPointerException} to be
4b57f9da8192 4247235: (spec str) StringBuffer.insert(int, char[]) specification is inconsistent
jgish
parents: 14997
diff changeset
    40
 * thrown.
4b57f9da8192 4247235: (spec str) StringBuffer.insert(int, char[]) specification is inconsistent
jgish
parents: 14997
diff changeset
    41
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author      Michael McCloskey
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
    43
 * @author      Martin Buchholz
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
    44
 * @author      Ulf Zibis
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @since       1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
abstract class AbstractStringBuilder implements Appendable, CharSequence {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * The value is used for character storage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
    51
    char[] value;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * The count is the number of characters used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * This no-arg constructor is necessary for serialization of subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    AbstractStringBuilder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Creates an AbstractStringBuilder of the specified capacity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    AbstractStringBuilder(int capacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        value = new char[capacity];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Returns the length (character count).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @return  the length of the sequence of characters currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *          represented by this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
14332
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
    77
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public int length() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Returns the current capacity. The capacity is the amount of storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * available for newly inserted characters, beyond which an allocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * will occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @return  the current capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public int capacity() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        return value.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Ensures that the capacity is at least equal to the specified minimum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * If the current capacity is less than the argument, then a new internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * array is allocated with greater capacity. The new capacity is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * larger of:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * <ul>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
    99
     * <li>The {@code minimumCapacity} argument.
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   100
     * <li>Twice the old capacity, plus {@code 2}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * </ul>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   102
     * If the {@code minimumCapacity} argument is nonpositive, this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * method takes no action and simply returns.
13821
02266c2bf3a8 4722265: (spec str) StringBuffer.ensureCapacity() should mention that capacity is mutable
jgish
parents: 11676
diff changeset
   104
     * Note that subsequent operations on this object can reduce the
02266c2bf3a8 4722265: (spec str) StringBuffer.ensureCapacity() should mention that capacity is mutable
jgish
parents: 11676
diff changeset
   105
     * actual capacity below that requested here.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @param   minimumCapacity   the minimum desired capacity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public void ensureCapacity(int minimumCapacity) {
7020
25638687fe82 6992121: StringBuilder.ensureCapacity(int minCap) throws OutOfMemoryError with minCap=Integer.MIN_VALUE
mchung
parents: 6295
diff changeset
   110
        if (minimumCapacity > 0)
25638687fe82 6992121: StringBuilder.ensureCapacity(int minCap) throws OutOfMemoryError with minCap=Integer.MIN_VALUE
mchung
parents: 6295
diff changeset
   111
            ensureCapacityInternal(minimumCapacity);
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   112
    }
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   113
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   114
    /**
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   115
     * This method has the same contract as ensureCapacity, but is
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   116
     * never synchronized.
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   117
     */
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   118
    private void ensureCapacityInternal(int minimumCapacity) {
7020
25638687fe82 6992121: StringBuilder.ensureCapacity(int minCap) throws OutOfMemoryError with minCap=Integer.MIN_VALUE
mchung
parents: 6295
diff changeset
   119
        // overflow-conscious code
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   120
        if (minimumCapacity - value.length > 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            expandCapacity(minimumCapacity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * This implements the expansion semantics of ensureCapacity with no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * size check or synchronization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    void expandCapacity(int minimumCapacity) {
5603
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents: 5466
diff changeset
   129
        int newCapacity = value.length * 2 + 2;
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   130
        if (newCapacity - minimumCapacity < 0)
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   131
            newCapacity = minimumCapacity;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (newCapacity < 0) {
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   133
            if (minimumCapacity < 0) // overflow
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   134
                throw new OutOfMemoryError();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            newCapacity = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        value = Arrays.copyOf(value, newCapacity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Attempts to reduce storage used for the character sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * If the buffer is larger than necessary to hold its current sequence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * characters, then it may be resized to become more space efficient.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Calling this method may, but is not required to, affect the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * returned by a subsequent call to the {@link #capacity()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public void trimToSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (count < value.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            value = Arrays.copyOf(value, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Sets the length of the character sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * The sequence is changed to a new character sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * whose length is specified by the argument. For every nonnegative
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   157
     * index <i>k</i> less than {@code newLength}, the character at
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * index <i>k</i> in the new character sequence is the same as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * character at index <i>k</i> in the old sequence if <i>k</i> is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * than the length of the old character sequence; otherwise, it is the
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   161
     * null character {@code '\u005Cu0000'}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   163
     * In other words, if the {@code newLength} argument is less than
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * the current length, the length is changed to the specified length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * <p>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   166
     * If the {@code newLength} argument is greater than or equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * to the current length, sufficient null characters
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   168
     * ({@code '\u005Cu0000'}) are appended so that
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   169
     * length becomes the {@code newLength} argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * <p>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   171
     * The {@code newLength} argument must be greater than or equal
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   172
     * to {@code 0}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @param      newLength   the new length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @throws     IndexOutOfBoundsException  if the
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   176
     *               {@code newLength} argument is negative.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public void setLength(int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            throw new StringIndexOutOfBoundsException(newLength);
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   181
        ensureCapacityInternal(newLength);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (count < newLength) {
14686
fb59583d33b2 6553074: String{Buffer,Builder}.indexOf(Str, int) contains unnecessary allocation
mduigou
parents: 14332
diff changeset
   184
            Arrays.fill(value, count, newLength, '\0');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
14686
fb59583d33b2 6553074: String{Buffer,Builder}.indexOf(Str, int) contains unnecessary allocation
mduigou
parents: 14332
diff changeset
   186
fb59583d33b2 6553074: String{Buffer,Builder}.indexOf(Str, int) contains unnecessary allocation
mduigou
parents: 14332
diff changeset
   187
        count = newLength;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    /**
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   191
     * Returns the {@code char} value in this sequence at the specified index.
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   192
     * The first {@code char} value is at index {@code 0}, the next at index
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   193
     * {@code 1}, and so on, as in array indexing.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * The index argument must be greater than or equal to
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   196
     * {@code 0}, and less than the length of this sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   198
     * <p>If the {@code char} value specified by the index is a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * <a href="Character.html#unicode">surrogate</a>, the surrogate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * value is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   202
     * @param      index   the index of the desired {@code char} value.
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   203
     * @return     the {@code char} value at the specified index.
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   204
     * @throws     IndexOutOfBoundsException  if {@code index} is
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   205
     *             negative or greater than or equal to {@code length()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     */
14332
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
   207
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public char charAt(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if ((index < 0) || (index >= count))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            throw new StringIndexOutOfBoundsException(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return value[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * Returns the character (Unicode code point) at the specified
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   216
     * index. The index refers to {@code char} values
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   217
     * (Unicode code units) and ranges from {@code 0} to
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   218
     * {@link #length()}{@code  - 1}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   220
     * <p> If the {@code char} value specified at the given index
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * is in the high-surrogate range, the following index is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * than the length of this sequence, and the
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   223
     * {@code char} value at the following index is in the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * low-surrogate range, then the supplementary code point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * corresponding to this surrogate pair is returned. Otherwise,
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   226
     * the {@code char} value at the given index is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   228
     * @param      index the index to the {@code char} values
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @return     the code point value of the character at the
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   230
     *             {@code index}
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   231
     * @exception  IndexOutOfBoundsException  if the {@code index}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *             argument is negative or not less than the length of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *             sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public int codePointAt(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        if ((index < 0) || (index >= count)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            throw new StringIndexOutOfBoundsException(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
16714
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 15312
diff changeset
   239
        return Character.codePointAtImpl(value, index, count);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Returns the character (Unicode code point) before the specified
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   244
     * index. The index refers to {@code char} values
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   245
     * (Unicode code units) and ranges from {@code 1} to {@link
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * #length()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   248
     * <p> If the {@code char} value at {@code (index - 1)}
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   249
     * is in the low-surrogate range, {@code (index - 2)} is not
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   250
     * negative, and the {@code char} value at {@code (index -
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   251
     * 2)} is in the high-surrogate range, then the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * supplementary code point value of the surrogate pair is
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   253
     * returned. If the {@code char} value at {@code index -
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   254
     * 1} is an unpaired low-surrogate or a high-surrogate, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * surrogate value is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @param     index the index following the code point that should be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @return    the Unicode code point value before the given index.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   259
     * @exception IndexOutOfBoundsException if the {@code index}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *            argument is less than 1 or greater than the length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *            of this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    public int codePointBefore(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        int i = index - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        if ((i < 0) || (i >= count)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            throw new StringIndexOutOfBoundsException(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
16714
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 15312
diff changeset
   268
        return Character.codePointBeforeImpl(value, index, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * Returns the number of Unicode code points in the specified text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * range of this sequence. The text range begins at the specified
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   274
     * {@code beginIndex} and extends to the {@code char} at
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   275
     * index {@code endIndex - 1}. Thus the length (in
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   276
     * {@code char}s) of the text range is
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   277
     * {@code endIndex-beginIndex}. Unpaired surrogates within
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * this sequence count as one code point each.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   280
     * @param beginIndex the index to the first {@code char} of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * the text range.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   282
     * @param endIndex the index after the last {@code char} of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * the text range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @return the number of Unicode code points in the specified text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @exception IndexOutOfBoundsException if the
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   287
     * {@code beginIndex} is negative, or {@code endIndex}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * is larger than the length of this sequence, or
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   289
     * {@code beginIndex} is larger than {@code endIndex}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public int codePointCount(int beginIndex, int endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (beginIndex < 0 || endIndex > count || beginIndex > endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        return Character.codePointCountImpl(value, beginIndex, endIndex-beginIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * Returns the index within this sequence that is offset from the
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   300
     * given {@code index} by {@code codePointOffset} code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * points. Unpaired surrogates within the text range given by
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   302
     * {@code index} and {@code codePointOffset} count as
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * one code point each.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @param index the index to be offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @param codePointOffset the offset in code points
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @return the index within this sequence
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   308
     * @exception IndexOutOfBoundsException if {@code index}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *   is negative or larger then the length of this sequence,
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   310
     *   or if {@code codePointOffset} is positive and the subsequence
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   311
     *   starting with {@code index} has fewer than
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   312
     *   {@code codePointOffset} code points,
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   313
     *   or if {@code codePointOffset} is negative and the subsequence
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   314
     *   before {@code index} has fewer than the absolute value of
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   315
     *   {@code codePointOffset} code points.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    public int offsetByCodePoints(int index, int codePointOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (index < 0 || index > count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        return Character.offsetByCodePointsImpl(value, 0, count,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                                                index, codePointOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * Characters are copied from this sequence into the
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   327
     * destination character array {@code dst}. The first character to
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   328
     * be copied is at index {@code srcBegin}; the last character to
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   329
     * be copied is at index {@code srcEnd-1}. The total number of
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   330
     * characters to be copied is {@code srcEnd-srcBegin}. The
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   331
     * characters are copied into the subarray of {@code dst} starting
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   332
     * at index {@code dstBegin} and ending at index:
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19591
diff changeset
   333
     * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * dstbegin + (srcEnd-srcBegin) - 1
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19591
diff changeset
   335
     * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @param      srcBegin   start copying at this offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @param      srcEnd     stop copying at this offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @param      dst        the array to copy the data into.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   340
     * @param      dstBegin   offset into {@code dst}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @throws     IndexOutOfBoundsException  if any of the following is true:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *             <ul>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   343
     *             <li>{@code srcBegin} is negative
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   344
     *             <li>{@code dstBegin} is negative
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   345
     *             <li>the {@code srcBegin} argument is greater than
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   346
     *             the {@code srcEnd} argument.
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   347
     *             <li>{@code srcEnd} is greater than
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   348
     *             {@code this.length()}.
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   349
     *             <li>{@code dstBegin+srcEnd-srcBegin} is greater than
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   350
     *             {@code dst.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *             </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     */
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   353
    public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        if (srcBegin < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            throw new StringIndexOutOfBoundsException(srcBegin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        if ((srcEnd < 0) || (srcEnd > count))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            throw new StringIndexOutOfBoundsException(srcEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        if (srcBegin > srcEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            throw new StringIndexOutOfBoundsException("srcBegin > srcEnd");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    /**
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   365
     * The character at the specified index is set to {@code ch}. This
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * sequence is altered to represent a new character sequence that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * identical to the old character sequence, except that it contains the
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   368
     * character {@code ch} at position {@code index}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * The index argument must be greater than or equal to
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   371
     * {@code 0}, and less than the length of this sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @param      index   the index of the character to modify.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @param      ch      the new character.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   375
     * @throws     IndexOutOfBoundsException  if {@code index} is
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   376
     *             negative or greater than or equal to {@code length()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    public void setCharAt(int index, char ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        if ((index < 0) || (index >= count))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            throw new StringIndexOutOfBoundsException(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        value[index] = ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   385
     * Appends the string representation of the {@code Object} argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   387
     * The overall effect is exactly as if the argument were converted
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   388
     * to a string by the method {@link String#valueOf(Object)},
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   389
     * and the characters of that string were then
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   390
     * {@link #append(String) appended} to this character sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   392
     * @param   obj   an {@code Object}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @return  a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    public AbstractStringBuilder append(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        return append(String.valueOf(obj));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * Appends the specified string to this character sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   402
     * The characters of the {@code String} argument are appended, in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * order, increasing the length of this sequence by the length of the
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   404
     * argument. If {@code str} is {@code null}, then the four
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   405
     * characters {@code "null"} are appended.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * Let <i>n</i> be the length of this character sequence just prior to
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   408
     * execution of the {@code append} method. Then the character at
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * index <i>k</i> in the new character sequence is equal to the character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * at index <i>k</i> in the old character sequence, if <i>k</i> is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * than <i>n</i>; otherwise, it is equal to the character at index
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   412
     * <i>k-n</i> in the argument {@code str}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * @param   str   a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * @return  a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    public AbstractStringBuilder append(String str) {
16742
e6b0ac6581f1 8010849: (str) Optimize StringBuilder.append(null)
martin
parents: 16714
diff changeset
   418
        if (str == null)
e6b0ac6581f1 8010849: (str) Optimize StringBuilder.append(null)
martin
parents: 16714
diff changeset
   419
            return appendNull();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        int len = str.length();
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   421
        ensureCapacityInternal(count + len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        str.getChars(0, len, value, count);
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   423
        count += len;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    // Documentation in subclasses because of synchro difference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    public AbstractStringBuilder append(StringBuffer sb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        if (sb == null)
16742
e6b0ac6581f1 8010849: (str) Optimize StringBuilder.append(null)
martin
parents: 16714
diff changeset
   430
            return appendNull();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        int len = sb.length();
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   432
        ensureCapacityInternal(count + len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        sb.getChars(0, len, value, count);
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   434
        count += len;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
14332
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
   438
    /**
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
   439
     * @since 1.8
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
   440
     */
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
   441
    AbstractStringBuilder append(AbstractStringBuilder asb) {
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
   442
        if (asb == null)
16742
e6b0ac6581f1 8010849: (str) Optimize StringBuilder.append(null)
martin
parents: 16714
diff changeset
   443
            return appendNull();
14332
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
   444
        int len = asb.length();
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
   445
        ensureCapacityInternal(count + len);
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
   446
        asb.getChars(0, len, value, count);
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
   447
        count += len;
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
   448
        return this;
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
   449
    }
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
   450
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    // Documentation in subclasses because of synchro difference
14332
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
   452
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    public AbstractStringBuilder append(CharSequence s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        if (s == null)
16742
e6b0ac6581f1 8010849: (str) Optimize StringBuilder.append(null)
martin
parents: 16714
diff changeset
   455
            return appendNull();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        if (s instanceof String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            return this.append((String)s);
14332
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
   458
        if (s instanceof AbstractStringBuilder)
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
   459
            return this.append((AbstractStringBuilder)s);
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
   460
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        return this.append(s, 0, s.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
16742
e6b0ac6581f1 8010849: (str) Optimize StringBuilder.append(null)
martin
parents: 16714
diff changeset
   464
    private AbstractStringBuilder appendNull() {
e6b0ac6581f1 8010849: (str) Optimize StringBuilder.append(null)
martin
parents: 16714
diff changeset
   465
        int c = count;
e6b0ac6581f1 8010849: (str) Optimize StringBuilder.append(null)
martin
parents: 16714
diff changeset
   466
        ensureCapacityInternal(c + 4);
e6b0ac6581f1 8010849: (str) Optimize StringBuilder.append(null)
martin
parents: 16714
diff changeset
   467
        final char[] value = this.value;
e6b0ac6581f1 8010849: (str) Optimize StringBuilder.append(null)
martin
parents: 16714
diff changeset
   468
        value[c++] = 'n';
e6b0ac6581f1 8010849: (str) Optimize StringBuilder.append(null)
martin
parents: 16714
diff changeset
   469
        value[c++] = 'u';
e6b0ac6581f1 8010849: (str) Optimize StringBuilder.append(null)
martin
parents: 16714
diff changeset
   470
        value[c++] = 'l';
e6b0ac6581f1 8010849: (str) Optimize StringBuilder.append(null)
martin
parents: 16714
diff changeset
   471
        value[c++] = 'l';
e6b0ac6581f1 8010849: (str) Optimize StringBuilder.append(null)
martin
parents: 16714
diff changeset
   472
        count = c;
e6b0ac6581f1 8010849: (str) Optimize StringBuilder.append(null)
martin
parents: 16714
diff changeset
   473
        return this;
e6b0ac6581f1 8010849: (str) Optimize StringBuilder.append(null)
martin
parents: 16714
diff changeset
   474
    }
e6b0ac6581f1 8010849: (str) Optimize StringBuilder.append(null)
martin
parents: 16714
diff changeset
   475
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   477
     * Appends a subsequence of the specified {@code CharSequence} to this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   480
     * Characters of the argument {@code s}, starting at
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   481
     * index {@code start}, are appended, in order, to the contents of
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   482
     * this sequence up to the (exclusive) index {@code end}. The length
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   483
     * of this sequence is increased by the value of {@code end - start}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * Let <i>n</i> be the length of this character sequence just prior to
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   486
     * execution of the {@code append} method. Then the character at
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * index <i>k</i> in this character sequence becomes equal to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * character at index <i>k</i> in this sequence, if <i>k</i> is less than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * <i>n</i>; otherwise, it is equal to the character at index
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   490
     * <i>k+start-n</i> in the argument {@code s}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   492
     * If {@code s} is {@code null}, then this method appends
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * characters as if the s parameter was a sequence containing the four
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   494
     * characters {@code "null"}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * @param   s the sequence to append.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * @param   start   the starting index of the subsequence to be appended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * @param   end     the end index of the subsequence to be appended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * @return  a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * @throws     IndexOutOfBoundsException if
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   501
     *             {@code start} is negative, or
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   502
     *             {@code start} is greater than {@code end} or
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   503
     *             {@code end} is greater than {@code s.length()}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     */
14332
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
   505
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    public AbstractStringBuilder append(CharSequence s, int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        if (s == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            s = "null";
6295
91f1c55cf47e 6955504: (str) String[Builder/Buffer].append(char[],int,int) throws OutOfMemoryError in b94
martin
parents: 5986
diff changeset
   509
        if ((start < 0) || (start > end) || (end > s.length()))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            throw new IndexOutOfBoundsException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                "start " + start + ", end " + end + ", s.length() "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                + s.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        int len = end - start;
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   514
        ensureCapacityInternal(count + len);
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   515
        for (int i = start, j = count; i < end; i++, j++)
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   516
            value[j] = s.charAt(i);
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   517
        count += len;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   522
     * Appends the string representation of the {@code char} array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * argument to this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * The characters of the array argument are appended, in order, to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * the contents of this sequence. The length of this sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * increases by the length of the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   529
     * The overall effect is exactly as if the argument were converted
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   530
     * to a string by the method {@link String#valueOf(char[])},
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   531
     * and the characters of that string were then
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   532
     * {@link #append(String) appended} to this character sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * @param   str   the characters to be appended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * @return  a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     */
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   537
    public AbstractStringBuilder append(char[] str) {
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   538
        int len = str.length;
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   539
        ensureCapacityInternal(count + len);
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   540
        System.arraycopy(str, 0, value, count, len);
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   541
        count += len;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * Appends the string representation of a subarray of the
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   547
     * {@code char} array argument to this sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   549
     * Characters of the {@code char} array {@code str}, starting at
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   550
     * index {@code offset}, are appended, in order, to the contents
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * of this sequence. The length of this sequence increases
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   552
     * by the value of {@code len}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   554
     * The overall effect is exactly as if the arguments were converted
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   555
     * to a string by the method {@link String#valueOf(char[],int,int)},
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   556
     * and the characters of that string were then
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   557
     * {@link #append(String) appended} to this character sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * @param   str      the characters to be appended.
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   560
     * @param   offset   the index of the first {@code char} to append.
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   561
     * @param   len      the number of {@code char}s to append.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * @return  a reference to this object.
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   563
     * @throws IndexOutOfBoundsException
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   564
     *         if {@code offset < 0} or {@code len < 0}
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   565
     *         or {@code offset+len > str.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    public AbstractStringBuilder append(char str[], int offset, int len) {
6295
91f1c55cf47e 6955504: (str) String[Builder/Buffer].append(char[],int,int) throws OutOfMemoryError in b94
martin
parents: 5986
diff changeset
   568
        if (len > 0)                // let arraycopy report AIOOBE for len < 0
91f1c55cf47e 6955504: (str) String[Builder/Buffer].append(char[],int,int) throws OutOfMemoryError in b94
martin
parents: 5986
diff changeset
   569
            ensureCapacityInternal(count + len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        System.arraycopy(str, offset, value, count, len);
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   571
        count += len;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   576
     * Appends the string representation of the {@code boolean}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * argument to the sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   579
     * The overall effect is exactly as if the argument were converted
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   580
     * to a string by the method {@link String#valueOf(boolean)},
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   581
     * and the characters of that string were then
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   582
     * {@link #append(String) appended} to this character sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     *
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   584
     * @param   b   a {@code boolean}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * @return  a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    public AbstractStringBuilder append(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        if (b) {
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   589
            ensureCapacityInternal(count + 4);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            value[count++] = 't';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            value[count++] = 'r';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            value[count++] = 'u';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            value[count++] = 'e';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        } else {
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   595
            ensureCapacityInternal(count + 5);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            value[count++] = 'f';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            value[count++] = 'a';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            value[count++] = 'l';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            value[count++] = 's';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            value[count++] = 'e';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   606
     * Appends the string representation of the {@code char}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * argument to this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * The argument is appended to the contents of this sequence.
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   610
     * The length of this sequence increases by {@code 1}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   612
     * The overall effect is exactly as if the argument were converted
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   613
     * to a string by the method {@link String#valueOf(char)},
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   614
     * and the character in that string were then
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   615
     * {@link #append(String) appended} to this character sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     *
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   617
     * @param   c   a {@code char}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * @return  a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     */
14332
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
   620
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    public AbstractStringBuilder append(char c) {
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   622
        ensureCapacityInternal(count + 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        value[count++] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   628
     * Appends the string representation of the {@code int}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * argument to this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   631
     * The overall effect is exactly as if the argument were converted
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   632
     * to a string by the method {@link String#valueOf(int)},
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   633
     * and the characters of that string were then
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   634
     * {@link #append(String) appended} to this character sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     *
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   636
     * @param   i   an {@code int}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * @return  a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    public AbstractStringBuilder append(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        if (i == Integer.MIN_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            append("-2147483648");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        int appendedLength = (i < 0) ? Integer.stringSize(-i) + 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                                     : Integer.stringSize(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        int spaceNeeded = count + appendedLength;
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   647
        ensureCapacityInternal(spaceNeeded);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        Integer.getChars(i, spaceNeeded, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        count = spaceNeeded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   654
     * Appends the string representation of the {@code long}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * argument to this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   657
     * The overall effect is exactly as if the argument were converted
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   658
     * to a string by the method {@link String#valueOf(long)},
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   659
     * and the characters of that string were then
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   660
     * {@link #append(String) appended} to this character sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     *
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   662
     * @param   l   a {@code long}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * @return  a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    public AbstractStringBuilder append(long l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        if (l == Long.MIN_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            append("-9223372036854775808");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        int appendedLength = (l < 0) ? Long.stringSize(-l) + 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                                     : Long.stringSize(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        int spaceNeeded = count + appendedLength;
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   673
        ensureCapacityInternal(spaceNeeded);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        Long.getChars(l, spaceNeeded, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        count = spaceNeeded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   680
     * Appends the string representation of the {@code float}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * argument to this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   683
     * The overall effect is exactly as if the argument were converted
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   684
     * to a string by the method {@link String#valueOf(float)},
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   685
     * and the characters of that string were then
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   686
     * {@link #append(String) appended} to this character sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     *
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   688
     * @param   f   a {@code float}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * @return  a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    public AbstractStringBuilder append(float f) {
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 16742
diff changeset
   692
        FloatingDecimal.appendTo(f,this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   697
     * Appends the string representation of the {@code double}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * argument to this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   700
     * The overall effect is exactly as if the argument were converted
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   701
     * to a string by the method {@link String#valueOf(double)},
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   702
     * and the characters of that string were then
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   703
     * {@link #append(String) appended} to this character sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     *
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   705
     * @param   d   a {@code double}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * @return  a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    public AbstractStringBuilder append(double d) {
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 16742
diff changeset
   709
        FloatingDecimal.appendTo(d,this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * Removes the characters in a substring of this sequence.
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   715
     * The substring begins at the specified {@code start} and extends to
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   716
     * the character at index {@code end - 1} or to the end of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * sequence if no such character exists. If
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   718
     * {@code start} is equal to {@code end}, no changes are made.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * @param      start  The beginning index, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * @param      end    The ending index, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * @return     This object.
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   723
     * @throws     StringIndexOutOfBoundsException  if {@code start}
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   724
     *             is negative, greater than {@code length()}, or
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   725
     *             greater than {@code end}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    public AbstractStringBuilder delete(int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        if (start < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            throw new StringIndexOutOfBoundsException(start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        if (end > count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            end = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        if (start > end)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            throw new StringIndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        int len = end - start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        if (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            System.arraycopy(value, start+len, value, start, count-end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            count -= len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   743
     * Appends the string representation of the {@code codePoint}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * argument to this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * <p> The argument is appended to the contents of this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * The length of this sequence increases by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * {@link Character#charCount(int) Character.charCount(codePoint)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * <p> The overall effect is exactly as if the argument were
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   751
     * converted to a {@code char} array by the method
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   752
     * {@link Character#toChars(int)} and the character in that array
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   753
     * were then {@link #append(char[]) appended} to this character
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * @param   codePoint   a Unicode code point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * @return  a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * @exception IllegalArgumentException if the specified
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   759
     * {@code codePoint} isn't a valid Unicode code point
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    public AbstractStringBuilder appendCodePoint(int codePoint) {
5986
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5627
diff changeset
   762
        final int count = this.count;
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5627
diff changeset
   763
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5627
diff changeset
   764
        if (Character.isBmpCodePoint(codePoint)) {
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5627
diff changeset
   765
            ensureCapacityInternal(count + 1);
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5627
diff changeset
   766
            value[count] = (char) codePoint;
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5627
diff changeset
   767
            this.count = count + 1;
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5627
diff changeset
   768
        } else if (Character.isValidCodePoint(codePoint)) {
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5627
diff changeset
   769
            ensureCapacityInternal(count + 2);
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5627
diff changeset
   770
            Character.toSurrogates(codePoint, value, count);
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5627
diff changeset
   771
            this.count = count + 2;
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5627
diff changeset
   772
        } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    /**
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   779
     * Removes the {@code char} at the specified position in this
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   780
     * sequence. This sequence is shortened by one {@code char}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * <p>Note: If the character at the given index is a supplementary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * character, this method does not remove the entire character. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * correct handling of supplementary characters is required,
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   785
     * determine the number of {@code char}s to remove by calling
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   786
     * {@code Character.charCount(thisSequence.codePointAt(index))},
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   787
     * where {@code thisSequence} is this sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   789
     * @param       index  Index of {@code char} to remove
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * @return      This object.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   791
     * @throws      StringIndexOutOfBoundsException  if the {@code index}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     *              is negative or greater than or equal to
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   793
     *              {@code length()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    public AbstractStringBuilder deleteCharAt(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        if ((index < 0) || (index >= count))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            throw new StringIndexOutOfBoundsException(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        System.arraycopy(value, index+1, value, index, count-index-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        count--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * Replaces the characters in a substring of this sequence
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   805
     * with characters in the specified {@code String}. The substring
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   806
     * begins at the specified {@code start} and extends to the character
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   807
     * at index {@code end - 1} or to the end of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * sequence if no such character exists. First the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * characters in the substring are removed and then the specified
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   810
     * {@code String} is inserted at {@code start}. (This
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * sequence will be lengthened to accommodate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * specified String if necessary.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     * @param      start    The beginning index, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * @param      end      The ending index, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * @param      str   String that will replace previous contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * @return     This object.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   818
     * @throws     StringIndexOutOfBoundsException  if {@code start}
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   819
     *             is negative, greater than {@code length()}, or
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   820
     *             greater than {@code end}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    public AbstractStringBuilder replace(int start, int end, String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        if (start < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            throw new StringIndexOutOfBoundsException(start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        if (start > count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            throw new StringIndexOutOfBoundsException("start > length()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        if (start > end)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            throw new StringIndexOutOfBoundsException("start > end");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        if (end > count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            end = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        int len = str.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        int newCount = count + len - (end - start);
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   834
        ensureCapacityInternal(newCount);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        System.arraycopy(value, end, value, start + len, count - end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        str.getChars(value, start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        count = newCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    /**
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   843
     * Returns a new {@code String} that contains a subsequence of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * characters currently contained in this character sequence. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * substring begins at the specified index and extends to the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * @param      start    The beginning index, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * @return     The new string.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   850
     * @throws     StringIndexOutOfBoundsException  if {@code start} is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     *             less than zero, or greater than the length of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    public String substring(int start) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        return substring(start, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * Returns a new character sequence that is a subsequence of this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * <p> An invocation of this method of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     *
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19591
diff changeset
   862
     * <pre>{@code
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19591
diff changeset
   863
     * sb.subSequence(begin,&nbsp;end)}</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * behaves in exactly the same way as the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     *
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19591
diff changeset
   867
     * <pre>{@code
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19591
diff changeset
   868
     * sb.substring(begin,&nbsp;end)}</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * This method is provided so that this class can
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19591
diff changeset
   871
     * implement the {@link CharSequence} interface.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * @param      start   the start index, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * @param      end     the end index, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * @return     the specified subsequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * @throws  IndexOutOfBoundsException
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
   878
     *          if {@code start} or {@code end} are negative,
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
   879
     *          if {@code end} is greater than {@code length()},
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
   880
     *          or if {@code start} is greater than {@code end}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     */
14332
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
   883
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    public CharSequence subSequence(int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        return substring(start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    /**
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   889
     * Returns a new {@code String} that contains a subsequence of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * characters currently contained in this sequence. The
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   891
     * substring begins at the specified {@code start} and
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   892
     * extends to the character at index {@code end - 1}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * @param      start    The beginning index, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * @param      end      The ending index, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * @return     The new string.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   897
     * @throws     StringIndexOutOfBoundsException  if {@code start}
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   898
     *             or {@code end} are negative or greater than
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   899
     *             {@code length()}, or {@code start} is
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   900
     *             greater than {@code end}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    public String substring(int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        if (start < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            throw new StringIndexOutOfBoundsException(start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        if (end > count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            throw new StringIndexOutOfBoundsException(end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        if (start > end)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            throw new StringIndexOutOfBoundsException(end - start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        return new String(value, start, end - start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   913
     * Inserts the string representation of a subarray of the {@code str}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * array argument into this sequence. The subarray begins at the
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   915
     * specified {@code offset} and extends {@code len} {@code char}s.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * The characters of the subarray are inserted into this sequence at
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   917
     * the position indicated by {@code index}. The length of this
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   918
     * sequence increases by {@code len} {@code char}s.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * @param      index    position at which to insert subarray.
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   921
     * @param      str       A {@code char} array.
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   922
     * @param      offset   the index of the first {@code char} in subarray to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     *             be inserted.
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   924
     * @param      len      the number of {@code char}s in the subarray to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     *             be inserted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * @return     This object
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   927
     * @throws     StringIndexOutOfBoundsException  if {@code index}
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   928
     *             is negative or greater than {@code length()}, or
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   929
     *             {@code offset} or {@code len} are negative, or
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   930
     *             {@code (offset+len)} is greater than
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   931
     *             {@code str.length}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     */
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   933
    public AbstractStringBuilder insert(int index, char[] str, int offset,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                                        int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        if ((index < 0) || (index > length()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            throw new StringIndexOutOfBoundsException(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        if ((offset < 0) || (len < 0) || (offset > str.length - len))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            throw new StringIndexOutOfBoundsException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                "offset " + offset + ", len " + len + ", str.length "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                + str.length);
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   942
        ensureCapacityInternal(count + len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        System.arraycopy(value, index, value, index + len, count - index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        System.arraycopy(str, offset, value, index, len);
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   945
        count += len;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   950
     * Inserts the string representation of the {@code Object}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * argument into this character sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   953
     * The overall effect is exactly as if the second argument were
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   954
     * converted to a string by the method {@link String#valueOf(Object)},
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   955
     * and the characters of that string were then
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   956
     * {@link #insert(int,String) inserted} into this character
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   957
     * sequence at the indicated offset.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   959
     * The {@code offset} argument must be greater than or equal to
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   960
     * {@code 0}, and less than or equal to the {@linkplain #length() length}
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   961
     * of this sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * @param      offset   the offset.
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   964
     * @param      obj      an {@code Object}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * @return     a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    public AbstractStringBuilder insert(int offset, Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        return insert(offset, String.valueOf(obj));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * Inserts the string into this character sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   975
     * The characters of the {@code String} argument are inserted, in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * order, into this sequence at the indicated offset, moving up any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * characters originally above that position and increasing the length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * of this sequence by the length of the argument. If
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   979
     * {@code str} is {@code null}, then the four characters
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   980
     * {@code "null"} are inserted into this sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * The character at index <i>k</i> in the new character sequence is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * equal to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * <li>the character at index <i>k</i> in the old character sequence, if
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   986
     * <i>k</i> is less than {@code offset}
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   987
     * <li>the character at index <i>k</i>{@code -offset} in the
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   988
     * argument {@code str}, if <i>k</i> is not less than
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   989
     * {@code offset} but is less than {@code offset+str.length()}
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   990
     * <li>the character at index <i>k</i>{@code -str.length()} in the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * old character sequence, if <i>k</i> is not less than
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   992
     * {@code offset+str.length()}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * </ul><p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   994
     * The {@code offset} argument must be greater than or equal to
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   995
     * {@code 0}, and less than or equal to the {@linkplain #length() length}
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   996
     * of this sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * @param      offset   the offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * @param      str      a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * @return     a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
    public AbstractStringBuilder insert(int offset, String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        if ((offset < 0) || (offset > length()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            throw new StringIndexOutOfBoundsException(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        if (str == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            str = "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        int len = str.length();
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
  1009
        ensureCapacityInternal(count + len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        System.arraycopy(value, offset, value, offset + len, count - offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        str.getChars(value, offset);
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
  1012
        count += len;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1017
     * Inserts the string representation of the {@code char} array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * argument into this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * The characters of the array argument are inserted into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * contents of this sequence at the position indicated by
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1022
     * {@code offset}. The length of this sequence increases by
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * the length of the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1025
     * The overall effect is exactly as if the second argument were
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1026
     * converted to a string by the method {@link String#valueOf(char[])},
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1027
     * and the characters of that string were then
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1028
     * {@link #insert(int,String) inserted} into this character
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1029
     * sequence at the indicated offset.
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1030
     * <p>
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1031
     * The {@code offset} argument must be greater than or equal to
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1032
     * {@code 0}, and less than or equal to the {@linkplain #length() length}
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1033
     * of this sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     * @param      offset   the offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     * @param      str      a character array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     * @return     a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     */
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1040
    public AbstractStringBuilder insert(int offset, char[] str) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        if ((offset < 0) || (offset > length()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
            throw new StringIndexOutOfBoundsException(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        int len = str.length;
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
  1044
        ensureCapacityInternal(count + len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        System.arraycopy(value, offset, value, offset + len, count - offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        System.arraycopy(str, 0, value, offset, len);
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
  1047
        count += len;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1052
     * Inserts the specified {@code CharSequence} into this sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1054
     * The characters of the {@code CharSequence} argument are inserted,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * in order, into this sequence at the indicated offset, moving up
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * any characters originally above that position and increasing the length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * of this sequence by the length of the argument s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * The result of this method is exactly the same as if it were an
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1060
     * invocation of this object's
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1061
     * {@link #insert(int,CharSequence,int,int) insert}(dstOffset, s, 0, s.length())
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1062
     * method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     *
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1064
     * <p>If {@code s} is {@code null}, then the four characters
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1065
     * {@code "null"} are inserted into this sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * @param      dstOffset   the offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * @param      s the sequence to be inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * @return     a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * @throws     IndexOutOfBoundsException  if the offset is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
    public AbstractStringBuilder insert(int dstOffset, CharSequence s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        if (s == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            s = "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        if (s instanceof String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
            return this.insert(dstOffset, (String)s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        return this.insert(dstOffset, s, 0, s.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1081
     * Inserts a subsequence of the specified {@code CharSequence} into
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1084
     * The subsequence of the argument {@code s} specified by
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1085
     * {@code start} and {@code end} are inserted,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * in order, into this sequence at the specified destination offset, moving
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * up any characters originally above that position. The length of this
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1088
     * sequence is increased by {@code end - start}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * The character at index <i>k</i> in this sequence becomes equal to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * <li>the character at index <i>k</i> in this sequence, if
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1093
     * <i>k</i> is less than {@code dstOffset}
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1094
     * <li>the character at index <i>k</i>{@code +start-dstOffset} in
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1095
     * the argument {@code s}, if <i>k</i> is greater than or equal to
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1096
     * {@code dstOffset} but is less than {@code dstOffset+end-start}
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1097
     * <li>the character at index <i>k</i>{@code -(end-start)} in this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * sequence, if <i>k</i> is greater than or equal to
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1099
     * {@code dstOffset+end-start}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * </ul><p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1101
     * The {@code dstOffset} argument must be greater than or equal to
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1102
     * {@code 0}, and less than or equal to the {@linkplain #length() length}
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1103
     * of this sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * <p>The start argument must be nonnegative, and not greater than
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1105
     * {@code end}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     * <p>The end argument must be greater than or equal to
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1107
     * {@code start}, and less than or equal to the length of s.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     *
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1109
     * <p>If {@code s} is {@code null}, then this method inserts
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * characters as if the s parameter was a sequence containing the four
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1111
     * characters {@code "null"}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     * @param      dstOffset   the offset in this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     * @param      s       the sequence to be inserted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     * @param      start   the starting index of the subsequence to be inserted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     * @param      end     the end index of the subsequence to be inserted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     * @return     a reference to this object.
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1118
     * @throws     IndexOutOfBoundsException  if {@code dstOffset}
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1119
     *             is negative or greater than {@code this.length()}, or
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1120
     *              {@code start} or {@code end} are negative, or
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1121
     *              {@code start} is greater than {@code end} or
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1122
     *              {@code end} is greater than {@code s.length()}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     public AbstractStringBuilder insert(int dstOffset, CharSequence s,
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1125
                                         int start, int end) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        if (s == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            s = "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        if ((dstOffset < 0) || (dstOffset > this.length()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            throw new IndexOutOfBoundsException("dstOffset "+dstOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        if ((start < 0) || (end < 0) || (start > end) || (end > s.length()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
            throw new IndexOutOfBoundsException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                "start " + start + ", end " + end + ", s.length() "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                + s.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        int len = end - start;
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
  1135
        ensureCapacityInternal(count + len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        System.arraycopy(value, dstOffset, value, dstOffset + len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                         count - dstOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        for (int i=start; i<end; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
            value[dstOffset++] = s.charAt(i);
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
  1140
        count += len;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1145
     * Inserts the string representation of the {@code boolean}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * argument into this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1148
     * The overall effect is exactly as if the second argument were
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1149
     * converted to a string by the method {@link String#valueOf(boolean)},
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1150
     * and the characters of that string were then
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1151
     * {@link #insert(int,String) inserted} into this character
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1152
     * sequence at the indicated offset.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1154
     * The {@code offset} argument must be greater than or equal to
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1155
     * {@code 0}, and less than or equal to the {@linkplain #length() length}
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1156
     * of this sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * @param      offset   the offset.
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1159
     * @param      b        a {@code boolean}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * @return     a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
    public AbstractStringBuilder insert(int offset, boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        return insert(offset, String.valueOf(b));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1168
     * Inserts the string representation of the {@code char}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * argument into this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1171
     * The overall effect is exactly as if the second argument were
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1172
     * converted to a string by the method {@link String#valueOf(char)},
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1173
     * and the character in that string were then
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1174
     * {@link #insert(int,String) inserted} into this character
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1175
     * sequence at the indicated offset.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1177
     * The {@code offset} argument must be greater than or equal to
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1178
     * {@code 0}, and less than or equal to the {@linkplain #length() length}
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1179
     * of this sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     * @param      offset   the offset.
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1182
     * @param      c        a {@code char}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     * @return     a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * @throws     IndexOutOfBoundsException  if the offset is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    public AbstractStringBuilder insert(int offset, char c) {
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
  1187
        ensureCapacityInternal(count + 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        System.arraycopy(value, offset, value, offset + 1, count - offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        value[offset] = c;
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
  1190
        count += 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1195
     * Inserts the string representation of the second {@code int}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     * argument into this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1198
     * The overall effect is exactly as if the second argument were
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1199
     * converted to a string by the method {@link String#valueOf(int)},
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1200
     * and the characters of that string were then
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1201
     * {@link #insert(int,String) inserted} into this character
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1202
     * sequence at the indicated offset.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1204
     * The {@code offset} argument must be greater than or equal to
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1205
     * {@code 0}, and less than or equal to the {@linkplain #length() length}
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1206
     * of this sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     * @param      offset   the offset.
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1209
     * @param      i        an {@code int}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * @return     a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
    public AbstractStringBuilder insert(int offset, int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        return insert(offset, String.valueOf(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1218
     * Inserts the string representation of the {@code long}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * argument into this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1221
     * The overall effect is exactly as if the second argument were
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1222
     * converted to a string by the method {@link String#valueOf(long)},
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1223
     * and the characters of that string were then
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1224
     * {@link #insert(int,String) inserted} into this character
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1225
     * sequence at the indicated offset.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1227
     * The {@code offset} argument must be greater than or equal to
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1228
     * {@code 0}, and less than or equal to the {@linkplain #length() length}
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1229
     * of this sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     * @param      offset   the offset.
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1232
     * @param      l        a {@code long}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     * @return     a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
    public AbstractStringBuilder insert(int offset, long l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        return insert(offset, String.valueOf(l));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1241
     * Inserts the string representation of the {@code float}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     * argument into this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1244
     * The overall effect is exactly as if the second argument were
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1245
     * converted to a string by the method {@link String#valueOf(float)},
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1246
     * and the characters of that string were then
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1247
     * {@link #insert(int,String) inserted} into this character
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1248
     * sequence at the indicated offset.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1250
     * The {@code offset} argument must be greater than or equal to
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1251
     * {@code 0}, and less than or equal to the {@linkplain #length() length}
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1252
     * of this sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * @param      offset   the offset.
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1255
     * @param      f        a {@code float}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * @return     a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
    public AbstractStringBuilder insert(int offset, float f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        return insert(offset, String.valueOf(f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1264
     * Inserts the string representation of the {@code double}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     * argument into this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1267
     * The overall effect is exactly as if the second argument were
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1268
     * converted to a string by the method {@link String#valueOf(double)},
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1269
     * and the characters of that string were then
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1270
     * {@link #insert(int,String) inserted} into this character
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1271
     * sequence at the indicated offset.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1273
     * The {@code offset} argument must be greater than or equal to
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1274
     * {@code 0}, and less than or equal to the {@linkplain #length() length}
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1275
     * of this sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     * @param      offset   the offset.
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1278
     * @param      d        a {@code double}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     * @return     a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
    public AbstractStringBuilder insert(int offset, double d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        return insert(offset, String.valueOf(d));
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
     * Returns the index within this string of the first occurrence of the
23024
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1288
     * specified substring.
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1289
     *
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1290
     * <p>The returned index is the smallest value {@code k} for which:
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19591
diff changeset
  1291
     * <pre>{@code
23024
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1292
     * this.toString().startsWith(str, k)
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19591
diff changeset
  1293
     * }</pre>
23024
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1294
     * If no such value of {@code k} exists, then {@code -1} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     *
23024
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1296
     * @param   str   the substring to search for.
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1297
     * @return  the index of the first occurrence of the specified substring,
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1298
     *          or {@code -1} if there is no such occurrence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
    public int indexOf(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        return indexOf(str, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
     * Returns the index within this string of the first occurrence of the
23024
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1306
     * specified substring, starting at the specified index.
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1307
     *
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1308
     * <p>The returned index is the smallest value {@code k} for which:
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19591
diff changeset
  1309
     * <pre>{@code
19591
80e86f94c0e9 8016018: Typo in AbstractStringBuilder#indexOf and #lastIndexOf descriptions
igerasim
parents: 18143
diff changeset
  1310
     *     k >= Math.min(fromIndex, this.length()) &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     *                   this.toString().startsWith(str, k)
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19591
diff changeset
  1312
     * }</pre>
23024
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1313
     * If no such value of {@code k} exists, then {@code -1} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     *
23024
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1315
     * @param   str         the substring to search for.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     * @param   fromIndex   the index from which to start the search.
23024
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1317
     * @return  the index of the first occurrence of the specified substring,
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1318
     *          starting at the specified index,
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1319
     *          or {@code -1} if there is no such occurrence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
    public int indexOf(String str, int fromIndex) {
14686
fb59583d33b2 6553074: String{Buffer,Builder}.indexOf(Str, int) contains unnecessary allocation
mduigou
parents: 14332
diff changeset
  1322
        return String.indexOf(value, 0, count, str, fromIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
    /**
23024
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1326
     * Returns the index within this string of the last occurrence of the
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1327
     * specified substring.  The last occurrence of the empty string "" is
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
  1328
     * considered to occur at the index value {@code this.length()}.
23024
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1329
     *
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1330
     * <p>The returned index is the largest value {@code k} for which:
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19591
diff changeset
  1331
     * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     * this.toString().startsWith(str, k)
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19591
diff changeset
  1333
     * }</pre>
23024
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1334
     * If no such value of {@code k} exists, then {@code -1} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     * @param   str   the substring to search for.
23024
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1337
     * @return  the index of the last occurrence of the specified substring,
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1338
     *          or {@code -1} if there is no such occurrence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
    public int lastIndexOf(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        return lastIndexOf(str, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     * Returns the index within this string of the last occurrence of the
23024
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1346
     * specified substring, searching backward starting at the specified index.
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1347
     *
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1348
     * <p>The returned index is the largest value {@code k} for which:
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19591
diff changeset
  1349
     * <pre>{@code
19591
80e86f94c0e9 8016018: Typo in AbstractStringBuilder#indexOf and #lastIndexOf descriptions
igerasim
parents: 18143
diff changeset
  1350
     *     k <= Math.min(fromIndex, this.length()) &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
     *                   this.toString().startsWith(str, k)
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19591
diff changeset
  1352
     * }</pre>
23024
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1353
     * If no such value of {@code k} exists, then {@code -1} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
     * @param   str         the substring to search for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     * @param   fromIndex   the index to start the search from.
23024
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1357
     * @return  the index of the last occurrence of the specified substring,
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1358
     *          searching backward from the specified index,
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1359
     *          or {@code -1} if there is no such occurrence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
    public int lastIndexOf(String str, int fromIndex) {
14686
fb59583d33b2 6553074: String{Buffer,Builder}.indexOf(Str, int) contains unnecessary allocation
mduigou
parents: 14332
diff changeset
  1362
        return String.lastIndexOf(value, 0, count, str, fromIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
     * Causes this character sequence to be replaced by the reverse of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     * the sequence. If there are any surrogate pairs included in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     * sequence, these are treated as single characters for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     * reverse operation. Thus, the order of the high-low surrogates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     * is never reversed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     * Let <i>n</i> be the character length of this character sequence
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
  1373
     * (not the length in {@code char} values) just prior to
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
  1374
     * execution of the {@code reverse} method. Then the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     * character at index <i>k</i> in the new character sequence is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     * equal to the character at index <i>n-k-1</i> in the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
     * character sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
     * <p>Note that the reverse operation may result in producing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
     * surrogate pairs that were unpaired low-surrogates and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
     * high-surrogates before the operation. For example, reversing
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
  1382
     * "\u005CuDC00\u005CuD800" produces "\u005CuD800\u005CuDC00" which is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
     * a valid surrogate pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
     * @return  a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
    public AbstractStringBuilder reverse() {
16714
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 15312
diff changeset
  1388
        boolean hasSurrogates = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        int n = count - 1;
16714
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 15312
diff changeset
  1390
        for (int j = (n-1) >> 1; j >= 0; j--) {
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 15312
diff changeset
  1391
            int k = n - j;
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 15312
diff changeset
  1392
            char cj = value[j];
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 15312
diff changeset
  1393
            char ck = value[k];
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 15312
diff changeset
  1394
            value[j] = ck;
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 15312
diff changeset
  1395
            value[k] = cj;
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 15312
diff changeset
  1396
            if (Character.isSurrogate(cj) ||
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 15312
diff changeset
  1397
                Character.isSurrogate(ck)) {
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 15312
diff changeset
  1398
                hasSurrogates = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
            }
16714
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 15312
diff changeset
  1400
        }
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 15312
diff changeset
  1401
        if (hasSurrogates) {
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 15312
diff changeset
  1402
            reverseAllValidSurrogatePairs();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
        }
16714
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 15312
diff changeset
  1404
        return this;
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 15312
diff changeset
  1405
    }
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 15312
diff changeset
  1406
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 15312
diff changeset
  1407
    /** Outlined helper method for reverse() */
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 15312
diff changeset
  1408
    private void reverseAllValidSurrogatePairs() {
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 15312
diff changeset
  1409
        for (int i = 0; i < count - 1; i++) {
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 15312
diff changeset
  1410
            char c2 = value[i];
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 15312
diff changeset
  1411
            if (Character.isLowSurrogate(c2)) {
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 15312
diff changeset
  1412
                char c1 = value[i + 1];
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 15312
diff changeset
  1413
                if (Character.isHighSurrogate(c1)) {
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 15312
diff changeset
  1414
                    value[i++] = c1;
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 15312
diff changeset
  1415
                    value[i] = c2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     * Returns a string representing the data in this sequence.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
  1423
     * A new {@code String} object is allocated and initialized to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     * contain the character sequence currently represented by this
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
  1425
     * object. This {@code String} is then returned. Subsequent
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     * changes to this sequence do not affect the contents of the
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
  1427
     * {@code String}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
     * @return  a string representation of this sequence of characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
     */
14332
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
  1431
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
    public abstract String toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
    /**
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  1435
     * Needed by {@code String} for the contentEquals method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
    final char[] getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
}