1 /* AbstractStringBuffer.java -- Growable strings
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2008
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
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)
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.
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
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
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. */
41 import java
.io
.Serializable
;
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.
49 abstract class AbstractStringBuffer
50 implements Serializable
, CharSequence
, Appendable
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.
58 * @serial the number of characters in the buffer
63 * The buffer. Note that this has permissions set this way so that String
71 * The default capacity of a buffer.
73 private static final int DEFAULT_CAPACITY
= 16;
76 * Create a new AbstractStringBuffer with default capacity 16.
78 AbstractStringBuffer()
80 this(DEFAULT_CAPACITY
);
84 * Create an empty <code>StringBuffer</code> with the specified initial
87 * @param capacity the initial capacity
88 * @throws NegativeArraySizeException if capacity is negative
90 AbstractStringBuffer(int capacity
)
92 value
= new char[capacity
];
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
100 * @param str the <code>String</code> to convert
101 * @throws NullPointerException if str is null
103 AbstractStringBuffer(String str
)
106 value
= new char[count
+ DEFAULT_CAPACITY
];
107 str
.getChars(0, count
, value
, 0);
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.
116 * @param seq the initializing <code>CharSequence</code>
117 * @throws NullPointerException if str is null
120 AbstractStringBuffer(CharSequence seq
)
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
);
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.
136 * @param minimumCapacity the new capacity
139 public void ensureCapacity(int minimumCapacity
)
141 ensureCapacity_unsynchronized(minimumCapacity
);
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.
151 * @param newLength the new length
152 * @throws IndexOutOfBoundsException if the new length is negative
153 * (while unspecified, this is a StringIndexOutOfBoundsException)
156 public void setLength(int newLength
)
159 throw new StringIndexOutOfBoundsException(newLength
);
161 int valueLength
= value
.length
;
163 /* Always call ensureCapacity_unsynchronized in order to preserve
164 copy-on-write semantics. */
165 ensureCapacity_unsynchronized(newLength
);
167 if (newLength
< valueLength
)
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'. */
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';
184 * Get the character at the specified index.
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)
191 public char charAt(int index
)
193 if (index
< 0 || index
>= count
)
194 throw new StringIndexOutOfBoundsException(index
);
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()
208 public int codePointAt(int index
)
210 return Character
.codePointAt(value
, index
, count
);
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()
222 public int codePointBefore(int index
)
224 // Character.codePointBefore() doesn't perform this check. We
225 // could use the CharSequence overload, but this is just as easy.
227 throw new IndexOutOfBoundsException();
228 return Character
.codePointBefore(value
, index
, 1);
232 * Get the specified array of characters. <code>srcOffset - srcEnd</code>
233 * characters will be copied into the array you pass in.
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)
246 public void getChars(int srcOffset
, int srcEnd
,
247 char[] dst
, int dstOffset
)
249 if (srcOffset
< 0 || srcEnd
> count
|| srcEnd
< srcOffset
)
250 throw new StringIndexOutOfBoundsException();
251 VMSystem
.arraycopy(value
, srcOffset
, dst
, dstOffset
, srcEnd
- srcOffset
);
255 * Set the character at the specified index.
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)
262 public void setCharAt(int index
, char ch
)
264 if (index
< 0 || index
>= count
)
265 throw new StringIndexOutOfBoundsException(index
);
266 // Call ensureCapacity to enforce copy-on-write.
267 ensureCapacity_unsynchronized(count
);
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>.
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)
281 public AbstractStringBuffer
append(Object obj
)
283 return append(String
.valueOf(obj
));
287 * Append the <code>String</code> to this <code>StringBuffer</code>. If
288 * str is null, the String "null" is appended.
290 * @param str the <code>String</code> to append
291 * @return this <code>StringBuffer</code>
293 public AbstractStringBuffer
append(String str
)
298 ensureCapacity_unsynchronized(count
+ len
);
299 str
.getChars(0, len
, value
, count
);
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.
309 * @param stringBuffer the <code>StringBuilder</code> to convert and append
310 * @return this <code>StringBuilder</code>
311 * @see #append(Object)
313 public AbstractStringBuffer
append(StringBuffer stringBuffer
)
315 if (stringBuffer
== null)
316 return append("null");
317 synchronized (stringBuffer
)
319 int len
= stringBuffer
.count
;
320 ensureCapacity(count
+ len
);
321 VMSystem
.arraycopy(stringBuffer
.value
, 0, value
, count
, len
);
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.
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)
337 public AbstractStringBuffer
append(char[] data
)
339 return append(data
, 0, data
.length
);
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
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)
356 public AbstractStringBuffer
append(char[] data
, int offset
, int count
)
358 if (offset
< 0 || count
< 0 || offset
> data
.length
- count
)
359 throw new StringIndexOutOfBoundsException();
360 ensureCapacity_unsynchronized(this.count
+ count
);
361 VMSystem
.arraycopy(data
, offset
, value
, this.count
, count
);
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>.
371 * @param bool the <code>boolean</code> to convert and append
372 * @return this <code>StringBuffer</code>
373 * @see String#valueOf(boolean)
375 public AbstractStringBuffer
append(boolean bool
)
377 return append(bool ?
"true" : "false");
381 * Append the <code>char</code> to this <code>StringBuffer</code>.
383 * @param ch the <code>char</code> to append
384 * @return this <code>StringBuffer</code>
386 public AbstractStringBuffer
append(char ch
)
388 ensureCapacity_unsynchronized(count
+ 1);
394 * Append the characters in the <code>CharSequence</code> to this
397 * @param seq the <code>CharSequence</code> providing the characters
398 * @return this <code>StringBuffer</code>
401 public AbstractStringBuffer
append(CharSequence seq
)
403 return append(seq
, 0, seq
.length());
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>.
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>
417 public AbstractStringBuffer
append(CharSequence seq
, int start
, int end
)
423 ensureCapacity_unsynchronized(count
+ end
- start
);
424 for (; start
< end
; ++start
)
425 value
[count
++] = seq
.charAt(start
);
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>.
435 * @param inum the <code>int</code> to convert and append
436 * @return this <code>StringBuffer</code>
437 * @see String#valueOf(int)
439 // This is native in libgcj, for efficiency.
440 public AbstractStringBuffer
append(int inum
)
442 return append(String
.valueOf(inum
));
446 * Append the <code>String</code> value of the argument to this
447 * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
448 * to <code>String</code>.
450 * @param lnum the <code>long</code> to convert and append
451 * @return this <code>StringBuffer</code>
452 * @see String#valueOf(long)
454 public AbstractStringBuffer
append(long lnum
)
456 return append(Long
.toString(lnum
, 10));
460 * Append the <code>String</code> value of the argument to this
461 * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
462 * to <code>String</code>.
464 * @param fnum the <code>float</code> to convert and append
465 * @return this <code>StringBuffer</code>
466 * @see String#valueOf(float)
468 public AbstractStringBuffer
append(float fnum
)
470 return append(Float
.toString(fnum
));
474 * Append the <code>String</code> value of the argument to this
475 * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
476 * to <code>String</code>.
478 * @param dnum the <code>double</code> to convert and append
479 * @return this <code>StringBuffer</code>
480 * @see String#valueOf(double)
482 public AbstractStringBuffer
append(double dnum
)
484 return append(Double
.toString(dnum
));
488 * Append the code point to this <code>StringBuffer</code>.
489 * This is like #append(char), but will append two characters
490 * if a supplementary code point is given.
492 * @param code the code point to append
493 * @return this <code>StringBuffer</code>
494 * @see Character#toChars(int, char[], int)
497 public AbstractStringBuffer
appendCodePoint(int code
)
499 int len
= Character
.charCount(code
);
500 ensureCapacity_unsynchronized(count
+ len
);
501 Character
.toChars(code
, value
, count
);
507 * Delete characters from this <code>StringBuffer</code>.
508 * <code>delete(10, 12)</code> will delete 10 and 11, but not 12. It is
509 * harmless for end to be larger than length().
511 * @param start the first character to delete
512 * @param end the index after the last character to delete
513 * @return this <code>StringBuffer</code>
514 * @throws StringIndexOutOfBoundsException if start or end are out of bounds
517 public AbstractStringBuffer
delete(int start
, int end
)
519 if (start
< 0 || start
> count
|| start
> end
)
520 throw new StringIndexOutOfBoundsException(start
);
523 ensureCapacity_unsynchronized(count
);
524 if (count
- end
!= 0)
525 VMSystem
.arraycopy(value
, end
, value
, start
, count
- end
);
526 count
-= end
- start
;
531 * Delete a character from this <code>StringBuffer</code>.
533 * @param index the index of the character to delete
534 * @return this <code>StringBuffer</code>
535 * @throws StringIndexOutOfBoundsException if index is out of bounds
538 public AbstractStringBuffer
deleteCharAt(int index
)
540 return delete(index
, index
+ 1);
544 * Replace characters between index <code>start</code> (inclusive) and
545 * <code>end</code> (exclusive) with <code>str</code>. If <code>end</code>
546 * is larger than the size of this StringBuffer, all characters after
547 * <code>start</code> are replaced.
549 * @param start the beginning index of characters to delete (inclusive)
550 * @param end the ending index of characters to delete (exclusive)
551 * @param str the new <code>String</code> to insert
552 * @return this <code>StringBuffer</code>
553 * @throws StringIndexOutOfBoundsException if start or end are out of bounds
554 * @throws NullPointerException if str is null
557 public AbstractStringBuffer
replace(int start
, int end
, String str
)
559 if (start
< 0 || start
> count
|| start
> end
)
560 throw new StringIndexOutOfBoundsException(start
);
563 // Calculate the difference in 'count' after the replace.
564 int delta
= len
- (end
> count ? count
: end
) + start
;
565 ensureCapacity_unsynchronized(count
+ delta
);
567 if (delta
!= 0 && end
< count
)
568 VMSystem
.arraycopy(value
, end
, value
, end
+ delta
, count
- end
);
570 str
.getChars(0, len
, value
, start
);
576 * Insert a subarray of the <code>char[]</code> argument into this
577 * <code>StringBuffer</code>.
579 * @param offset the place to insert in this buffer
580 * @param str the <code>char[]</code> to insert
581 * @param str_offset the index in <code>str</code> to start inserting from
582 * @param len the number of characters to insert
583 * @return this <code>StringBuffer</code>
584 * @throws NullPointerException if <code>str</code> is <code>null</code>
585 * @throws StringIndexOutOfBoundsException if any index is out of bounds
588 public AbstractStringBuffer
insert(int offset
, char[] str
, int str_offset
, int len
)
590 if (offset
< 0 || offset
> count
|| len
< 0
591 || str_offset
< 0 || str_offset
> str
.length
- len
)
592 throw new StringIndexOutOfBoundsException();
593 ensureCapacity_unsynchronized(count
+ len
);
594 VMSystem
.arraycopy(value
, offset
, value
, offset
+ len
, count
- offset
);
595 VMSystem
.arraycopy(str
, str_offset
, value
, offset
, len
);
601 * Insert the <code>String</code> value of the argument into this
602 * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
603 * to <code>String</code>.
605 * @param offset the place to insert in this buffer
606 * @param obj the <code>Object</code> to convert and insert
607 * @return this <code>StringBuffer</code>
608 * @exception StringIndexOutOfBoundsException if offset is out of bounds
609 * @see String#valueOf(Object)
611 public AbstractStringBuffer
insert(int offset
, Object obj
)
613 return insert(offset
, obj
== null ?
"null" : obj
.toString());
617 * Insert the <code>String</code> argument into this
618 * <code>StringBuffer</code>. If str is null, the String "null" is used
621 * @param offset the place to insert in this buffer
622 * @param str the <code>String</code> to insert
623 * @return this <code>StringBuffer</code>
624 * @throws StringIndexOutOfBoundsException if offset is out of bounds
626 public AbstractStringBuffer
insert(int offset
, String str
)
628 if (offset
< 0 || offset
> count
)
629 throw new StringIndexOutOfBoundsException(offset
);
633 ensureCapacity_unsynchronized(count
+ len
);
634 VMSystem
.arraycopy(value
, offset
, value
, offset
+ len
, count
- offset
);
635 str
.getChars(0, len
, value
, offset
);
641 * Insert the <code>CharSequence</code> argument into this
642 * <code>StringBuffer</code>. If the sequence is null, the String
643 * "null" is used instead.
645 * @param offset the place to insert in this buffer
646 * @param sequence the <code>CharSequence</code> to insert
647 * @return this <code>StringBuffer</code>
648 * @throws IndexOutOfBoundsException if offset is out of bounds
651 public AbstractStringBuffer
insert(int offset
, CharSequence sequence
)
653 if (sequence
== null)
655 return insert(offset
, sequence
, 0, sequence
.length());
659 * Insert a subsequence of the <code>CharSequence</code> argument into this
660 * <code>StringBuffer</code>. If the sequence is null, the String
661 * "null" is used instead.
663 * @param offset the place to insert in this buffer
664 * @param sequence the <code>CharSequence</code> to insert
665 * @param start the starting index of the subsequence
666 * @param end one past the ending index of the subsequence
667 * @return this <code>StringBuffer</code>
668 * @throws IndexOutOfBoundsException if offset, start,
669 * or end are out of bounds
672 public AbstractStringBuffer
insert(int offset
, CharSequence sequence
, int start
, int end
)
674 if (sequence
== null)
676 if (start
< 0 || end
< 0 || start
> end
|| end
> sequence
.length())
677 throw new IndexOutOfBoundsException();
678 int len
= end
- start
;
679 ensureCapacity_unsynchronized(count
+ len
);
680 VMSystem
.arraycopy(value
, offset
, value
, offset
+ len
, count
- offset
);
681 for (int i
= start
; i
< end
; ++i
)
682 value
[offset
++] = sequence
.charAt(i
);
688 * Insert the <code>char[]</code> argument into this
689 * <code>StringBuffer</code>.
691 * @param offset the place to insert in this buffer
692 * @param data the <code>char[]</code> to insert
693 * @return this <code>StringBuffer</code>
694 * @throws NullPointerException if <code>data</code> is <code>null</code>
695 * @throws StringIndexOutOfBoundsException if offset is out of bounds
696 * @see #insert(int, char[], int, int)
698 public AbstractStringBuffer
insert(int offset
, char[] data
)
700 return insert(offset
, data
, 0, data
.length
);
704 * Insert the <code>String</code> value of the argument into this
705 * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
706 * to <code>String</code>.
708 * @param offset the place to insert in this buffer
709 * @param bool the <code>boolean</code> to convert and insert
710 * @return this <code>StringBuffer</code>
711 * @throws StringIndexOutOfBoundsException if offset is out of bounds
712 * @see String#valueOf(boolean)
714 public AbstractStringBuffer
insert(int offset
, boolean bool
)
716 return insert(offset
, bool ?
"true" : "false");
720 * Insert the <code>char</code> argument into this <code>StringBuffer</code>.
722 * @param offset the place to insert in this buffer
723 * @param ch the <code>char</code> to insert
724 * @return this <code>StringBuffer</code>
725 * @throws StringIndexOutOfBoundsException if offset is out of bounds
727 public AbstractStringBuffer
insert(int offset
, char ch
)
729 if (offset
< 0 || offset
> count
)
730 throw new StringIndexOutOfBoundsException(offset
);
731 ensureCapacity_unsynchronized(count
+ 1);
732 VMSystem
.arraycopy(value
, offset
, value
, offset
+ 1, count
- offset
);
739 * Insert the <code>String</code> value of the argument into this
740 * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
741 * to <code>String</code>.
743 * @param offset the place to insert in this buffer
744 * @param inum the <code>int</code> to convert and insert
745 * @return this <code>StringBuffer</code>
746 * @throws StringIndexOutOfBoundsException if offset is out of bounds
747 * @see String#valueOf(int)
749 public AbstractStringBuffer
insert(int offset
, int inum
)
751 return insert(offset
, String
.valueOf(inum
));
755 * Insert the <code>String</code> value of the argument into this
756 * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
757 * to <code>String</code>.
759 * @param offset the place to insert in this buffer
760 * @param lnum the <code>long</code> to convert and insert
761 * @return this <code>StringBuffer</code>
762 * @throws StringIndexOutOfBoundsException if offset is out of bounds
763 * @see String#valueOf(long)
765 public AbstractStringBuffer
insert(int offset
, long lnum
)
767 return insert(offset
, Long
.toString(lnum
, 10));
771 * Insert the <code>String</code> value of the argument into this
772 * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
773 * to <code>String</code>.
775 * @param offset the place to insert in this buffer
776 * @param fnum the <code>float</code> to convert and insert
777 * @return this <code>StringBuffer</code>
778 * @throws StringIndexOutOfBoundsException if offset is out of bounds
779 * @see String#valueOf(float)
781 public AbstractStringBuffer
insert(int offset
, float fnum
)
783 return insert(offset
, Float
.toString(fnum
));
787 * Insert the <code>String</code> value of the argument into this
788 * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
789 * to <code>String</code>.
791 * @param offset the place to insert in this buffer
792 * @param dnum the <code>double</code> to convert and insert
793 * @return this <code>StringBuffer</code>
794 * @throws StringIndexOutOfBoundsException if offset is out of bounds
795 * @see String#valueOf(double)
797 public AbstractStringBuffer
insert(int offset
, double dnum
)
799 return insert(offset
, Double
.toString(dnum
));
803 * Finds the first instance of a substring in this StringBuilder.
805 * @param str String to find
806 * @return location (base 0) of the String, or -1 if not found
807 * @throws NullPointerException if str is null
808 * @see #indexOf(String, int)
810 public int indexOf(String str
)
812 return indexOf(str
, 0);
816 * Finds the first instance of a String in this StringBuffer, starting at
817 * a given index. If starting index is less than 0, the search starts at
818 * the beginning of this String. If the starting index is greater than the
819 * length of this String, or the substring is not found, -1 is returned.
821 * @param str String to find
822 * @param fromIndex index to start the search
823 * @return location (base 0) of the String, or -1 if not found
824 * @throws NullPointerException if str is null
827 public int indexOf(String str
, int fromIndex
)
831 int limit
= count
- str
.count
;
832 for ( ; fromIndex
<= limit
; fromIndex
++)
833 if (regionMatches(fromIndex
, str
))
839 * Finds the last instance of a substring in this StringBuffer.
841 * @param str String to find
842 * @return location (base 0) of the String, or -1 if not found
843 * @throws NullPointerException if str is null
844 * @see #lastIndexOf(String, int)
847 public int lastIndexOf(String str
)
849 return lastIndexOf(str
, count
- str
.count
);
853 * Finds the last instance of a String in this StringBuffer, starting at a
854 * given index. If starting index is greater than the maximum valid index,
855 * then the search begins at the end of this String. If the starting index
856 * is less than zero, or the substring is not found, -1 is returned.
858 * @param str String to find
859 * @param fromIndex index to start the search
860 * @return location (base 0) of the String, or -1 if not found
861 * @throws NullPointerException if str is null
864 public int lastIndexOf(String str
, int fromIndex
)
866 fromIndex
= Math
.min(fromIndex
, count
- str
.count
);
867 for ( ; fromIndex
>= 0; fromIndex
--)
868 if (regionMatches(fromIndex
, str
))
874 * Reverse the characters in this StringBuffer. The same sequence of
875 * characters exists, but in the reverse index ordering.
877 * @return this <code>StringBuffer</code>
879 public AbstractStringBuffer
reverse()
881 // Call ensureCapacity to enforce copy-on-write.
882 ensureCapacity_unsynchronized(count
);
883 for (int i
= count
>> 1, j
= count
- i
; --i
>= 0; ++j
)
893 * This may reduce the amount of memory used by the StringBuffer,
894 * by resizing the internal array to remove unused space. However,
895 * this method is not required to resize, so this behavior cannot
899 public void trimToSize()
901 int wouldSave
= value
.length
- count
;
902 // Some random heuristics: if we save less than 20 characters, who
906 // If we save more than 200 characters, shrink.
907 // If we save more than 1/4 of the buffer, shrink.
908 if (wouldSave
> 200 || wouldSave
* 4 > value
.length
)
910 char[] newValue
= new char[count
];
911 VMSystem
.arraycopy(value
, 0, newValue
, 0, count
);
917 * Return the number of code points between two indices in the
918 * <code>StringBuffer</code>. An unpaired surrogate counts as a
919 * code point for this purpose. Characters outside the indicated
920 * range are not examined, even if the range ends in the middle of a
923 * @param start the starting index
924 * @param end one past the ending index
925 * @return the number of code points
928 public int codePointCount(int start
, int end
)
930 if (start
< 0 || end
>= count
|| start
> end
)
931 throw new StringIndexOutOfBoundsException();
936 char base
= value
[start
];
937 if (base
< Character
.MIN_HIGH_SURROGATE
938 || base
> Character
.MAX_HIGH_SURROGATE
941 || value
[start
+ 1] < Character
.MIN_LOW_SURROGATE
942 || value
[start
+ 1] > Character
.MAX_LOW_SURROGATE
)
958 * Starting at the given index, this counts forward by the indicated
959 * number of code points, and then returns the resulting index. An
960 * unpaired surrogate counts as a single code point for this
963 * @param start the starting index
964 * @param codePoints the number of code points
965 * @return the resulting index
968 public int offsetByCodePoints(int start
, int codePoints
)
970 while (codePoints
> 0)
972 char base
= value
[start
];
973 if (base
< Character
.MIN_HIGH_SURROGATE
974 || base
> Character
.MAX_HIGH_SURROGATE
976 || value
[start
+ 1] < Character
.MIN_LOW_SURROGATE
977 || value
[start
+ 1] > Character
.MAX_LOW_SURROGATE
)
993 * Increase the capacity of this <code>StringBuilder</code>. This will
994 * ensure that an expensive growing operation will not occur until
995 * <code>minimumCapacity</code> is reached. The buffer is grown to the
996 * larger of <code>minimumCapacity</code> and
997 * <code>capacity() * 2 + 2</code>, if it is not already large enough.
999 * @param minimumCapacity the new capacity
1002 void ensureCapacity_unsynchronized(int minimumCapacity
)
1004 if (minimumCapacity
> value
.length
)
1006 int max
= value
.length
* 2 + 2;
1007 minimumCapacity
= (minimumCapacity
< max ? max
: minimumCapacity
);
1008 char[] nb
= new char[minimumCapacity
];
1009 VMSystem
.arraycopy(value
, 0, nb
, 0, count
);
1015 * Predicate which determines if a substring of this matches another String
1016 * starting at a specified offset for each String and continuing for a
1017 * specified length. This is more efficient than creating a String to call
1020 * @param toffset index to start comparison at for this String
1021 * @param other non-null String to compare to region of this
1022 * @return true if regions match, false otherwise
1023 * @see #indexOf(String, int)
1024 * @see #lastIndexOf(String, int)
1025 * @see String#regionMatches(boolean, int, String, int, int)
1027 private boolean regionMatches(int toffset
, String other
)
1029 int len
= other
.count
;
1030 int index
= other
.offset
;
1032 if (value
[toffset
++] != other
.value
[index
++])