1 /* ClasspathStringBuffer.java -- Growable strings without locking or copying
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. */
39 package gnu.java.lang;
41 import java.io.Serializable;
44 * This class is based on java.lang.AbstractStringBuffer but
45 * without the copying of the string by toString.
46 * If you modify this, please consider also modifying that code.
47 * This code is not thread-safe; limit its use to internal use within
50 public final class CPStringBuilder
51 implements Serializable, CharSequence, Appendable
55 * Index of next available character (and thus the size of the current
56 * string contents). Note that this has permissions set this way so that
57 * String can get the value.
59 * @serial the number of characters in the buffer
64 * The buffer. Note that this has permissions set this way so that String
72 * The default capacity of a buffer.
74 private static final int DEFAULT_CAPACITY = 16;
77 * Create a new CPStringBuilder with default capacity 16.
79 public CPStringBuilder()
81 this(DEFAULT_CAPACITY);
85 * Create an empty <code>StringBuffer</code> with the specified initial
88 * @param capacity the initial capacity
89 * @throws NegativeArraySizeException if capacity is negative
91 public CPStringBuilder(int capacity)
93 value = new char[capacity];
97 * Create a new <code>StringBuffer</code> with the characters in the
98 * specified <code>String</code>. Initial capacity will be the size of the
101 * @param str the <code>String</code> to convert
102 * @throws NullPointerException if str is null
104 public CPStringBuilder(String str)
106 count = str.length();
107 value = new char[count + DEFAULT_CAPACITY];
108 str.getChars(0, count, value, 0);
112 * Create a new <code>StringBuffer</code> with the characters in the
113 * specified <code>CharSequence</code>. Initial capacity will be the
114 * length of the sequence plus 16; if the sequence reports a length
115 * less than or equal to 0, then the initial capacity will be 16.
117 * @param seq the initializing <code>CharSequence</code>
118 * @throws NullPointerException if str is null
121 public CPStringBuilder(CharSequence seq)
123 int len = seq.length();
124 count = len <= 0 ? 0 : len;
125 value = new char[count + DEFAULT_CAPACITY];
126 for (int i = 0; i < len; ++i)
127 value[i] = seq.charAt(i);
131 * Increase the capacity of this <code>StringBuffer</code>. This will
132 * ensure that an expensive growing operation will not occur until
133 * <code>minimumCapacity</code> is reached. The buffer is grown to the
134 * larger of <code>minimumCapacity</code> and
135 * <code>capacity() * 2 + 2</code>, if it is not already large enough.
137 * @param minimumCapacity the new capacity
140 public void ensureCapacity(int minimumCapacity)
142 ensureCapacity_unsynchronized(minimumCapacity);
146 * Set the length of this StringBuffer. If the new length is greater than
147 * the current length, all the new characters are set to '\0'. If the new
148 * length is less than the current length, the first <code>newLength</code>
149 * characters of the old array will be preserved, and the remaining
150 * characters are truncated.
152 * @param newLength the new length
153 * @throws IndexOutOfBoundsException if the new length is negative
154 * (while unspecified, this is a StringIndexOutOfBoundsException)
157 public void setLength(int newLength)
160 throw new StringIndexOutOfBoundsException(newLength);
162 int valueLength = value.length;
164 /* Always call ensureCapacity_unsynchronized in order to preserve
165 copy-on-write semantics. */
166 ensureCapacity_unsynchronized(newLength);
168 if (newLength < valueLength)
170 /* If the StringBuffer's value just grew, then we know that
171 value is newly allocated and the region between count and
172 newLength is filled with '\0'. */
177 /* The StringBuffer's value doesn't need to grow. However,
178 we should clear out any cruft that may exist. */
179 while (count < newLength)
180 value[count++] = '\0';
185 * Get the character at the specified index.
187 * @param index the index of the character to get, starting at 0
188 * @return the character at the specified index
189 * @throws IndexOutOfBoundsException if index is negative or >= length()
190 * (while unspecified, this is a StringIndexOutOfBoundsException)
192 public char charAt(int index)
194 if (index < 0 || index >= count)
195 throw new StringIndexOutOfBoundsException(index);
200 * Get the code point at the specified index. This is like #charAt(int),
201 * but if the character is the start of a surrogate pair, and the
202 * following character completes the pair, then the corresponding
203 * supplementary code point is returned.
204 * @param index the index of the codepoint to get, starting at 0
205 * @return the codepoint at the specified index
206 * @throws IndexOutOfBoundsException if index is negative or >= length()
209 public int codePointAt(int index)
211 return Character.codePointAt(value, index, count);
215 * Get the code point before the specified index. This is like
216 * #codePointAt(int), but checks the characters at <code>index-1</code> and
217 * <code>index-2</code> to see if they form a supplementary code point.
218 * @param index the index just past the codepoint to get, starting at 0
219 * @return the codepoint at the specified index
220 * @throws IndexOutOfBoundsException if index is negative or >= length()
223 public int codePointBefore(int index)
225 // Character.codePointBefore() doesn't perform this check. We
226 // could use the CharSequence overload, but this is just as easy.
228 throw new IndexOutOfBoundsException();
229 return Character.codePointBefore(value, index, 1);
233 * Get the specified array of characters. <code>srcOffset - srcEnd</code>
234 * characters will be copied into the array you pass in.
236 * @param srcOffset the index to start copying from (inclusive)
237 * @param srcEnd the index to stop copying from (exclusive)
238 * @param dst the array to copy into
239 * @param dstOffset the index to start copying into
240 * @throws NullPointerException if dst is null
241 * @throws IndexOutOfBoundsException if any source or target indices are
242 * out of range (while unspecified, source problems cause a
243 * StringIndexOutOfBoundsException, and dest problems cause an
244 * ArrayIndexOutOfBoundsException)
245 * @see System#arraycopy(Object, int, Object, int, int)
247 public void getChars(int srcOffset, int srcEnd,
248 char[] dst, int dstOffset)
250 if (srcOffset < 0 || srcEnd > count || srcEnd < srcOffset)
251 throw new StringIndexOutOfBoundsException();
252 System.arraycopy(value, srcOffset, dst, dstOffset, srcEnd - srcOffset);
256 * Set the character at the specified index.
258 * @param index the index of the character to set starting at 0
259 * @param ch the value to set that character to
260 * @throws IndexOutOfBoundsException if index is negative or >= length()
261 * (while unspecified, this is a StringIndexOutOfBoundsException)
263 public void setCharAt(int index, char ch)
265 if (index < 0 || index >= count)
266 throw new StringIndexOutOfBoundsException(index);
267 // Call ensureCapacity to enforce copy-on-write.
268 ensureCapacity_unsynchronized(count);
273 * Append the <code>String</code> value of the argument to this
274 * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
275 * to <code>String</code>.
277 * @param obj the <code>Object</code> to convert and append
278 * @return this <code>StringBuffer</code>
279 * @see String#valueOf(Object)
280 * @see #append(String)
282 public CPStringBuilder append(Object obj)
284 return append(String.valueOf(obj));
288 * Append the <code>String</code> to this <code>StringBuffer</code>. If
289 * str is null, the String "null" is appended.
291 * @param str the <code>String</code> to append
292 * @return this <code>StringBuffer</code>
294 public CPStringBuilder append(String str)
298 int len = str.length();
299 ensureCapacity_unsynchronized(count + len);
300 str.getChars(0, len, value, count);
306 * Append the <code>StringBuilder</code> value of the argument to this
307 * <code>StringBuilder</code>. This behaves the same as
308 * <code>append((Object) stringBuffer)</code>, except it is more efficient.
310 * @param stringBuffer the <code>StringBuilder</code> to convert and append
311 * @return this <code>StringBuilder</code>
312 * @see #append(Object)
314 public CPStringBuilder append(StringBuffer stringBuffer)
316 if (stringBuffer == null)
317 return append("null");
318 synchronized (stringBuffer)
320 int len = stringBuffer.length();
321 ensureCapacity(count + len);
322 stringBuffer.getChars(0, len, value, count);
329 * Append the <code>char</code> array to this <code>StringBuffer</code>.
330 * This is similar (but more efficient) than
331 * <code>append(new String(data))</code>, except in the case of null.
333 * @param data the <code>char[]</code> to append
334 * @return this <code>StringBuffer</code>
335 * @throws NullPointerException if <code>str</code> is <code>null</code>
336 * @see #append(char[], int, int)
338 public CPStringBuilder append(char[] data)
340 return append(data, 0, data.length);
344 * Append part of the <code>char</code> array to this
345 * <code>StringBuffer</code>. This is similar (but more efficient) than
346 * <code>append(new String(data, offset, count))</code>, except in the case
349 * @param data the <code>char[]</code> to append
350 * @param offset the start location in <code>str</code>
351 * @param count the number of characters to get from <code>str</code>
352 * @return this <code>StringBuffer</code>
353 * @throws NullPointerException if <code>str</code> is <code>null</code>
354 * @throws IndexOutOfBoundsException if offset or count is out of range
355 * (while unspecified, this is a StringIndexOutOfBoundsException)
357 public CPStringBuilder append(char[] data, int offset, int count)
359 if (offset < 0 || count < 0 || offset > data.length - count)
360 throw new StringIndexOutOfBoundsException();
361 ensureCapacity_unsynchronized(this.count + count);
362 System.arraycopy(data, offset, value, this.count, count);
368 * Append the <code>String</code> value of the argument to this
369 * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
370 * to <code>String</code>.
372 * @param bool the <code>boolean</code> to convert and append
373 * @return this <code>StringBuffer</code>
374 * @see String#valueOf(boolean)
376 public CPStringBuilder append(boolean bool)
378 return append(bool ? "true" : "false");
382 * Append the <code>char</code> to this <code>StringBuffer</code>.
384 * @param ch the <code>char</code> to append
385 * @return this <code>StringBuffer</code>
387 public CPStringBuilder append(char ch)
389 ensureCapacity_unsynchronized(count + 1);
395 * Append the characters in the <code>CharSequence</code> to this
398 * @param seq the <code>CharSequence</code> providing the characters
399 * @return this <code>StringBuffer</code>
402 public CPStringBuilder append(CharSequence seq)
404 return append(seq, 0, seq.length());
408 * Append some characters from the <code>CharSequence</code> to this
409 * buffer. If the argument is null, the four characters "null" are
412 * @param seq the <code>CharSequence</code> providing the characters
413 * @param start the starting index
414 * @param end one past the final index
415 * @return this <code>StringBuffer</code>
418 public CPStringBuilder append(CharSequence seq, int start, int end)
421 return append("null");
424 ensureCapacity_unsynchronized(count + end - start);
425 for (; start < end; ++start)
426 value[count++] = seq.charAt(start);
432 * Append the <code>String</code> value of the argument to this
433 * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
434 * to <code>String</code>.
436 * @param inum the <code>int</code> to convert and append
437 * @return this <code>StringBuffer</code>
438 * @see String#valueOf(int)
440 // This is native in libgcj, for efficiency.
441 public CPStringBuilder append(int inum)
443 return append(String.valueOf(inum));
447 * Append the <code>String</code> value of the argument to this
448 * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
449 * to <code>String</code>.
451 * @param lnum the <code>long</code> to convert and append
452 * @return this <code>StringBuffer</code>
453 * @see String#valueOf(long)
455 public CPStringBuilder append(long lnum)
457 return append(Long.toString(lnum, 10));
461 * Append the <code>String</code> value of the argument to this
462 * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
463 * to <code>String</code>.
465 * @param fnum the <code>float</code> to convert and append
466 * @return this <code>StringBuffer</code>
467 * @see String#valueOf(float)
469 public CPStringBuilder append(float fnum)
471 return append(Float.toString(fnum));
475 * Append the <code>String</code> value of the argument to this
476 * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
477 * to <code>String</code>.
479 * @param dnum the <code>double</code> to convert and append
480 * @return this <code>StringBuffer</code>
481 * @see String#valueOf(double)
483 public CPStringBuilder append(double dnum)
485 return append(Double.toString(dnum));
489 * Append the code point to this <code>StringBuffer</code>.
490 * This is like #append(char), but will append two characters
491 * if a supplementary code point is given.
493 * @param code the code point to append
494 * @return this <code>StringBuffer</code>
495 * @see Character#toChars(int, char[], int)
498 public CPStringBuilder appendCodePoint(int code)
500 int len = Character.charCount(code);
501 ensureCapacity_unsynchronized(count + len);
502 Character.toChars(code, value, count);
508 * Delete characters from this <code>StringBuffer</code>.
509 * <code>delete(10, 12)</code> will delete 10 and 11, but not 12. It is
510 * harmless for end to be larger than length().
512 * @param start the first character to delete
513 * @param end the index after the last character to delete
514 * @return this <code>StringBuffer</code>
515 * @throws StringIndexOutOfBoundsException if start or end are out of bounds
518 public CPStringBuilder delete(int start, int end)
520 if (start < 0 || start > count || start > end)
521 throw new StringIndexOutOfBoundsException(start);
524 ensureCapacity_unsynchronized(count);
525 if (count - end != 0)
526 System.arraycopy(value, end, value, start, count - end);
527 count -= end - start;
532 * Delete a character from this <code>StringBuffer</code>.
534 * @param index the index of the character to delete
535 * @return this <code>StringBuffer</code>
536 * @throws StringIndexOutOfBoundsException if index is out of bounds
539 public CPStringBuilder deleteCharAt(int index)
541 return delete(index, index + 1);
545 * Replace characters between index <code>start</code> (inclusive) and
546 * <code>end</code> (exclusive) with <code>str</code>. If <code>end</code>
547 * is larger than the size of this StringBuffer, all characters after
548 * <code>start</code> are replaced.
550 * @param start the beginning index of characters to delete (inclusive)
551 * @param end the ending index of characters to delete (exclusive)
552 * @param str the new <code>String</code> to insert
553 * @return this <code>StringBuffer</code>
554 * @throws StringIndexOutOfBoundsException if start or end are out of bounds
555 * @throws NullPointerException if str is null
558 public CPStringBuilder replace(int start, int end, String str)
560 if (start < 0 || start > count || start > end)
561 throw new StringIndexOutOfBoundsException(start);
563 int len = str.length();
564 // Calculate the difference in 'count' after the replace.
565 int delta = len - (end > count ? count : end) + start;
566 ensureCapacity_unsynchronized(count + delta);
568 if (delta != 0 && end < count)
569 System.arraycopy(value, end, value, end + delta, count - end);
571 str.getChars(0, len, value, start);
577 * Insert a subarray of the <code>char[]</code> argument into this
578 * <code>StringBuffer</code>.
580 * @param offset the place to insert in this buffer
581 * @param str the <code>char[]</code> to insert
582 * @param str_offset the index in <code>str</code> to start inserting from
583 * @param len the number of characters to insert
584 * @return this <code>StringBuffer</code>
585 * @throws NullPointerException if <code>str</code> is <code>null</code>
586 * @throws StringIndexOutOfBoundsException if any index is out of bounds
589 public CPStringBuilder insert(int offset, char[] str, int str_offset, int len)
591 if (offset < 0 || offset > count || len < 0
592 || str_offset < 0 || str_offset > str.length - len)
593 throw new StringIndexOutOfBoundsException();
594 ensureCapacity_unsynchronized(count + len);
595 System.arraycopy(value, offset, value, offset + len, count - offset);
596 System.arraycopy(str, str_offset, value, offset, len);
602 * Insert the <code>String</code> value of the argument into this
603 * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
604 * to <code>String</code>.
606 * @param offset the place to insert in this buffer
607 * @param obj the <code>Object</code> to convert and insert
608 * @return this <code>StringBuffer</code>
609 * @exception StringIndexOutOfBoundsException if offset is out of bounds
610 * @see String#valueOf(Object)
612 public CPStringBuilder insert(int offset, Object obj)
614 return insert(offset, obj == null ? "null" : obj.toString());
618 * Insert the <code>String</code> argument into this
619 * <code>StringBuffer</code>. If str is null, the String "null" is used
622 * @param offset the place to insert in this buffer
623 * @param str the <code>String</code> to insert
624 * @return this <code>StringBuffer</code>
625 * @throws StringIndexOutOfBoundsException if offset is out of bounds
627 public CPStringBuilder insert(int offset, String str)
629 if (offset < 0 || offset > count)
630 throw new StringIndexOutOfBoundsException(offset);
633 int len = str.length();
634 ensureCapacity_unsynchronized(count + len);
635 System.arraycopy(value, offset, value, offset + len, count - offset);
636 str.getChars(0, len, value, offset);
642 * Insert the <code>CharSequence</code> argument into this
643 * <code>StringBuffer</code>. If the sequence is null, the String
644 * "null" is used instead.
646 * @param offset the place to insert in this buffer
647 * @param sequence the <code>CharSequence</code> to insert
648 * @return this <code>StringBuffer</code>
649 * @throws IndexOutOfBoundsException if offset is out of bounds
652 public CPStringBuilder insert(int offset, CharSequence sequence)
654 if (sequence == null)
656 return insert(offset, sequence, 0, sequence.length());
660 * Insert a subsequence of the <code>CharSequence</code> argument into this
661 * <code>StringBuffer</code>. If the sequence is null, the String
662 * "null" is used instead.
664 * @param offset the place to insert in this buffer
665 * @param sequence the <code>CharSequence</code> to insert
666 * @param start the starting index of the subsequence
667 * @param end one past the ending index of the subsequence
668 * @return this <code>StringBuffer</code>
669 * @throws IndexOutOfBoundsException if offset, start,
670 * or end are out of bounds
673 public CPStringBuilder insert(int offset, CharSequence sequence, int start, int end)
675 if (sequence == null)
677 if (start < 0 || end < 0 || start > end || end > sequence.length())
678 throw new IndexOutOfBoundsException();
679 int len = end - start;
680 ensureCapacity_unsynchronized(count + len);
681 System.arraycopy(value, offset, value, offset + len, count - offset);
682 for (int i = start; i < end; ++i)
683 value[offset++] = sequence.charAt(i);
689 * Insert the <code>char[]</code> argument into this
690 * <code>StringBuffer</code>.
692 * @param offset the place to insert in this buffer
693 * @param data the <code>char[]</code> to insert
694 * @return this <code>StringBuffer</code>
695 * @throws NullPointerException if <code>data</code> is <code>null</code>
696 * @throws StringIndexOutOfBoundsException if offset is out of bounds
697 * @see #insert(int, char[], int, int)
699 public CPStringBuilder insert(int offset, char[] data)
701 return insert(offset, data, 0, data.length);
705 * Insert the <code>String</code> value of the argument into this
706 * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
707 * to <code>String</code>.
709 * @param offset the place to insert in this buffer
710 * @param bool the <code>boolean</code> to convert and insert
711 * @return this <code>StringBuffer</code>
712 * @throws StringIndexOutOfBoundsException if offset is out of bounds
713 * @see String#valueOf(boolean)
715 public CPStringBuilder insert(int offset, boolean bool)
717 return insert(offset, bool ? "true" : "false");
721 * Insert the <code>char</code> argument into this <code>StringBuffer</code>.
723 * @param offset the place to insert in this buffer
724 * @param ch the <code>char</code> to insert
725 * @return this <code>StringBuffer</code>
726 * @throws StringIndexOutOfBoundsException if offset is out of bounds
728 public CPStringBuilder insert(int offset, char ch)
730 if (offset < 0 || offset > count)
731 throw new StringIndexOutOfBoundsException(offset);
732 ensureCapacity_unsynchronized(count + 1);
733 System.arraycopy(value, offset, value, offset + 1, count - offset);
740 * Insert the <code>String</code> value of the argument into this
741 * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
742 * to <code>String</code>.
744 * @param offset the place to insert in this buffer
745 * @param inum the <code>int</code> to convert and insert
746 * @return this <code>StringBuffer</code>
747 * @throws StringIndexOutOfBoundsException if offset is out of bounds
748 * @see String#valueOf(int)
750 public CPStringBuilder insert(int offset, int inum)
752 return insert(offset, String.valueOf(inum));
756 * Insert the <code>String</code> value of the argument into this
757 * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
758 * to <code>String</code>.
760 * @param offset the place to insert in this buffer
761 * @param lnum the <code>long</code> to convert and insert
762 * @return this <code>StringBuffer</code>
763 * @throws StringIndexOutOfBoundsException if offset is out of bounds
764 * @see String#valueOf(long)
766 public CPStringBuilder insert(int offset, long lnum)
768 return insert(offset, Long.toString(lnum, 10));
772 * Insert the <code>String</code> value of the argument into this
773 * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
774 * to <code>String</code>.
776 * @param offset the place to insert in this buffer
777 * @param fnum the <code>float</code> to convert and insert
778 * @return this <code>StringBuffer</code>
779 * @throws StringIndexOutOfBoundsException if offset is out of bounds
780 * @see String#valueOf(float)
782 public CPStringBuilder insert(int offset, float fnum)
784 return insert(offset, Float.toString(fnum));
788 * Insert the <code>String</code> value of the argument into this
789 * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
790 * to <code>String</code>.
792 * @param offset the place to insert in this buffer
793 * @param dnum the <code>double</code> to convert and insert
794 * @return this <code>StringBuffer</code>
795 * @throws StringIndexOutOfBoundsException if offset is out of bounds
796 * @see String#valueOf(double)
798 public CPStringBuilder insert(int offset, double dnum)
800 return insert(offset, Double.toString(dnum));
804 * Finds the first instance of a substring in this StringBuilder.
806 * @param str String to find
807 * @return location (base 0) of the String, or -1 if not found
808 * @throws NullPointerException if str is null
809 * @see #indexOf(String, int)
811 public int indexOf(String str)
813 return indexOf(str, 0);
817 * Finds the first instance of a String in this StringBuffer, starting at
818 * a given index. If starting index is less than 0, the search starts at
819 * the beginning of this String. If the starting index is greater than the
820 * length of this String, or the substring is not found, -1 is returned.
822 * @param str String to find
823 * @param fromIndex index to start the search
824 * @return location (base 0) of the String, or -1 if not found
825 * @throws NullPointerException if str is null
828 public int indexOf(String str, int fromIndex)
832 int olength = str.length();
833 int limit = count - olength;
834 String s = VMCPStringBuilder.toString(value, 0, count);
835 for (; fromIndex <= limit; ++fromIndex)
836 if (s.regionMatches(fromIndex, str, 0, olength))
842 * Finds the last instance of a substring in this StringBuffer.
844 * @param str String to find
845 * @return location (base 0) of the String, or -1 if not found
846 * @throws NullPointerException if str is null
847 * @see #lastIndexOf(String, int)
850 public int lastIndexOf(String str)
852 return lastIndexOf(str, count - str.length());
856 * Finds the last instance of a String in this StringBuffer, starting at a
857 * given index. If starting index is greater than the maximum valid index,
858 * then the search begins at the end of this String. If the starting index
859 * is less than zero, or the substring is not found, -1 is returned.
861 * @param str String to find
862 * @param fromIndex index to start the search
863 * @return location (base 0) of the String, or -1 if not found
864 * @throws NullPointerException if str is null
867 public int lastIndexOf(String str, int fromIndex)
869 fromIndex = Math.min(fromIndex, count - str.length());
870 String s = VMCPStringBuilder.toString(value, 0, count);
871 int olength = str.length();
872 for ( ; fromIndex >= 0; fromIndex--)
873 if (s.regionMatches(fromIndex, str, 0, olength))
879 * Reverse the characters in this StringBuffer. The same sequence of
880 * characters exists, but in the reverse index ordering.
882 * @return this <code>StringBuffer</code>
884 public CPStringBuilder reverse()
886 // Call ensureCapacity to enforce copy-on-write.
887 ensureCapacity_unsynchronized(count);
888 for (int i = count >> 1, j = count - i; --i >= 0; ++j)
898 * This may reduce the amount of memory used by the StringBuffer,
899 * by resizing the internal array to remove unused space. However,
900 * this method is not required to resize, so this behavior cannot
904 public void trimToSize()
906 int wouldSave = value.length - count;
907 // Some random heuristics: if we save less than 20 characters, who
911 // If we save more than 200 characters, shrink.
912 // If we save more than 1/4 of the buffer, shrink.
913 if (wouldSave > 200 || wouldSave * 4 > value.length)
915 char[] newValue = new char[count];
916 System.arraycopy(value, 0, newValue, 0, count);
922 * Return the number of code points between two indices in the
923 * <code>StringBuffer</code>. An unpaired surrogate counts as a
924 * code point for this purpose. Characters outside the indicated
925 * range are not examined, even if the range ends in the middle of a
928 * @param start the starting index
929 * @param end one past the ending index
930 * @return the number of code points
933 public int codePointCount(int start, int end)
935 if (start < 0 || end >= count || start > end)
936 throw new StringIndexOutOfBoundsException();
941 char base = value[start];
942 if (base < Character.MIN_HIGH_SURROGATE
943 || base > Character.MAX_HIGH_SURROGATE
946 || value[start + 1] < Character.MIN_LOW_SURROGATE
947 || value[start + 1] > Character.MAX_LOW_SURROGATE)
963 * Starting at the given index, this counts forward by the indicated
964 * number of code points, and then returns the resulting index. An
965 * unpaired surrogate counts as a single code point for this
968 * @param start the starting index
969 * @param codePoints the number of code points
970 * @return the resulting index
973 public int offsetByCodePoints(int start, int codePoints)
975 while (codePoints > 0)
977 char base = value[start];
978 if (base < Character.MIN_HIGH_SURROGATE
979 || base > Character.MAX_HIGH_SURROGATE
981 || value[start + 1] < Character.MIN_LOW_SURROGATE
982 || value[start + 1] > Character.MAX_LOW_SURROGATE)
998 * Increase the capacity of this <code>StringBuilder</code>. This will
999 * ensure that an expensive growing operation will not occur until
1000 * <code>minimumCapacity</code> is reached. The buffer is grown to the
1001 * larger of <code>minimumCapacity</code> and
1002 * <code>capacity() * 2 + 2</code>, if it is not already large enough.
1004 * @param minimumCapacity the new capacity
1007 protected void ensureCapacity_unsynchronized(int minimumCapacity)
1009 if (minimumCapacity > value.length)
1011 int max = value.length * 2 + 2;
1012 minimumCapacity = (minimumCapacity < max ? max : minimumCapacity);
1013 char[] nb = new char[minimumCapacity];
1014 System.arraycopy(value, 0, nb, 0, count);
1020 * Get the length of the <code>String</code> this <code>StringBuilder</code>
1021 * would create. Not to be confused with the <em>capacity</em> of the
1022 * <code>StringBuilder</code>.
1024 * @return the length of this <code>StringBuilder</code>
1026 * @see #setLength(int)
1034 * Creates a substring of this StringBuilder, starting at a specified index
1035 * and ending at one character before a specified index. This is implemented
1036 * the same as <code>substring(beginIndex, endIndex)</code>, to satisfy
1037 * the CharSequence interface.
1039 * @param beginIndex index to start at (inclusive, base 0)
1040 * @param endIndex index to end at (exclusive)
1041 * @return new String which is a substring of this StringBuilder
1042 * @throws IndexOutOfBoundsException if beginIndex or endIndex is out of
1044 * @see #substring(int, int)
1046 public CharSequence subSequence(int beginIndex, int endIndex)
1048 return substring(beginIndex, endIndex);
1052 * Creates a substring of this StringBuilder, starting at a specified index
1053 * and ending at one character before a specified index.
1055 * @param beginIndex index to start at (inclusive, base 0)
1056 * @param endIndex index to end at (exclusive)
1057 * @return new String which is a substring of this StringBuilder
1058 * @throws StringIndexOutOfBoundsException if beginIndex or endIndex is out
1061 public String substring(int beginIndex, int endIndex)
1063 if (beginIndex < 0 || endIndex > count || endIndex < beginIndex)
1064 throw new StringIndexOutOfBoundsException();
1065 int len = endIndex - beginIndex;
1068 return VMCPStringBuilder.toString(value, beginIndex, len);
1072 * Convert this <code>StringBuilder</code> to a <code>String</code>. The
1073 * String is composed of the characters currently in this StringBuilder. Note
1074 * that the result is not a copy, so future modifications to this buffer
1075 * do affect the String.
1077 * @return the characters in this StringBuilder
1079 public String toString()
1081 return VMCPStringBuilder.toString(value, 0, count);