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