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