2 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
28 import org.apidesign.bck2brwsr.core.JavaScriptBody;
31 * A mutable sequence of characters.
33 * Implements a modifiable string. At any point in time it contains some
34 * particular sequence of characters, but the length and content of the
35 * sequence can be changed through certain method calls.
37 * @author Michael McCloskey
38 * @author Martin Buchholz
42 abstract class AbstractStringBuilder implements Appendable, CharSequence {
44 * The value is used for character storage.
49 * The count is the number of characters used.
54 * This no-arg constructor is necessary for serialization of subclasses.
56 AbstractStringBuilder() {
60 * Creates an AbstractStringBuilder of the specified capacity.
62 AbstractStringBuilder(int capacity) {
63 value = new char[capacity];
67 * Returns the length (character count).
69 * @return the length of the sequence of characters currently
70 * represented by this object
77 * Returns the current capacity. The capacity is the amount of storage
78 * available for newly inserted characters, beyond which an allocation
81 * @return the current capacity
83 public int capacity() {
88 * Ensures that the capacity is at least equal to the specified minimum.
89 * If the current capacity is less than the argument, then a new internal
90 * array is allocated with greater capacity. The new capacity is the
93 * <li>The <code>minimumCapacity</code> argument.
94 * <li>Twice the old capacity, plus <code>2</code>.
96 * If the <code>minimumCapacity</code> argument is nonpositive, this
97 * method takes no action and simply returns.
99 * @param minimumCapacity the minimum desired capacity.
101 public void ensureCapacity(int minimumCapacity) {
102 if (minimumCapacity > 0)
103 ensureCapacityInternal(minimumCapacity);
107 * This method has the same contract as ensureCapacity, but is
108 * never synchronized.
110 private void ensureCapacityInternal(int minimumCapacity) {
111 // overflow-conscious code
112 if (minimumCapacity - value.length > 0)
113 expandCapacity(minimumCapacity);
117 * This implements the expansion semantics of ensureCapacity with no
118 * size check or synchronization.
120 void expandCapacity(int minimumCapacity) {
121 int newCapacity = value.length * 2 + 2;
122 if (newCapacity - minimumCapacity < 0)
123 newCapacity = minimumCapacity;
124 if (newCapacity < 0) {
125 if (minimumCapacity < 0) // overflow
126 throw new OutOfMemoryError();
127 newCapacity = Integer.MAX_VALUE;
129 value = copyOf(value, newCapacity);
133 * Attempts to reduce storage used for the character sequence.
134 * If the buffer is larger than necessary to hold its current sequence of
135 * characters, then it may be resized to become more space efficient.
136 * Calling this method may, but is not required to, affect the value
137 * returned by a subsequent call to the {@link #capacity()} method.
139 public void trimToSize() {
140 if (count < value.length) {
141 value = copyOf(value, count);
146 * Sets the length of the character sequence.
147 * The sequence is changed to a new character sequence
148 * whose length is specified by the argument. For every nonnegative
149 * index <i>k</i> less than <code>newLength</code>, the character at
150 * index <i>k</i> in the new character sequence is the same as the
151 * character at index <i>k</i> in the old sequence if <i>k</i> is less
152 * than the length of the old character sequence; otherwise, it is the
153 * null character <code>'\u0000'</code>.
155 * In other words, if the <code>newLength</code> argument is less than
156 * the current length, the length is changed to the specified length.
158 * If the <code>newLength</code> argument is greater than or equal
159 * to the current length, sufficient null characters
160 * (<code>'\u0000'</code>) are appended so that
161 * length becomes the <code>newLength</code> argument.
163 * The <code>newLength</code> argument must be greater than or equal
166 * @param newLength the new length
167 * @throws IndexOutOfBoundsException if the
168 * <code>newLength</code> argument is negative.
170 public void setLength(int newLength) {
172 throw new StringIndexOutOfBoundsException(newLength);
173 ensureCapacityInternal(newLength);
175 if (count < newLength) {
176 for (; count < newLength; count++)
184 * Returns the <code>char</code> value in this sequence at the specified index.
185 * The first <code>char</code> value is at index <code>0</code>, the next at index
186 * <code>1</code>, and so on, as in array indexing.
188 * The index argument must be greater than or equal to
189 * <code>0</code>, and less than the length of this sequence.
191 * <p>If the <code>char</code> value specified by the index is a
192 * <a href="Character.html#unicode">surrogate</a>, the surrogate
195 * @param index the index of the desired <code>char</code> value.
196 * @return the <code>char</code> value at the specified index.
197 * @throws IndexOutOfBoundsException if <code>index</code> is
198 * negative or greater than or equal to <code>length()</code>.
200 public char charAt(int index) {
201 if ((index < 0) || (index >= count))
202 throw new StringIndexOutOfBoundsException(index);
207 * Returns the character (Unicode code point) at the specified
208 * index. The index refers to <code>char</code> values
209 * (Unicode code units) and ranges from <code>0</code> to
210 * {@link #length()}<code> - 1</code>.
212 * <p> If the <code>char</code> value specified at the given index
213 * is in the high-surrogate range, the following index is less
214 * than the length of this sequence, and the
215 * <code>char</code> value at the following index is in the
216 * low-surrogate range, then the supplementary code point
217 * corresponding to this surrogate pair is returned. Otherwise,
218 * the <code>char</code> value at the given index is returned.
220 * @param index the index to the <code>char</code> values
221 * @return the code point value of the character at the
223 * @exception IndexOutOfBoundsException if the <code>index</code>
224 * argument is negative or not less than the length of this
227 public int codePointAt(int index) {
228 if ((index < 0) || (index >= count)) {
229 throw new StringIndexOutOfBoundsException(index);
231 return Character.codePointAt(value, index);
235 * Returns the character (Unicode code point) before the specified
236 * index. The index refers to <code>char</code> values
237 * (Unicode code units) and ranges from <code>1</code> to {@link
240 * <p> If the <code>char</code> value at <code>(index - 1)</code>
241 * is in the low-surrogate range, <code>(index - 2)</code> is not
242 * negative, and the <code>char</code> value at <code>(index -
243 * 2)</code> is in the high-surrogate range, then the
244 * supplementary code point value of the surrogate pair is
245 * returned. If the <code>char</code> value at <code>index -
246 * 1</code> is an unpaired low-surrogate or a high-surrogate, the
247 * surrogate value is returned.
249 * @param index the index following the code point that should be returned
250 * @return the Unicode code point value before the given index.
251 * @exception IndexOutOfBoundsException if the <code>index</code>
252 * argument is less than 1 or greater than the length
255 public int codePointBefore(int index) {
257 if ((i < 0) || (i >= count)) {
258 throw new StringIndexOutOfBoundsException(index);
260 return Character.codePointBefore(value, index);
264 * Returns the number of Unicode code points in the specified text
265 * range of this sequence. The text range begins at the specified
266 * <code>beginIndex</code> and extends to the <code>char</code> at
267 * index <code>endIndex - 1</code>. Thus the length (in
268 * <code>char</code>s) of the text range is
269 * <code>endIndex-beginIndex</code>. Unpaired surrogates within
270 * this sequence count as one code point each.
272 * @param beginIndex the index to the first <code>char</code> of
274 * @param endIndex the index after the last <code>char</code> of
276 * @return the number of Unicode code points in the specified text
278 * @exception IndexOutOfBoundsException if the
279 * <code>beginIndex</code> is negative, or <code>endIndex</code>
280 * is larger than the length of this sequence, or
281 * <code>beginIndex</code> is larger than <code>endIndex</code>.
283 public int codePointCount(int beginIndex, int endIndex) {
284 if (beginIndex < 0 || endIndex > count || beginIndex > endIndex) {
285 throw new IndexOutOfBoundsException();
287 return Character.codePointCountImpl(value, beginIndex, endIndex-beginIndex);
291 * Returns the index within this sequence that is offset from the
292 * given <code>index</code> by <code>codePointOffset</code> code
293 * points. Unpaired surrogates within the text range given by
294 * <code>index</code> and <code>codePointOffset</code> count as
295 * one code point each.
297 * @param index the index to be offset
298 * @param codePointOffset the offset in code points
299 * @return the index within this sequence
300 * @exception IndexOutOfBoundsException if <code>index</code>
301 * is negative or larger then the length of this sequence,
302 * or if <code>codePointOffset</code> is positive and the subsequence
303 * starting with <code>index</code> has fewer than
304 * <code>codePointOffset</code> code points,
305 * or if <code>codePointOffset</code> is negative and the subsequence
306 * before <code>index</code> has fewer than the absolute value of
307 * <code>codePointOffset</code> code points.
309 public int offsetByCodePoints(int index, int codePointOffset) {
310 if (index < 0 || index > count) {
311 throw new IndexOutOfBoundsException();
313 return Character.offsetByCodePointsImpl(value, 0, count,
314 index, codePointOffset);
318 * Characters are copied from this sequence into the
319 * destination character array <code>dst</code>. The first character to
320 * be copied is at index <code>srcBegin</code>; the last character to
321 * be copied is at index <code>srcEnd-1</code>. The total number of
322 * characters to be copied is <code>srcEnd-srcBegin</code>. The
323 * characters are copied into the subarray of <code>dst</code> starting
324 * at index <code>dstBegin</code> and ending at index:
325 * <p><blockquote><pre>
326 * dstbegin + (srcEnd-srcBegin) - 1
327 * </pre></blockquote>
329 * @param srcBegin start copying at this offset.
330 * @param srcEnd stop copying at this offset.
331 * @param dst the array to copy the data into.
332 * @param dstBegin offset into <code>dst</code>.
333 * @throws NullPointerException if <code>dst</code> is
335 * @throws IndexOutOfBoundsException if any of the following is true:
337 * <li><code>srcBegin</code> is negative
338 * <li><code>dstBegin</code> is negative
339 * <li>the <code>srcBegin</code> argument is greater than
340 * the <code>srcEnd</code> argument.
341 * <li><code>srcEnd</code> is greater than
342 * <code>this.length()</code>.
343 * <li><code>dstBegin+srcEnd-srcBegin</code> is greater than
344 * <code>dst.length</code>
347 public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
350 throw new StringIndexOutOfBoundsException(srcBegin);
351 if ((srcEnd < 0) || (srcEnd > count))
352 throw new StringIndexOutOfBoundsException(srcEnd);
353 if (srcBegin > srcEnd)
354 throw new StringIndexOutOfBoundsException("srcBegin > srcEnd");
355 arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
359 * The character at the specified index is set to <code>ch</code>. This
360 * sequence is altered to represent a new character sequence that is
361 * identical to the old character sequence, except that it contains the
362 * character <code>ch</code> at position <code>index</code>.
364 * The index argument must be greater than or equal to
365 * <code>0</code>, and less than the length of this sequence.
367 * @param index the index of the character to modify.
368 * @param ch the new character.
369 * @throws IndexOutOfBoundsException if <code>index</code> is
370 * negative or greater than or equal to <code>length()</code>.
372 public void setCharAt(int index, char ch) {
373 if ((index < 0) || (index >= count))
374 throw new StringIndexOutOfBoundsException(index);
379 * Appends the string representation of the {@code Object} argument.
381 * The overall effect is exactly as if the argument were converted
382 * to a string by the method {@link String#valueOf(Object)},
383 * and the characters of that string were then
384 * {@link #append(String) appended} to this character sequence.
386 * @param obj an {@code Object}.
387 * @return a reference to this object.
389 public AbstractStringBuilder append(Object obj) {
390 return append(String.valueOf(obj));
394 * Appends the specified string to this character sequence.
396 * The characters of the {@code String} argument are appended, in
397 * order, increasing the length of this sequence by the length of the
398 * argument. If {@code str} is {@code null}, then the four
399 * characters {@code "null"} are appended.
401 * Let <i>n</i> be the length of this character sequence just prior to
402 * execution of the {@code append} method. Then the character at
403 * index <i>k</i> in the new character sequence is equal to the character
404 * at index <i>k</i> in the old character sequence, if <i>k</i> is less
405 * than <i>n</i>; otherwise, it is equal to the character at index
406 * <i>k-n</i> in the argument {@code str}.
408 * @param str a string.
409 * @return a reference to this object.
411 public AbstractStringBuilder append(String str) {
412 if (str == null) str = "null";
413 int len = str.length();
414 ensureCapacityInternal(count + len);
415 str.getChars(0, len, value, count);
420 // Documentation in subclasses because of synchro difference
421 public AbstractStringBuilder append(StringBuffer sb) {
423 return append("null");
424 int len = sb.length();
425 ensureCapacityInternal(count + len);
426 sb.getChars(0, len, value, count);
431 // Documentation in subclasses because of synchro difference
432 public AbstractStringBuilder append(CharSequence s) {
435 if (s instanceof String)
436 return this.append((String)s);
437 if (s instanceof StringBuffer)
438 return this.append((StringBuffer)s);
439 return this.append(s, 0, s.length());
443 * Appends a subsequence of the specified {@code CharSequence} to this
446 * Characters of the argument {@code s}, starting at
447 * index {@code start}, are appended, in order, to the contents of
448 * this sequence up to the (exclusive) index {@code end}. The length
449 * of this sequence is increased by the value of {@code end - start}.
451 * Let <i>n</i> be the length of this character sequence just prior to
452 * execution of the {@code append} method. Then the character at
453 * index <i>k</i> in this character sequence becomes equal to the
454 * character at index <i>k</i> in this sequence, if <i>k</i> is less than
455 * <i>n</i>; otherwise, it is equal to the character at index
456 * <i>k+start-n</i> in the argument {@code s}.
458 * If {@code s} is {@code null}, then this method appends
459 * characters as if the s parameter was a sequence containing the four
460 * characters {@code "null"}.
462 * @param s the sequence to append.
463 * @param start the starting index of the subsequence to be appended.
464 * @param end the end index of the subsequence to be appended.
465 * @return a reference to this object.
466 * @throws IndexOutOfBoundsException if
467 * {@code start} is negative, or
468 * {@code start} is greater than {@code end} or
469 * {@code end} is greater than {@code s.length()}
471 public AbstractStringBuilder append(CharSequence s, int start, int end) {
474 if ((start < 0) || (start > end) || (end > s.length()))
475 throw new IndexOutOfBoundsException(
476 "start " + start + ", end " + end + ", s.length() "
478 int len = end - start;
479 ensureCapacityInternal(count + len);
480 for (int i = start, j = count; i < end; i++, j++)
481 value[j] = s.charAt(i);
487 * Appends the string representation of the {@code char} array
488 * argument to this sequence.
490 * The characters of the array argument are appended, in order, to
491 * the contents of this sequence. The length of this sequence
492 * increases by the length of the argument.
494 * The overall effect is exactly as if the argument were converted
495 * to a string by the method {@link String#valueOf(char[])},
496 * and the characters of that string were then
497 * {@link #append(String) appended} to this character sequence.
499 * @param str the characters to be appended.
500 * @return a reference to this object.
502 public AbstractStringBuilder append(char[] str) {
503 int len = str.length;
504 ensureCapacityInternal(count + len);
505 arraycopy(str, 0, value, count, len);
511 * Appends the string representation of a subarray of the
512 * {@code char} array argument to this sequence.
514 * Characters of the {@code char} array {@code str}, starting at
515 * index {@code offset}, are appended, in order, to the contents
516 * of this sequence. The length of this sequence increases
517 * by the value of {@code len}.
519 * The overall effect is exactly as if the arguments were converted
520 * to a string by the method {@link String#valueOf(char[],int,int)},
521 * and the characters of that string were then
522 * {@link #append(String) appended} to this character sequence.
524 * @param str the characters to be appended.
525 * @param offset the index of the first {@code char} to append.
526 * @param len the number of {@code char}s to append.
527 * @return a reference to this object.
528 * @throws IndexOutOfBoundsException
529 * if {@code offset < 0} or {@code len < 0}
530 * or {@code offset+len > str.length}
532 public AbstractStringBuilder append(char str[], int offset, int len) {
533 if (len > 0) // let arraycopy report AIOOBE for len < 0
534 ensureCapacityInternal(count + len);
535 arraycopy(str, offset, value, count, len);
541 * Appends the string representation of the {@code boolean}
542 * argument to the sequence.
544 * The overall effect is exactly as if the argument were converted
545 * to a string by the method {@link String#valueOf(boolean)},
546 * and the characters of that string were then
547 * {@link #append(String) appended} to this character sequence.
549 * @param b a {@code boolean}.
550 * @return a reference to this object.
552 public AbstractStringBuilder append(boolean b) {
554 ensureCapacityInternal(count + 4);
555 value[count++] = 't';
556 value[count++] = 'r';
557 value[count++] = 'u';
558 value[count++] = 'e';
560 ensureCapacityInternal(count + 5);
561 value[count++] = 'f';
562 value[count++] = 'a';
563 value[count++] = 'l';
564 value[count++] = 's';
565 value[count++] = 'e';
571 * Appends the string representation of the {@code char}
572 * argument to this sequence.
574 * The argument is appended to the contents of this sequence.
575 * The length of this sequence increases by {@code 1}.
577 * The overall effect is exactly as if the argument were converted
578 * to a string by the method {@link String#valueOf(char)},
579 * and the character in that string were then
580 * {@link #append(String) appended} to this character sequence.
582 * @param c a {@code char}.
583 * @return a reference to this object.
585 public AbstractStringBuilder append(char c) {
586 ensureCapacityInternal(count + 1);
592 * Appends the string representation of the {@code int}
593 * argument to this sequence.
595 * The overall effect is exactly as if the argument were converted
596 * to a string by the method {@link String#valueOf(int)},
597 * and the characters of that string were then
598 * {@link #append(String) appended} to this character sequence.
600 * @param i an {@code int}.
601 * @return a reference to this object.
604 args={ "self", "i" },
605 body="return java_lang_AbstractStringBuilder(true).appendLjava_lang_AbstractStringBuilderLjava_lang_String(self,i.toString());"
607 public AbstractStringBuilder append(int i) {
608 if (i == Integer.MIN_VALUE) {
609 append("-2147483648");
612 int appendedLength = (i < 0) ? Integer.stringSize(-i) + 1
613 : Integer.stringSize(i);
614 int spaceNeeded = count + appendedLength;
615 ensureCapacityInternal(spaceNeeded);
616 Integer.getChars(i, spaceNeeded, value);
622 * Appends the string representation of the {@code long}
623 * argument to this sequence.
625 * The overall effect is exactly as if the argument were converted
626 * to a string by the method {@link String#valueOf(long)},
627 * and the characters of that string were then
628 * {@link #append(String) appended} to this character sequence.
630 * @param l a {@code long}.
631 * @return a reference to this object.
633 public AbstractStringBuilder append(long l) {
634 if (l == Long.MIN_VALUE) {
635 append("-9223372036854775808");
638 int appendedLength = (l < 0) ? Long.stringSize(-l) + 1
639 : Long.stringSize(l);
640 int spaceNeeded = count + appendedLength;
641 ensureCapacityInternal(spaceNeeded);
642 Long.getChars(l, spaceNeeded, value);
648 * Appends the string representation of the {@code float}
649 * argument to this sequence.
651 * The overall effect is exactly as if the argument were converted
652 * to a string by the method {@link String#valueOf(float)},
653 * and the characters of that string were then
654 * {@link #append(String) appended} to this character sequence.
656 * @param f a {@code float}.
657 * @return a reference to this object.
659 public AbstractStringBuilder append(float f) {
660 return append(Float.toString(f));
664 * Appends the string representation of the {@code double}
665 * argument to this sequence.
667 * The overall effect is exactly as if the argument were converted
668 * to a string by the method {@link String#valueOf(double)},
669 * and the characters of that string were then
670 * {@link #append(String) appended} to this character sequence.
672 * @param d a {@code double}.
673 * @return a reference to this object.
675 public AbstractStringBuilder append(double d) {
676 return append(Double.toString(d));
680 * Removes the characters in a substring of this sequence.
681 * The substring begins at the specified {@code start} and extends to
682 * the character at index {@code end - 1} or to the end of the
683 * sequence if no such character exists. If
684 * {@code start} is equal to {@code end}, no changes are made.
686 * @param start The beginning index, inclusive.
687 * @param end The ending index, exclusive.
688 * @return This object.
689 * @throws StringIndexOutOfBoundsException if {@code start}
690 * is negative, greater than {@code length()}, or
691 * greater than {@code end}.
693 public AbstractStringBuilder delete(int start, int end) {
695 throw new StringIndexOutOfBoundsException(start);
699 throw new StringIndexOutOfBoundsException();
700 int len = end - start;
702 arraycopy(value, start+len, value, start, count-end);
709 * Appends the string representation of the {@code codePoint}
710 * argument to this sequence.
712 * <p> The argument is appended to the contents of this sequence.
713 * The length of this sequence increases by
714 * {@link Character#charCount(int) Character.charCount(codePoint)}.
716 * <p> The overall effect is exactly as if the argument were
717 * converted to a {@code char} array by the method
718 * {@link Character#toChars(int)} and the character in that array
719 * were then {@link #append(char[]) appended} to this character
722 * @param codePoint a Unicode code point
723 * @return a reference to this object.
724 * @exception IllegalArgumentException if the specified
725 * {@code codePoint} isn't a valid Unicode code point
727 public AbstractStringBuilder appendCodePoint(int codePoint) {
728 final int count = this.count;
730 if (Character.isBmpCodePoint(codePoint)) {
731 ensureCapacityInternal(count + 1);
732 value[count] = (char) codePoint;
733 this.count = count + 1;
734 } else if (Character.isValidCodePoint(codePoint)) {
735 ensureCapacityInternal(count + 2);
736 Character.toSurrogates(codePoint, value, count);
737 this.count = count + 2;
739 throw new IllegalArgumentException();
745 * Removes the <code>char</code> at the specified position in this
746 * sequence. This sequence is shortened by one <code>char</code>.
748 * <p>Note: If the character at the given index is a supplementary
749 * character, this method does not remove the entire character. If
750 * correct handling of supplementary characters is required,
751 * determine the number of <code>char</code>s to remove by calling
752 * <code>Character.charCount(thisSequence.codePointAt(index))</code>,
753 * where <code>thisSequence</code> is this sequence.
755 * @param index Index of <code>char</code> to remove
756 * @return This object.
757 * @throws StringIndexOutOfBoundsException if the <code>index</code>
758 * is negative or greater than or equal to
759 * <code>length()</code>.
761 public AbstractStringBuilder deleteCharAt(int index) {
762 if ((index < 0) || (index >= count))
763 throw new StringIndexOutOfBoundsException(index);
764 arraycopy(value, index+1, value, index, count-index-1);
770 * Replaces the characters in a substring of this sequence
771 * with characters in the specified <code>String</code>. The substring
772 * begins at the specified <code>start</code> and extends to the character
773 * at index <code>end - 1</code> or to the end of the
774 * sequence if no such character exists. First the
775 * characters in the substring are removed and then the specified
776 * <code>String</code> is inserted at <code>start</code>. (This
777 * sequence will be lengthened to accommodate the
778 * specified String if necessary.)
780 * @param start The beginning index, inclusive.
781 * @param end The ending index, exclusive.
782 * @param str String that will replace previous contents.
783 * @return This object.
784 * @throws StringIndexOutOfBoundsException if <code>start</code>
785 * is negative, greater than <code>length()</code>, or
786 * greater than <code>end</code>.
788 public AbstractStringBuilder replace(int start, int end, String str) {
790 throw new StringIndexOutOfBoundsException(start);
792 throw new StringIndexOutOfBoundsException("start > length()");
794 throw new StringIndexOutOfBoundsException("start > end");
798 int len = str.length();
799 int newCount = count + len - (end - start);
800 ensureCapacityInternal(newCount);
802 arraycopy(value, end, value, start + len, count - end);
803 str.getChars(value, start);
809 * Returns a new <code>String</code> that contains a subsequence of
810 * characters currently contained in this character sequence. The
811 * substring begins at the specified index and extends to the end of
814 * @param start The beginning index, inclusive.
815 * @return The new string.
816 * @throws StringIndexOutOfBoundsException if <code>start</code> is
817 * less than zero, or greater than the length of this object.
819 public String substring(int start) {
820 return substring(start, count);
824 * Returns a new character sequence that is a subsequence of this sequence.
826 * <p> An invocation of this method of the form
829 * sb.subSequence(begin, end)</pre></blockquote>
831 * behaves in exactly the same way as the invocation
834 * sb.substring(begin, end)</pre></blockquote>
836 * This method is provided so that this class can
837 * implement the {@link CharSequence} interface. </p>
839 * @param start the start index, inclusive.
840 * @param end the end index, exclusive.
841 * @return the specified subsequence.
843 * @throws IndexOutOfBoundsException
844 * if <tt>start</tt> or <tt>end</tt> are negative,
845 * if <tt>end</tt> is greater than <tt>length()</tt>,
846 * or if <tt>start</tt> is greater than <tt>end</tt>
849 public CharSequence subSequence(int start, int end) {
850 return substring(start, end);
854 * Returns a new <code>String</code> that contains a subsequence of
855 * characters currently contained in this sequence. The
856 * substring begins at the specified <code>start</code> and
857 * extends to the character at index <code>end - 1</code>.
859 * @param start The beginning index, inclusive.
860 * @param end The ending index, exclusive.
861 * @return The new string.
862 * @throws StringIndexOutOfBoundsException if <code>start</code>
863 * or <code>end</code> are negative or greater than
864 * <code>length()</code>, or <code>start</code> is
865 * greater than <code>end</code>.
867 public String substring(int start, int end) {
869 throw new StringIndexOutOfBoundsException(start);
871 throw new StringIndexOutOfBoundsException(end);
873 throw new StringIndexOutOfBoundsException(end - start);
874 return new String(value, start, end - start);
878 * Inserts the string representation of a subarray of the {@code str}
879 * array argument into this sequence. The subarray begins at the
880 * specified {@code offset} and extends {@code len} {@code char}s.
881 * The characters of the subarray are inserted into this sequence at
882 * the position indicated by {@code index}. The length of this
883 * sequence increases by {@code len} {@code char}s.
885 * @param index position at which to insert subarray.
886 * @param str A {@code char} array.
887 * @param offset the index of the first {@code char} in subarray to
889 * @param len the number of {@code char}s in the subarray to
891 * @return This object
892 * @throws StringIndexOutOfBoundsException if {@code index}
893 * is negative or greater than {@code length()}, or
894 * {@code offset} or {@code len} are negative, or
895 * {@code (offset+len)} is greater than
896 * {@code str.length}.
898 public AbstractStringBuilder insert(int index, char[] str, int offset,
901 if ((index < 0) || (index > length()))
902 throw new StringIndexOutOfBoundsException(index);
903 if ((offset < 0) || (len < 0) || (offset > str.length - len))
904 throw new StringIndexOutOfBoundsException(
905 "offset " + offset + ", len " + len + ", str.length "
907 ensureCapacityInternal(count + len);
908 arraycopy(value, index, value, index + len, count - index);
909 arraycopy(str, offset, value, index, len);
915 * Inserts the string representation of the {@code Object}
916 * argument into this character sequence.
918 * The overall effect is exactly as if the second argument were
919 * converted to a string by the method {@link String#valueOf(Object)},
920 * and the characters of that string were then
921 * {@link #insert(int,String) inserted} into this character
922 * sequence at the indicated offset.
924 * The {@code offset} argument must be greater than or equal to
925 * {@code 0}, and less than or equal to the {@linkplain #length() length}
928 * @param offset the offset.
929 * @param obj an {@code Object}.
930 * @return a reference to this object.
931 * @throws StringIndexOutOfBoundsException if the offset is invalid.
933 public AbstractStringBuilder insert(int offset, Object obj) {
934 return insert(offset, String.valueOf(obj));
938 * Inserts the string into this character sequence.
940 * The characters of the {@code String} argument are inserted, in
941 * order, into this sequence at the indicated offset, moving up any
942 * characters originally above that position and increasing the length
943 * of this sequence by the length of the argument. If
944 * {@code str} is {@code null}, then the four characters
945 * {@code "null"} are inserted into this sequence.
947 * The character at index <i>k</i> in the new character sequence is
950 * <li>the character at index <i>k</i> in the old character sequence, if
951 * <i>k</i> is less than {@code offset}
952 * <li>the character at index <i>k</i>{@code -offset} in the
953 * argument {@code str}, if <i>k</i> is not less than
954 * {@code offset} but is less than {@code offset+str.length()}
955 * <li>the character at index <i>k</i>{@code -str.length()} in the
956 * old character sequence, if <i>k</i> is not less than
957 * {@code offset+str.length()}
959 * The {@code offset} argument must be greater than or equal to
960 * {@code 0}, and less than or equal to the {@linkplain #length() length}
963 * @param offset the offset.
964 * @param str a string.
965 * @return a reference to this object.
966 * @throws StringIndexOutOfBoundsException if the offset is invalid.
968 public AbstractStringBuilder insert(int offset, String str) {
969 if ((offset < 0) || (offset > length()))
970 throw new StringIndexOutOfBoundsException(offset);
973 int len = str.length();
974 ensureCapacityInternal(count + len);
975 arraycopy(value, offset, value, offset + len, count - offset);
976 str.getChars(value, offset);
982 * Inserts the string representation of the {@code char} array
983 * argument into this sequence.
985 * The characters of the array argument are inserted into the
986 * contents of this sequence at the position indicated by
987 * {@code offset}. The length of this sequence increases by
988 * the length of the argument.
990 * The overall effect is exactly as if the second argument were
991 * converted to a string by the method {@link String#valueOf(char[])},
992 * and the characters of that string were then
993 * {@link #insert(int,String) inserted} into this character
994 * sequence at the indicated offset.
996 * The {@code offset} argument must be greater than or equal to
997 * {@code 0}, and less than or equal to the {@linkplain #length() length}
1000 * @param offset the offset.
1001 * @param str a character array.
1002 * @return a reference to this object.
1003 * @throws StringIndexOutOfBoundsException if the offset is invalid.
1005 public AbstractStringBuilder insert(int offset, char[] str) {
1006 if ((offset < 0) || (offset > length()))
1007 throw new StringIndexOutOfBoundsException(offset);
1008 int len = str.length;
1009 ensureCapacityInternal(count + len);
1010 arraycopy(value, offset, value, offset + len, count - offset);
1011 arraycopy(str, 0, value, offset, len);
1017 * Inserts the specified {@code CharSequence} into this sequence.
1019 * The characters of the {@code CharSequence} argument are inserted,
1020 * in order, into this sequence at the indicated offset, moving up
1021 * any characters originally above that position and increasing the length
1022 * of this sequence by the length of the argument s.
1024 * The result of this method is exactly the same as if it were an
1025 * invocation of this object's
1026 * {@link #insert(int,CharSequence,int,int) insert}(dstOffset, s, 0, s.length())
1029 * <p>If {@code s} is {@code null}, then the four characters
1030 * {@code "null"} are inserted into this sequence.
1032 * @param dstOffset the offset.
1033 * @param s the sequence to be inserted
1034 * @return a reference to this object.
1035 * @throws IndexOutOfBoundsException if the offset is invalid.
1037 public AbstractStringBuilder insert(int dstOffset, CharSequence s) {
1040 if (s instanceof String)
1041 return this.insert(dstOffset, (String)s);
1042 return this.insert(dstOffset, s, 0, s.length());
1046 * Inserts a subsequence of the specified {@code CharSequence} into
1049 * The subsequence of the argument {@code s} specified by
1050 * {@code start} and {@code end} are inserted,
1051 * in order, into this sequence at the specified destination offset, moving
1052 * up any characters originally above that position. The length of this
1053 * sequence is increased by {@code end - start}.
1055 * The character at index <i>k</i> in this sequence becomes equal to:
1057 * <li>the character at index <i>k</i> in this sequence, if
1058 * <i>k</i> is less than {@code dstOffset}
1059 * <li>the character at index <i>k</i>{@code +start-dstOffset} in
1060 * the argument {@code s}, if <i>k</i> is greater than or equal to
1061 * {@code dstOffset} but is less than {@code dstOffset+end-start}
1062 * <li>the character at index <i>k</i>{@code -(end-start)} in this
1063 * sequence, if <i>k</i> is greater than or equal to
1064 * {@code dstOffset+end-start}
1066 * The {@code dstOffset} argument must be greater than or equal to
1067 * {@code 0}, and less than or equal to the {@linkplain #length() length}
1069 * <p>The start argument must be nonnegative, and not greater than
1071 * <p>The end argument must be greater than or equal to
1072 * {@code start}, and less than or equal to the length of s.
1074 * <p>If {@code s} is {@code null}, then this method inserts
1075 * characters as if the s parameter was a sequence containing the four
1076 * characters {@code "null"}.
1078 * @param dstOffset the offset in this sequence.
1079 * @param s the sequence to be inserted.
1080 * @param start the starting index of the subsequence to be inserted.
1081 * @param end the end index of the subsequence to be inserted.
1082 * @return a reference to this object.
1083 * @throws IndexOutOfBoundsException if {@code dstOffset}
1084 * is negative or greater than {@code this.length()}, or
1085 * {@code start} or {@code end} are negative, or
1086 * {@code start} is greater than {@code end} or
1087 * {@code end} is greater than {@code s.length()}
1089 public AbstractStringBuilder insert(int dstOffset, CharSequence s,
1090 int start, int end) {
1093 if ((dstOffset < 0) || (dstOffset > this.length()))
1094 throw new IndexOutOfBoundsException("dstOffset "+dstOffset);
1095 if ((start < 0) || (end < 0) || (start > end) || (end > s.length()))
1096 throw new IndexOutOfBoundsException(
1097 "start " + start + ", end " + end + ", s.length() "
1099 int len = end - start;
1100 ensureCapacityInternal(count + len);
1101 arraycopy(value, dstOffset, value, dstOffset + len,
1103 for (int i=start; i<end; i++)
1104 value[dstOffset++] = s.charAt(i);
1110 * Inserts the string representation of the {@code boolean}
1111 * argument into this sequence.
1113 * The overall effect is exactly as if the second argument were
1114 * converted to a string by the method {@link String#valueOf(boolean)},
1115 * and the characters of that string were then
1116 * {@link #insert(int,String) inserted} into this character
1117 * sequence at the indicated offset.
1119 * The {@code offset} argument must be greater than or equal to
1120 * {@code 0}, and less than or equal to the {@linkplain #length() length}
1123 * @param offset the offset.
1124 * @param b a {@code boolean}.
1125 * @return a reference to this object.
1126 * @throws StringIndexOutOfBoundsException if the offset is invalid.
1128 public AbstractStringBuilder insert(int offset, boolean b) {
1129 return insert(offset, String.valueOf(b));
1133 * Inserts the string representation of the {@code char}
1134 * argument into this sequence.
1136 * The overall effect is exactly as if the second argument were
1137 * converted to a string by the method {@link String#valueOf(char)},
1138 * and the character in that string were then
1139 * {@link #insert(int,String) inserted} into this character
1140 * sequence at the indicated offset.
1142 * The {@code offset} argument must be greater than or equal to
1143 * {@code 0}, and less than or equal to the {@linkplain #length() length}
1146 * @param offset the offset.
1147 * @param c a {@code char}.
1148 * @return a reference to this object.
1149 * @throws IndexOutOfBoundsException if the offset is invalid.
1151 public AbstractStringBuilder insert(int offset, char c) {
1152 ensureCapacityInternal(count + 1);
1153 arraycopy(value, offset, value, offset + 1, count - offset);
1160 * Inserts the string representation of the second {@code int}
1161 * argument into this sequence.
1163 * The overall effect is exactly as if the second argument were
1164 * converted to a string by the method {@link String#valueOf(int)},
1165 * and the characters of that string were then
1166 * {@link #insert(int,String) inserted} into this character
1167 * sequence at the indicated offset.
1169 * The {@code offset} argument must be greater than or equal to
1170 * {@code 0}, and less than or equal to the {@linkplain #length() length}
1173 * @param offset the offset.
1174 * @param i an {@code int}.
1175 * @return a reference to this object.
1176 * @throws StringIndexOutOfBoundsException if the offset is invalid.
1178 public AbstractStringBuilder insert(int offset, int i) {
1179 return insert(offset, String.valueOf(i));
1183 * Inserts the string representation of the {@code long}
1184 * argument into this sequence.
1186 * The overall effect is exactly as if the second argument were
1187 * converted to a string by the method {@link String#valueOf(long)},
1188 * and the characters of that string were then
1189 * {@link #insert(int,String) inserted} into this character
1190 * sequence at the indicated offset.
1192 * The {@code offset} argument must be greater than or equal to
1193 * {@code 0}, and less than or equal to the {@linkplain #length() length}
1196 * @param offset the offset.
1197 * @param l a {@code long}.
1198 * @return a reference to this object.
1199 * @throws StringIndexOutOfBoundsException if the offset is invalid.
1201 public AbstractStringBuilder insert(int offset, long l) {
1202 return insert(offset, String.valueOf(l));
1206 * Inserts the string representation of the {@code float}
1207 * argument into this sequence.
1209 * The overall effect is exactly as if the second argument were
1210 * converted to a string by the method {@link String#valueOf(float)},
1211 * and the characters of that string were then
1212 * {@link #insert(int,String) inserted} into this character
1213 * sequence at the indicated offset.
1215 * The {@code offset} argument must be greater than or equal to
1216 * {@code 0}, and less than or equal to the {@linkplain #length() length}
1219 * @param offset the offset.
1220 * @param f a {@code float}.
1221 * @return a reference to this object.
1222 * @throws StringIndexOutOfBoundsException if the offset is invalid.
1224 public AbstractStringBuilder insert(int offset, float f) {
1225 return insert(offset, String.valueOf(f));
1229 * Inserts the string representation of the {@code double}
1230 * argument into this sequence.
1232 * The overall effect is exactly as if the second argument were
1233 * converted to a string by the method {@link String#valueOf(double)},
1234 * and the characters of that string were then
1235 * {@link #insert(int,String) inserted} into this character
1236 * sequence at the indicated offset.
1238 * The {@code offset} argument must be greater than or equal to
1239 * {@code 0}, and less than or equal to the {@linkplain #length() length}
1242 * @param offset the offset.
1243 * @param d a {@code double}.
1244 * @return a reference to this object.
1245 * @throws StringIndexOutOfBoundsException if the offset is invalid.
1247 public AbstractStringBuilder insert(int offset, double d) {
1248 return insert(offset, String.valueOf(d));
1252 * Returns the index within this string of the first occurrence of the
1253 * specified substring. The integer returned is the smallest value
1254 * <i>k</i> such that:
1256 * this.toString().startsWith(str, <i>k</i>)
1257 * </pre></blockquote>
1258 * is <code>true</code>.
1260 * @param str any string.
1261 * @return if the string argument occurs as a substring within this
1262 * object, then the index of the first character of the first
1263 * such substring is returned; if it does not occur as a
1264 * substring, <code>-1</code> is returned.
1265 * @throws java.lang.NullPointerException if <code>str</code> is
1266 * <code>null</code>.
1268 public int indexOf(String str) {
1269 return indexOf(str, 0);
1273 * Returns the index within this string of the first occurrence of the
1274 * specified substring, starting at the specified index. The integer
1275 * returned is the smallest value <tt>k</tt> for which:
1277 * k >= Math.min(fromIndex, str.length()) &&
1278 * this.toString().startsWith(str, k)
1279 * </pre></blockquote>
1280 * If no such value of <i>k</i> exists, then -1 is returned.
1282 * @param str the substring for which to search.
1283 * @param fromIndex the index from which to start the search.
1284 * @return the index within this string of the first occurrence of the
1285 * specified substring, starting at the specified index.
1286 * @throws java.lang.NullPointerException if <code>str</code> is
1287 * <code>null</code>.
1289 public int indexOf(String str, int fromIndex) {
1290 return String.indexOf(value, 0, count,
1291 str.toCharArray(), 0, str.length(), fromIndex);
1295 * Returns the index within this string of the rightmost occurrence
1296 * of the specified substring. The rightmost empty string "" is
1297 * considered to occur at the index value <code>this.length()</code>.
1298 * The returned index is the largest value <i>k</i> such that
1300 * this.toString().startsWith(str, k)
1301 * </pre></blockquote>
1304 * @param str the substring to search for.
1305 * @return if the string argument occurs one or more times as a substring
1306 * within this object, then the index of the first character of
1307 * the last such substring is returned. If it does not occur as
1308 * a substring, <code>-1</code> is returned.
1309 * @throws java.lang.NullPointerException if <code>str</code> is
1310 * <code>null</code>.
1312 public int lastIndexOf(String str) {
1313 return lastIndexOf(str, count);
1317 * Returns the index within this string of the last occurrence of the
1318 * specified substring. The integer returned is the largest value <i>k</i>
1321 * k <= Math.min(fromIndex, str.length()) &&
1322 * this.toString().startsWith(str, k)
1323 * </pre></blockquote>
1324 * If no such value of <i>k</i> exists, then -1 is returned.
1326 * @param str the substring to search for.
1327 * @param fromIndex the index to start the search from.
1328 * @return the index within this sequence of the last occurrence of the
1329 * specified substring.
1330 * @throws java.lang.NullPointerException if <code>str</code> is
1331 * <code>null</code>.
1333 public int lastIndexOf(String str, int fromIndex) {
1334 return String.lastIndexOf(value, 0, count,
1335 str.toCharArray(), 0, str.length(), fromIndex);
1339 * Causes this character sequence to be replaced by the reverse of
1340 * the sequence. If there are any surrogate pairs included in the
1341 * sequence, these are treated as single characters for the
1342 * reverse operation. Thus, the order of the high-low surrogates
1343 * is never reversed.
1345 * Let <i>n</i> be the character length of this character sequence
1346 * (not the length in <code>char</code> values) just prior to
1347 * execution of the <code>reverse</code> method. Then the
1348 * character at index <i>k</i> in the new character sequence is
1349 * equal to the character at index <i>n-k-1</i> in the old
1350 * character sequence.
1352 * <p>Note that the reverse operation may result in producing
1353 * surrogate pairs that were unpaired low-surrogates and
1354 * high-surrogates before the operation. For example, reversing
1355 * "\uDC00\uD800" produces "\uD800\uDC00" which is
1356 * a valid surrogate pair.
1358 * @return a reference to this object.
1360 public AbstractStringBuilder reverse() {
1361 boolean hasSurrogate = false;
1363 for (int j = (n-1) >> 1; j >= 0; --j) {
1364 char temp = value[j];
1365 char temp2 = value[n - j];
1366 if (!hasSurrogate) {
1367 hasSurrogate = (temp >= Character.MIN_SURROGATE && temp <= Character.MAX_SURROGATE)
1368 || (temp2 >= Character.MIN_SURROGATE && temp2 <= Character.MAX_SURROGATE);
1371 value[n - j] = temp;
1374 // Reverse back all valid surrogate pairs
1375 for (int i = 0; i < count - 1; i++) {
1377 if (Character.isLowSurrogate(c2)) {
1378 char c1 = value[i + 1];
1379 if (Character.isHighSurrogate(c1)) {
1390 * Returns a string representing the data in this sequence.
1391 * A new <code>String</code> object is allocated and initialized to
1392 * contain the character sequence currently represented by this
1393 * object. This <code>String</code> is then returned. Subsequent
1394 * changes to this sequence do not affect the contents of the
1395 * <code>String</code>.
1397 * @return a string representation of this sequence of characters.
1399 public abstract String toString();
1402 * Needed by <tt>String</tt> for the contentEquals method.
1404 final char[] getValue() {
1408 static char[] copyOfRange(char[] original, int from, int to) {
1409 int newLength = to - from;
1410 if (newLength < 0) {
1411 throw new IllegalArgumentException(from + " > " + to);
1413 char[] copy = new char[newLength];
1414 arraycopy(original, from, copy, 0, Math.min(original.length - from, newLength));
1418 static void arraycopy(char[] value, int srcBegin, char[] dst, int dstBegin, int count) {
1419 if (srcBegin < dstBegin) {
1420 while (count-- > 0) {
1421 dst[dstBegin + count] = value[srcBegin + count];
1424 while (count-- > 0) {
1425 dst[dstBegin++] = value[srcBegin++];
1430 // access system property
1431 static String getProperty(String nm) {
1435 static char[] copyOf(char[] original, int newLength) {
1436 char[] copy = new char[newLength];
1437 arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));