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