jdk/src/java.base/share/classes/java/lang/AbstractStringBuilder.java
author rriggs
Tue, 16 Feb 2016 11:36:20 -0500
changeset 35978 752d505da547
parent 35302 e4d2275861c3
child 36217 cc64f9fb2062
permissions -rw-r--r--
8148775: Spec for j.l.ProcessBuilder.Redirect.DISCARD need to be improved Reviewed-by: martin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
28667
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 25859
diff changeset
     2
 * Copyright (c) 2003, 2015, 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
34781
479b1724ab80 8145990: Move sun.misc math support classes to jdk.internal.math
chegar
parents: 34518
diff changeset
    28
import jdk.internal.math.FloatingDecimal;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Arrays;
28667
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 25859
diff changeset
    30
import java.util.Spliterator;
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 25859
diff changeset
    31
import java.util.stream.IntStream;
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 25859
diff changeset
    32
import java.util.stream.StreamSupport;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
    34
import static java.lang.String.COMPACT_STRINGS;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
    35
import static java.lang.String.UTF16;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
    36
import static java.lang.String.LATIN1;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
    37
import static java.lang.String.checkIndex;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
    38
import static java.lang.String.checkOffset;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
    39
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * A mutable sequence of characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Implements a modifiable string. At any point in time it contains some
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * particular sequence of characters, but the length and content of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * sequence can be changed through certain method calls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
15312
4b57f9da8192 4247235: (spec str) StringBuffer.insert(int, char[]) specification is inconsistent
jgish
parents: 14997
diff changeset
    47
 * <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
    48
 * 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
    49
 * thrown.
4b57f9da8192 4247235: (spec str) StringBuffer.insert(int, char[]) specification is inconsistent
jgish
parents: 14997
diff changeset
    50
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author      Michael McCloskey
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
    52
 * @author      Martin Buchholz
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
    53
 * @author      Ulf Zibis
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @since       1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
abstract class AbstractStringBuilder implements Appendable, CharSequence {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * The value is used for character storage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
    60
    byte[] value;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
    61
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
    62
    /**
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
    63
     * The id of the encoding used to encode the bytes in {@code value}.
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
    64
     */
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
    65
    byte coder;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * The count is the number of characters used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * This no-arg constructor is necessary for serialization of subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    AbstractStringBuilder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Creates an AbstractStringBuilder of the specified capacity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    AbstractStringBuilder(int capacity) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
    82
        if (COMPACT_STRINGS) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
    83
            value = new byte[capacity];
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
    84
            coder = LATIN1;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
    85
        } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
    86
            value = StringUTF16.newBytesFor(capacity);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
    87
            coder = UTF16;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
    88
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Returns the length (character count).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @return  the length of the sequence of characters currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *          represented by this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
14332
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
    97
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public int length() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Returns the current capacity. The capacity is the amount of storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * available for newly inserted characters, beyond which an allocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * will occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @return  the current capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public int capacity() {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   110
        return value.length >> coder;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Ensures that the capacity is at least equal to the specified minimum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * If the current capacity is less than the argument, then a new internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * array is allocated with greater capacity. The new capacity is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * larger of:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * <ul>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   119
     * <li>The {@code minimumCapacity} argument.
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   120
     * <li>Twice the old capacity, plus {@code 2}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * </ul>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   122
     * If the {@code minimumCapacity} argument is nonpositive, this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * 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
   124
     * 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
   125
     * actual capacity below that requested here.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @param   minimumCapacity   the minimum desired capacity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public void ensureCapacity(int minimumCapacity) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   130
        if (minimumCapacity > 0) {
7020
25638687fe82 6992121: StringBuilder.ensureCapacity(int minCap) throws OutOfMemoryError with minCap=Integer.MIN_VALUE
mchung
parents: 6295
diff changeset
   131
            ensureCapacityInternal(minimumCapacity);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   132
        }
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   133
    }
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   134
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   135
    /**
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   136
     * This method has the same contract as ensureCapacity, but is
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   137
     * never synchronized.
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   138
     */
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   139
    private void ensureCapacityInternal(int minimumCapacity) {
7020
25638687fe82 6992121: StringBuilder.ensureCapacity(int minCap) throws OutOfMemoryError with minCap=Integer.MIN_VALUE
mchung
parents: 6295
diff changeset
   140
        // overflow-conscious code
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   141
        int capacity = value.length >> coder;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   142
        if (minimumCapacity - capacity > 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            expandCapacity(minimumCapacity);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   144
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * This implements the expansion semantics of ensureCapacity with no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * size check or synchronization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   151
    private void expandCapacity(int minimumCapacity) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   152
        int newCapacity = (value.length >> coder) * 2 + 2;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   153
        if (newCapacity - minimumCapacity < 0) {
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   154
            newCapacity = minimumCapacity;
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   155
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (newCapacity < 0) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   157
            if (minimumCapacity < 0) {// overflow
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   158
                throw new OutOfMemoryError();
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   159
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            newCapacity = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   162
        if (coder != LATIN1 && newCapacity > StringUTF16.MAX_LENGTH) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   163
            if (minimumCapacity >= StringUTF16.MAX_LENGTH) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   164
                throw new OutOfMemoryError();
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   165
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   166
            newCapacity = StringUTF16.MAX_LENGTH;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   167
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   168
        this.value = Arrays.copyOf(value, newCapacity << coder);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   169
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   170
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   171
    /**
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   172
     * If the coder is "isLatin1", this inflates the internal 8-bit storage
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   173
     * to 16-bit <hi=0, low> pair storage.
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   174
     */
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   175
    private void inflate() {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   176
        if (!isLatin1()) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   177
            return;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   178
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   179
        byte[] buf = StringUTF16.newBytesFor(value.length);
34517
c6e795a80c80 8142303: C2 compilation fails with "bad AD file"
thartmann
parents: 33871
diff changeset
   180
        StringLatin1.inflate(value, 0, buf, 0, count);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   181
        this.value = buf;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   182
        this.coder = UTF16;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Attempts to reduce storage used for the character sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * If the buffer is larger than necessary to hold its current sequence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * characters, then it may be resized to become more space efficient.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Calling this method may, but is not required to, affect the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * returned by a subsequent call to the {@link #capacity()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public void trimToSize() {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   193
        int length = count << coder;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   194
        if (length < value.length) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   195
            value = Arrays.copyOf(value, length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Sets the length of the character sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * The sequence is changed to a new character sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * 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
   203
     * index <i>k</i> less than {@code newLength}, the character at
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * index <i>k</i> in the new character sequence is the same as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * character at index <i>k</i> in the old sequence if <i>k</i> is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * 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
   207
     * null character {@code '\u005Cu0000'}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   209
     * In other words, if the {@code newLength} argument is less than
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * the current length, the length is changed to the specified length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * <p>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   212
     * If the {@code newLength} argument is greater than or equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * to the current length, sufficient null characters
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   214
     * ({@code '\u005Cu0000'}) are appended so that
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   215
     * length becomes the {@code newLength} argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * <p>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   217
     * The {@code newLength} argument must be greater than or equal
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   218
     * to {@code 0}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @param      newLength   the new length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @throws     IndexOutOfBoundsException  if the
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   222
     *               {@code newLength} argument is negative.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public void setLength(int newLength) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   225
        if (newLength < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            throw new StringIndexOutOfBoundsException(newLength);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   227
        }
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   228
        ensureCapacityInternal(newLength);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (count < newLength) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   230
            if (isLatin1()) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   231
                StringLatin1.fillNull(value, count, newLength);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   232
            } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   233
                StringUTF16.fillNull(value, count, newLength);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   234
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
14686
fb59583d33b2 6553074: String{Buffer,Builder}.indexOf(Str, int) contains unnecessary allocation
mduigou
parents: 14332
diff changeset
   236
        count = newLength;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   240
     * 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
   241
     * 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
   242
     * {@code 1}, and so on, as in array indexing.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * 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
   245
     * {@code 0}, and less than the length of this sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   247
     * <p>If the {@code char} value specified by the index is a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * <a href="Character.html#unicode">surrogate</a>, the surrogate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * value is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   251
     * @param      index   the index of the desired {@code char} value.
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   252
     * @return     the {@code char} value at the specified index.
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   253
     * @throws     IndexOutOfBoundsException  if {@code index} is
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   254
     *             negative or greater than or equal to {@code length()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
14332
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
   256
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    public char charAt(int index) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   258
        checkIndex(index, count);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   259
        if (isLatin1()) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   260
            return (char)(value[index] & 0xff);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   261
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   262
        return StringUTF16.charAt(value, index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * Returns the character (Unicode code point) at the specified
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   267
     * index. The index refers to {@code char} values
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   268
     * (Unicode code units) and ranges from {@code 0} to
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   269
     * {@link #length()}{@code  - 1}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   271
     * <p> If the {@code char} value specified at the given index
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * is in the high-surrogate range, the following index is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * than the length of this sequence, and the
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   274
     * {@code char} value at the following index is in the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * low-surrogate range, then the supplementary code point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * corresponding to this surrogate pair is returned. Otherwise,
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   277
     * the {@code char} value at the given index is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   279
     * @param      index the index to the {@code char} values
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @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
   281
     *             {@code index}
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   282
     * @exception  IndexOutOfBoundsException  if the {@code index}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *             argument is negative or not less than the length of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *             sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    public int codePointAt(int index) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   287
        checkIndex(index, count);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   288
        if (isLatin1()) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   289
            return value[index] & 0xff;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   291
        return StringUTF16.codePointAtSB(value, index, count);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * Returns the character (Unicode code point) before the specified
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   296
     * index. The index refers to {@code char} values
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   297
     * (Unicode code units) and ranges from {@code 1} to {@link
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * #length()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   300
     * <p> If the {@code char} value at {@code (index - 1)}
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   301
     * 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
   302
     * negative, and the {@code char} value at {@code (index -
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   303
     * 2)} is in the high-surrogate range, then the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * supplementary code point value of the surrogate pair is
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   305
     * returned. If the {@code char} value at {@code index -
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   306
     * 1} is an unpaired low-surrogate or a high-surrogate, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * surrogate value is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @param     index the index following the code point that should be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @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
   311
     * @exception IndexOutOfBoundsException if the {@code index}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *            argument is less than 1 or greater than the length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *            of this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    public int codePointBefore(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        int i = index - 1;
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   317
        if (i < 0 || i >= count) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            throw new StringIndexOutOfBoundsException(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   320
        if (isLatin1()) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   321
            return value[i] & 0xff;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   322
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   323
        return StringUTF16.codePointBeforeSB(value, index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * Returns the number of Unicode code points in the specified text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * 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
   329
     * {@code beginIndex} and extends to the {@code char} at
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   330
     * index {@code endIndex - 1}. Thus the length (in
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   331
     * {@code char}s) of the text range is
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   332
     * {@code endIndex-beginIndex}. Unpaired surrogates within
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * this sequence count as one code point each.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   335
     * @param beginIndex the index to the first {@code char} of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * the text range.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   337
     * @param endIndex the index after the last {@code char} of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * the text range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @return the number of Unicode code points in the specified text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @exception IndexOutOfBoundsException if the
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   342
     * {@code beginIndex} is negative, or {@code endIndex}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * is larger than the length of this sequence, or
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   344
     * {@code beginIndex} is larger than {@code endIndex}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    public int codePointCount(int beginIndex, int endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        if (beginIndex < 0 || endIndex > count || beginIndex > endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   350
        if (isLatin1()) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   351
            return endIndex - beginIndex;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   352
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   353
        return StringUTF16.codePointCountSB(value, beginIndex, endIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * 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
   358
     * given {@code index} by {@code codePointOffset} code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * points. Unpaired surrogates within the text range given by
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   360
     * {@code index} and {@code codePointOffset} count as
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * one code point each.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @param index the index to be offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @param codePointOffset the offset in code points
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @return the index within this sequence
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   366
     * @exception IndexOutOfBoundsException if {@code index}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *   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
   368
     *   or if {@code codePointOffset} is positive and the subsequence
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   369
     *   starting with {@code index} has fewer than
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   370
     *   {@code codePointOffset} code points,
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   371
     *   or if {@code codePointOffset} is negative and the subsequence
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   372
     *   before {@code index} has fewer than the absolute value of
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   373
     *   {@code codePointOffset} code points.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    public int offsetByCodePoints(int index, int codePointOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        if (index < 0 || index > count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   379
        return Character.offsetByCodePoints(this,
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   380
                                            index, codePointOffset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * Characters are copied from this sequence into the
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   385
     * destination character array {@code dst}. The first character to
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   386
     * 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
   387
     * 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
   388
     * characters to be copied is {@code srcEnd-srcBegin}. The
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   389
     * characters are copied into the subarray of {@code dst} starting
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   390
     * at index {@code dstBegin} and ending at index:
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19591
diff changeset
   391
     * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * dstbegin + (srcEnd-srcBegin) - 1
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19591
diff changeset
   393
     * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @param      srcBegin   start copying at this offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * @param      srcEnd     stop copying at this offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * @param      dst        the array to copy the data into.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   398
     * @param      dstBegin   offset into {@code dst}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @throws     IndexOutOfBoundsException  if any of the following is true:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     *             <ul>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   401
     *             <li>{@code srcBegin} is negative
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   402
     *             <li>{@code dstBegin} is negative
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   403
     *             <li>the {@code srcBegin} argument is greater than
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   404
     *             the {@code srcEnd} argument.
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   405
     *             <li>{@code srcEnd} is greater than
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   406
     *             {@code this.length()}.
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   407
     *             <li>{@code dstBegin+srcEnd-srcBegin} is greater than
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   408
     *             {@code dst.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *             </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     */
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   411
    public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   413
        checkRangeSIOOBE(srcBegin, srcEnd, count);  // compatible to old version
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   414
        int n = srcEnd - srcBegin;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   415
        checkRange(dstBegin, dstBegin + n, dst.length);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   416
        if (isLatin1()) {
34517
c6e795a80c80 8142303: C2 compilation fails with "bad AD file"
thartmann
parents: 33871
diff changeset
   417
            StringLatin1.getChars(value, srcBegin, srcEnd, dst, dstBegin);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   418
        } else {
34517
c6e795a80c80 8142303: C2 compilation fails with "bad AD file"
thartmann
parents: 33871
diff changeset
   419
            StringUTF16.getChars(value, srcBegin, srcEnd, dst, dstBegin);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   420
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    /**
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   424
     * The character at the specified index is set to {@code ch}. This
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * sequence is altered to represent a new character sequence that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * 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
   427
     * character {@code ch} at position {@code index}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * 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
   430
     * {@code 0}, and less than the length of this sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * @param      index   the index of the character to modify.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @param      ch      the new character.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   434
     * @throws     IndexOutOfBoundsException  if {@code index} is
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   435
     *             negative or greater than or equal to {@code length()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    public void setCharAt(int index, char ch) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   438
        checkIndex(index, count);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   439
        if (isLatin1() && StringLatin1.canEncode(ch)) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   440
            value[index] = (byte)ch;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   441
        } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   442
            if (isLatin1()) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   443
                inflate();
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   444
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   445
            StringUTF16.putCharSB(value, index, ch);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   446
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   450
     * Appends the string representation of the {@code Object} argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   452
     * 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
   453
     * 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
   454
     * and the characters of that string were then
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   455
     * {@link #append(String) appended} to this character sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     *
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   457
     * @param   obj   an {@code Object}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * @return  a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    public AbstractStringBuilder append(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        return append(String.valueOf(obj));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * Appends the specified string to this character sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   467
     * The characters of the {@code String} argument are appended, in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * 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
   469
     * 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
   470
     * characters {@code "null"} are appended.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * 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
   473
     * execution of the {@code append} method. Then the character at
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * index <i>k</i> in the new character sequence is equal to the character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * at index <i>k</i> in the old character sequence, if <i>k</i> is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * 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
   477
     * <i>k-n</i> in the argument {@code str}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * @param   str   a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @return  a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    public AbstractStringBuilder append(String str) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   483
        if (str == null) {
16742
e6b0ac6581f1 8010849: (str) Optimize StringBuilder.append(null)
martin
parents: 16714
diff changeset
   484
            return appendNull();
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   485
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        int len = str.length();
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   487
        ensureCapacityInternal(count + len);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   488
        putStringAt(count, str);
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   489
        count += len;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    // Documentation in subclasses because of synchro difference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    public AbstractStringBuilder append(StringBuffer sb) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   495
        return this.append((AbstractStringBuilder)sb);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
14332
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
   498
    /**
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
   499
     * @since 1.8
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
   500
     */
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
   501
    AbstractStringBuilder append(AbstractStringBuilder asb) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   502
        if (asb == null) {
16742
e6b0ac6581f1 8010849: (str) Optimize StringBuilder.append(null)
martin
parents: 16714
diff changeset
   503
            return appendNull();
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   504
        }
14332
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
   505
        int len = asb.length();
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
   506
        ensureCapacityInternal(count + len);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   507
        if (getCoder() != asb.getCoder()) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   508
            inflate();
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   509
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   510
        asb.getBytes(value, count, coder);
14332
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
   511
        count += len;
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
   512
        return this;
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
   513
    }
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
   514
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    // 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
   516
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    public AbstractStringBuilder append(CharSequence s) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   518
        if (s == null) {
16742
e6b0ac6581f1 8010849: (str) Optimize StringBuilder.append(null)
martin
parents: 16714
diff changeset
   519
            return appendNull();
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   520
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   521
        if (s instanceof String) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            return this.append((String)s);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   523
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   524
        if (s instanceof AbstractStringBuilder) {
14332
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
   525
            return this.append((AbstractStringBuilder)s);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   526
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        return this.append(s, 0, s.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
16742
e6b0ac6581f1 8010849: (str) Optimize StringBuilder.append(null)
martin
parents: 16714
diff changeset
   530
    private AbstractStringBuilder appendNull() {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   531
        ensureCapacityInternal(count + 4);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   532
        int count = this.count;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   533
        byte[] val = this.value;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   534
        if (isLatin1()) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   535
            val[count++] = 'n';
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   536
            val[count++] = 'u';
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   537
            val[count++] = 'l';
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   538
            val[count++] = 'l';
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   539
        } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   540
            checkOffset(count + 4, val.length >> 1);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   541
            StringUTF16.putChar(val, count++, 'n');
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   542
            StringUTF16.putChar(val, count++, 'u');
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   543
            StringUTF16.putChar(val, count++, 'l');
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   544
            StringUTF16.putChar(val, count++, 'l');
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   545
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   546
        this.count = count;
16742
e6b0ac6581f1 8010849: (str) Optimize StringBuilder.append(null)
martin
parents: 16714
diff changeset
   547
        return this;
e6b0ac6581f1 8010849: (str) Optimize StringBuilder.append(null)
martin
parents: 16714
diff changeset
   548
    }
e6b0ac6581f1 8010849: (str) Optimize StringBuilder.append(null)
martin
parents: 16714
diff changeset
   549
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   551
     * Appends a subsequence of the specified {@code CharSequence} to this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * sequence.
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
     * Characters of the argument {@code s}, starting at
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   555
     * 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
   556
     * 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
   557
     * of this sequence is increased by the value of {@code end - start}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * 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
   560
     * execution of the {@code append} method. Then the character at
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * index <i>k</i> in this character sequence becomes equal to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * character at index <i>k</i> in this sequence, if <i>k</i> is less than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * <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
   564
     * <i>k+start-n</i> in the argument {@code s}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   566
     * If {@code s} is {@code null}, then this method appends
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * 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
   568
     * characters {@code "null"}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * @param   s the sequence to append.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * @param   start   the starting index of the subsequence to be appended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * @param   end     the end index of the subsequence to be appended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * @return  a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * @throws     IndexOutOfBoundsException if
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   575
     *             {@code start} is negative, or
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   576
     *             {@code start} is greater than {@code end} or
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   577
     *             {@code end} is greater than {@code s.length()}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     */
14332
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
   579
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    public AbstractStringBuilder append(CharSequence s, int start, int end) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   581
        if (s == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            s = "null";
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   583
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   584
        checkRange(start, end, s.length());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        int len = end - start;
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   586
        ensureCapacityInternal(count + len);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   587
        appendChars(s, start, end);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   592
     * Appends the string representation of the {@code char} array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * argument to this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * The characters of the array argument are appended, in order, to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * the contents of this sequence. The length of this sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * increases by the length of the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   599
     * 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
   600
     * 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
   601
     * and the characters of that string were then
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   602
     * {@link #append(String) appended} to this character sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * @param   str   the characters to be appended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * @return  a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     */
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   607
    public AbstractStringBuilder append(char[] str) {
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   608
        int len = str.length;
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   609
        ensureCapacityInternal(count + len);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   610
        appendChars(str, 0, len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * 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
   616
     * {@code char} array argument to this sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   618
     * 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
   619
     * index {@code offset}, are appended, in order, to the contents
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * 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
   621
     * by the value of {@code len}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   623
     * 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
   624
     * 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
   625
     * and the characters of that string were then
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   626
     * {@link #append(String) appended} to this character sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * @param   str      the characters to be appended.
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   629
     * @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
   630
     * @param   len      the number of {@code char}s to append.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * @return  a reference to this object.
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   632
     * @throws IndexOutOfBoundsException
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   633
     *         if {@code offset < 0} or {@code len < 0}
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   634
     *         or {@code offset+len > str.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    public AbstractStringBuilder append(char str[], int offset, int len) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   637
        int end = offset + len;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   638
        checkRange(offset, end, str.length);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   639
        ensureCapacityInternal(count + len);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   640
        appendChars(str, offset, end);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   645
     * Appends the string representation of the {@code boolean}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * argument to the sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   648
     * 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
   649
     * 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
   650
     * and the characters of that string were then
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   651
     * {@link #append(String) appended} to this character sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     *
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   653
     * @param   b   a {@code boolean}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * @return  a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    public AbstractStringBuilder append(boolean b) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   657
        ensureCapacityInternal(count + (b ? 4 : 5));
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   658
        int count = this.count;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   659
        byte[] val = this.value;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   660
        if (isLatin1()) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   661
            if (b) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   662
                val[count++] = 't';
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   663
                val[count++] = 'r';
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   664
                val[count++] = 'u';
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   665
                val[count++] = 'e';
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   666
            } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   667
                val[count++] = 'f';
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   668
                val[count++] = 'a';
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   669
                val[count++] = 'l';
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   670
                val[count++] = 's';
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   671
                val[count++] = 'e';
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   672
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        } else {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   674
            if (b) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   675
                checkOffset(count + 4, val.length >> 1);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   676
                StringUTF16.putChar(val, count++, 't');
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   677
                StringUTF16.putChar(val, count++, 'r');
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   678
                StringUTF16.putChar(val, count++, 'u');
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   679
                StringUTF16.putChar(val, count++, 'e');
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   680
            } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   681
                checkOffset(count + 5, val.length >> 1);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   682
                StringUTF16.putChar(val, count++, 'f');
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   683
                StringUTF16.putChar(val, count++, 'a');
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   684
                StringUTF16.putChar(val, count++, 'l');
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   685
                StringUTF16.putChar(val, count++, 's');
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   686
                StringUTF16.putChar(val, count++, 'e');
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   687
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   689
        this.count = count;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   694
     * Appends the string representation of the {@code char}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * argument to this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * 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
   698
     * The length of this sequence increases by {@code 1}.
2
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(char)},
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   702
     * and the character in 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   c   a {@code char}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * @return  a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     */
14332
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
   708
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    public AbstractStringBuilder append(char c) {
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   710
        ensureCapacityInternal(count + 1);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   711
        if (isLatin1() && StringLatin1.canEncode(c)) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   712
            value[count++] = (byte)c;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   713
        } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   714
            if (isLatin1()) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   715
                inflate();
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   716
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   717
            StringUTF16.putCharSB(value, count++, c);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   718
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   723
     * Appends the string representation of the {@code int}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * argument to this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   726
     * 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
   727
     * 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
   728
     * and the characters of that string were then
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   729
     * {@link #append(String) appended} to this character sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     *
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   731
     * @param   i   an {@code int}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * @return  a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    public AbstractStringBuilder append(int i) {
34331
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33871
diff changeset
   735
        int spaceNeeded = count + Integer.stringSize(i);
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   736
        ensureCapacityInternal(spaceNeeded);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   737
        if (isLatin1()) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   738
            Integer.getChars(i, spaceNeeded, value);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   739
        } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   740
            byte[] val = this.value;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   741
            checkOffset(spaceNeeded, val.length >> 1);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   742
            Integer.getCharsUTF16(i, spaceNeeded, val);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   743
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        count = spaceNeeded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   749
     * Appends the string representation of the {@code long}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * argument to this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   752
     * 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
   753
     * 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
   754
     * and the characters of that string were then
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   755
     * {@link #append(String) appended} to this character sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     *
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   757
     * @param   l   a {@code long}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * @return  a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    public AbstractStringBuilder append(long l) {
34331
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33871
diff changeset
   761
        int spaceNeeded = count + Long.stringSize(l);
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   762
        ensureCapacityInternal(spaceNeeded);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   763
        if (isLatin1()) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   764
            Long.getChars(l, spaceNeeded, value);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   765
        } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   766
            byte[] val = this.value;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   767
            checkOffset(spaceNeeded, val.length >> 1);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   768
            Long.getCharsUTF16(l, spaceNeeded, val);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   769
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        count = spaceNeeded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   775
     * Appends the string representation of the {@code float}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * argument to this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   778
     * 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
   779
     * 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
   780
     * and the characters of that string were then
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   781
     * {@link #append(String) appended} to this character sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     *
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   783
     * @param   f   a {@code float}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * @return  a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    public AbstractStringBuilder append(float f) {
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 16742
diff changeset
   787
        FloatingDecimal.appendTo(f,this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   792
     * Appends the string representation of the {@code double}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * argument to this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   795
     * 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
   796
     * 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
   797
     * and the characters of that string were then
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   798
     * {@link #append(String) appended} to this character sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     *
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   800
     * @param   d   a {@code double}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * @return  a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    public AbstractStringBuilder append(double d) {
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 16742
diff changeset
   804
        FloatingDecimal.appendTo(d,this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * 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
   810
     * 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
   811
     * the character at index {@code end - 1} or to the end of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * sequence if no such character exists. If
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   813
     * {@code start} is equal to {@code end}, no changes are made.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * @param      start  The beginning index, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * @param      end    The ending index, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * @return     This object.
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   818
     * @throws     StringIndexOutOfBoundsException  if {@code start}
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   819
     *             is negative, greater than {@code length()}, or
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
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 delete(int start, int end) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   823
        if (end > count) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            end = count;
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   825
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   826
        checkRangeSIOOBE(start, end, count);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        int len = end - start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        if (len > 0) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   829
            shift(end, -len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            count -= len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   836
     * Appends the string representation of the {@code codePoint}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * argument to this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * <p> The argument is appended to the contents of this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * The length of this sequence increases by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * {@link Character#charCount(int) Character.charCount(codePoint)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * <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
   844
     * converted to a {@code char} array by the method
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   845
     * {@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
   846
     * were then {@link #append(char[]) appended} to this character
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * @param   codePoint   a Unicode code point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * @return  a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * @exception IllegalArgumentException if the specified
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   852
     * {@code codePoint} isn't a valid Unicode code point
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    public AbstractStringBuilder appendCodePoint(int codePoint) {
5986
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5627
diff changeset
   855
        if (Character.isBmpCodePoint(codePoint)) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   856
            return append((char)codePoint);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   858
        return append(Character.toChars(codePoint));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    /**
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   862
     * Removes the {@code char} at the specified position in this
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   863
     * sequence. This sequence is shortened by one {@code char}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * <p>Note: If the character at the given index is a supplementary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * character, this method does not remove the entire character. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * correct handling of supplementary characters is required,
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   868
     * 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
   869
     * {@code Character.charCount(thisSequence.codePointAt(index))},
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   870
     * where {@code thisSequence} is this sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   872
     * @param       index  Index of {@code char} to remove
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * @return      This object.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   874
     * @throws      StringIndexOutOfBoundsException  if the {@code index}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     *              is negative or greater than or equal to
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   876
     *              {@code length()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    public AbstractStringBuilder deleteCharAt(int index) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   879
        checkIndex(index, count);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   880
        shift(index + 1, -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        count--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * Replaces the characters in a substring of this sequence
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   887
     * with characters in the specified {@code String}. The substring
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   888
     * 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
   889
     * at index {@code end - 1} or to the end of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * sequence if no such character exists. First the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * 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
   892
     * {@code String} is inserted at {@code start}. (This
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * sequence will be lengthened to accommodate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * specified String if necessary.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * @param      start    The beginning index, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * @param      end      The ending index, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * @param      str   String that will replace previous contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * @return     This object.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   900
     * @throws     StringIndexOutOfBoundsException  if {@code start}
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   901
     *             is negative, greater than {@code length()}, or
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   902
     *             greater than {@code end}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    public AbstractStringBuilder replace(int start, int end, String str) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   905
        if (end > count) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            end = count;
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   907
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   908
        checkRangeSIOOBE(start, end, count);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        int len = str.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        int newCount = count + len - (end - start);
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
   911
        ensureCapacityInternal(newCount);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   912
        shift(end, newCount - count);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        count = newCount;
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   914
        putStringAt(start, str);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    /**
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   919
     * Returns a new {@code String} that contains a subsequence of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * characters currently contained in this character sequence. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * substring begins at the specified index and extends to the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * @param      start    The beginning index, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * @return     The new string.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   926
     * @throws     StringIndexOutOfBoundsException  if {@code start} is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     *             less than zero, or greater than the length of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
    public String substring(int start) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        return substring(start, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * Returns a new character sequence that is a subsequence of this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * <p> An invocation of this method of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     *
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19591
diff changeset
   938
     * <pre>{@code
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19591
diff changeset
   939
     * sb.subSequence(begin,&nbsp;end)}</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * behaves in exactly the same way as the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     *
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19591
diff changeset
   943
     * <pre>{@code
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19591
diff changeset
   944
     * sb.substring(begin,&nbsp;end)}</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * This method is provided so that this class can
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19591
diff changeset
   947
     * implement the {@link CharSequence} interface.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * @param      start   the start index, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * @param      end     the end index, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * @return     the specified subsequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * @throws  IndexOutOfBoundsException
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
   954
     *          if {@code start} or {@code end} are negative,
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
   955
     *          if {@code end} is greater than {@code length()},
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
   956
     *          or if {@code start} is greater than {@code end}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     */
14332
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
   959
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    public CharSequence subSequence(int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        return substring(start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    /**
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   965
     * Returns a new {@code String} that contains a subsequence of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * characters currently contained in this sequence. The
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   967
     * substring begins at the specified {@code start} and
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   968
     * extends to the character at index {@code end - 1}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * @param      start    The beginning index, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * @param      end      The ending index, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * @return     The new string.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   973
     * @throws     StringIndexOutOfBoundsException  if {@code start}
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   974
     *             or {@code end} are negative or greater than
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   975
     *             {@code length()}, or {@code start} is
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
   976
     *             greater than {@code end}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    public String substring(int start, int end) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   979
        checkRangeSIOOBE(start, end, count);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   980
        if (isLatin1()) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   981
            return StringLatin1.newString(value, start, end - start);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   982
        }
34517
c6e795a80c80 8142303: C2 compilation fails with "bad AD file"
thartmann
parents: 33871
diff changeset
   983
        return StringUTF16.newString(value, start, end - start);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   984
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   985
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   986
    private void shift(int offset, int n) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   987
        System.arraycopy(value, offset << coder,
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
   988
                         value, (offset + n) << coder, (count - offset) << coder);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
   992
     * Inserts the string representation of a subarray of the {@code str}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * 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
   994
     * specified {@code offset} and extends {@code len} {@code char}s.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     * 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
   996
     * 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
   997
     * sequence increases by {@code len} {@code char}s.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * @param      index    position at which to insert subarray.
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1000
     * @param      str       A {@code char} array.
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1001
     * @param      offset   the index of the first {@code char} in subarray to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     *             be inserted.
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1003
     * @param      len      the number of {@code char}s in the subarray to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     *             be inserted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * @return     This object
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1006
     * @throws     StringIndexOutOfBoundsException  if {@code index}
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1007
     *             is negative or greater than {@code length()}, or
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1008
     *             {@code offset} or {@code len} are negative, or
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1009
     *             {@code (offset+len)} is greater than
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1010
     *             {@code str.length}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     */
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1012
    public AbstractStringBuilder insert(int index, char[] str, int offset,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                                        int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1015
        checkOffset(index, count);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1016
        checkRangeSIOOBE(offset, offset + len, str.length);
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
  1017
        ensureCapacityInternal(count + len);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1018
        shift(index, len);
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
  1019
        count += len;
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1020
        putCharsAt(index, str, offset, offset + len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1025
     * Inserts the string representation of the {@code Object}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * argument into this character sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1028
     * 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
  1029
     * 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
  1030
     * and the characters of that string were then
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1031
     * {@link #insert(int,String) inserted} into this character
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1032
     * sequence at the indicated offset.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1034
     * 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
  1035
     * {@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
  1036
     * of this sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * @param      offset   the offset.
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1039
     * @param      obj      an {@code Object}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * @return     a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    public AbstractStringBuilder insert(int offset, Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        return insert(offset, String.valueOf(obj));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     * Inserts the string into this character sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1050
     * The characters of the {@code String} argument are inserted, in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * order, into this sequence at the indicated offset, moving up any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * characters originally above that position and increasing the length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * 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
  1054
     * {@code str} is {@code null}, then the four characters
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1055
     * {@code "null"} are inserted into this sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * The character at index <i>k</i> in the new character sequence is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * equal to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * <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
  1061
     * <i>k</i> is less than {@code offset}
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1062
     * <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
  1063
     * 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
  1064
     * {@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
  1065
     * <li>the character at index <i>k</i>{@code -str.length()} in the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * 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
  1067
     * {@code offset+str.length()}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * </ul><p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1069
     * 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
  1070
     * {@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
  1071
     * of this sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     * @param      offset   the offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * @param      str      a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * @return     a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
    public AbstractStringBuilder insert(int offset, String str) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1079
        checkOffset(offset, count);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1080
        if (str == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            str = "null";
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1082
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        int len = str.length();
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
  1084
        ensureCapacityInternal(count + len);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1085
        shift(offset, len);
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
  1086
        count += len;
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1087
        putStringAt(offset, str);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1092
     * Inserts the string representation of the {@code char} array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * argument into this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * The characters of the array argument are inserted into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * 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
  1097
     * {@code offset}. The length of this sequence increases by
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * the length of the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1100
     * 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
  1101
     * 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
  1102
     * and the characters of that string were then
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1103
     * {@link #insert(int,String) inserted} into this character
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1104
     * sequence at the indicated offset.
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1105
     * <p>
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1106
     * 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
  1107
     * {@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
  1108
     * of this sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * @param      offset   the offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     * @param      str      a character array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     * @return     a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     */
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1115
    public AbstractStringBuilder insert(int offset, char[] str) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1116
        checkOffset(offset, count);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        int len = str.length;
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
  1118
        ensureCapacityInternal(count + len);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1119
        shift(offset, len);
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
  1120
        count += len;
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1121
        putCharsAt(offset, str, 0, len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1126
     * Inserts the specified {@code CharSequence} into this sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1128
     * The characters of the {@code CharSequence} argument are inserted,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     * in order, into this sequence at the indicated offset, moving up
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     * any characters originally above that position and increasing the length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     * of this sequence by the length of the argument s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     * 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
  1134
     * invocation of this object's
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1135
     * {@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
  1136
     * method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     *
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1138
     * <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
  1139
     * {@code "null"} are inserted into this sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     * @param      dstOffset   the offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     * @param      s the sequence to be inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * @return     a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * @throws     IndexOutOfBoundsException  if the offset is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
    public AbstractStringBuilder insert(int dstOffset, CharSequence s) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1147
        if (s == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            s = "null";
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1149
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1150
        if (s instanceof String) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            return this.insert(dstOffset, (String)s);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1152
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        return this.insert(dstOffset, s, 0, s.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1157
     * Inserts a subsequence of the specified {@code CharSequence} into
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1160
     * The subsequence of the argument {@code s} specified by
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1161
     * {@code start} and {@code end} are inserted,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * in order, into this sequence at the specified destination offset, moving
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * 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
  1164
     * sequence is increased by {@code end - start}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * The character at index <i>k</i> in this sequence becomes equal to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * <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
  1169
     * <i>k</i> is less than {@code dstOffset}
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1170
     * <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
  1171
     * 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
  1172
     * {@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
  1173
     * <li>the character at index <i>k</i>{@code -(end-start)} in this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * 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
  1175
     * {@code dstOffset+end-start}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     * </ul><p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1177
     * 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
  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
     * <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
  1181
     * {@code end}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     * <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
  1183
     * {@code start}, and less than or equal to the length of s.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     *
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1185
     * <p>If {@code s} is {@code null}, then this method inserts
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     * 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
  1187
     * characters {@code "null"}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     * @param      dstOffset   the offset in this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * @param      s       the sequence to be inserted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * @param      start   the starting index of the subsequence to be inserted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * @param      end     the end index of the subsequence to be inserted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     * @return     a reference to this object.
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1194
     * @throws     IndexOutOfBoundsException  if {@code dstOffset}
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1195
     *             is negative or greater than {@code this.length()}, or
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1196
     *              {@code start} or {@code end} are negative, or
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1197
     *              {@code start} is greater than {@code end} or
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1198
     *              {@code end} is greater than {@code s.length()}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     */
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1200
    public AbstractStringBuilder insert(int dstOffset, CharSequence s,
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1201
                                        int start, int end)
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1202
    {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1203
        if (s == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            s = "null";
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1205
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1206
        checkOffset(dstOffset, count);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1207
        checkRange(start, end, s.length());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        int len = end - start;
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
  1209
        ensureCapacityInternal(count + len);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1210
        shift(dstOffset, len);
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
  1211
        count += len;
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1212
        putCharsAt(dstOffset, s, start, end);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1217
     * Inserts the string representation of the {@code boolean}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * argument into this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1220
     * 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
  1221
     * 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
  1222
     * and the characters of that string were then
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1223
     * {@link #insert(int,String) inserted} into this character
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1224
     * sequence at the indicated offset.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1226
     * 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
  1227
     * {@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
  1228
     * of this sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     * @param      offset   the offset.
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1231
     * @param      b        a {@code boolean}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     * @return     a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
    public AbstractStringBuilder insert(int offset, boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        return insert(offset, String.valueOf(b));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1240
     * Inserts the string representation of the {@code char}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     * argument into this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1243
     * 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
  1244
     * 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
  1245
     * and the character in that string were then
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1246
     * {@link #insert(int,String) inserted} into this character
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1247
     * sequence at the indicated offset.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1249
     * 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
  1250
     * {@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
  1251
     * of this sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     * @param      offset   the offset.
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1254
     * @param      c        a {@code char}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     * @return     a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * @throws     IndexOutOfBoundsException  if the offset is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
    public AbstractStringBuilder insert(int offset, char c) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1259
        checkOffset(offset, count);
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
  1260
        ensureCapacityInternal(count + 1);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1261
        shift(offset, 1);
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 1247
diff changeset
  1262
        count += 1;
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1263
        if (isLatin1() && StringLatin1.canEncode(c)) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1264
            value[offset] = (byte)c;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1265
        } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1266
            if (isLatin1()) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1267
                inflate();
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1268
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1269
            StringUTF16.putCharSB(value, offset, c);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1270
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1275
     * Inserts the string representation of the second {@code int}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     * argument into this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1278
     * 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
  1279
     * 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
  1280
     * and the characters of that string were then
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1281
     * {@link #insert(int,String) inserted} into this character
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1282
     * sequence at the indicated offset.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1284
     * 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
  1285
     * {@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
  1286
     * of this sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     * @param      offset   the offset.
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1289
     * @param      i        an {@code int}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
     * @return     a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
    public AbstractStringBuilder insert(int offset, int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        return insert(offset, String.valueOf(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1298
     * Inserts the string representation of the {@code long}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     * argument into this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1301
     * 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
  1302
     * 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
  1303
     * and the characters of that string were then
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1304
     * {@link #insert(int,String) inserted} into this character
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1305
     * sequence at the indicated offset.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1307
     * 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
  1308
     * {@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
  1309
     * of this sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     * @param      offset   the offset.
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1312
     * @param      l        a {@code long}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     * @return     a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
    public AbstractStringBuilder insert(int offset, long l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        return insert(offset, String.valueOf(l));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1321
     * Inserts the string representation of the {@code float}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     * argument into this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1324
     * 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
  1325
     * 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
  1326
     * and the characters of that string were then
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1327
     * {@link #insert(int,String) inserted} into this character
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1328
     * sequence at the indicated offset.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1330
     * 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
  1331
     * {@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
  1332
     * of this sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     * @param      offset   the offset.
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1335
     * @param      f        a {@code float}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     * @return     a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
    public AbstractStringBuilder insert(int offset, float f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
        return insert(offset, String.valueOf(f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
    /**
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1344
     * Inserts the string representation of the {@code double}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     * argument into this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1347
     * 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
  1348
     * 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
  1349
     * and the characters of that string were then
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1350
     * {@link #insert(int,String) inserted} into this character
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1351
     * sequence at the indicated offset.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
     * <p>
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1353
     * 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
  1354
     * {@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
  1355
     * of this sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
     * @param      offset   the offset.
1223
5c1037124466 6728229: (str) StringBuilder.append(CharSequence) does not throw IndexOutOfBoundsException
martin
parents: 2
diff changeset
  1358
     * @param      d        a {@code double}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     * @return     a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
    public AbstractStringBuilder insert(int offset, double d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        return insert(offset, String.valueOf(d));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     * 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
  1368
     * specified substring.
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1369
     *
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1370
     * <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
  1371
     * <pre>{@code
23024
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1372
     * this.toString().startsWith(str, k)
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19591
diff changeset
  1373
     * }</pre>
23024
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1374
     * If no such value of {@code k} exists, then {@code -1} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     *
23024
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1376
     * @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
  1377
     * @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
  1378
     *          or {@code -1} if there is no such occurrence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
    public int indexOf(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        return indexOf(str, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
     * 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
  1386
     * 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
  1387
     *
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1388
     * <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
  1389
     * <pre>{@code
19591
80e86f94c0e9 8016018: Typo in AbstractStringBuilder#indexOf and #lastIndexOf descriptions
igerasim
parents: 18143
diff changeset
  1390
     *     k >= Math.min(fromIndex, this.length()) &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     *                   this.toString().startsWith(str, k)
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19591
diff changeset
  1392
     * }</pre>
23024
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1393
     * If no such value of {@code k} exists, then {@code -1} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     *
23024
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1395
     * @param   str         the substring to search for.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     * @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
  1397
     * @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
  1398
     *          starting at the specified index,
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1399
     *          or {@code -1} if there is no such occurrence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
    public int indexOf(String str, int fromIndex) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1402
        return String.indexOf(value, coder, count, str, fromIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
    /**
23024
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1406
     * 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
  1407
     * 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
  1408
     * 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
  1409
     *
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1410
     * <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
  1411
     * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
     * this.toString().startsWith(str, k)
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19591
diff changeset
  1413
     * }</pre>
23024
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1414
     * If no such value of {@code k} exists, then {@code -1} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
     * @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
  1417
     * @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
  1418
     *          or {@code -1} if there is no such occurrence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
    public int lastIndexOf(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
        return lastIndexOf(str, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     * 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
  1426
     * 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
  1427
     *
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1428
     * <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
  1429
     * <pre>{@code
19591
80e86f94c0e9 8016018: Typo in AbstractStringBuilder#indexOf and #lastIndexOf descriptions
igerasim
parents: 18143
diff changeset
  1430
     *     k <= Math.min(fromIndex, this.length()) &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
     *                   this.toString().startsWith(str, k)
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19591
diff changeset
  1432
     * }</pre>
23024
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1433
     * If no such value of {@code k} exists, then {@code -1} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
     * @param   str         the substring to search for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
     * @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
  1437
     * @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
  1438
     *          searching backward from the specified index,
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 21334
diff changeset
  1439
     *          or {@code -1} if there is no such occurrence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
    public int lastIndexOf(String str, int fromIndex) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1442
        return String.lastIndexOf(value, coder, count, str, fromIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
     * Causes this character sequence to be replaced by the reverse of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
     * the sequence. If there are any surrogate pairs included in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
     * sequence, these are treated as single characters for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
     * reverse operation. Thus, the order of the high-low surrogates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
     * is never reversed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
     * 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
  1453
     * (not the length in {@code char} values) just prior to
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
  1454
     * execution of the {@code reverse} method. Then the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
     * character at index <i>k</i> in the new character sequence is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
     * equal to the character at index <i>n-k-1</i> in the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
     * character sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
     * <p>Note that the reverse operation may result in producing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
     * surrogate pairs that were unpaired low-surrogates and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
     * high-surrogates before the operation. For example, reversing
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
  1462
     * "\u005CuDC00\u005CuD800" produces "\u005CuD800\u005CuDC00" which is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
     * a valid surrogate pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
     * @return  a reference to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
    public AbstractStringBuilder reverse() {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1468
        byte[] val = this.value;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1469
        int count = this.count;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1470
        int coder = this.coder;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
        int n = count - 1;
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1472
        if (COMPACT_STRINGS && coder == LATIN1) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1473
            for (int j = (n-1) >> 1; j >= 0; j--) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1474
                int k = n - j;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1475
                byte cj = val[j];
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1476
                val[j] = val[k];
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1477
                val[k] = cj;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
            }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1479
        } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1480
            checkOffset(count, val.length >> 1);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1481
            boolean hasSurrogates = false;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1482
            for (int j = (n-1) >> 1; j >= 0; j--) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1483
                int k = n - j;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1484
                char cj = StringUTF16.getChar(val, j);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1485
                char ck = StringUTF16.getChar(val, k);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1486
                StringUTF16.putChar(val, j, ck);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1487
                StringUTF16.putChar(val, k, cj);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1488
                if (Character.isSurrogate(cj) ||
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1489
                    Character.isSurrogate(ck)) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1490
                    hasSurrogates = true;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1491
                }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1492
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1493
            if (hasSurrogates) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1494
                reverseAllValidSurrogatePairs(val, count);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1495
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
        }
16714
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 15312
diff changeset
  1497
        return this;
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 15312
diff changeset
  1498
    }
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 15312
diff changeset
  1499
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 15312
diff changeset
  1500
    /** Outlined helper method for reverse() */
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1501
    private void reverseAllValidSurrogatePairs(byte[] val, int count) {
16714
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 15312
diff changeset
  1502
        for (int i = 0; i < count - 1; i++) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1503
            char c2 = StringUTF16.getChar(val, i);
16714
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 15312
diff changeset
  1504
            if (Character.isLowSurrogate(c2)) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1505
                char c1 = StringUTF16.getChar(val, i + 1);
16714
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 15312
diff changeset
  1506
                if (Character.isHighSurrogate(c1)) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1507
                    StringUTF16.putChar(val, i++, c1);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1508
                    StringUTF16.putChar(val, i, c2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
     * Returns a string representing the data in this sequence.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
  1516
     * A new {@code String} object is allocated and initialized to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
     * contain the character sequence currently represented by this
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 7668
diff changeset
  1518
     * object. This {@code String} is then returned. Subsequent
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
     * 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
  1520
     * {@code String}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
     * @return  a string representation of this sequence of characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
     */
14332
451c5dd717dc 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
jgish
parents: 13822
diff changeset
  1524
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
    public abstract String toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
    /**
28667
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 25859
diff changeset
  1528
     * {@inheritDoc}
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 34781
diff changeset
  1529
     * @since 9
28667
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 25859
diff changeset
  1530
     */
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 25859
diff changeset
  1531
    @Override
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 25859
diff changeset
  1532
    public IntStream chars() {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1533
        byte[] val = this.value; int count = this.count; byte coder = this.coder;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1534
        checkOffset(count, val.length >> coder);
28667
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 25859
diff changeset
  1535
        // Reuse String-based spliterator. This requires a supplier to
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 25859
diff changeset
  1536
        // capture the value and count when the terminal operation is executed
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 25859
diff changeset
  1537
        return StreamSupport.intStream(
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1538
                () -> coder == LATIN1 ? new StringLatin1.CharsSpliterator(val, 0, count, 0)
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1539
                                      : new StringUTF16.CharsSpliterator(val, 0, count, 0),
28667
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 25859
diff changeset
  1540
                Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED,
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 25859
diff changeset
  1541
                false);
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 25859
diff changeset
  1542
    }
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 25859
diff changeset
  1543
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 25859
diff changeset
  1544
    /**
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 25859
diff changeset
  1545
     * {@inheritDoc}
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 34781
diff changeset
  1546
     * @since 9
28667
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 25859
diff changeset
  1547
     */
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 25859
diff changeset
  1548
    @Override
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 25859
diff changeset
  1549
    public IntStream codePoints() {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1550
        byte[] val = this.value; int count = this.count; byte coder = this.coder;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1551
        checkOffset(count, val.length >> coder);
28667
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 25859
diff changeset
  1552
        // Reuse String-based spliterator. This requires a supplier to
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 25859
diff changeset
  1553
        // capture the value and count when the terminal operation is executed
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 25859
diff changeset
  1554
        return StreamSupport.intStream(
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1555
                () -> coder == LATIN1 ? new StringLatin1.CharsSpliterator(val, 0, count, 0)
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1556
                                      : new StringUTF16.CodePointsSpliterator(val, 0, count, 0),
28667
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 25859
diff changeset
  1557
                Spliterator.ORDERED,
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 25859
diff changeset
  1558
                false);
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 25859
diff changeset
  1559
    }
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 25859
diff changeset
  1560
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 25859
diff changeset
  1561
    /**
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  1562
     * Needed by {@code String} for the contentEquals method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
     */
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1564
    final byte[] getValue() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1568
    /*
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1569
     * Invoker guarantees it is in UTF16 (inflate itself for asb), if two
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1570
     * coders are different and the dstBegin has enough space
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1571
     *
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1572
     * @param dstBegin  the char index, not offset of byte[]
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1573
     * @param coder     the coder of dst[]
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1574
     */
33871
73d87430102d 8143330: Two implementation methods of AbstractStringBuilder are mistakenly declared as "protected" in JDK9b93
sherman
parents: 33663
diff changeset
  1575
    void getBytes(byte dst[], int dstBegin, byte coder) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1576
        if (this.coder == coder) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1577
            System.arraycopy(value, 0, dst, dstBegin << coder, count << coder);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1578
        } else {        // this.coder == LATIN && coder == UTF16
34517
c6e795a80c80 8142303: C2 compilation fails with "bad AD file"
thartmann
parents: 33871
diff changeset
  1579
            StringLatin1.inflate(value, 0, dst, dstBegin, count);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1580
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1581
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1582
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1583
    /* for readObject() */
33871
73d87430102d 8143330: Two implementation methods of AbstractStringBuilder are mistakenly declared as "protected" in JDK9b93
sherman
parents: 33663
diff changeset
  1584
    void initBytes(char[] value, int off, int len) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1585
        if (String.COMPACT_STRINGS) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1586
            this.value = StringUTF16.compress(value, off, len);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1587
            if (this.value != null) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1588
                this.coder = LATIN1;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1589
                return;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1590
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1591
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1592
        this.coder = UTF16;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1593
        this.value = StringUTF16.toBytes(value, off, len);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1594
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1595
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1596
    final byte getCoder() {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1597
        return COMPACT_STRINGS ? coder : UTF16;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1598
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1599
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1600
    final boolean isLatin1() {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1601
        return COMPACT_STRINGS && coder == LATIN1;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1602
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1603
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1604
    private final void putCharsAt(int index, char[] s, int off, int end) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1605
        if (isLatin1()) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1606
            byte[] val = this.value;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1607
            for (int i = off, j = index; i < end; i++) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1608
                char c = s[i];
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1609
                if (StringLatin1.canEncode(c)) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1610
                    val[j++] = (byte)c;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1611
                } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1612
                    inflate();
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1613
                    StringUTF16.putCharsSB(this.value, j, s, i, end);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1614
                    return;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1615
                }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1616
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1617
        } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1618
            StringUTF16.putCharsSB(this.value, index, s, off, end);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1619
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1620
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1621
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1622
    private final void putCharsAt(int index, CharSequence s, int off, int end) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1623
        if (isLatin1()) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1624
            byte[] val = this.value;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1625
            for (int i = off, j = index; i < end; i++) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1626
                char c = s.charAt(i);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1627
                if (StringLatin1.canEncode(c)) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1628
                    val[j++] = (byte)c;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1629
                } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1630
                    inflate();
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1631
                    StringUTF16.putCharsSB(this.value, j, s, i, end);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1632
                    return;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1633
                }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1634
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1635
        } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1636
            StringUTF16.putCharsSB(this.value, index, s, off, end);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1637
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1638
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1639
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1640
    private final void putStringAt(int index, String str) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1641
        if (getCoder() != str.coder()) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1642
            inflate();
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1643
        }
34517
c6e795a80c80 8142303: C2 compilation fails with "bad AD file"
thartmann
parents: 33871
diff changeset
  1644
        str.getBytes(value, index, coder);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1645
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1646
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1647
    private final void appendChars(char[] s, int off, int end) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1648
        if (isLatin1()) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1649
            byte[] val = this.value;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1650
            for (int i = off, j = count; i < end; i++) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1651
                char c = s[i];
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1652
                if (StringLatin1.canEncode(c)) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1653
                    val[j++] = (byte)c;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1654
                } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1655
                    count = j;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1656
                    inflate();
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1657
                    StringUTF16.putCharsSB(this.value, j, s, i, end);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1658
                    count += end - i;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1659
                    return;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1660
                }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1661
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1662
        } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1663
            StringUTF16.putCharsSB(this.value, count, s, off, end);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1664
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1665
        count += end - off;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1666
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1667
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1668
    private final void appendChars(CharSequence s, int off, int end) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1669
        if (isLatin1()) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1670
            byte[] val = this.value;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1671
            for (int i = off, j = count; i < end; i++) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1672
                char c = s.charAt(i);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1673
                if (StringLatin1.canEncode(c)) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1674
                    val[j++] = (byte)c;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1675
                } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1676
                    count = j;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1677
                    inflate();
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1678
                    StringUTF16.putCharsSB(this.value, j, s, i, end);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1679
                    count += end - i;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1680
                    return;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1681
                }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1682
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1683
        } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1684
            StringUTF16.putCharsSB(this.value, count, s, off, end);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1685
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1686
        count += end - off;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1687
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1688
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1689
    /* IndexOutOfBoundsException, if out of bounds */
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1690
    private static void checkRange(int start, int end, int len) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1691
        if (start < 0 || start > end || end > len) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1692
            throw new IndexOutOfBoundsException(
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1693
                "start " + start + ", end " + end + ", length " + len);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1694
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1695
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1696
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1697
    /* StringIndexOutOfBoundsException, if out of bounds */
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1698
    private static void checkRangeSIOOBE(int start, int end, int len) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1699
        if (start < 0 || start > end || end > len) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1700
            throw new StringIndexOutOfBoundsException(
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1701
                "start " + start + ", end " + end + ", length " + len);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1702
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 31471
diff changeset
  1703
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
}