Fix mysterious unremovable file part 2 ?
[cacao.git] / src / classes / gnuclasspath / gnu / java / lang / CPStringBuilder.java
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.
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 gnu.java.lang;
40
41 import java.io.Serializable;
42
43 /**
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
48  * methods.
49  */
50 public final class CPStringBuilder
51   implements Serializable, CharSequence, Appendable
52 {
53
54   /**
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.
58    *
59    * @serial the number of characters in the buffer
60    */
61   int count;
62
63   /**
64    * The buffer.  Note that this has permissions set this way so that String
65    * can get the value.
66    *
67    * @serial the buffer
68    */
69   char[] value;
70
71   /**
72    * The default capacity of a buffer.
73    */
74   private static final int DEFAULT_CAPACITY = 16;
75
76   /**
77    * Create a new CPStringBuilder with default capacity 16.
78    */
79   public CPStringBuilder()
80   {
81     this(DEFAULT_CAPACITY);
82   }
83
84   /**
85    * Create an empty <code>StringBuffer</code> with the specified initial
86    * capacity.
87    *
88    * @param capacity the initial capacity
89    * @throws NegativeArraySizeException if capacity is negative
90    */
91   public CPStringBuilder(int capacity)
92   {
93     value = new char[capacity];
94   }
95
96   /**
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
99    * String plus 16.
100    *
101    * @param str the <code>String</code> to convert
102    * @throws NullPointerException if str is null
103    */
104   public CPStringBuilder(String str)
105   {
106     count = str.length();
107     value = new char[count + DEFAULT_CAPACITY];
108     str.getChars(0, count, value, 0);
109   }
110
111   /**
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.
116    *
117    * @param seq the initializing <code>CharSequence</code>
118    * @throws NullPointerException if str is null
119    * @since 1.5
120    */
121   public CPStringBuilder(CharSequence seq)
122   {
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);
128   }
129
130   /**
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.
136    *
137    * @param minimumCapacity the new capacity
138    * @see #capacity()
139    */
140   public void ensureCapacity(int minimumCapacity)
141   {
142     ensureCapacity_unsynchronized(minimumCapacity);
143   }
144
145   /**
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.
151    *
152    * @param newLength the new length
153    * @throws IndexOutOfBoundsException if the new length is negative
154    *         (while unspecified, this is a StringIndexOutOfBoundsException)
155    * @see #length()
156    */
157   public void setLength(int newLength)
158   {
159     if (newLength < 0)
160       throw new StringIndexOutOfBoundsException(newLength);
161
162     int valueLength = value.length;
163
164     /* Always call ensureCapacity_unsynchronized in order to preserve
165        copy-on-write semantics.  */
166     ensureCapacity_unsynchronized(newLength);
167
168     if (newLength < valueLength)
169       {
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'.  */
173         count = newLength;
174       }
175     else
176       {
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';
181       }
182   }
183
184   /**
185    * Get the character at the specified index.
186    *
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 &gt;= length()
190    *         (while unspecified, this is a StringIndexOutOfBoundsException)
191    */
192   public char charAt(int index)
193   {
194     if (index < 0 || index >= count)
195       throw new StringIndexOutOfBoundsException(index);
196     return value[index];
197   }
198
199   /**
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 &gt;= length()
207    * @since 1.5
208    */
209   public int codePointAt(int index)
210   {
211     return Character.codePointAt(value, index, count);
212   }
213
214   /**
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 &gt;= length()
221    * @since 1.5
222    */
223   public int codePointBefore(int index)
224   {
225     // Character.codePointBefore() doesn't perform this check.  We
226     // could use the CharSequence overload, but this is just as easy.
227     if (index >= count)
228       throw new IndexOutOfBoundsException();
229     return Character.codePointBefore(value, index, 1);
230   }
231
232   /**
233    * Get the specified array of characters. <code>srcOffset - srcEnd</code>
234    * characters will be copied into the array you pass in.
235    *
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)
246    */
247   public void getChars(int srcOffset, int srcEnd,
248                        char[] dst, int dstOffset)
249   {
250     if (srcOffset < 0 || srcEnd > count || srcEnd < srcOffset)
251       throw new StringIndexOutOfBoundsException();
252     System.arraycopy(value, srcOffset, dst, dstOffset, srcEnd - srcOffset);
253   }
254
255   /**
256    * Set the character at the specified index.
257    *
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 &gt;= length()
261    *         (while unspecified, this is a StringIndexOutOfBoundsException)
262    */
263   public void setCharAt(int index, char ch)
264   {
265     if (index < 0 || index >= count)
266       throw new StringIndexOutOfBoundsException(index);
267     // Call ensureCapacity to enforce copy-on-write.
268     ensureCapacity_unsynchronized(count);
269     value[index] = ch;
270   }
271
272   /**
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>.
276    *
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)
281    */
282   public CPStringBuilder append(Object obj)
283   {
284     return append(String.valueOf(obj));
285   }
286
287   /**
288    * Append the <code>String</code> to this <code>StringBuffer</code>. If
289    * str is null, the String "null" is appended.
290    *
291    * @param str the <code>String</code> to append
292    * @return this <code>StringBuffer</code>
293    */
294   public CPStringBuilder append(String str)
295   {
296     if (str == null)
297       str = "null";
298     int len = str.length();
299     ensureCapacity_unsynchronized(count + len);
300     str.getChars(0, len, value, count);
301     count += len;
302     return this;
303   }
304
305   /**
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.
309    *
310    * @param stringBuffer the <code>StringBuilder</code> to convert and append
311    * @return this <code>StringBuilder</code>
312    * @see #append(Object)
313    */
314   public CPStringBuilder append(StringBuffer stringBuffer)
315   {
316     if (stringBuffer == null)
317       return append("null");
318     synchronized (stringBuffer)
319       {
320         int len = stringBuffer.length();
321         ensureCapacity(count + len);
322         stringBuffer.getChars(0, len, value, count);
323         count += len;
324       }
325     return this;
326   }
327
328   /**
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.
332    *
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)
337    */
338   public CPStringBuilder append(char[] data)
339   {
340     return append(data, 0, data.length);
341   }
342
343   /**
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
347    * of null.
348    *
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)
356    */
357   public CPStringBuilder append(char[] data, int offset, int count)
358   {
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);
363     this.count += count;
364     return this;
365   }
366
367   /**
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>.
371    *
372    * @param bool the <code>boolean</code> to convert and append
373    * @return this <code>StringBuffer</code>
374    * @see String#valueOf(boolean)
375    */
376   public CPStringBuilder append(boolean bool)
377   {
378     return append(bool ? "true" : "false");
379   }
380
381   /**
382    * Append the <code>char</code> to this <code>StringBuffer</code>.
383    *
384    * @param ch the <code>char</code> to append
385    * @return this <code>StringBuffer</code>
386    */
387   public CPStringBuilder append(char ch)
388   {
389     ensureCapacity_unsynchronized(count + 1);
390     value[count++] = ch;
391     return this;
392   }
393
394   /**
395    * Append the characters in the <code>CharSequence</code> to this
396    * buffer.
397    *
398    * @param seq the <code>CharSequence</code> providing the characters
399    * @return this <code>StringBuffer</code>
400    * @since 1.5
401    */
402   public CPStringBuilder append(CharSequence seq)
403   {
404     return append(seq, 0, seq.length());
405   }
406
407   /**
408    * Append some characters from the <code>CharSequence</code> to this
409    * buffer.  If the argument is null, the four characters "null" are
410    * appended.
411    *
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>
416    * @since 1.5
417    */
418   public CPStringBuilder append(CharSequence seq, int start, int end)
419   {
420     if (seq == null)
421       return append("null");
422     if (end - start > 0)
423       {
424         ensureCapacity_unsynchronized(count + end - start);
425         for (; start < end; ++start)
426           value[count++] = seq.charAt(start);
427       }
428     return this;
429   }
430
431   /**
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>.
435    *
436    * @param inum the <code>int</code> to convert and append
437    * @return this <code>StringBuffer</code>
438    * @see String#valueOf(int)
439    */
440   // This is native in libgcj, for efficiency.
441   public CPStringBuilder append(int inum)
442   {
443     return append(String.valueOf(inum));
444   }
445
446   /**
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>.
450    *
451    * @param lnum the <code>long</code> to convert and append
452    * @return this <code>StringBuffer</code>
453    * @see String#valueOf(long)
454    */
455   public CPStringBuilder append(long lnum)
456   {
457     return append(Long.toString(lnum, 10));
458   }
459
460   /**
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>.
464    *
465    * @param fnum the <code>float</code> to convert and append
466    * @return this <code>StringBuffer</code>
467    * @see String#valueOf(float)
468    */
469   public CPStringBuilder append(float fnum)
470   {
471     return append(Float.toString(fnum));
472   }
473
474   /**
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>.
478    *
479    * @param dnum the <code>double</code> to convert and append
480    * @return this <code>StringBuffer</code>
481    * @see String#valueOf(double)
482    */
483   public CPStringBuilder append(double dnum)
484   {
485     return append(Double.toString(dnum));
486   }
487
488   /**
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.
492    *
493    * @param code the code point to append
494    * @return this <code>StringBuffer</code>
495    * @see Character#toChars(int, char[], int)
496    * @since 1.5
497    */
498   public CPStringBuilder appendCodePoint(int code)
499   {
500     int len = Character.charCount(code);
501     ensureCapacity_unsynchronized(count + len);
502     Character.toChars(code, value, count);
503     count += len;
504     return this;
505   }
506
507   /**
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().
511    *
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
516    * @since 1.2
517    */
518   public CPStringBuilder delete(int start, int end)
519   {
520     if (start < 0 || start > count || start > end)
521       throw new StringIndexOutOfBoundsException(start);
522     if (end > count)
523       end = count;
524     ensureCapacity_unsynchronized(count);
525     if (count - end != 0)
526       System.arraycopy(value, end, value, start, count - end);
527     count -= end - start;
528     return this;
529   }
530
531   /**
532    * Delete a character from this <code>StringBuffer</code>.
533    *
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
537    * @since 1.2
538    */
539   public CPStringBuilder deleteCharAt(int index)
540   {
541     return delete(index, index + 1);
542   }
543
544   /**
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.
549    *
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
556    * @since 1.2
557    */
558   public CPStringBuilder replace(int start, int end, String str)
559   {
560     if (start < 0 || start > count || start > end)
561       throw new StringIndexOutOfBoundsException(start);
562
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);
567
568     if (delta != 0 && end < count)
569       System.arraycopy(value, end, value, end + delta, count - end);
570
571     str.getChars(0, len, value, start);
572     count += delta;
573     return this;
574   }
575
576   /**
577    * Insert a subarray of the <code>char[]</code> argument into this
578    * <code>StringBuffer</code>.
579    *
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
587    * @since 1.2
588    */
589   public CPStringBuilder insert(int offset, char[] str, int str_offset, int len)
590   {
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);
597     count += len;
598     return this;
599   }
600
601   /**
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>.
605    *
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)
611    */
612   public CPStringBuilder insert(int offset, Object obj)
613   {
614     return insert(offset, obj == null ? "null" : obj.toString());
615   }
616
617   /**
618    * Insert the <code>String</code> argument into this
619    * <code>StringBuffer</code>. If str is null, the String "null" is used
620    * instead.
621    *
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
626    */
627   public CPStringBuilder insert(int offset, String str)
628   {
629     if (offset < 0 || offset > count)
630       throw new StringIndexOutOfBoundsException(offset);
631     if (str == null)
632       str = "null";
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);
637     count += len;
638     return this;
639   }
640
641   /**
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.
645    *
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
650    * @since 1.5
651    */
652   public CPStringBuilder insert(int offset, CharSequence sequence)
653   {
654     if (sequence == null)
655       sequence = "null";
656     return insert(offset, sequence, 0, sequence.length());
657   }
658
659   /**
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.
663    *
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
671    * @since 1.5
672    */
673   public CPStringBuilder insert(int offset, CharSequence sequence, int start, int end)
674   {
675     if (sequence == null)
676       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);
684     count += len;
685     return this;
686   }
687
688   /**
689    * Insert the <code>char[]</code> argument into this
690    * <code>StringBuffer</code>.
691    *
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)
698    */
699   public CPStringBuilder insert(int offset, char[] data)
700   {
701     return insert(offset, data, 0, data.length);
702   }
703
704   /**
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>.
708    *
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)
714    */
715   public CPStringBuilder insert(int offset, boolean bool)
716   {
717     return insert(offset, bool ? "true" : "false");
718   }
719
720   /**
721    * Insert the <code>char</code> argument into this <code>StringBuffer</code>.
722    *
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
727    */
728   public CPStringBuilder insert(int offset, char ch)
729   {
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);
734     value[offset] = ch;
735     count++;
736     return this;
737   }
738
739   /**
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>.
743    *
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)
749    */
750   public CPStringBuilder insert(int offset, int inum)
751   {
752     return insert(offset, String.valueOf(inum));
753   }
754
755   /**
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>.
759    *
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)
765    */
766   public CPStringBuilder insert(int offset, long lnum)
767   {
768     return insert(offset, Long.toString(lnum, 10));
769   }
770
771   /**
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>.
775    *
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)
781    */
782   public CPStringBuilder insert(int offset, float fnum)
783   {
784     return insert(offset, Float.toString(fnum));
785   }
786
787   /**
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>.
791    *
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)
797    */
798   public CPStringBuilder insert(int offset, double dnum)
799   {
800     return insert(offset, Double.toString(dnum));
801   }
802
803   /**
804    * Finds the first instance of a substring in this StringBuilder.
805    *
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)
810    */
811   public int indexOf(String str)
812   {
813     return indexOf(str, 0);
814   }
815
816   /**
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.
821    *
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
826    * @since 1.4
827    */
828   public int indexOf(String str, int fromIndex)
829   {
830     if (fromIndex < 0)
831       fromIndex = 0;
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))
837         return fromIndex;
838     return -1;
839   }
840
841   /**
842    * Finds the last instance of a substring in this StringBuffer.
843    *
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)
848    * @since 1.4
849    */
850   public int lastIndexOf(String str)
851   {
852     return lastIndexOf(str, count - str.length());
853   }
854
855   /**
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.
860    *
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
865    * @since 1.4
866    */
867   public int lastIndexOf(String str, int fromIndex)
868   {
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))
874         return fromIndex;
875     return -1;
876   }
877
878   /**
879    * Reverse the characters in this StringBuffer. The same sequence of
880    * characters exists, but in the reverse index ordering.
881    *
882    * @return this <code>StringBuffer</code>
883    */
884   public CPStringBuilder reverse()
885   {
886     // Call ensureCapacity to enforce copy-on-write.
887     ensureCapacity_unsynchronized(count);
888     for (int i = count >> 1, j = count - i; --i >= 0; ++j)
889       {
890         char c = value[i];
891         value[i] = value[j];
892         value[j] = c;
893       }
894     return this;
895   }
896
897   /**
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
901    * be relied upon.
902    * @since 1.5
903    */
904   public void trimToSize()
905   {
906     int wouldSave = value.length - count;
907     // Some random heuristics: if we save less than 20 characters, who
908     // cares.
909     if (wouldSave < 20)
910       return;
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)
914       {
915         char[] newValue = new char[count];
916         System.arraycopy(value, 0, newValue, 0, count);
917         value = newValue;
918       }
919   }
920
921   /**
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
926    * surrogate pair.
927    *
928    * @param start the starting index
929    * @param end one past the ending index
930    * @return the number of code points
931    * @since 1.5
932    */
933   public int codePointCount(int start, int end)
934   {
935     if (start < 0 || end >= count || start > end)
936       throw new StringIndexOutOfBoundsException();
937
938     int count = 0;
939     while (start < end)
940       {
941         char base = value[start];
942         if (base < Character.MIN_HIGH_SURROGATE
943             || base > Character.MAX_HIGH_SURROGATE
944             || start == end
945             || start == count
946             || value[start + 1] < Character.MIN_LOW_SURROGATE
947             || value[start + 1] > Character.MAX_LOW_SURROGATE)
948           {
949             // Nothing.
950           }
951         else
952           {
953             // Surrogate pair.
954             ++start;
955           }
956         ++start;
957         ++count;
958       }
959     return count;
960   }
961
962   /**
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
966    * purpose.
967    *
968    * @param start the starting index
969    * @param codePoints the number of code points
970    * @return the resulting index
971    * @since 1.5
972    */
973   public int offsetByCodePoints(int start, int codePoints)
974   {
975     while (codePoints > 0)
976       {
977         char base = value[start];
978         if (base < Character.MIN_HIGH_SURROGATE
979             || base > Character.MAX_HIGH_SURROGATE
980             || start == count
981             || value[start + 1] < Character.MIN_LOW_SURROGATE
982             || value[start + 1] > Character.MAX_LOW_SURROGATE)
983           {
984             // Nothing.
985           }
986         else
987           {
988             // Surrogate pair.
989             ++start;
990           }
991         ++start;
992         --codePoints;
993       }
994     return start;
995   }
996
997   /**
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.
1003    *
1004    * @param minimumCapacity the new capacity
1005    * @see #capacity()
1006    */
1007   protected void ensureCapacity_unsynchronized(int minimumCapacity)
1008   {
1009     if (minimumCapacity > value.length)
1010       {
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);
1015         value = nb;
1016       }
1017   }
1018
1019   /**
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>.
1023    *
1024    * @return the length of this <code>StringBuilder</code>
1025    * @see #capacity()
1026    * @see #setLength(int)
1027    */
1028   public int length()
1029   {
1030     return count;
1031   }
1032
1033   /**
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.
1038    *
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
1043    *         bounds
1044    * @see #substring(int, int)
1045    */
1046   public CharSequence subSequence(int beginIndex, int endIndex)
1047   {
1048     return substring(beginIndex, endIndex);
1049   }
1050
1051   /**
1052    * Creates a substring of this StringBuilder, starting at a specified index
1053    * and ending at one character before a specified index.
1054    *
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
1059    *         of bounds
1060    */
1061   public String substring(int beginIndex, int endIndex)
1062   {
1063     if (beginIndex < 0 || endIndex > count || endIndex < beginIndex)
1064       throw new StringIndexOutOfBoundsException();
1065     int len = endIndex - beginIndex;
1066     if (len == 0)
1067       return "";
1068     return VMCPStringBuilder.toString(value, beginIndex, len);
1069   }
1070
1071   /**
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.
1076    *
1077    * @return the characters in this StringBuilder
1078    */
1079   public String toString()
1080   {
1081     return VMCPStringBuilder.toString(value, 0, count);
1082   }
1083
1084 }