1 /*
   2  * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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.
  10  *
  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).
  16  *
  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.
  20  *
  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
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import sun.misc.FloatingDecimal;
  29 import java.util.Arrays;
  30 
  31 /**
  32  * A mutable sequence of characters.
  33  * <p>
  34  * Implements a modifiable string. At any point in time it contains some
  35  * particular sequence of characters, but the length and content of the
  36  * sequence can be changed through certain method calls.
  37  *
  38  * @author      Michael McCloskey
  39  * @author      Martin Buchholz
  40  * @author      Ulf Zibis
  41  * @since       1.5
  42  */
  43 abstract class AbstractStringBuilder implements Appendable, CharSequence {
  44     /**
  45      * The value is used for character storage.
  46      */
  47     char[] value;
  48 
  49     /**
  50      * The count is the number of characters used.
  51      */
  52     int count;
  53 
  54     /**
  55      * This no-arg constructor is necessary for serialization of subclasses.
  56      */
  57     AbstractStringBuilder() {
  58     }
  59 
  60     /**
  61      * Creates an AbstractStringBuilder of the specified capacity.
  62      */
  63     AbstractStringBuilder(int capacity) {
  64         value = new char[capacity];
  65     }
  66 
  67     /**
  68      * Returns the length (character count).
  69      *
  70      * @return  the length of the sequence of characters currently
  71      *          represented by this object
  72      */
  73     public int length() {
  74         return count;
  75     }
  76 
  77     /**
  78      * Returns the current capacity. The capacity is the amount of storage
  79      * available for newly inserted characters, beyond which an allocation
  80      * will occur.
  81      *
  82      * @return  the current capacity
  83      */
  84     public int capacity() {
  85         return value.length;
  86     }
  87 
  88     /**
  89      * Ensures that the capacity is at least equal to the specified minimum.
  90      * If the current capacity is less than the argument, then a new internal
  91      * array is allocated with greater capacity. The new capacity is the
  92      * larger of:
  93      * <ul>
  94      * <li>The {@code minimumCapacity} argument.
  95      * <li>Twice the old capacity, plus {@code 2}.
  96      * </ul>
  97      * If the {@code minimumCapacity} argument is nonpositive, this
  98      * method takes no action and simply returns.
  99      * Note that subsequent operations on this object can reduce the
 100      * actual capacity below that requested here.
 101      *
 102      * @param   minimumCapacity   the minimum desired capacity.
 103      */
 104     public void ensureCapacity(int minimumCapacity) {
 105         if (minimumCapacity > 0)
 106             ensureCapacityInternal(minimumCapacity);
 107     }
 108 
 109     /**
 110      * This method has the same contract as ensureCapacity, but is
 111      * never synchronized.
 112      */
 113     private void ensureCapacityInternal(int minimumCapacity) {
 114         // overflow-conscious code
 115         if (minimumCapacity - value.length > 0)
 116             expandCapacity(minimumCapacity);
 117     }
 118 
 119     /**
 120      * This implements the expansion semantics of ensureCapacity with no
 121      * size check or synchronization.
 122      */
 123     void expandCapacity(int minimumCapacity) {
 124         int newCapacity = value.length * 2 + 2;
 125         if (newCapacity - minimumCapacity < 0)
 126             newCapacity = minimumCapacity;
 127         if (newCapacity < 0) {
 128             if (minimumCapacity < 0) // overflow
 129                 throw new OutOfMemoryError();
 130             newCapacity = Integer.MAX_VALUE;
 131         }
 132         value = Arrays.copyOf(value, newCapacity);
 133     }
 134 
 135     /**
 136      * Attempts to reduce storage used for the character sequence.
 137      * If the buffer is larger than necessary to hold its current sequence of
 138      * characters, then it may be resized to become more space efficient.
 139      * Calling this method may, but is not required to, affect the value
 140      * returned by a subsequent call to the {@link #capacity()} method.
 141      */
 142     public void trimToSize() {
 143         if (count < value.length) {
 144             value = Arrays.copyOf(value, count);
 145         }
 146     }
 147 
 148     /**
 149      * Sets the length of the character sequence.
 150      * The sequence is changed to a new character sequence
 151      * whose length is specified by the argument. For every nonnegative
 152      * index <i>k</i> less than {@code newLength}, the character at
 153      * index <i>k</i> in the new character sequence is the same as the
 154      * character at index <i>k</i> in the old sequence if <i>k</i> is less
 155      * than the length of the old character sequence; otherwise, it is the
 156      * null character {@code '\u005Cu0000'}.
 157      *
 158      * In other words, if the {@code newLength} argument is less than
 159      * the current length, the length is changed to the specified length.
 160      * <p>
 161      * If the {@code newLength} argument is greater than or equal
 162      * to the current length, sufficient null characters
 163      * ({@code '\u005Cu0000'}) are appended so that
 164      * length becomes the {@code newLength} argument.
 165      * <p>
 166      * The {@code newLength} argument must be greater than or equal
 167      * to {@code 0}.
 168      *
 169      * @param      newLength   the new length
 170      * @throws     IndexOutOfBoundsException  if the
 171      *               {@code newLength} argument is negative.
 172      */
 173     public void setLength(int newLength) {
 174         if (newLength < 0)
 175             throw new StringIndexOutOfBoundsException(newLength);
 176         ensureCapacityInternal(newLength);
 177 
 178         if (count < newLength) {
 179             for (; count < newLength; count++)
 180                 value[count] = '\0';
 181         } else {
 182             count = newLength;
 183         }
 184     }
 185 
 186     /**
 187      * Returns the {@code char} value in this sequence at the specified index.
 188      * The first {@code char} value is at index {@code 0}, the next at index
 189      * {@code 1}, and so on, as in array indexing.
 190      * <p>
 191      * The index argument must be greater than or equal to
 192      * {@code 0}, and less than the length of this sequence.
 193      *
 194      * <p>If the {@code char} value specified by the index is a
 195      * <a href="Character.html#unicode">surrogate</a>, the surrogate
 196      * value is returned.
 197      *
 198      * @param      index   the index of the desired {@code char} value.
 199      * @return     the {@code char} value at the specified index.
 200      * @throws     IndexOutOfBoundsException  if {@code index} is
 201      *             negative or greater than or equal to {@code length()}.
 202      */
 203     public char charAt(int index) {
 204         if ((index < 0) || (index >= count))
 205             throw new StringIndexOutOfBoundsException(index);
 206         return value[index];
 207     }
 208 
 209     /**
 210      * Returns the character (Unicode code point) at the specified
 211      * index. The index refers to {@code char} values
 212      * (Unicode code units) and ranges from {@code 0} to
 213      * {@link #length()}{@code  - 1}.
 214      *
 215      * <p> If the {@code char} value specified at the given index
 216      * is in the high-surrogate range, the following index is less
 217      * than the length of this sequence, and the
 218      * {@code char} value at the following index is in the
 219      * low-surrogate range, then the supplementary code point
 220      * corresponding to this surrogate pair is returned. Otherwise,
 221      * the {@code char} value at the given index is returned.
 222      *
 223      * @param      index the index to the {@code char} values
 224      * @return     the code point value of the character at the
 225      *             {@code index}
 226      * @exception  IndexOutOfBoundsException  if the {@code index}
 227      *             argument is negative or not less than the length of this
 228      *             sequence.
 229      */
 230     public int codePointAt(int index) {
 231         if ((index < 0) || (index >= count)) {
 232             throw new StringIndexOutOfBoundsException(index);
 233         }
 234         return Character.codePointAt(value, index);
 235     }
 236 
 237     /**
 238      * Returns the character (Unicode code point) before the specified
 239      * index. The index refers to {@code char} values
 240      * (Unicode code units) and ranges from {@code 1} to {@link
 241      * #length()}.
 242      *
 243      * <p> If the {@code char} value at {@code (index - 1)}
 244      * is in the low-surrogate range, {@code (index - 2)} is not
 245      * negative, and the {@code char} value at {@code (index -
 246      * 2)} is in the high-surrogate range, then the
 247      * supplementary code point value of the surrogate pair is
 248      * returned. If the {@code char} value at {@code index -
 249      * 1} is an unpaired low-surrogate or a high-surrogate, the
 250      * surrogate value is returned.
 251      *
 252      * @param     index the index following the code point that should be returned
 253      * @return    the Unicode code point value before the given index.
 254      * @exception IndexOutOfBoundsException if the {@code index}
 255      *            argument is less than 1 or greater than the length
 256      *            of this sequence.
 257      */
 258     public int codePointBefore(int index) {
 259         int i = index - 1;
 260         if ((i < 0) || (i >= count)) {
 261             throw new StringIndexOutOfBoundsException(index);
 262         }
 263         return Character.codePointBefore(value, index);
 264     }
 265 
 266     /**
 267      * Returns the number of Unicode code points in the specified text
 268      * range of this sequence. The text range begins at the specified
 269      * {@code beginIndex} and extends to the {@code char} at
 270      * index {@code endIndex - 1}. Thus the length (in
 271      * {@code char}s) of the text range is
 272      * {@code endIndex-beginIndex}. Unpaired surrogates within
 273      * this sequence count as one code point each.
 274      *
 275      * @param beginIndex the index to the first {@code char} of
 276      * the text range.
 277      * @param endIndex the index after the last {@code char} of
 278      * the text range.
 279      * @return the number of Unicode code points in the specified text
 280      * range
 281      * @exception IndexOutOfBoundsException if the
 282      * {@code beginIndex} is negative, or {@code endIndex}
 283      * is larger than the length of this sequence, or
 284      * {@code beginIndex} is larger than {@code endIndex}.
 285      */
 286     public int codePointCount(int beginIndex, int endIndex) {
 287         if (beginIndex < 0 || endIndex > count || beginIndex > endIndex) {
 288             throw new IndexOutOfBoundsException();
 289         }
 290         return Character.codePointCountImpl(value, beginIndex, endIndex-beginIndex);
 291     }
 292 
 293     /**
 294      * Returns the index within this sequence that is offset from the
 295      * given {@code index} by {@code codePointOffset} code
 296      * points. Unpaired surrogates within the text range given by
 297      * {@code index} and {@code codePointOffset} count as
 298      * one code point each.
 299      *
 300      * @param index the index to be offset
 301      * @param codePointOffset the offset in code points
 302      * @return the index within this sequence
 303      * @exception IndexOutOfBoundsException if {@code index}
 304      *   is negative or larger then the length of this sequence,
 305      *   or if {@code codePointOffset} is positive and the subsequence
 306      *   starting with {@code index} has fewer than
 307      *   {@code codePointOffset} code points,
 308      *   or if {@code codePointOffset} is negative and the subsequence
 309      *   before {@code index} has fewer than the absolute value of
 310      *   {@code codePointOffset} code points.
 311      */
 312     public int offsetByCodePoints(int index, int codePointOffset) {
 313         if (index < 0 || index > count) {
 314             throw new IndexOutOfBoundsException();
 315         }
 316         return Character.offsetByCodePointsImpl(value, 0, count,
 317                                                 index, codePointOffset);
 318     }
 319 
 320     /**
 321      * Characters are copied from this sequence into the
 322      * destination character array {@code dst}. The first character to
 323      * be copied is at index {@code srcBegin}; the last character to
 324      * be copied is at index {@code srcEnd-1}. The total number of
 325      * characters to be copied is {@code srcEnd-srcBegin}. The
 326      * characters are copied into the subarray of {@code dst} starting
 327      * at index {@code dstBegin} and ending at index:
 328      * <p><blockquote><pre>
 329      * dstbegin + (srcEnd-srcBegin) - 1
 330      * </pre></blockquote>
 331      *
 332      * @param      srcBegin   start copying at this offset.
 333      * @param      srcEnd     stop copying at this offset.
 334      * @param      dst        the array to copy the data into.
 335      * @param      dstBegin   offset into {@code dst}.
 336      * @throws     NullPointerException if {@code dst} is
 337      *             {@code null}.
 338      * @throws     IndexOutOfBoundsException  if any of the following is true:
 339      *             <ul>
 340      *             <li>{@code srcBegin} is negative
 341      *             <li>{@code dstBegin} is negative
 342      *             <li>the {@code srcBegin} argument is greater than
 343      *             the {@code srcEnd} argument.
 344      *             <li>{@code srcEnd} is greater than
 345      *             {@code this.length()}.
 346      *             <li>{@code dstBegin+srcEnd-srcBegin} is greater than
 347      *             {@code dst.length}
 348      *             </ul>
 349      */
 350     public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
 351     {
 352         if (srcBegin < 0)
 353             throw new StringIndexOutOfBoundsException(srcBegin);
 354         if ((srcEnd < 0) || (srcEnd > count))
 355             throw new StringIndexOutOfBoundsException(srcEnd);
 356         if (srcBegin > srcEnd)
 357             throw new StringIndexOutOfBoundsException("srcBegin > srcEnd");
 358         System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
 359     }
 360 
 361     /**
 362      * The character at the specified index is set to {@code ch}. This
 363      * sequence is altered to represent a new character sequence that is
 364      * identical to the old character sequence, except that it contains the
 365      * character {@code ch} at position {@code index}.
 366      * <p>
 367      * The index argument must be greater than or equal to
 368      * {@code 0}, and less than the length of this sequence.
 369      *
 370      * @param      index   the index of the character to modify.
 371      * @param      ch      the new character.
 372      * @throws     IndexOutOfBoundsException  if {@code index} is
 373      *             negative or greater than or equal to {@code length()}.
 374      */
 375     public void setCharAt(int index, char ch) {
 376         if ((index < 0) || (index >= count))
 377             throw new StringIndexOutOfBoundsException(index);
 378         value[index] = ch;
 379     }
 380 
 381     /**
 382      * Appends the string representation of the {@code Object} argument.
 383      * <p>
 384      * The overall effect is exactly as if the argument were converted
 385      * to a string by the method {@link String#valueOf(Object)},
 386      * and the characters of that string were then
 387      * {@link #append(String) appended} to this character sequence.
 388      *
 389      * @param   obj   an {@code Object}.
 390      * @return  a reference to this object.
 391      */
 392     public AbstractStringBuilder append(Object obj) {
 393         return append(String.valueOf(obj));
 394     }
 395 
 396     /**
 397      * Appends the specified string to this character sequence.
 398      * <p>
 399      * The characters of the {@code String} argument are appended, in
 400      * order, increasing the length of this sequence by the length of the
 401      * argument. If {@code str} is {@code null}, then the four
 402      * characters {@code "null"} are appended.
 403      * <p>
 404      * Let <i>n</i> be the length of this character sequence just prior to
 405      * execution of the {@code append} method. Then the character at
 406      * index <i>k</i> in the new character sequence is equal to the character
 407      * at index <i>k</i> in the old character sequence, if <i>k</i> is less
 408      * than <i>n</i>; otherwise, it is equal to the character at index
 409      * <i>k-n</i> in the argument {@code str}.
 410      *
 411      * @param   str   a string.
 412      * @return  a reference to this object.
 413      */
 414     public AbstractStringBuilder append(String str) {
 415         if (str == null) str = "null";
 416         int len = str.length();
 417         ensureCapacityInternal(count + len);
 418         str.getChars(0, len, value, count);
 419         count += len;
 420         return this;
 421     }
 422 
 423     // Documentation in subclasses because of synchro difference
 424     public AbstractStringBuilder append(StringBuffer sb) {
 425         if (sb == null)
 426             return append("null");
 427         int len = sb.length();
 428         ensureCapacityInternal(count + len);
 429         sb.getChars(0, len, value, count);
 430         count += len;
 431         return this;
 432     }
 433 
 434     // Documentation in subclasses because of synchro difference
 435     public AbstractStringBuilder append(CharSequence s) {
 436         if (s == null)
 437             s = "null";
 438         if (s instanceof String)
 439             return this.append((String)s);
 440         if (s instanceof StringBuffer)
 441             return this.append((StringBuffer)s);
 442         return this.append(s, 0, s.length());
 443     }
 444 
 445     /**
 446      * Appends a subsequence of the specified {@code CharSequence} to this
 447      * sequence.
 448      * <p>
 449      * Characters of the argument {@code s}, starting at
 450      * index {@code start}, are appended, in order, to the contents of
 451      * this sequence up to the (exclusive) index {@code end}. The length
 452      * of this sequence is increased by the value of {@code end - start}.
 453      * <p>
 454      * Let <i>n</i> be the length of this character sequence just prior to
 455      * execution of the {@code append} method. Then the character at
 456      * index <i>k</i> in this character sequence becomes equal to the
 457      * character at index <i>k</i> in this sequence, if <i>k</i> is less than
 458      * <i>n</i>; otherwise, it is equal to the character at index
 459      * <i>k+start-n</i> in the argument {@code s}.
 460      * <p>
 461      * If {@code s} is {@code null}, then this method appends
 462      * characters as if the s parameter was a sequence containing the four
 463      * characters {@code "null"}.
 464      *
 465      * @param   s the sequence to append.
 466      * @param   start   the starting index of the subsequence to be appended.
 467      * @param   end     the end index of the subsequence to be appended.
 468      * @return  a reference to this object.
 469      * @throws     IndexOutOfBoundsException if
 470      *             {@code start} is negative, or
 471      *             {@code start} is greater than {@code end} or
 472      *             {@code end} is greater than {@code s.length()}
 473      */
 474     public AbstractStringBuilder append(CharSequence s, int start, int end) {
 475         if (s == null)
 476             s = "null";
 477         if ((start < 0) || (start > end) || (end > s.length()))
 478             throw new IndexOutOfBoundsException(
 479                 "start " + start + ", end " + end + ", s.length() "
 480                 + s.length());
 481         int len = end - start;
 482         ensureCapacityInternal(count + len);
 483         for (int i = start, j = count; i < end; i++, j++)
 484             value[j] = s.charAt(i);
 485         count += len;
 486         return this;
 487     }
 488 
 489     /**
 490      * Appends the string representation of the {@code char} array
 491      * argument to this sequence.
 492      * <p>
 493      * The characters of the array argument are appended, in order, to
 494      * the contents of this sequence. The length of this sequence
 495      * increases by the length of the argument.
 496      * <p>
 497      * The overall effect is exactly as if the argument were converted
 498      * to a string by the method {@link String#valueOf(char[])},
 499      * and the characters of that string were then
 500      * {@link #append(String) appended} to this character sequence.
 501      *
 502      * @param   str   the characters to be appended.
 503      * @return  a reference to this object.
 504      */
 505     public AbstractStringBuilder append(char[] str) {
 506         int len = str.length;
 507         ensureCapacityInternal(count + len);
 508         System.arraycopy(str, 0, value, count, len);
 509         count += len;
 510         return this;
 511     }
 512 
 513     /**
 514      * Appends the string representation of a subarray of the
 515      * {@code char} array argument to this sequence.
 516      * <p>
 517      * Characters of the {@code char} array {@code str}, starting at
 518      * index {@code offset}, are appended, in order, to the contents
 519      * of this sequence. The length of this sequence increases
 520      * by the value of {@code len}.
 521      * <p>
 522      * The overall effect is exactly as if the arguments were converted
 523      * to a string by the method {@link String#valueOf(char[],int,int)},
 524      * and the characters of that string were then
 525      * {@link #append(String) appended} to this character sequence.
 526      *
 527      * @param   str      the characters to be appended.
 528      * @param   offset   the index of the first {@code char} to append.
 529      * @param   len      the number of {@code char}s to append.
 530      * @return  a reference to this object.
 531      * @throws IndexOutOfBoundsException
 532      *         if {@code offset < 0} or {@code len < 0}
 533      *         or {@code offset+len > str.length}
 534      */
 535     public AbstractStringBuilder append(char str[], int offset, int len) {
 536         if (len > 0)                // let arraycopy report AIOOBE for len < 0
 537             ensureCapacityInternal(count + len);
 538         System.arraycopy(str, offset, value, count, len);
 539         count += len;
 540         return this;
 541     }
 542 
 543     /**
 544      * Appends the string representation of the {@code boolean}
 545      * argument to the sequence.
 546      * <p>
 547      * The overall effect is exactly as if the argument were converted
 548      * to a string by the method {@link String#valueOf(boolean)},
 549      * and the characters of that string were then
 550      * {@link #append(String) appended} to this character sequence.
 551      *
 552      * @param   b   a {@code boolean}.
 553      * @return  a reference to this object.
 554      */
 555     public AbstractStringBuilder append(boolean b) {
 556         if (b) {
 557             ensureCapacityInternal(count + 4);
 558             value[count++] = 't';
 559             value[count++] = 'r';
 560             value[count++] = 'u';
 561             value[count++] = 'e';
 562         } else {
 563             ensureCapacityInternal(count + 5);
 564             value[count++] = 'f';
 565             value[count++] = 'a';
 566             value[count++] = 'l';
 567             value[count++] = 's';
 568             value[count++] = 'e';
 569         }
 570         return this;
 571     }
 572 
 573     /**
 574      * Appends the string representation of the {@code char}
 575      * argument to this sequence.
 576      * <p>
 577      * The argument is appended to the contents of this sequence.
 578      * The length of this sequence increases by {@code 1}.
 579      * <p>
 580      * The overall effect is exactly as if the argument were converted
 581      * to a string by the method {@link String#valueOf(char)},
 582      * and the character in that string were then
 583      * {@link #append(String) appended} to this character sequence.
 584      *
 585      * @param   c   a {@code char}.
 586      * @return  a reference to this object.
 587      */
 588     public AbstractStringBuilder append(char c) {
 589         ensureCapacityInternal(count + 1);
 590         value[count++] = c;
 591         return this;
 592     }
 593 
 594     /**
 595      * Appends the string representation of the {@code int}
 596      * argument to this sequence.
 597      * <p>
 598      * The overall effect is exactly as if the argument were converted
 599      * to a string by the method {@link String#valueOf(int)},
 600      * and the characters of that string were then
 601      * {@link #append(String) appended} to this character sequence.
 602      *
 603      * @param   i   an {@code int}.
 604      * @return  a reference to this object.
 605      */
 606     public AbstractStringBuilder append(int i) {
 607         if (i == Integer.MIN_VALUE) {
 608             append("-2147483648");
 609             return this;
 610         }
 611         int appendedLength = (i < 0) ? Integer.stringSize(-i) + 1
 612                                      : Integer.stringSize(i);
 613         int spaceNeeded = count + appendedLength;
 614         ensureCapacityInternal(spaceNeeded);
 615         Integer.getChars(i, spaceNeeded, value);
 616         count = spaceNeeded;
 617         return this;
 618     }
 619 
 620     /**
 621      * Appends the string representation of the {@code long}
 622      * argument to this sequence.
 623      * <p>
 624      * The overall effect is exactly as if the argument were converted
 625      * to a string by the method {@link String#valueOf(long)},
 626      * and the characters of that string were then
 627      * {@link #append(String) appended} to this character sequence.
 628      *
 629      * @param   l   a {@code long}.
 630      * @return  a reference to this object.
 631      */
 632     public AbstractStringBuilder append(long l) {
 633         if (l == Long.MIN_VALUE) {
 634             append("-9223372036854775808");
 635             return this;
 636         }
 637         int appendedLength = (l < 0) ? Long.stringSize(-l) + 1
 638                                      : Long.stringSize(l);
 639         int spaceNeeded = count + appendedLength;
 640         ensureCapacityInternal(spaceNeeded);
 641         Long.getChars(l, spaceNeeded, value);
 642         count = spaceNeeded;
 643         return this;
 644     }
 645 
 646     /**
 647      * Appends the string representation of the {@code float}
 648      * argument to this sequence.
 649      * <p>
 650      * The overall effect is exactly as if the argument were converted
 651      * to a string by the method {@link String#valueOf(float)},
 652      * and the characters of that string were then
 653      * {@link #append(String) appended} to this character sequence.
 654      *
 655      * @param   f   a {@code float}.
 656      * @return  a reference to this object.
 657      */
 658     public AbstractStringBuilder append(float f) {
 659         new FloatingDecimal(f).appendTo(this);
 660         return this;
 661     }
 662 
 663     /**
 664      * Appends the string representation of the {@code double}
 665      * argument to this sequence.
 666      * <p>
 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.
 671      *
 672      * @param   d   a {@code double}.
 673      * @return  a reference to this object.
 674      */
 675     public AbstractStringBuilder append(double d) {
 676         new FloatingDecimal(d).appendTo(this);
 677         return this;
 678     }
 679 
 680     /**
 681      * Removes the characters in a substring of this sequence.
 682      * The substring begins at the specified {@code start} and extends to
 683      * the character at index {@code end - 1} or to the end of the
 684      * sequence if no such character exists. If
 685      * {@code start} is equal to {@code end}, no changes are made.
 686      *
 687      * @param      start  The beginning index, inclusive.
 688      * @param      end    The ending index, exclusive.
 689      * @return     This object.
 690      * @throws     StringIndexOutOfBoundsException  if {@code start}
 691      *             is negative, greater than {@code length()}, or
 692      *             greater than {@code end}.
 693      */
 694     public AbstractStringBuilder delete(int start, int end) {
 695         if (start < 0)
 696             throw new StringIndexOutOfBoundsException(start);
 697         if (end > count)
 698             end = count;
 699         if (start > end)
 700             throw new StringIndexOutOfBoundsException();
 701         int len = end - start;
 702         if (len > 0) {
 703             System.arraycopy(value, start+len, value, start, count-end);
 704             count -= len;
 705         }
 706         return this;
 707     }
 708 
 709     /**
 710      * Appends the string representation of the {@code codePoint}
 711      * argument to this sequence.
 712      *
 713      * <p> The argument is appended to the contents of this sequence.
 714      * The length of this sequence increases by
 715      * {@link Character#charCount(int) Character.charCount(codePoint)}.
 716      *
 717      * <p> The overall effect is exactly as if the argument were
 718      * converted to a {@code char} array by the method
 719      * {@link Character#toChars(int)} and the character in that array
 720      * were then {@link #append(char[]) appended} to this character
 721      * sequence.
 722      *
 723      * @param   codePoint   a Unicode code point
 724      * @return  a reference to this object.
 725      * @exception IllegalArgumentException if the specified
 726      * {@code codePoint} isn't a valid Unicode code point
 727      */
 728     public AbstractStringBuilder appendCodePoint(int codePoint) {
 729         final int count = this.count;
 730 
 731         if (Character.isBmpCodePoint(codePoint)) {
 732             ensureCapacityInternal(count + 1);
 733             value[count] = (char) codePoint;
 734             this.count = count + 1;
 735         } else if (Character.isValidCodePoint(codePoint)) {
 736             ensureCapacityInternal(count + 2);
 737             Character.toSurrogates(codePoint, value, count);
 738             this.count = count + 2;
 739         } else {
 740             throw new IllegalArgumentException();
 741         }
 742         return this;
 743     }
 744 
 745     /**
 746      * Removes the {@code char} at the specified position in this
 747      * sequence. This sequence is shortened by one {@code char}.
 748      *
 749      * <p>Note: If the character at the given index is a supplementary
 750      * character, this method does not remove the entire character. If
 751      * correct handling of supplementary characters is required,
 752      * determine the number of {@code char}s to remove by calling
 753      * {@code Character.charCount(thisSequence.codePointAt(index))},
 754      * where {@code thisSequence} is this sequence.
 755      *
 756      * @param       index  Index of {@code char} to remove
 757      * @return      This object.
 758      * @throws      StringIndexOutOfBoundsException  if the {@code index}
 759      *              is negative or greater than or equal to
 760      *              {@code length()}.
 761      */
 762     public AbstractStringBuilder deleteCharAt(int index) {
 763         if ((index < 0) || (index >= count))
 764             throw new StringIndexOutOfBoundsException(index);
 765         System.arraycopy(value, index+1, value, index, count-index-1);
 766         count--;
 767         return this;
 768     }
 769 
 770     /**
 771      * Replaces the characters in a substring of this sequence
 772      * with characters in the specified {@code String}. The substring
 773      * begins at the specified {@code start} and extends to the character
 774      * at index {@code end - 1} or to the end of the
 775      * sequence if no such character exists. First the
 776      * characters in the substring are removed and then the specified
 777      * {@code String} is inserted at {@code start}. (This
 778      * sequence will be lengthened to accommodate the
 779      * specified String if necessary.)
 780      *
 781      * @param      start    The beginning index, inclusive.
 782      * @param      end      The ending index, exclusive.
 783      * @param      str   String that will replace previous contents.
 784      * @return     This object.
 785      * @throws     StringIndexOutOfBoundsException  if {@code start}
 786      *             is negative, greater than {@code length()}, or
 787      *             greater than {@code end}.
 788      */
 789     public AbstractStringBuilder replace(int start, int end, String str) {
 790         if (start < 0)
 791             throw new StringIndexOutOfBoundsException(start);
 792         if (start > count)
 793             throw new StringIndexOutOfBoundsException("start > length()");
 794         if (start > end)
 795             throw new StringIndexOutOfBoundsException("start > end");
 796 
 797         if (end > count)
 798             end = count;
 799         int len = str.length();
 800         int newCount = count + len - (end - start);
 801         ensureCapacityInternal(newCount);
 802 
 803         System.arraycopy(value, end, value, start + len, count - end);
 804         str.getChars(value, start);
 805         count = newCount;
 806         return this;
 807     }
 808 
 809     /**
 810      * Returns a new {@code String} that contains a subsequence of
 811      * characters currently contained in this character sequence. The
 812      * substring begins at the specified index and extends to the end of
 813      * this sequence.
 814      *
 815      * @param      start    The beginning index, inclusive.
 816      * @return     The new string.
 817      * @throws     StringIndexOutOfBoundsException  if {@code start} is
 818      *             less than zero, or greater than the length of this object.
 819      */
 820     public String substring(int start) {
 821         return substring(start, count);
 822     }
 823 
 824     /**
 825      * Returns a new character sequence that is a subsequence of this sequence.
 826      *
 827      * <p> An invocation of this method of the form
 828      *
 829      * <blockquote><pre>
 830      * sb.subSequence(begin,&nbsp;end)</pre></blockquote>
 831      *
 832      * behaves in exactly the same way as the invocation
 833      *
 834      * <blockquote><pre>
 835      * sb.substring(begin,&nbsp;end)</pre></blockquote>
 836      *
 837      * This method is provided so that this class can
 838      * implement the {@link CharSequence} interface. </p>
 839      *
 840      * @param      start   the start index, inclusive.
 841      * @param      end     the end index, exclusive.
 842      * @return     the specified subsequence.
 843      *
 844      * @throws  IndexOutOfBoundsException
 845      *          if <tt>start</tt> or <tt>end</tt> are negative,
 846      *          if <tt>end</tt> is greater than <tt>length()</tt>,
 847      *          or if <tt>start</tt> is greater than <tt>end</tt>
 848      * @spec JSR-51
 849      */
 850     public CharSequence subSequence(int start, int end) {
 851         return substring(start, end);
 852     }
 853 
 854     /**
 855      * Returns a new {@code String} that contains a subsequence of
 856      * characters currently contained in this sequence. The
 857      * substring begins at the specified {@code start} and
 858      * extends to the character at index {@code end - 1}.
 859      *
 860      * @param      start    The beginning index, inclusive.
 861      * @param      end      The ending index, exclusive.
 862      * @return     The new string.
 863      * @throws     StringIndexOutOfBoundsException  if {@code start}
 864      *             or {@code end} are negative or greater than
 865      *             {@code length()}, or {@code start} is
 866      *             greater than {@code end}.
 867      */
 868     public String substring(int start, int end) {
 869         if (start < 0)
 870             throw new StringIndexOutOfBoundsException(start);
 871         if (end > count)
 872             throw new StringIndexOutOfBoundsException(end);
 873         if (start > end)
 874             throw new StringIndexOutOfBoundsException(end - start);
 875         return new String(value, start, end - start);
 876     }
 877 
 878     /**
 879      * Inserts the string representation of a subarray of the {@code str}
 880      * array argument into this sequence. The subarray begins at the
 881      * specified {@code offset} and extends {@code len} {@code char}s.
 882      * The characters of the subarray are inserted into this sequence at
 883      * the position indicated by {@code index}. The length of this
 884      * sequence increases by {@code len} {@code char}s.
 885      *
 886      * @param      index    position at which to insert subarray.
 887      * @param      str       A {@code char} array.
 888      * @param      offset   the index of the first {@code char} in subarray to
 889      *             be inserted.
 890      * @param      len      the number of {@code char}s in the subarray to
 891      *             be inserted.
 892      * @return     This object
 893      * @throws     StringIndexOutOfBoundsException  if {@code index}
 894      *             is negative or greater than {@code length()}, or
 895      *             {@code offset} or {@code len} are negative, or
 896      *             {@code (offset+len)} is greater than
 897      *             {@code str.length}.
 898      */
 899     public AbstractStringBuilder insert(int index, char[] str, int offset,
 900                                         int len)
 901     {
 902         if ((index < 0) || (index > length()))
 903             throw new StringIndexOutOfBoundsException(index);
 904         if ((offset < 0) || (len < 0) || (offset > str.length - len))
 905             throw new StringIndexOutOfBoundsException(
 906                 "offset " + offset + ", len " + len + ", str.length "
 907                 + str.length);
 908         ensureCapacityInternal(count + len);
 909         System.arraycopy(value, index, value, index + len, count - index);
 910         System.arraycopy(str, offset, value, index, len);
 911         count += len;
 912         return this;
 913     }
 914 
 915     /**
 916      * Inserts the string representation of the {@code Object}
 917      * argument into this character sequence.
 918      * <p>
 919      * The overall effect is exactly as if the second argument were
 920      * converted to a string by the method {@link String#valueOf(Object)},
 921      * and the characters of that string were then
 922      * {@link #insert(int,String) inserted} into this character
 923      * sequence at the indicated offset.
 924      * <p>
 925      * The {@code offset} argument must be greater than or equal to
 926      * {@code 0}, and less than or equal to the {@linkplain #length() length}
 927      * of this sequence.
 928      *
 929      * @param      offset   the offset.
 930      * @param      obj      an {@code Object}.
 931      * @return     a reference to this object.
 932      * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
 933      */
 934     public AbstractStringBuilder insert(int offset, Object obj) {
 935         return insert(offset, String.valueOf(obj));
 936     }
 937 
 938     /**
 939      * Inserts the string into this character sequence.
 940      * <p>
 941      * The characters of the {@code String} argument are inserted, in
 942      * order, into this sequence at the indicated offset, moving up any
 943      * characters originally above that position and increasing the length
 944      * of this sequence by the length of the argument. If
 945      * {@code str} is {@code null}, then the four characters
 946      * {@code "null"} are inserted into this sequence.
 947      * <p>
 948      * The character at index <i>k</i> in the new character sequence is
 949      * equal to:
 950      * <ul>
 951      * <li>the character at index <i>k</i> in the old character sequence, if
 952      * <i>k</i> is less than {@code offset}
 953      * <li>the character at index <i>k</i>{@code -offset} in the
 954      * argument {@code str}, if <i>k</i> is not less than
 955      * {@code offset} but is less than {@code offset+str.length()}
 956      * <li>the character at index <i>k</i>{@code -str.length()} in the
 957      * old character sequence, if <i>k</i> is not less than
 958      * {@code offset+str.length()}
 959      * </ul><p>
 960      * The {@code offset} argument must be greater than or equal to
 961      * {@code 0}, and less than or equal to the {@linkplain #length() length}
 962      * of this sequence.
 963      *
 964      * @param      offset   the offset.
 965      * @param      str      a string.
 966      * @return     a reference to this object.
 967      * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
 968      */
 969     public AbstractStringBuilder insert(int offset, String str) {
 970         if ((offset < 0) || (offset > length()))
 971             throw new StringIndexOutOfBoundsException(offset);
 972         if (str == null)
 973             str = "null";
 974         int len = str.length();
 975         ensureCapacityInternal(count + len);
 976         System.arraycopy(value, offset, value, offset + len, count - offset);
 977         str.getChars(value, offset);
 978         count += len;
 979         return this;
 980     }
 981 
 982     /**
 983      * Inserts the string representation of the {@code char} array
 984      * argument into this sequence.
 985      * <p>
 986      * The characters of the array argument are inserted into the
 987      * contents of this sequence at the position indicated by
 988      * {@code offset}. The length of this sequence increases by
 989      * the length of the argument.
 990      * <p>
 991      * The overall effect is exactly as if the second argument were
 992      * converted to a string by the method {@link String#valueOf(char[])},
 993      * and the characters of that string were then
 994      * {@link #insert(int,String) inserted} into this character
 995      * sequence at the indicated offset.
 996      * <p>
 997      * The {@code offset} argument must be greater than or equal to
 998      * {@code 0}, and less than or equal to the {@linkplain #length() length}
 999      * of this sequence.
1000      *
1001      * @param      offset   the offset.
1002      * @param      str      a character array.
1003      * @return     a reference to this object.
1004      * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
1005      */
1006     public AbstractStringBuilder insert(int offset, char[] str) {
1007         if ((offset < 0) || (offset > length()))
1008             throw new StringIndexOutOfBoundsException(offset);
1009         int len = str.length;
1010         ensureCapacityInternal(count + len);
1011         System.arraycopy(value, offset, value, offset + len, count - offset);
1012         System.arraycopy(str, 0, value, offset, len);
1013         count += len;
1014         return this;
1015     }
1016 
1017     /**
1018      * Inserts the specified {@code CharSequence} into this sequence.
1019      * <p>
1020      * The characters of the {@code CharSequence} argument are inserted,
1021      * in order, into this sequence at the indicated offset, moving up
1022      * any characters originally above that position and increasing the length
1023      * of this sequence by the length of the argument s.
1024      * <p>
1025      * The result of this method is exactly the same as if it were an
1026      * invocation of this object's
1027      * {@link #insert(int,CharSequence,int,int) insert}(dstOffset, s, 0, s.length())
1028      * method.
1029      *
1030      * <p>If {@code s} is {@code null}, then the four characters
1031      * {@code "null"} are inserted into this sequence.
1032      *
1033      * @param      dstOffset   the offset.
1034      * @param      s the sequence to be inserted
1035      * @return     a reference to this object.
1036      * @throws     IndexOutOfBoundsException  if the offset is invalid.
1037      */
1038     public AbstractStringBuilder insert(int dstOffset, CharSequence s) {
1039         if (s == null)
1040             s = "null";
1041         if (s instanceof String)
1042             return this.insert(dstOffset, (String)s);
1043         return this.insert(dstOffset, s, 0, s.length());
1044     }
1045 
1046     /**
1047      * Inserts a subsequence of the specified {@code CharSequence} into
1048      * this sequence.
1049      * <p>
1050      * The subsequence of the argument {@code s} specified by
1051      * {@code start} and {@code end} are inserted,
1052      * in order, into this sequence at the specified destination offset, moving
1053      * up any characters originally above that position. The length of this
1054      * sequence is increased by {@code end - start}.
1055      * <p>
1056      * The character at index <i>k</i> in this sequence becomes equal to:
1057      * <ul>
1058      * <li>the character at index <i>k</i> in this sequence, if
1059      * <i>k</i> is less than {@code dstOffset}
1060      * <li>the character at index <i>k</i>{@code +start-dstOffset} in
1061      * the argument {@code s}, if <i>k</i> is greater than or equal to
1062      * {@code dstOffset} but is less than {@code dstOffset+end-start}
1063      * <li>the character at index <i>k</i>{@code -(end-start)} in this
1064      * sequence, if <i>k</i> is greater than or equal to
1065      * {@code dstOffset+end-start}
1066      * </ul><p>
1067      * The {@code dstOffset} argument must be greater than or equal to
1068      * {@code 0}, and less than or equal to the {@linkplain #length() length}
1069      * of this sequence.
1070      * <p>The start argument must be nonnegative, and not greater than
1071      * {@code end}.
1072      * <p>The end argument must be greater than or equal to
1073      * {@code start}, and less than or equal to the length of s.
1074      *
1075      * <p>If {@code s} is {@code null}, then this method inserts
1076      * characters as if the s parameter was a sequence containing the four
1077      * characters {@code "null"}.
1078      *
1079      * @param      dstOffset   the offset in this sequence.
1080      * @param      s       the sequence to be inserted.
1081      * @param      start   the starting index of the subsequence to be inserted.
1082      * @param      end     the end index of the subsequence to be inserted.
1083      * @return     a reference to this object.
1084      * @throws     IndexOutOfBoundsException  if {@code dstOffset}
1085      *             is negative or greater than {@code this.length()}, or
1086      *              {@code start} or {@code end} are negative, or
1087      *              {@code start} is greater than {@code end} or
1088      *              {@code end} is greater than {@code s.length()}
1089      */
1090      public AbstractStringBuilder insert(int dstOffset, CharSequence s,
1091                                          int start, int end) {
1092         if (s == null)
1093             s = "null";
1094         if ((dstOffset < 0) || (dstOffset > this.length()))
1095             throw new IndexOutOfBoundsException("dstOffset "+dstOffset);
1096         if ((start < 0) || (end < 0) || (start > end) || (end > s.length()))
1097             throw new IndexOutOfBoundsException(
1098                 "start " + start + ", end " + end + ", s.length() "
1099                 + s.length());
1100         int len = end - start;
1101         ensureCapacityInternal(count + len);
1102         System.arraycopy(value, dstOffset, value, dstOffset + len,
1103                          count - dstOffset);
1104         for (int i=start; i<end; i++)
1105             value[dstOffset++] = s.charAt(i);
1106         count += len;
1107         return this;
1108     }
1109 
1110     /**
1111      * Inserts the string representation of the {@code boolean}
1112      * argument into this sequence.
1113      * <p>
1114      * The overall effect is exactly as if the second argument were
1115      * converted to a string by the method {@link String#valueOf(boolean)},
1116      * and the characters of that string were then
1117      * {@link #insert(int,String) inserted} into this character
1118      * sequence at the indicated offset.
1119      * <p>
1120      * The {@code offset} argument must be greater than or equal to
1121      * {@code 0}, and less than or equal to the {@linkplain #length() length}
1122      * of this sequence.
1123      *
1124      * @param      offset   the offset.
1125      * @param      b        a {@code boolean}.
1126      * @return     a reference to this object.
1127      * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
1128      */
1129     public AbstractStringBuilder insert(int offset, boolean b) {
1130         return insert(offset, String.valueOf(b));
1131     }
1132 
1133     /**
1134      * Inserts the string representation of the {@code char}
1135      * argument into this sequence.
1136      * <p>
1137      * The overall effect is exactly as if the second argument were
1138      * converted to a string by the method {@link String#valueOf(char)},
1139      * and the character in that string were then
1140      * {@link #insert(int,String) inserted} into this character
1141      * sequence at the indicated offset.
1142      * <p>
1143      * The {@code offset} argument must be greater than or equal to
1144      * {@code 0}, and less than or equal to the {@linkplain #length() length}
1145      * of this sequence.
1146      *
1147      * @param      offset   the offset.
1148      * @param      c        a {@code char}.
1149      * @return     a reference to this object.
1150      * @throws     IndexOutOfBoundsException  if the offset is invalid.
1151      */
1152     public AbstractStringBuilder insert(int offset, char c) {
1153         ensureCapacityInternal(count + 1);
1154         System.arraycopy(value, offset, value, offset + 1, count - offset);
1155         value[offset] = c;
1156         count += 1;
1157         return this;
1158     }
1159 
1160     /**
1161      * Inserts the string representation of the second {@code int}
1162      * argument into this sequence.
1163      * <p>
1164      * The overall effect is exactly as if the second argument were
1165      * converted to a string by the method {@link String#valueOf(int)},
1166      * and the characters of that string were then
1167      * {@link #insert(int,String) inserted} into this character
1168      * sequence at the indicated offset.
1169      * <p>
1170      * The {@code offset} argument must be greater than or equal to
1171      * {@code 0}, and less than or equal to the {@linkplain #length() length}
1172      * of this sequence.
1173      *
1174      * @param      offset   the offset.
1175      * @param      i        an {@code int}.
1176      * @return     a reference to this object.
1177      * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
1178      */
1179     public AbstractStringBuilder insert(int offset, int i) {
1180         return insert(offset, String.valueOf(i));
1181     }
1182 
1183     /**
1184      * Inserts the string representation of the {@code long}
1185      * argument into this sequence.
1186      * <p>
1187      * The overall effect is exactly as if the second argument were
1188      * converted to a string by the method {@link String#valueOf(long)},
1189      * and the characters of that string were then
1190      * {@link #insert(int,String) inserted} into this character
1191      * sequence at the indicated offset.
1192      * <p>
1193      * The {@code offset} argument must be greater than or equal to
1194      * {@code 0}, and less than or equal to the {@linkplain #length() length}
1195      * of this sequence.
1196      *
1197      * @param      offset   the offset.
1198      * @param      l        a {@code long}.
1199      * @return     a reference to this object.
1200      * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
1201      */
1202     public AbstractStringBuilder insert(int offset, long l) {
1203         return insert(offset, String.valueOf(l));
1204     }
1205 
1206     /**
1207      * Inserts the string representation of the {@code float}
1208      * argument into this sequence.
1209      * <p>
1210      * The overall effect is exactly as if the second argument were
1211      * converted to a string by the method {@link String#valueOf(float)},
1212      * and the characters of that string were then
1213      * {@link #insert(int,String) inserted} into this character
1214      * sequence at the indicated offset.
1215      * <p>
1216      * The {@code offset} argument must be greater than or equal to
1217      * {@code 0}, and less than or equal to the {@linkplain #length() length}
1218      * of this sequence.
1219      *
1220      * @param      offset   the offset.
1221      * @param      f        a {@code float}.
1222      * @return     a reference to this object.
1223      * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
1224      */
1225     public AbstractStringBuilder insert(int offset, float f) {
1226         return insert(offset, String.valueOf(f));
1227     }
1228 
1229     /**
1230      * Inserts the string representation of the {@code double}
1231      * argument into this sequence.
1232      * <p>
1233      * The overall effect is exactly as if the second argument were
1234      * converted to a string by the method {@link String#valueOf(double)},
1235      * and the characters of that string were then
1236      * {@link #insert(int,String) inserted} into this character
1237      * sequence at the indicated offset.
1238      * <p>
1239      * The {@code offset} argument must be greater than or equal to
1240      * {@code 0}, and less than or equal to the {@linkplain #length() length}
1241      * of this sequence.
1242      *
1243      * @param      offset   the offset.
1244      * @param      d        a {@code double}.
1245      * @return     a reference to this object.
1246      * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
1247      */
1248     public AbstractStringBuilder insert(int offset, double d) {
1249         return insert(offset, String.valueOf(d));
1250     }
1251 
1252     /**
1253      * Returns the index within this string of the first occurrence of the
1254      * specified substring. The integer returned is the smallest value
1255      * <i>k</i> such that:
1256      * <blockquote><pre>
1257      * this.toString().startsWith(str, <i>k</i>)
1258      * </pre></blockquote>
1259      * is {@code true}.
1260      *
1261      * @param   str   any string.
1262      * @return  if the string argument occurs as a substring within this
1263      *          object, then the index of the first character of the first
1264      *          such substring is returned; if it does not occur as a
1265      *          substring, {@code -1} is returned.
1266      * @throws  java.lang.NullPointerException if {@code str} is
1267      *          {@code null}.
1268      */
1269     public int indexOf(String str) {
1270         return indexOf(str, 0);
1271     }
1272 
1273     /**
1274      * Returns the index within this string of the first occurrence of the
1275      * specified substring, starting at the specified index.  The integer
1276      * returned is the smallest value <tt>k</tt> for which:
1277      * <blockquote><pre>
1278      *     k >= Math.min(fromIndex, str.length()) &&
1279      *                   this.toString().startsWith(str, k)
1280      * </pre></blockquote>
1281      * If no such value of <i>k</i> exists, then -1 is returned.
1282      *
1283      * @param   str         the substring for which to search.
1284      * @param   fromIndex   the index from which to start the search.
1285      * @return  the index within this string of the first occurrence of the
1286      *          specified substring, starting at the specified index.
1287      * @throws  java.lang.NullPointerException if {@code str} is
1288      *            {@code null}.
1289      */
1290     public int indexOf(String str, int fromIndex) {
1291         return String.indexOf(value, 0, count,
1292                               str.toCharArray(), 0, str.length(), fromIndex);
1293     }
1294 
1295     /**
1296      * Returns the index within this string of the rightmost occurrence
1297      * of the specified substring.  The rightmost empty string "" is
1298      * considered to occur at the index value {@code this.length()}.
1299      * The returned index is the largest value <i>k</i> such that
1300      * <blockquote><pre>
1301      * this.toString().startsWith(str, k)
1302      * </pre></blockquote>
1303      * is true.
1304      *
1305      * @param   str   the substring to search for.
1306      * @return  if the string argument occurs one or more times as a substring
1307      *          within this object, then the index of the first character of
1308      *          the last such substring is returned. If it does not occur as
1309      *          a substring, {@code -1} is returned.
1310      * @throws  java.lang.NullPointerException  if {@code str} is
1311      *          {@code null}.
1312      */
1313     public int lastIndexOf(String str) {
1314         return lastIndexOf(str, count);
1315     }
1316 
1317     /**
1318      * Returns the index within this string of the last occurrence of the
1319      * specified substring. The integer returned is the largest value <i>k</i>
1320      * such that:
1321      * <blockquote><pre>
1322      *     k <= Math.min(fromIndex, str.length()) &&
1323      *                   this.toString().startsWith(str, k)
1324      * </pre></blockquote>
1325      * If no such value of <i>k</i> exists, then -1 is returned.
1326      *
1327      * @param   str         the substring to search for.
1328      * @param   fromIndex   the index to start the search from.
1329      * @return  the index within this sequence of the last occurrence of the
1330      *          specified substring.
1331      * @throws  java.lang.NullPointerException if {@code str} is
1332      *          {@code null}.
1333      */
1334     public int lastIndexOf(String str, int fromIndex) {
1335         return String.lastIndexOf(value, 0, count,
1336                               str.toCharArray(), 0, str.length(), fromIndex);
1337     }
1338 
1339     /**
1340      * Causes this character sequence to be replaced by the reverse of
1341      * the sequence. If there are any surrogate pairs included in the
1342      * sequence, these are treated as single characters for the
1343      * reverse operation. Thus, the order of the high-low surrogates
1344      * is never reversed.
1345      *
1346      * Let <i>n</i> be the character length of this character sequence
1347      * (not the length in {@code char} values) just prior to
1348      * execution of the {@code reverse} method. Then the
1349      * character at index <i>k</i> in the new character sequence is
1350      * equal to the character at index <i>n-k-1</i> in the old
1351      * character sequence.
1352      *
1353      * <p>Note that the reverse operation may result in producing
1354      * surrogate pairs that were unpaired low-surrogates and
1355      * high-surrogates before the operation. For example, reversing
1356      * "\u005CuDC00\u005CuD800" produces "\u005CuD800\u005CuDC00" which is
1357      * a valid surrogate pair.
1358      *
1359      * @return  a reference to this object.
1360      */
1361     public AbstractStringBuilder reverse() {
1362         boolean hasSurrogate = false;
1363         int n = count - 1;
1364         for (int j = (n-1) >> 1; j >= 0; --j) {
1365             char temp = value[j];
1366             char temp2 = value[n - j];
1367             if (!hasSurrogate) {
1368                 hasSurrogate = (temp >= Character.MIN_SURROGATE && temp <= Character.MAX_SURROGATE)
1369                     || (temp2 >= Character.MIN_SURROGATE && temp2 <= Character.MAX_SURROGATE);
1370             }
1371             value[j] = temp2;
1372             value[n - j] = temp;
1373         }
1374         if (hasSurrogate) {
1375             // Reverse back all valid surrogate pairs
1376             for (int i = 0; i < count - 1; i++) {
1377                 char c2 = value[i];
1378                 if (Character.isLowSurrogate(c2)) {
1379                     char c1 = value[i + 1];
1380                     if (Character.isHighSurrogate(c1)) {
1381                         value[i++] = c1;
1382                         value[i] = c2;
1383                     }
1384                 }
1385             }
1386         }
1387         return this;
1388     }
1389 
1390     /**
1391      * Returns a string representing the data in this sequence.
1392      * A new {@code String} object is allocated and initialized to
1393      * contain the character sequence currently represented by this
1394      * object. This {@code String} is then returned. Subsequent
1395      * changes to this sequence do not affect the contents of the
1396      * {@code String}.
1397      *
1398      * @return  a string representation of this sequence of characters.
1399      */
1400     public abstract String toString();
1401 
1402     /**
1403      * Needed by <tt>String</tt> for the contentEquals method.
1404      */
1405     final char[] getValue() {
1406         return value;
1407     }
1408 
1409 }