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