1 |
758 |
jeremybenn |
/* AbstractStringBuffer.java -- Growable strings
|
2 |
|
|
Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2008
|
3 |
|
|
Free Software Foundation, Inc.
|
4 |
|
|
|
5 |
|
|
This file is part of GNU Classpath.
|
6 |
|
|
|
7 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
8 |
|
|
it under the terms of the GNU General Public License as published by
|
9 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
10 |
|
|
any later version.
|
11 |
|
|
|
12 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
13 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
15 |
|
|
General Public License for more details.
|
16 |
|
|
|
17 |
|
|
You should have received a copy of the GNU General Public License
|
18 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
19 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
20 |
|
|
02110-1301 USA.
|
21 |
|
|
|
22 |
|
|
Linking this library statically or dynamically with other modules is
|
23 |
|
|
making a combined work based on this library. Thus, the terms and
|
24 |
|
|
conditions of the GNU General Public License cover the whole
|
25 |
|
|
combination.
|
26 |
|
|
|
27 |
|
|
As a special exception, the copyright holders of this library give you
|
28 |
|
|
permission to link this library with independent modules to produce an
|
29 |
|
|
executable, regardless of the license terms of these independent
|
30 |
|
|
modules, and to copy and distribute the resulting executable under
|
31 |
|
|
terms of your choice, provided that you also meet, for each linked
|
32 |
|
|
independent module, the terms and conditions of the license of that
|
33 |
|
|
module. An independent module is a module which is not derived from
|
34 |
|
|
or based on this library. If you modify this library, you may extend
|
35 |
|
|
this exception to your version of the library, but you are not
|
36 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
37 |
|
|
exception statement from your version. */
|
38 |
|
|
|
39 |
|
|
package java.lang;
|
40 |
|
|
|
41 |
|
|
import java.io.Serializable;
|
42 |
|
|
|
43 |
|
|
/**
|
44 |
|
|
* This class is based on gnu.classpath.ClasspathStringBuffer but
|
45 |
|
|
* is package-private to java.lang so it can be used as the basis
|
46 |
|
|
* for StringBuffer and StringBuilder.
|
47 |
|
|
* If you modify this, please consider also modifying that code.
|
48 |
|
|
*/
|
49 |
|
|
abstract class AbstractStringBuffer
|
50 |
|
|
implements Serializable, CharSequence, Appendable
|
51 |
|
|
{
|
52 |
|
|
|
53 |
|
|
/**
|
54 |
|
|
* Index of next available character (and thus the size of the current
|
55 |
|
|
* string contents). Note that this has permissions set this way so that
|
56 |
|
|
* String can get the value.
|
57 |
|
|
*
|
58 |
|
|
* @serial the number of characters in the buffer
|
59 |
|
|
*/
|
60 |
|
|
int count;
|
61 |
|
|
|
62 |
|
|
/**
|
63 |
|
|
* The buffer. Note that this has permissions set this way so that String
|
64 |
|
|
* can get the value.
|
65 |
|
|
*
|
66 |
|
|
* @serial the buffer
|
67 |
|
|
*/
|
68 |
|
|
char[] value;
|
69 |
|
|
|
70 |
|
|
/**
|
71 |
|
|
* The default capacity of a buffer.
|
72 |
|
|
*/
|
73 |
|
|
private static final int DEFAULT_CAPACITY = 16;
|
74 |
|
|
|
75 |
|
|
/**
|
76 |
|
|
* Create a new AbstractStringBuffer with default capacity 16.
|
77 |
|
|
*/
|
78 |
|
|
AbstractStringBuffer()
|
79 |
|
|
{
|
80 |
|
|
this(DEFAULT_CAPACITY);
|
81 |
|
|
}
|
82 |
|
|
|
83 |
|
|
/**
|
84 |
|
|
* Create an empty <code>StringBuffer</code> with the specified initial
|
85 |
|
|
* capacity.
|
86 |
|
|
*
|
87 |
|
|
* @param capacity the initial capacity
|
88 |
|
|
* @throws NegativeArraySizeException if capacity is negative
|
89 |
|
|
*/
|
90 |
|
|
AbstractStringBuffer(int capacity)
|
91 |
|
|
{
|
92 |
|
|
value = new char[capacity];
|
93 |
|
|
}
|
94 |
|
|
|
95 |
|
|
/**
|
96 |
|
|
* Create a new <code>StringBuffer</code> with the characters in the
|
97 |
|
|
* specified <code>String</code>. Initial capacity will be the size of the
|
98 |
|
|
* String plus 16.
|
99 |
|
|
*
|
100 |
|
|
* @param str the <code>String</code> to convert
|
101 |
|
|
* @throws NullPointerException if str is null
|
102 |
|
|
*/
|
103 |
|
|
AbstractStringBuffer(String str)
|
104 |
|
|
{
|
105 |
|
|
count = str.count;
|
106 |
|
|
value = new char[count + DEFAULT_CAPACITY];
|
107 |
|
|
str.getChars(0, count, value, 0);
|
108 |
|
|
}
|
109 |
|
|
|
110 |
|
|
/**
|
111 |
|
|
* Create a new <code>StringBuffer</code> with the characters in the
|
112 |
|
|
* specified <code>CharSequence</code>. Initial capacity will be the
|
113 |
|
|
* length of the sequence plus 16; if the sequence reports a length
|
114 |
|
|
* less than or equal to 0, then the initial capacity will be 16.
|
115 |
|
|
*
|
116 |
|
|
* @param seq the initializing <code>CharSequence</code>
|
117 |
|
|
* @throws NullPointerException if str is null
|
118 |
|
|
* @since 1.5
|
119 |
|
|
*/
|
120 |
|
|
AbstractStringBuffer(CharSequence seq)
|
121 |
|
|
{
|
122 |
|
|
int len = seq.length();
|
123 |
|
|
count = len <= 0 ? 0 : len;
|
124 |
|
|
value = new char[count + DEFAULT_CAPACITY];
|
125 |
|
|
for (int i = 0; i < len; ++i)
|
126 |
|
|
value[i] = seq.charAt(i);
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
/**
|
130 |
|
|
* Increase the capacity of this <code>StringBuffer</code>. This will
|
131 |
|
|
* ensure that an expensive growing operation will not occur until
|
132 |
|
|
* <code>minimumCapacity</code> is reached. The buffer is grown to the
|
133 |
|
|
* larger of <code>minimumCapacity</code> and
|
134 |
|
|
* <code>capacity() * 2 + 2</code>, if it is not already large enough.
|
135 |
|
|
*
|
136 |
|
|
* @param minimumCapacity the new capacity
|
137 |
|
|
* @see #capacity()
|
138 |
|
|
*/
|
139 |
|
|
public void ensureCapacity(int minimumCapacity)
|
140 |
|
|
{
|
141 |
|
|
ensureCapacity_unsynchronized(minimumCapacity);
|
142 |
|
|
}
|
143 |
|
|
|
144 |
|
|
/**
|
145 |
|
|
* Set the length of this StringBuffer. If the new length is greater than
|
146 |
|
|
* the current length, all the new characters are set to '\0'. If the new
|
147 |
|
|
* length is less than the current length, the first <code>newLength</code>
|
148 |
|
|
* characters of the old array will be preserved, and the remaining
|
149 |
|
|
* characters are truncated.
|
150 |
|
|
*
|
151 |
|
|
* @param newLength the new length
|
152 |
|
|
* @throws IndexOutOfBoundsException if the new length is negative
|
153 |
|
|
* (while unspecified, this is a StringIndexOutOfBoundsException)
|
154 |
|
|
* @see #length()
|
155 |
|
|
*/
|
156 |
|
|
public void setLength(int newLength)
|
157 |
|
|
{
|
158 |
|
|
if (newLength < 0)
|
159 |
|
|
throw new StringIndexOutOfBoundsException(newLength);
|
160 |
|
|
|
161 |
|
|
int valueLength = value.length;
|
162 |
|
|
|
163 |
|
|
/* Always call ensureCapacity_unsynchronized in order to preserve
|
164 |
|
|
copy-on-write semantics. */
|
165 |
|
|
ensureCapacity_unsynchronized(newLength);
|
166 |
|
|
|
167 |
|
|
if (newLength < valueLength)
|
168 |
|
|
{
|
169 |
|
|
/* If the StringBuffer's value just grew, then we know that
|
170 |
|
|
value is newly allocated and the region between count and
|
171 |
|
|
newLength is filled with '\0'. */
|
172 |
|
|
count = newLength;
|
173 |
|
|
}
|
174 |
|
|
else
|
175 |
|
|
{
|
176 |
|
|
/* The StringBuffer's value doesn't need to grow. However,
|
177 |
|
|
we should clear out any cruft that may exist. */
|
178 |
|
|
while (count < newLength)
|
179 |
|
|
value[count++] = '\0';
|
180 |
|
|
}
|
181 |
|
|
}
|
182 |
|
|
|
183 |
|
|
/**
|
184 |
|
|
* Get the character at the specified index.
|
185 |
|
|
*
|
186 |
|
|
* @param index the index of the character to get, starting at 0
|
187 |
|
|
* @return the character at the specified index
|
188 |
|
|
* @throws IndexOutOfBoundsException if index is negative or >= length()
|
189 |
|
|
* (while unspecified, this is a StringIndexOutOfBoundsException)
|
190 |
|
|
*/
|
191 |
|
|
public char charAt(int index)
|
192 |
|
|
{
|
193 |
|
|
if (index < 0 || index >= count)
|
194 |
|
|
throw new StringIndexOutOfBoundsException(index);
|
195 |
|
|
return value[index];
|
196 |
|
|
}
|
197 |
|
|
|
198 |
|
|
/**
|
199 |
|
|
* Get the code point at the specified index. This is like #charAt(int),
|
200 |
|
|
* but if the character is the start of a surrogate pair, and the
|
201 |
|
|
* following character completes the pair, then the corresponding
|
202 |
|
|
* supplementary code point is returned.
|
203 |
|
|
* @param index the index of the codepoint to get, starting at 0
|
204 |
|
|
* @return the codepoint at the specified index
|
205 |
|
|
* @throws IndexOutOfBoundsException if index is negative or >= length()
|
206 |
|
|
* @since 1.5
|
207 |
|
|
*/
|
208 |
|
|
public int codePointAt(int index)
|
209 |
|
|
{
|
210 |
|
|
return Character.codePointAt(value, index, count);
|
211 |
|
|
}
|
212 |
|
|
|
213 |
|
|
/**
|
214 |
|
|
* Get the code point before the specified index. This is like
|
215 |
|
|
* #codePointAt(int), but checks the characters at <code>index-1</code> and
|
216 |
|
|
* <code>index-2</code> to see if they form a supplementary code point.
|
217 |
|
|
* @param index the index just past the codepoint to get, starting at 0
|
218 |
|
|
* @return the codepoint at the specified index
|
219 |
|
|
* @throws IndexOutOfBoundsException if index is negative or >= length()
|
220 |
|
|
* @since 1.5
|
221 |
|
|
*/
|
222 |
|
|
public int codePointBefore(int index)
|
223 |
|
|
{
|
224 |
|
|
// Character.codePointBefore() doesn't perform this check. We
|
225 |
|
|
// could use the CharSequence overload, but this is just as easy.
|
226 |
|
|
if (index >= count)
|
227 |
|
|
throw new IndexOutOfBoundsException();
|
228 |
|
|
return Character.codePointBefore(value, index, 1);
|
229 |
|
|
}
|
230 |
|
|
|
231 |
|
|
/**
|
232 |
|
|
* Get the specified array of characters. <code>srcOffset - srcEnd</code>
|
233 |
|
|
* characters will be copied into the array you pass in.
|
234 |
|
|
*
|
235 |
|
|
* @param srcOffset the index to start copying from (inclusive)
|
236 |
|
|
* @param srcEnd the index to stop copying from (exclusive)
|
237 |
|
|
* @param dst the array to copy into
|
238 |
|
|
* @param dstOffset the index to start copying into
|
239 |
|
|
* @throws NullPointerException if dst is null
|
240 |
|
|
* @throws IndexOutOfBoundsException if any source or target indices are
|
241 |
|
|
* out of range (while unspecified, source problems cause a
|
242 |
|
|
* StringIndexOutOfBoundsException, and dest problems cause an
|
243 |
|
|
* ArrayIndexOutOfBoundsException)
|
244 |
|
|
* @see System#arraycopy(Object, int, Object, int, int)
|
245 |
|
|
*/
|
246 |
|
|
public void getChars(int srcOffset, int srcEnd,
|
247 |
|
|
char[] dst, int dstOffset)
|
248 |
|
|
{
|
249 |
|
|
if (srcOffset < 0 || srcEnd > count || srcEnd < srcOffset)
|
250 |
|
|
throw new StringIndexOutOfBoundsException();
|
251 |
|
|
System.arraycopy(value, srcOffset, dst, dstOffset, srcEnd - srcOffset);
|
252 |
|
|
}
|
253 |
|
|
|
254 |
|
|
/**
|
255 |
|
|
* Set the character at the specified index.
|
256 |
|
|
*
|
257 |
|
|
* @param index the index of the character to set starting at 0
|
258 |
|
|
* @param ch the value to set that character to
|
259 |
|
|
* @throws IndexOutOfBoundsException if index is negative or >= length()
|
260 |
|
|
* (while unspecified, this is a StringIndexOutOfBoundsException)
|
261 |
|
|
*/
|
262 |
|
|
public void setCharAt(int index, char ch)
|
263 |
|
|
{
|
264 |
|
|
if (index < 0 || index >= count)
|
265 |
|
|
throw new StringIndexOutOfBoundsException(index);
|
266 |
|
|
// Call ensureCapacity to enforce copy-on-write.
|
267 |
|
|
ensureCapacity_unsynchronized(count);
|
268 |
|
|
value[index] = ch;
|
269 |
|
|
}
|
270 |
|
|
|
271 |
|
|
/**
|
272 |
|
|
* Append the <code>String</code> value of the argument to this
|
273 |
|
|
* <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
|
274 |
|
|
* to <code>String</code>.
|
275 |
|
|
*
|
276 |
|
|
* @param obj the <code>Object</code> to convert and append
|
277 |
|
|
* @return this <code>StringBuffer</code>
|
278 |
|
|
* @see String#valueOf(Object)
|
279 |
|
|
* @see #append(String)
|
280 |
|
|
*/
|
281 |
|
|
public AbstractStringBuffer append(Object obj)
|
282 |
|
|
{
|
283 |
|
|
return append(String.valueOf(obj));
|
284 |
|
|
}
|
285 |
|
|
|
286 |
|
|
/**
|
287 |
|
|
* Append the <code>String</code> to this <code>StringBuffer</code>. If
|
288 |
|
|
* str is null, the String "null" is appended.
|
289 |
|
|
*
|
290 |
|
|
* @param str the <code>String</code> to append
|
291 |
|
|
* @return this <code>StringBuffer</code>
|
292 |
|
|
*/
|
293 |
|
|
public AbstractStringBuffer append(String str)
|
294 |
|
|
{
|
295 |
|
|
if (str == null)
|
296 |
|
|
str = "null";
|
297 |
|
|
int len = str.count;
|
298 |
|
|
ensureCapacity_unsynchronized(count + len);
|
299 |
|
|
str.getChars(0, len, value, count);
|
300 |
|
|
count += len;
|
301 |
|
|
return this;
|
302 |
|
|
}
|
303 |
|
|
|
304 |
|
|
/**
|
305 |
|
|
* Append the <code>StringBuilder</code> value of the argument to this
|
306 |
|
|
* <code>StringBuilder</code>. This behaves the same as
|
307 |
|
|
* <code>append((Object) stringBuffer)</code>, except it is more efficient.
|
308 |
|
|
*
|
309 |
|
|
* @param stringBuffer the <code>StringBuilder</code> to convert and append
|
310 |
|
|
* @return this <code>StringBuilder</code>
|
311 |
|
|
* @see #append(Object)
|
312 |
|
|
*/
|
313 |
|
|
public AbstractStringBuffer append(StringBuffer stringBuffer)
|
314 |
|
|
{
|
315 |
|
|
if (stringBuffer == null)
|
316 |
|
|
return append("null");
|
317 |
|
|
synchronized (stringBuffer)
|
318 |
|
|
{
|
319 |
|
|
int len = stringBuffer.count;
|
320 |
|
|
ensureCapacity(count + len);
|
321 |
|
|
System.arraycopy(stringBuffer.value, 0, value, count, len);
|
322 |
|
|
count += len;
|
323 |
|
|
}
|
324 |
|
|
return this;
|
325 |
|
|
}
|
326 |
|
|
|
327 |
|
|
/**
|
328 |
|
|
* Append the <code>char</code> array to this <code>StringBuffer</code>.
|
329 |
|
|
* This is similar (but more efficient) than
|
330 |
|
|
* <code>append(new String(data))</code>, except in the case of null.
|
331 |
|
|
*
|
332 |
|
|
* @param data the <code>char[]</code> to append
|
333 |
|
|
* @return this <code>StringBuffer</code>
|
334 |
|
|
* @throws NullPointerException if <code>str</code> is <code>null</code>
|
335 |
|
|
* @see #append(char[], int, int)
|
336 |
|
|
*/
|
337 |
|
|
public AbstractStringBuffer append(char[] data)
|
338 |
|
|
{
|
339 |
|
|
return append(data, 0, data.length);
|
340 |
|
|
}
|
341 |
|
|
|
342 |
|
|
/**
|
343 |
|
|
* Append part of the <code>char</code> array to this
|
344 |
|
|
* <code>StringBuffer</code>. This is similar (but more efficient) than
|
345 |
|
|
* <code>append(new String(data, offset, count))</code>, except in the case
|
346 |
|
|
* of null.
|
347 |
|
|
*
|
348 |
|
|
* @param data the <code>char[]</code> to append
|
349 |
|
|
* @param offset the start location in <code>str</code>
|
350 |
|
|
* @param count the number of characters to get from <code>str</code>
|
351 |
|
|
* @return this <code>StringBuffer</code>
|
352 |
|
|
* @throws NullPointerException if <code>str</code> is <code>null</code>
|
353 |
|
|
* @throws IndexOutOfBoundsException if offset or count is out of range
|
354 |
|
|
* (while unspecified, this is a StringIndexOutOfBoundsException)
|
355 |
|
|
*/
|
356 |
|
|
public AbstractStringBuffer append(char[] data, int offset, int count)
|
357 |
|
|
{
|
358 |
|
|
if (offset < 0 || count < 0 || offset > data.length - count)
|
359 |
|
|
throw new StringIndexOutOfBoundsException();
|
360 |
|
|
ensureCapacity_unsynchronized(this.count + count);
|
361 |
|
|
System.arraycopy(data, offset, value, this.count, count);
|
362 |
|
|
this.count += count;
|
363 |
|
|
return this;
|
364 |
|
|
}
|
365 |
|
|
|
366 |
|
|
/**
|
367 |
|
|
* Append the <code>String</code> value of the argument to this
|
368 |
|
|
* <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
|
369 |
|
|
* to <code>String</code>.
|
370 |
|
|
*
|
371 |
|
|
* @param bool the <code>boolean</code> to convert and append
|
372 |
|
|
* @return this <code>StringBuffer</code>
|
373 |
|
|
* @see String#valueOf(boolean)
|
374 |
|
|
*/
|
375 |
|
|
public AbstractStringBuffer append(boolean bool)
|
376 |
|
|
{
|
377 |
|
|
return append(bool ? "true" : "false");
|
378 |
|
|
}
|
379 |
|
|
|
380 |
|
|
/**
|
381 |
|
|
* Append the <code>char</code> to this <code>StringBuffer</code>.
|
382 |
|
|
*
|
383 |
|
|
* @param ch the <code>char</code> to append
|
384 |
|
|
* @return this <code>StringBuffer</code>
|
385 |
|
|
*/
|
386 |
|
|
public AbstractStringBuffer append(char ch)
|
387 |
|
|
{
|
388 |
|
|
ensureCapacity_unsynchronized(count + 1);
|
389 |
|
|
value[count++] = ch;
|
390 |
|
|
return this;
|
391 |
|
|
}
|
392 |
|
|
|
393 |
|
|
/**
|
394 |
|
|
* Append the characters in the <code>CharSequence</code> to this
|
395 |
|
|
* buffer.
|
396 |
|
|
*
|
397 |
|
|
* @param seq the <code>CharSequence</code> providing the characters
|
398 |
|
|
* @return this <code>StringBuffer</code>
|
399 |
|
|
* @since 1.5
|
400 |
|
|
*/
|
401 |
|
|
public AbstractStringBuffer append(CharSequence seq)
|
402 |
|
|
{
|
403 |
|
|
return append(seq, 0, seq.length());
|
404 |
|
|
}
|
405 |
|
|
|
406 |
|
|
/**
|
407 |
|
|
* Append some characters from the <code>CharSequence</code> to this
|
408 |
|
|
* buffer. If the argument is null, the <code>seq</code> is assumed
|
409 |
|
|
* to be equal to the string <code>"null"</code>.
|
410 |
|
|
*
|
411 |
|
|
* @param seq the <code>CharSequence</code> providing the characters
|
412 |
|
|
* @param start the starting index
|
413 |
|
|
* @param end one past the final index
|
414 |
|
|
* @return this <code>StringBuffer</code>
|
415 |
|
|
* @since 1.5
|
416 |
|
|
*/
|
417 |
|
|
public AbstractStringBuffer append(CharSequence seq, int start, int end)
|
418 |
|
|
{
|
419 |
|
|
if (seq == null)
|
420 |
|
|
seq = "null";
|
421 |
|
|
if (end - start > 0)
|
422 |
|
|
{
|
423 |
|
|
ensureCapacity_unsynchronized(count + end - start);
|
424 |
|
|
for (; start < end; ++start)
|
425 |
|
|
value[count++] = seq.charAt(start);
|
426 |
|
|
}
|
427 |
|
|
return this;
|
428 |
|
|
}
|
429 |
|
|
|
430 |
|
|
/**
|
431 |
|
|
* Append the <code>String</code> value of the argument to this
|
432 |
|
|
* <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
|
433 |
|
|
* to <code>String</code>.
|
434 |
|
|
*
|
435 |
|
|
* @param inum the <code>int</code> to convert and append
|
436 |
|
|
* @return this <code>StringBuffer</code>
|
437 |
|
|
* @see String#valueOf(int)
|
438 |
|
|
*/
|
439 |
|
|
// GCJ LOCAL: this is native for efficiency.
|
440 |
|
|
public native AbstractStringBuffer append (int inum);
|
441 |
|
|
|
442 |
|
|
/**
|
443 |
|
|
* Append the <code>String</code> value of the argument to this
|
444 |
|
|
* <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
|
445 |
|
|
* to <code>String</code>.
|
446 |
|
|
*
|
447 |
|
|
* @param lnum the <code>long</code> to convert and append
|
448 |
|
|
* @return this <code>StringBuffer</code>
|
449 |
|
|
* @see String#valueOf(long)
|
450 |
|
|
*/
|
451 |
|
|
public AbstractStringBuffer append(long lnum)
|
452 |
|
|
{
|
453 |
|
|
return append(Long.toString(lnum, 10));
|
454 |
|
|
}
|
455 |
|
|
|
456 |
|
|
/**
|
457 |
|
|
* Append the <code>String</code> value of the argument to this
|
458 |
|
|
* <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
|
459 |
|
|
* to <code>String</code>.
|
460 |
|
|
*
|
461 |
|
|
* @param fnum the <code>float</code> to convert and append
|
462 |
|
|
* @return this <code>StringBuffer</code>
|
463 |
|
|
* @see String#valueOf(float)
|
464 |
|
|
*/
|
465 |
|
|
public AbstractStringBuffer append(float fnum)
|
466 |
|
|
{
|
467 |
|
|
return append(Float.toString(fnum));
|
468 |
|
|
}
|
469 |
|
|
|
470 |
|
|
/**
|
471 |
|
|
* Append the <code>String</code> value of the argument to this
|
472 |
|
|
* <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
|
473 |
|
|
* to <code>String</code>.
|
474 |
|
|
*
|
475 |
|
|
* @param dnum the <code>double</code> to convert and append
|
476 |
|
|
* @return this <code>StringBuffer</code>
|
477 |
|
|
* @see String#valueOf(double)
|
478 |
|
|
*/
|
479 |
|
|
public AbstractStringBuffer append(double dnum)
|
480 |
|
|
{
|
481 |
|
|
return append(Double.toString(dnum));
|
482 |
|
|
}
|
483 |
|
|
|
484 |
|
|
/**
|
485 |
|
|
* Append the code point to this <code>StringBuffer</code>.
|
486 |
|
|
* This is like #append(char), but will append two characters
|
487 |
|
|
* if a supplementary code point is given.
|
488 |
|
|
*
|
489 |
|
|
* @param code the code point to append
|
490 |
|
|
* @return this <code>StringBuffer</code>
|
491 |
|
|
* @see Character#toChars(int, char[], int)
|
492 |
|
|
* @since 1.5
|
493 |
|
|
*/
|
494 |
|
|
public AbstractStringBuffer appendCodePoint(int code)
|
495 |
|
|
{
|
496 |
|
|
int len = Character.charCount(code);
|
497 |
|
|
ensureCapacity_unsynchronized(count + len);
|
498 |
|
|
Character.toChars(code, value, count);
|
499 |
|
|
count += len;
|
500 |
|
|
return this;
|
501 |
|
|
}
|
502 |
|
|
|
503 |
|
|
/**
|
504 |
|
|
* Delete characters from this <code>StringBuffer</code>.
|
505 |
|
|
* <code>delete(10, 12)</code> will delete 10 and 11, but not 12. It is
|
506 |
|
|
* harmless for end to be larger than length().
|
507 |
|
|
*
|
508 |
|
|
* @param start the first character to delete
|
509 |
|
|
* @param end the index after the last character to delete
|
510 |
|
|
* @return this <code>StringBuffer</code>
|
511 |
|
|
* @throws StringIndexOutOfBoundsException if start or end are out of bounds
|
512 |
|
|
* @since 1.2
|
513 |
|
|
*/
|
514 |
|
|
public AbstractStringBuffer delete(int start, int end)
|
515 |
|
|
{
|
516 |
|
|
if (start < 0 || start > count || start > end)
|
517 |
|
|
throw new StringIndexOutOfBoundsException(start);
|
518 |
|
|
if (end > count)
|
519 |
|
|
end = count;
|
520 |
|
|
ensureCapacity_unsynchronized(count);
|
521 |
|
|
if (count - end != 0)
|
522 |
|
|
System.arraycopy(value, end, value, start, count - end);
|
523 |
|
|
count -= end - start;
|
524 |
|
|
return this;
|
525 |
|
|
}
|
526 |
|
|
|
527 |
|
|
/**
|
528 |
|
|
* Delete a character from this <code>StringBuffer</code>.
|
529 |
|
|
*
|
530 |
|
|
* @param index the index of the character to delete
|
531 |
|
|
* @return this <code>StringBuffer</code>
|
532 |
|
|
* @throws StringIndexOutOfBoundsException if index is out of bounds
|
533 |
|
|
* @since 1.2
|
534 |
|
|
*/
|
535 |
|
|
public AbstractStringBuffer deleteCharAt(int index)
|
536 |
|
|
{
|
537 |
|
|
return delete(index, index + 1);
|
538 |
|
|
}
|
539 |
|
|
|
540 |
|
|
/**
|
541 |
|
|
* Replace characters between index <code>start</code> (inclusive) and
|
542 |
|
|
* <code>end</code> (exclusive) with <code>str</code>. If <code>end</code>
|
543 |
|
|
* is larger than the size of this StringBuffer, all characters after
|
544 |
|
|
* <code>start</code> are replaced.
|
545 |
|
|
*
|
546 |
|
|
* @param start the beginning index of characters to delete (inclusive)
|
547 |
|
|
* @param end the ending index of characters to delete (exclusive)
|
548 |
|
|
* @param str the new <code>String</code> to insert
|
549 |
|
|
* @return this <code>StringBuffer</code>
|
550 |
|
|
* @throws StringIndexOutOfBoundsException if start or end are out of bounds
|
551 |
|
|
* @throws NullPointerException if str is null
|
552 |
|
|
* @since 1.2
|
553 |
|
|
*/
|
554 |
|
|
public AbstractStringBuffer replace(int start, int end, String str)
|
555 |
|
|
{
|
556 |
|
|
if (start < 0 || start > count || start > end)
|
557 |
|
|
throw new StringIndexOutOfBoundsException(start);
|
558 |
|
|
|
559 |
|
|
int len = str.count;
|
560 |
|
|
// Calculate the difference in 'count' after the replace.
|
561 |
|
|
int delta = len - (end > count ? count : end) + start;
|
562 |
|
|
ensureCapacity_unsynchronized(count + delta);
|
563 |
|
|
|
564 |
|
|
if (delta != 0 && end < count)
|
565 |
|
|
System.arraycopy(value, end, value, end + delta, count - end);
|
566 |
|
|
|
567 |
|
|
str.getChars(0, len, value, start);
|
568 |
|
|
count += delta;
|
569 |
|
|
return this;
|
570 |
|
|
}
|
571 |
|
|
|
572 |
|
|
/**
|
573 |
|
|
* Insert a subarray of the <code>char[]</code> argument into this
|
574 |
|
|
* <code>StringBuffer</code>.
|
575 |
|
|
*
|
576 |
|
|
* @param offset the place to insert in this buffer
|
577 |
|
|
* @param str the <code>char[]</code> to insert
|
578 |
|
|
* @param str_offset the index in <code>str</code> to start inserting from
|
579 |
|
|
* @param len the number of characters to insert
|
580 |
|
|
* @return this <code>StringBuffer</code>
|
581 |
|
|
* @throws NullPointerException if <code>str</code> is <code>null</code>
|
582 |
|
|
* @throws StringIndexOutOfBoundsException if any index is out of bounds
|
583 |
|
|
* @since 1.2
|
584 |
|
|
*/
|
585 |
|
|
public AbstractStringBuffer insert(int offset, char[] str, int str_offset, int len)
|
586 |
|
|
{
|
587 |
|
|
if (offset < 0 || offset > count || len < 0
|
588 |
|
|
|| str_offset < 0 || str_offset > str.length - len)
|
589 |
|
|
throw new StringIndexOutOfBoundsException();
|
590 |
|
|
ensureCapacity_unsynchronized(count + len);
|
591 |
|
|
System.arraycopy(value, offset, value, offset + len, count - offset);
|
592 |
|
|
System.arraycopy(str, str_offset, value, offset, len);
|
593 |
|
|
count += len;
|
594 |
|
|
return this;
|
595 |
|
|
}
|
596 |
|
|
|
597 |
|
|
/**
|
598 |
|
|
* Insert the <code>String</code> value of the argument into this
|
599 |
|
|
* <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
|
600 |
|
|
* to <code>String</code>.
|
601 |
|
|
*
|
602 |
|
|
* @param offset the place to insert in this buffer
|
603 |
|
|
* @param obj the <code>Object</code> to convert and insert
|
604 |
|
|
* @return this <code>StringBuffer</code>
|
605 |
|
|
* @exception StringIndexOutOfBoundsException if offset is out of bounds
|
606 |
|
|
* @see String#valueOf(Object)
|
607 |
|
|
*/
|
608 |
|
|
public AbstractStringBuffer insert(int offset, Object obj)
|
609 |
|
|
{
|
610 |
|
|
return insert(offset, obj == null ? "null" : obj.toString());
|
611 |
|
|
}
|
612 |
|
|
|
613 |
|
|
/**
|
614 |
|
|
* Insert the <code>String</code> argument into this
|
615 |
|
|
* <code>StringBuffer</code>. If str is null, the String "null" is used
|
616 |
|
|
* instead.
|
617 |
|
|
*
|
618 |
|
|
* @param offset the place to insert in this buffer
|
619 |
|
|
* @param str the <code>String</code> to insert
|
620 |
|
|
* @return this <code>StringBuffer</code>
|
621 |
|
|
* @throws StringIndexOutOfBoundsException if offset is out of bounds
|
622 |
|
|
*/
|
623 |
|
|
public AbstractStringBuffer insert(int offset, String str)
|
624 |
|
|
{
|
625 |
|
|
if (offset < 0 || offset > count)
|
626 |
|
|
throw new StringIndexOutOfBoundsException(offset);
|
627 |
|
|
if (str == null)
|
628 |
|
|
str = "null";
|
629 |
|
|
int len = str.count;
|
630 |
|
|
ensureCapacity_unsynchronized(count + len);
|
631 |
|
|
System.arraycopy(value, offset, value, offset + len, count - offset);
|
632 |
|
|
str.getChars(0, len, value, offset);
|
633 |
|
|
count += len;
|
634 |
|
|
return this;
|
635 |
|
|
}
|
636 |
|
|
|
637 |
|
|
/**
|
638 |
|
|
* Insert the <code>CharSequence</code> argument into this
|
639 |
|
|
* <code>StringBuffer</code>. If the sequence is null, the String
|
640 |
|
|
* "null" is used instead.
|
641 |
|
|
*
|
642 |
|
|
* @param offset the place to insert in this buffer
|
643 |
|
|
* @param sequence the <code>CharSequence</code> to insert
|
644 |
|
|
* @return this <code>StringBuffer</code>
|
645 |
|
|
* @throws IndexOutOfBoundsException if offset is out of bounds
|
646 |
|
|
* @since 1.5
|
647 |
|
|
*/
|
648 |
|
|
public AbstractStringBuffer insert(int offset, CharSequence sequence)
|
649 |
|
|
{
|
650 |
|
|
if (sequence == null)
|
651 |
|
|
sequence = "null";
|
652 |
|
|
return insert(offset, sequence, 0, sequence.length());
|
653 |
|
|
}
|
654 |
|
|
|
655 |
|
|
/**
|
656 |
|
|
* Insert a subsequence of the <code>CharSequence</code> argument into this
|
657 |
|
|
* <code>StringBuffer</code>. If the sequence is null, the String
|
658 |
|
|
* "null" is used instead.
|
659 |
|
|
*
|
660 |
|
|
* @param offset the place to insert in this buffer
|
661 |
|
|
* @param sequence the <code>CharSequence</code> to insert
|
662 |
|
|
* @param start the starting index of the subsequence
|
663 |
|
|
* @param end one past the ending index of the subsequence
|
664 |
|
|
* @return this <code>StringBuffer</code>
|
665 |
|
|
* @throws IndexOutOfBoundsException if offset, start,
|
666 |
|
|
* or end are out of bounds
|
667 |
|
|
* @since 1.5
|
668 |
|
|
*/
|
669 |
|
|
public AbstractStringBuffer insert(int offset, CharSequence sequence, int start, int end)
|
670 |
|
|
{
|
671 |
|
|
if (sequence == null)
|
672 |
|
|
sequence = "null";
|
673 |
|
|
if (start < 0 || end < 0 || start > end || end > sequence.length())
|
674 |
|
|
throw new IndexOutOfBoundsException();
|
675 |
|
|
int len = end - start;
|
676 |
|
|
ensureCapacity_unsynchronized(count + len);
|
677 |
|
|
System.arraycopy(value, offset, value, offset + len, count - offset);
|
678 |
|
|
for (int i = start; i < end; ++i)
|
679 |
|
|
value[offset++] = sequence.charAt(i);
|
680 |
|
|
count += len;
|
681 |
|
|
return this;
|
682 |
|
|
}
|
683 |
|
|
|
684 |
|
|
/**
|
685 |
|
|
* Insert the <code>char[]</code> argument into this
|
686 |
|
|
* <code>StringBuffer</code>.
|
687 |
|
|
*
|
688 |
|
|
* @param offset the place to insert in this buffer
|
689 |
|
|
* @param data the <code>char[]</code> to insert
|
690 |
|
|
* @return this <code>StringBuffer</code>
|
691 |
|
|
* @throws NullPointerException if <code>data</code> is <code>null</code>
|
692 |
|
|
* @throws StringIndexOutOfBoundsException if offset is out of bounds
|
693 |
|
|
* @see #insert(int, char[], int, int)
|
694 |
|
|
*/
|
695 |
|
|
public AbstractStringBuffer insert(int offset, char[] data)
|
696 |
|
|
{
|
697 |
|
|
return insert(offset, data, 0, data.length);
|
698 |
|
|
}
|
699 |
|
|
|
700 |
|
|
/**
|
701 |
|
|
* Insert the <code>String</code> value of the argument into this
|
702 |
|
|
* <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
|
703 |
|
|
* to <code>String</code>.
|
704 |
|
|
*
|
705 |
|
|
* @param offset the place to insert in this buffer
|
706 |
|
|
* @param bool the <code>boolean</code> to convert and insert
|
707 |
|
|
* @return this <code>StringBuffer</code>
|
708 |
|
|
* @throws StringIndexOutOfBoundsException if offset is out of bounds
|
709 |
|
|
* @see String#valueOf(boolean)
|
710 |
|
|
*/
|
711 |
|
|
public AbstractStringBuffer insert(int offset, boolean bool)
|
712 |
|
|
{
|
713 |
|
|
return insert(offset, bool ? "true" : "false");
|
714 |
|
|
}
|
715 |
|
|
|
716 |
|
|
/**
|
717 |
|
|
* Insert the <code>char</code> argument into this <code>StringBuffer</code>.
|
718 |
|
|
*
|
719 |
|
|
* @param offset the place to insert in this buffer
|
720 |
|
|
* @param ch the <code>char</code> to insert
|
721 |
|
|
* @return this <code>StringBuffer</code>
|
722 |
|
|
* @throws StringIndexOutOfBoundsException if offset is out of bounds
|
723 |
|
|
*/
|
724 |
|
|
public AbstractStringBuffer insert(int offset, char ch)
|
725 |
|
|
{
|
726 |
|
|
if (offset < 0 || offset > count)
|
727 |
|
|
throw new StringIndexOutOfBoundsException(offset);
|
728 |
|
|
ensureCapacity_unsynchronized(count + 1);
|
729 |
|
|
System.arraycopy(value, offset, value, offset + 1, count - offset);
|
730 |
|
|
value[offset] = ch;
|
731 |
|
|
count++;
|
732 |
|
|
return this;
|
733 |
|
|
}
|
734 |
|
|
|
735 |
|
|
/**
|
736 |
|
|
* Insert the <code>String</code> value of the argument into this
|
737 |
|
|
* <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
|
738 |
|
|
* to <code>String</code>.
|
739 |
|
|
*
|
740 |
|
|
* @param offset the place to insert in this buffer
|
741 |
|
|
* @param inum the <code>int</code> to convert and insert
|
742 |
|
|
* @return this <code>StringBuffer</code>
|
743 |
|
|
* @throws StringIndexOutOfBoundsException if offset is out of bounds
|
744 |
|
|
* @see String#valueOf(int)
|
745 |
|
|
*/
|
746 |
|
|
public AbstractStringBuffer insert(int offset, int inum)
|
747 |
|
|
{
|
748 |
|
|
return insert(offset, String.valueOf(inum));
|
749 |
|
|
}
|
750 |
|
|
|
751 |
|
|
/**
|
752 |
|
|
* Insert the <code>String</code> value of the argument into this
|
753 |
|
|
* <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
|
754 |
|
|
* to <code>String</code>.
|
755 |
|
|
*
|
756 |
|
|
* @param offset the place to insert in this buffer
|
757 |
|
|
* @param lnum the <code>long</code> to convert and insert
|
758 |
|
|
* @return this <code>StringBuffer</code>
|
759 |
|
|
* @throws StringIndexOutOfBoundsException if offset is out of bounds
|
760 |
|
|
* @see String#valueOf(long)
|
761 |
|
|
*/
|
762 |
|
|
public AbstractStringBuffer insert(int offset, long lnum)
|
763 |
|
|
{
|
764 |
|
|
return insert(offset, Long.toString(lnum, 10));
|
765 |
|
|
}
|
766 |
|
|
|
767 |
|
|
/**
|
768 |
|
|
* Insert the <code>String</code> value of the argument into this
|
769 |
|
|
* <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
|
770 |
|
|
* to <code>String</code>.
|
771 |
|
|
*
|
772 |
|
|
* @param offset the place to insert in this buffer
|
773 |
|
|
* @param fnum the <code>float</code> to convert and insert
|
774 |
|
|
* @return this <code>StringBuffer</code>
|
775 |
|
|
* @throws StringIndexOutOfBoundsException if offset is out of bounds
|
776 |
|
|
* @see String#valueOf(float)
|
777 |
|
|
*/
|
778 |
|
|
public AbstractStringBuffer insert(int offset, float fnum)
|
779 |
|
|
{
|
780 |
|
|
return insert(offset, Float.toString(fnum));
|
781 |
|
|
}
|
782 |
|
|
|
783 |
|
|
/**
|
784 |
|
|
* Insert the <code>String</code> value of the argument into this
|
785 |
|
|
* <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
|
786 |
|
|
* to <code>String</code>.
|
787 |
|
|
*
|
788 |
|
|
* @param offset the place to insert in this buffer
|
789 |
|
|
* @param dnum the <code>double</code> to convert and insert
|
790 |
|
|
* @return this <code>StringBuffer</code>
|
791 |
|
|
* @throws StringIndexOutOfBoundsException if offset is out of bounds
|
792 |
|
|
* @see String#valueOf(double)
|
793 |
|
|
*/
|
794 |
|
|
public AbstractStringBuffer insert(int offset, double dnum)
|
795 |
|
|
{
|
796 |
|
|
return insert(offset, Double.toString(dnum));
|
797 |
|
|
}
|
798 |
|
|
|
799 |
|
|
/**
|
800 |
|
|
* Finds the first instance of a substring in this StringBuilder.
|
801 |
|
|
*
|
802 |
|
|
* @param str String to find
|
803 |
|
|
* @return location (base 0) of the String, or -1 if not found
|
804 |
|
|
* @throws NullPointerException if str is null
|
805 |
|
|
* @see #indexOf(String, int)
|
806 |
|
|
*/
|
807 |
|
|
public int indexOf(String str)
|
808 |
|
|
{
|
809 |
|
|
return indexOf(str, 0);
|
810 |
|
|
}
|
811 |
|
|
|
812 |
|
|
/**
|
813 |
|
|
* Finds the first instance of a String in this StringBuffer, starting at
|
814 |
|
|
* a given index. If starting index is less than 0, the search starts at
|
815 |
|
|
* the beginning of this String. If the starting index is greater than the
|
816 |
|
|
* length of this String, or the substring is not found, -1 is returned.
|
817 |
|
|
*
|
818 |
|
|
* @param str String to find
|
819 |
|
|
* @param fromIndex index to start the search
|
820 |
|
|
* @return location (base 0) of the String, or -1 if not found
|
821 |
|
|
* @throws NullPointerException if str is null
|
822 |
|
|
* @since 1.4
|
823 |
|
|
*/
|
824 |
|
|
public int indexOf(String str, int fromIndex)
|
825 |
|
|
{
|
826 |
|
|
if (fromIndex < 0)
|
827 |
|
|
fromIndex = 0;
|
828 |
|
|
int limit = count - str.count;
|
829 |
|
|
for ( ; fromIndex <= limit; fromIndex++)
|
830 |
|
|
if (regionMatches(fromIndex, str))
|
831 |
|
|
return fromIndex;
|
832 |
|
|
return -1;
|
833 |
|
|
}
|
834 |
|
|
|
835 |
|
|
/**
|
836 |
|
|
* Finds the last instance of a substring in this StringBuffer.
|
837 |
|
|
*
|
838 |
|
|
* @param str String to find
|
839 |
|
|
* @return location (base 0) of the String, or -1 if not found
|
840 |
|
|
* @throws NullPointerException if str is null
|
841 |
|
|
* @see #lastIndexOf(String, int)
|
842 |
|
|
* @since 1.4
|
843 |
|
|
*/
|
844 |
|
|
public int lastIndexOf(String str)
|
845 |
|
|
{
|
846 |
|
|
return lastIndexOf(str, count - str.count);
|
847 |
|
|
}
|
848 |
|
|
|
849 |
|
|
/**
|
850 |
|
|
* Finds the last instance of a String in this StringBuffer, starting at a
|
851 |
|
|
* given index. If starting index is greater than the maximum valid index,
|
852 |
|
|
* then the search begins at the end of this String. If the starting index
|
853 |
|
|
* is less than zero, or the substring is not found, -1 is returned.
|
854 |
|
|
*
|
855 |
|
|
* @param str String to find
|
856 |
|
|
* @param fromIndex index to start the search
|
857 |
|
|
* @return location (base 0) of the String, or -1 if not found
|
858 |
|
|
* @throws NullPointerException if str is null
|
859 |
|
|
* @since 1.4
|
860 |
|
|
*/
|
861 |
|
|
public int lastIndexOf(String str, int fromIndex)
|
862 |
|
|
{
|
863 |
|
|
fromIndex = Math.min(fromIndex, count - str.count);
|
864 |
|
|
for ( ; fromIndex >= 0; fromIndex--)
|
865 |
|
|
if (regionMatches(fromIndex, str))
|
866 |
|
|
return fromIndex;
|
867 |
|
|
return -1;
|
868 |
|
|
}
|
869 |
|
|
|
870 |
|
|
/**
|
871 |
|
|
* Reverse the characters in this StringBuffer. The same sequence of
|
872 |
|
|
* characters exists, but in the reverse index ordering.
|
873 |
|
|
*
|
874 |
|
|
* @return this <code>StringBuffer</code>
|
875 |
|
|
*/
|
876 |
|
|
public AbstractStringBuffer reverse()
|
877 |
|
|
{
|
878 |
|
|
// Call ensureCapacity to enforce copy-on-write.
|
879 |
|
|
ensureCapacity_unsynchronized(count);
|
880 |
|
|
for (int i = count >> 1, j = count - i; --i >= 0; ++j)
|
881 |
|
|
{
|
882 |
|
|
char c = value[i];
|
883 |
|
|
value[i] = value[j];
|
884 |
|
|
value[j] = c;
|
885 |
|
|
}
|
886 |
|
|
return this;
|
887 |
|
|
}
|
888 |
|
|
|
889 |
|
|
/**
|
890 |
|
|
* This may reduce the amount of memory used by the StringBuffer,
|
891 |
|
|
* by resizing the internal array to remove unused space. However,
|
892 |
|
|
* this method is not required to resize, so this behavior cannot
|
893 |
|
|
* be relied upon.
|
894 |
|
|
* @since 1.5
|
895 |
|
|
*/
|
896 |
|
|
public void trimToSize()
|
897 |
|
|
{
|
898 |
|
|
int wouldSave = value.length - count;
|
899 |
|
|
// Some random heuristics: if we save less than 20 characters, who
|
900 |
|
|
// cares.
|
901 |
|
|
if (wouldSave < 20)
|
902 |
|
|
return;
|
903 |
|
|
// If we save more than 200 characters, shrink.
|
904 |
|
|
// If we save more than 1/4 of the buffer, shrink.
|
905 |
|
|
if (wouldSave > 200 || wouldSave * 4 > value.length)
|
906 |
|
|
{
|
907 |
|
|
char[] newValue = new char[count];
|
908 |
|
|
System.arraycopy(value, 0, newValue, 0, count);
|
909 |
|
|
value = newValue;
|
910 |
|
|
}
|
911 |
|
|
}
|
912 |
|
|
|
913 |
|
|
/**
|
914 |
|
|
* Return the number of code points between two indices in the
|
915 |
|
|
* <code>StringBuffer</code>. An unpaired surrogate counts as a
|
916 |
|
|
* code point for this purpose. Characters outside the indicated
|
917 |
|
|
* range are not examined, even if the range ends in the middle of a
|
918 |
|
|
* surrogate pair.
|
919 |
|
|
*
|
920 |
|
|
* @param start the starting index
|
921 |
|
|
* @param end one past the ending index
|
922 |
|
|
* @return the number of code points
|
923 |
|
|
* @since 1.5
|
924 |
|
|
*/
|
925 |
|
|
public int codePointCount(int start, int end)
|
926 |
|
|
{
|
927 |
|
|
if (start < 0 || end >= count || start > end)
|
928 |
|
|
throw new StringIndexOutOfBoundsException();
|
929 |
|
|
|
930 |
|
|
int count = 0;
|
931 |
|
|
while (start < end)
|
932 |
|
|
{
|
933 |
|
|
char base = value[start];
|
934 |
|
|
if (base < Character.MIN_HIGH_SURROGATE
|
935 |
|
|
|| base > Character.MAX_HIGH_SURROGATE
|
936 |
|
|
|| start == end
|
937 |
|
|
|| start == count
|
938 |
|
|
|| value[start + 1] < Character.MIN_LOW_SURROGATE
|
939 |
|
|
|| value[start + 1] > Character.MAX_LOW_SURROGATE)
|
940 |
|
|
{
|
941 |
|
|
// Nothing.
|
942 |
|
|
}
|
943 |
|
|
else
|
944 |
|
|
{
|
945 |
|
|
// Surrogate pair.
|
946 |
|
|
++start;
|
947 |
|
|
}
|
948 |
|
|
++start;
|
949 |
|
|
++count;
|
950 |
|
|
}
|
951 |
|
|
return count;
|
952 |
|
|
}
|
953 |
|
|
|
954 |
|
|
/**
|
955 |
|
|
* Starting at the given index, this counts forward by the indicated
|
956 |
|
|
* number of code points, and then returns the resulting index. An
|
957 |
|
|
* unpaired surrogate counts as a single code point for this
|
958 |
|
|
* purpose.
|
959 |
|
|
*
|
960 |
|
|
* @param start the starting index
|
961 |
|
|
* @param codePoints the number of code points
|
962 |
|
|
* @return the resulting index
|
963 |
|
|
* @since 1.5
|
964 |
|
|
*/
|
965 |
|
|
public int offsetByCodePoints(int start, int codePoints)
|
966 |
|
|
{
|
967 |
|
|
while (codePoints > 0)
|
968 |
|
|
{
|
969 |
|
|
char base = value[start];
|
970 |
|
|
if (base < Character.MIN_HIGH_SURROGATE
|
971 |
|
|
|| base > Character.MAX_HIGH_SURROGATE
|
972 |
|
|
|| start == count
|
973 |
|
|
|| value[start + 1] < Character.MIN_LOW_SURROGATE
|
974 |
|
|
|| value[start + 1] > Character.MAX_LOW_SURROGATE)
|
975 |
|
|
{
|
976 |
|
|
// Nothing.
|
977 |
|
|
}
|
978 |
|
|
else
|
979 |
|
|
{
|
980 |
|
|
// Surrogate pair.
|
981 |
|
|
++start;
|
982 |
|
|
}
|
983 |
|
|
++start;
|
984 |
|
|
--codePoints;
|
985 |
|
|
}
|
986 |
|
|
return start;
|
987 |
|
|
}
|
988 |
|
|
|
989 |
|
|
/**
|
990 |
|
|
* Increase the capacity of this <code>StringBuilder</code>. This will
|
991 |
|
|
* ensure that an expensive growing operation will not occur until
|
992 |
|
|
* <code>minimumCapacity</code> is reached. The buffer is grown to the
|
993 |
|
|
* larger of <code>minimumCapacity</code> and
|
994 |
|
|
* <code>capacity() * 2 + 2</code>, if it is not already large enough.
|
995 |
|
|
*
|
996 |
|
|
* @param minimumCapacity the new capacity
|
997 |
|
|
* @see #capacity()
|
998 |
|
|
*/
|
999 |
|
|
void ensureCapacity_unsynchronized(int minimumCapacity)
|
1000 |
|
|
{
|
1001 |
|
|
if (minimumCapacity > value.length)
|
1002 |
|
|
{
|
1003 |
|
|
int max = value.length * 2 + 2;
|
1004 |
|
|
minimumCapacity = (minimumCapacity < max ? max : minimumCapacity);
|
1005 |
|
|
char[] nb = new char[minimumCapacity];
|
1006 |
|
|
System.arraycopy(value, 0, nb, 0, count);
|
1007 |
|
|
value = nb;
|
1008 |
|
|
}
|
1009 |
|
|
}
|
1010 |
|
|
|
1011 |
|
|
/**
|
1012 |
|
|
* Predicate which determines if a substring of this matches another String
|
1013 |
|
|
* starting at a specified offset for each String and continuing for a
|
1014 |
|
|
* specified length. This is more efficient than creating a String to call
|
1015 |
|
|
* indexOf on.
|
1016 |
|
|
*
|
1017 |
|
|
* @param toffset index to start comparison at for this String
|
1018 |
|
|
* @param other non-null String to compare to region of this
|
1019 |
|
|
* @return true if regions match, false otherwise
|
1020 |
|
|
* @see #indexOf(String, int)
|
1021 |
|
|
* @see #lastIndexOf(String, int)
|
1022 |
|
|
* @see String#regionMatches(boolean, int, String, int, int)
|
1023 |
|
|
*/
|
1024 |
|
|
// GCJ LOCAL: native for gcj.
|
1025 |
|
|
private native boolean regionMatches(int toffset, String other);
|
1026 |
|
|
|
1027 |
|
|
}
|