emul/src/main/java/java/lang/AbstractStringBuilder.java
author Jaroslav Tulach <jtulach@netbeans.org>
Wed, 21 Nov 2012 21:48:51 +0100
changeset 192 3bb0eb51bed6
parent 188 51d08c49e9b6
child 203 c6a0b5b64133
permissions -rw-r--r--
StringBuilder.delete needs bottom-up array copy
     1 /*
     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.
     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 org.apidesign.bck2brwsr.core.JavaScriptBody;
    29 
    30 /**
    31  * A mutable sequence of characters.
    32  * <p>
    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.
    36  *
    37  * @author      Michael McCloskey
    38  * @author      Martin Buchholz
    39  * @author      Ulf Zibis
    40  * @since       1.5
    41  */
    42 abstract class AbstractStringBuilder implements Appendable, CharSequence {
    43     /**
    44      * The value is used for character storage.
    45      */
    46     char[] value;
    47 
    48     /**
    49      * The count is the number of characters used.
    50      */
    51     int count;
    52 
    53     /**
    54      * This no-arg constructor is necessary for serialization of subclasses.
    55      */
    56     AbstractStringBuilder() {
    57     }
    58 
    59     /**
    60      * Creates an AbstractStringBuilder of the specified capacity.
    61      */
    62     AbstractStringBuilder(int capacity) {
    63         value = new char[capacity];
    64     }
    65 
    66     /**
    67      * Returns the length (character count).
    68      *
    69      * @return  the length of the sequence of characters currently
    70      *          represented by this object
    71      */
    72     public int length() {
    73         return count;
    74     }
    75 
    76     /**
    77      * Returns the current capacity. The capacity is the amount of storage
    78      * available for newly inserted characters, beyond which an allocation
    79      * will occur.
    80      *
    81      * @return  the current capacity
    82      */
    83     public int capacity() {
    84         return value.length;
    85     }
    86 
    87     /**
    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
    91      * larger of:
    92      * <ul>
    93      * <li>The <code>minimumCapacity</code> argument.
    94      * <li>Twice the old capacity, plus <code>2</code>.
    95      * </ul>
    96      * If the <code>minimumCapacity</code> argument is nonpositive, this
    97      * method takes no action and simply returns.
    98      *
    99      * @param   minimumCapacity   the minimum desired capacity.
   100      */
   101     public void ensureCapacity(int minimumCapacity) {
   102         if (minimumCapacity > 0)
   103             ensureCapacityInternal(minimumCapacity);
   104     }
   105 
   106     /**
   107      * This method has the same contract as ensureCapacity, but is
   108      * never synchronized.
   109      */
   110     private void ensureCapacityInternal(int minimumCapacity) {
   111         // overflow-conscious code
   112         if (minimumCapacity - value.length > 0)
   113             expandCapacity(minimumCapacity);
   114     }
   115 
   116     /**
   117      * This implements the expansion semantics of ensureCapacity with no
   118      * size check or synchronization.
   119      */
   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;
   128         }
   129         value = copyOf(value, newCapacity);
   130     }
   131 
   132     /**
   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.
   138      */
   139     public void trimToSize() {
   140         if (count < value.length) {
   141             value = copyOf(value, count);
   142         }
   143     }
   144 
   145     /**
   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>'&#92;u0000'</code>.
   154      *
   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.
   157      * <p>
   158      * If the <code>newLength</code> argument is greater than or equal
   159      * to the current length, sufficient null characters
   160      * (<code>'&#92;u0000'</code>) are appended so that
   161      * length becomes the <code>newLength</code> argument.
   162      * <p>
   163      * The <code>newLength</code> argument must be greater than or equal
   164      * to <code>0</code>.
   165      *
   166      * @param      newLength   the new length
   167      * @throws     IndexOutOfBoundsException  if the
   168      *               <code>newLength</code> argument is negative.
   169      */
   170     public void setLength(int newLength) {
   171         if (newLength < 0)
   172             throw new StringIndexOutOfBoundsException(newLength);
   173         ensureCapacityInternal(newLength);
   174 
   175         if (count < newLength) {
   176             for (; count < newLength; count++)
   177                 value[count] = '\0';
   178         } else {
   179             count = newLength;
   180         }
   181     }
   182 
   183     /**
   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.
   187      * <p>
   188      * The index argument must be greater than or equal to
   189      * <code>0</code>, and less than the length of this sequence.
   190      *
   191      * <p>If the <code>char</code> value specified by the index is a
   192      * <a href="Character.html#unicode">surrogate</a>, the surrogate
   193      * value is returned.
   194      *
   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>.
   199      */
   200     public char charAt(int index) {
   201         if ((index < 0) || (index >= count))
   202             throw new StringIndexOutOfBoundsException(index);
   203         return value[index];
   204     }
   205 
   206     /**
   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>.
   211      *
   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.
   219      *
   220      * @param      index the index to the <code>char</code> values
   221      * @return     the code point value of the character at the
   222      *             <code>index</code>
   223      * @exception  IndexOutOfBoundsException  if the <code>index</code>
   224      *             argument is negative or not less than the length of this
   225      *             sequence.
   226      */
   227     public int codePointAt(int index) {
   228         if ((index < 0) || (index >= count)) {
   229             throw new StringIndexOutOfBoundsException(index);
   230         }
   231         return Character.codePointAt(value, index);
   232     }
   233 
   234     /**
   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
   238      * #length()}.
   239      *
   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.
   248      *
   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
   253      *            of this sequence.
   254      */
   255     public int codePointBefore(int index) {
   256         int i = index - 1;
   257         if ((i < 0) || (i >= count)) {
   258             throw new StringIndexOutOfBoundsException(index);
   259         }
   260         return Character.codePointBefore(value, index);
   261     }
   262 
   263     /**
   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.
   271      *
   272      * @param beginIndex the index to the first <code>char</code> of
   273      * the text range.
   274      * @param endIndex the index after the last <code>char</code> of
   275      * the text range.
   276      * @return the number of Unicode code points in the specified text
   277      * range
   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>.
   282      */
   283     public int codePointCount(int beginIndex, int endIndex) {
   284         if (beginIndex < 0 || endIndex > count || beginIndex > endIndex) {
   285             throw new IndexOutOfBoundsException();
   286         }
   287         return Character.codePointCountImpl(value, beginIndex, endIndex-beginIndex);
   288     }
   289 
   290     /**
   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.
   296      *
   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.
   308      */
   309     public int offsetByCodePoints(int index, int codePointOffset) {
   310         if (index < 0 || index > count) {
   311             throw new IndexOutOfBoundsException();
   312         }
   313         return Character.offsetByCodePointsImpl(value, 0, count,
   314                                                 index, codePointOffset);
   315     }
   316 
   317     /**
   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>
   328      *
   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
   334      *             <code>null</code>.
   335      * @throws     IndexOutOfBoundsException  if any of the following is true:
   336      *             <ul>
   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>
   345      *             </ul>
   346      */
   347     public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
   348     {
   349         if (srcBegin < 0)
   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);
   356     }
   357 
   358     /**
   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>.
   363      * <p>
   364      * The index argument must be greater than or equal to
   365      * <code>0</code>, and less than the length of this sequence.
   366      *
   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>.
   371      */
   372     public void setCharAt(int index, char ch) {
   373         if ((index < 0) || (index >= count))
   374             throw new StringIndexOutOfBoundsException(index);
   375         value[index] = ch;
   376     }
   377 
   378     /**
   379      * Appends the string representation of the {@code Object} argument.
   380      * <p>
   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.
   385      *
   386      * @param   obj   an {@code Object}.
   387      * @return  a reference to this object.
   388      */
   389     public AbstractStringBuilder append(Object obj) {
   390         return append(String.valueOf(obj));
   391     }
   392 
   393     /**
   394      * Appends the specified string to this character sequence.
   395      * <p>
   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.
   400      * <p>
   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}.
   407      *
   408      * @param   str   a string.
   409      * @return  a reference to this object.
   410      */
   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);
   416         count += len;
   417         return this;
   418     }
   419 
   420     // Documentation in subclasses because of synchro difference
   421     public AbstractStringBuilder append(StringBuffer sb) {
   422         if (sb == null)
   423             return append("null");
   424         int len = sb.length();
   425         ensureCapacityInternal(count + len);
   426         sb.getChars(0, len, value, count);
   427         count += len;
   428         return this;
   429     }
   430 
   431     // Documentation in subclasses because of synchro difference
   432     public AbstractStringBuilder append(CharSequence s) {
   433         if (s == null)
   434             s = "null";
   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());
   440     }
   441 
   442     /**
   443      * Appends a subsequence of the specified {@code CharSequence} to this
   444      * sequence.
   445      * <p>
   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}.
   450      * <p>
   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}.
   457      * <p>
   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"}.
   461      *
   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()}
   470      */
   471     public AbstractStringBuilder append(CharSequence s, int start, int end) {
   472         if (s == null)
   473             s = "null";
   474         if ((start < 0) || (start > end) || (end > s.length()))
   475             throw new IndexOutOfBoundsException(
   476                 "start " + start + ", end " + end + ", s.length() "
   477                 + 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);
   482         count += len;
   483         return this;
   484     }
   485 
   486     /**
   487      * Appends the string representation of the {@code char} array
   488      * argument to this sequence.
   489      * <p>
   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.
   493      * <p>
   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.
   498      *
   499      * @param   str   the characters to be appended.
   500      * @return  a reference to this object.
   501      */
   502     public AbstractStringBuilder append(char[] str) {
   503         int len = str.length;
   504         ensureCapacityInternal(count + len);
   505         arraycopy(str, 0, value, count, len);
   506         count += len;
   507         return this;
   508     }
   509 
   510     /**
   511      * Appends the string representation of a subarray of the
   512      * {@code char} array argument to this sequence.
   513      * <p>
   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}.
   518      * <p>
   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.
   523      *
   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}
   531      */
   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);
   536         count += len;
   537         return this;
   538     }
   539 
   540     /**
   541      * Appends the string representation of the {@code boolean}
   542      * argument to the sequence.
   543      * <p>
   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.
   548      *
   549      * @param   b   a {@code boolean}.
   550      * @return  a reference to this object.
   551      */
   552     public AbstractStringBuilder append(boolean b) {
   553         if (b) {
   554             ensureCapacityInternal(count + 4);
   555             value[count++] = 't';
   556             value[count++] = 'r';
   557             value[count++] = 'u';
   558             value[count++] = 'e';
   559         } else {
   560             ensureCapacityInternal(count + 5);
   561             value[count++] = 'f';
   562             value[count++] = 'a';
   563             value[count++] = 'l';
   564             value[count++] = 's';
   565             value[count++] = 'e';
   566         }
   567         return this;
   568     }
   569 
   570     /**
   571      * Appends the string representation of the {@code char}
   572      * argument to this sequence.
   573      * <p>
   574      * The argument is appended to the contents of this sequence.
   575      * The length of this sequence increases by {@code 1}.
   576      * <p>
   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.
   581      *
   582      * @param   c   a {@code char}.
   583      * @return  a reference to this object.
   584      */
   585     public AbstractStringBuilder append(char c) {
   586         ensureCapacityInternal(count + 1);
   587         value[count++] = c;
   588         return this;
   589     }
   590 
   591     /**
   592      * Appends the string representation of the {@code int}
   593      * argument to this sequence.
   594      * <p>
   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.
   599      *
   600      * @param   i   an {@code int}.
   601      * @return  a reference to this object.
   602      */
   603     @JavaScriptBody(
   604         args={ "self", "i" },
   605         body="return java_lang_AbstractStringBuilder_appendLjava_lang_AbstractStringBuilderLjava_lang_String(self,i.toString());"
   606     )
   607     public AbstractStringBuilder append(int i) {
   608         if (i == Integer.MIN_VALUE) {
   609             append("-2147483648");
   610             return this;
   611         }
   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);
   617         count = spaceNeeded;
   618         return this;
   619     }
   620 
   621     /**
   622      * Appends the string representation of the {@code long}
   623      * argument to this sequence.
   624      * <p>
   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.
   629      *
   630      * @param   l   a {@code long}.
   631      * @return  a reference to this object.
   632      */
   633     public AbstractStringBuilder append(long l) {
   634         if (l == Long.MIN_VALUE) {
   635             append("-9223372036854775808");
   636             return this;
   637         }
   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);
   643         count = spaceNeeded;
   644         return this;
   645     }
   646 
   647     /**
   648      * Appends the string representation of the {@code float}
   649      * argument to this sequence.
   650      * <p>
   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.
   655      *
   656      * @param   f   a {@code float}.
   657      * @return  a reference to this object.
   658      */
   659     public AbstractStringBuilder append(float f) {
   660         return append(Float.toString(f));
   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         return append(Double.toString(d));
   677     }
   678 
   679     /**
   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.
   685      *
   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}.
   692      */
   693     public AbstractStringBuilder delete(int start, int end) {
   694         if (start < 0)
   695             throw new StringIndexOutOfBoundsException(start);
   696         if (end > count)
   697             end = count;
   698         if (start > end)
   699             throw new StringIndexOutOfBoundsException();
   700         int len = end - start;
   701         if (len > 0) {
   702             arraycopy(value, start+len, value, start, count-end);
   703             count -= len;
   704         }
   705         return this;
   706     }
   707 
   708     /**
   709      * Appends the string representation of the {@code codePoint}
   710      * argument to this sequence.
   711      *
   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)}.
   715      *
   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
   720      * sequence.
   721      *
   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
   726      */
   727     public AbstractStringBuilder appendCodePoint(int codePoint) {
   728         final int count = this.count;
   729 
   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;
   738         } else {
   739             throw new IllegalArgumentException();
   740         }
   741         return this;
   742     }
   743 
   744     /**
   745      * Removes the <code>char</code> at the specified position in this
   746      * sequence. This sequence is shortened by one <code>char</code>.
   747      *
   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.
   754      *
   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>.
   760      */
   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);
   765         count--;
   766         return this;
   767     }
   768 
   769     /**
   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.)
   779      *
   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>.
   787      */
   788     public AbstractStringBuilder replace(int start, int end, String str) {
   789         if (start < 0)
   790             throw new StringIndexOutOfBoundsException(start);
   791         if (start > count)
   792             throw new StringIndexOutOfBoundsException("start > length()");
   793         if (start > end)
   794             throw new StringIndexOutOfBoundsException("start > end");
   795 
   796         if (end > count)
   797             end = count;
   798         int len = str.length();
   799         int newCount = count + len - (end - start);
   800         ensureCapacityInternal(newCount);
   801 
   802         arraycopy(value, end, value, start + len, count - end);
   803         str.getChars(value, start);
   804         count = newCount;
   805         return this;
   806     }
   807 
   808     /**
   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
   812      * this sequence.
   813      *
   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.
   818      */
   819     public String substring(int start) {
   820         return substring(start, count);
   821     }
   822 
   823     /**
   824      * Returns a new character sequence that is a subsequence of this sequence.
   825      *
   826      * <p> An invocation of this method of the form
   827      *
   828      * <blockquote><pre>
   829      * sb.subSequence(begin,&nbsp;end)</pre></blockquote>
   830      *
   831      * behaves in exactly the same way as the invocation
   832      *
   833      * <blockquote><pre>
   834      * sb.substring(begin,&nbsp;end)</pre></blockquote>
   835      *
   836      * This method is provided so that this class can
   837      * implement the {@link CharSequence} interface. </p>
   838      *
   839      * @param      start   the start index, inclusive.
   840      * @param      end     the end index, exclusive.
   841      * @return     the specified subsequence.
   842      *
   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>
   847      * @spec JSR-51
   848      */
   849     public CharSequence subSequence(int start, int end) {
   850         return substring(start, end);
   851     }
   852 
   853     /**
   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>.
   858      *
   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>.
   866      */
   867     public String substring(int start, int end) {
   868         if (start < 0)
   869             throw new StringIndexOutOfBoundsException(start);
   870         if (end > count)
   871             throw new StringIndexOutOfBoundsException(end);
   872         if (start > end)
   873             throw new StringIndexOutOfBoundsException(end - start);
   874         return new String(value, start, end - start);
   875     }
   876 
   877     /**
   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.
   884      *
   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
   888      *             be inserted.
   889      * @param      len      the number of {@code char}s in the subarray to
   890      *             be inserted.
   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}.
   897      */
   898     public AbstractStringBuilder insert(int index, char[] str, int offset,
   899                                         int len)
   900     {
   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 "
   906                 + str.length);
   907         ensureCapacityInternal(count + len);
   908         arraycopy(value, index, value, index + len, count - index);
   909         arraycopy(str, offset, value, index, len);
   910         count += len;
   911         return this;
   912     }
   913 
   914     /**
   915      * Inserts the string representation of the {@code Object}
   916      * argument into this character sequence.
   917      * <p>
   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.
   923      * <p>
   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}
   926      * of this sequence.
   927      *
   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.
   932      */
   933     public AbstractStringBuilder insert(int offset, Object obj) {
   934         return insert(offset, String.valueOf(obj));
   935     }
   936 
   937     /**
   938      * Inserts the string into this character sequence.
   939      * <p>
   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.
   946      * <p>
   947      * The character at index <i>k</i> in the new character sequence is
   948      * equal to:
   949      * <ul>
   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()}
   958      * </ul><p>
   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}
   961      * of this sequence.
   962      *
   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.
   967      */
   968     public AbstractStringBuilder insert(int offset, String str) {
   969         if ((offset < 0) || (offset > length()))
   970             throw new StringIndexOutOfBoundsException(offset);
   971         if (str == null)
   972             str = "null";
   973         int len = str.length();
   974         ensureCapacityInternal(count + len);
   975         arraycopy(value, offset, value, offset + len, count - offset);
   976         str.getChars(value, offset);
   977         count += len;
   978         return this;
   979     }
   980 
   981     /**
   982      * Inserts the string representation of the {@code char} array
   983      * argument into this sequence.
   984      * <p>
   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.
   989      * <p>
   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.
   995      * <p>
   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}
   998      * of this sequence.
   999      *
  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.
  1004      */
  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);
  1012         count += len;
  1013         return this;
  1014     }
  1015 
  1016     /**
  1017      * Inserts the specified {@code CharSequence} into this sequence.
  1018      * <p>
  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.
  1023      * <p>
  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())
  1027      * method.
  1028      *
  1029      * <p>If {@code s} is {@code null}, then the four characters
  1030      * {@code "null"} are inserted into this sequence.
  1031      *
  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.
  1036      */
  1037     public AbstractStringBuilder insert(int dstOffset, CharSequence s) {
  1038         if (s == null)
  1039             s = "null";
  1040         if (s instanceof String)
  1041             return this.insert(dstOffset, (String)s);
  1042         return this.insert(dstOffset, s, 0, s.length());
  1043     }
  1044 
  1045     /**
  1046      * Inserts a subsequence of the specified {@code CharSequence} into
  1047      * this sequence.
  1048      * <p>
  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}.
  1054      * <p>
  1055      * The character at index <i>k</i> in this sequence becomes equal to:
  1056      * <ul>
  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}
  1065      * </ul><p>
  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}
  1068      * of this sequence.
  1069      * <p>The start argument must be nonnegative, and not greater than
  1070      * {@code end}.
  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.
  1073      *
  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"}.
  1077      *
  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()}
  1088      */
  1089      public AbstractStringBuilder insert(int dstOffset, CharSequence s,
  1090                                          int start, int end) {
  1091         if (s == null)
  1092             s = "null";
  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() "
  1098                 + s.length());
  1099         int len = end - start;
  1100         ensureCapacityInternal(count + len);
  1101         arraycopy(value, dstOffset, value, dstOffset + len,
  1102                          count - dstOffset);
  1103         for (int i=start; i<end; i++)
  1104             value[dstOffset++] = s.charAt(i);
  1105         count += len;
  1106         return this;
  1107     }
  1108 
  1109     /**
  1110      * Inserts the string representation of the {@code boolean}
  1111      * argument into this sequence.
  1112      * <p>
  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.
  1118      * <p>
  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}
  1121      * of this sequence.
  1122      *
  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.
  1127      */
  1128     public AbstractStringBuilder insert(int offset, boolean b) {
  1129         return insert(offset, String.valueOf(b));
  1130     }
  1131 
  1132     /**
  1133      * Inserts the string representation of the {@code char}
  1134      * argument into this sequence.
  1135      * <p>
  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.
  1141      * <p>
  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}
  1144      * of this sequence.
  1145      *
  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.
  1150      */
  1151     public AbstractStringBuilder insert(int offset, char c) {
  1152         ensureCapacityInternal(count + 1);
  1153         arraycopy(value, offset, value, offset + 1, count - offset);
  1154         value[offset] = c;
  1155         count += 1;
  1156         return this;
  1157     }
  1158 
  1159     /**
  1160      * Inserts the string representation of the second {@code int}
  1161      * argument into this sequence.
  1162      * <p>
  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.
  1168      * <p>
  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}
  1171      * of this sequence.
  1172      *
  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.
  1177      */
  1178     public AbstractStringBuilder insert(int offset, int i) {
  1179         return insert(offset, String.valueOf(i));
  1180     }
  1181 
  1182     /**
  1183      * Inserts the string representation of the {@code long}
  1184      * argument into this sequence.
  1185      * <p>
  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.
  1191      * <p>
  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}
  1194      * of this sequence.
  1195      *
  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.
  1200      */
  1201     public AbstractStringBuilder insert(int offset, long l) {
  1202         return insert(offset, String.valueOf(l));
  1203     }
  1204 
  1205     /**
  1206      * Inserts the string representation of the {@code float}
  1207      * argument into this sequence.
  1208      * <p>
  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.
  1214      * <p>
  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}
  1217      * of this sequence.
  1218      *
  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.
  1223      */
  1224     public AbstractStringBuilder insert(int offset, float f) {
  1225         return insert(offset, String.valueOf(f));
  1226     }
  1227 
  1228     /**
  1229      * Inserts the string representation of the {@code double}
  1230      * argument into this sequence.
  1231      * <p>
  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.
  1237      * <p>
  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}
  1240      * of this sequence.
  1241      *
  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.
  1246      */
  1247     public AbstractStringBuilder insert(int offset, double d) {
  1248         return insert(offset, String.valueOf(d));
  1249     }
  1250 
  1251     /**
  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:
  1255      * <blockquote><pre>
  1256      * this.toString().startsWith(str, <i>k</i>)
  1257      * </pre></blockquote>
  1258      * is <code>true</code>.
  1259      *
  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>.
  1267      */
  1268     public int indexOf(String str) {
  1269         return indexOf(str, 0);
  1270     }
  1271 
  1272     /**
  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:
  1276      * <blockquote><pre>
  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.
  1281      *
  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>.
  1288      */
  1289     public int indexOf(String str, int fromIndex) {
  1290         return String.indexOf(value, 0, count,
  1291                               str.toCharArray(), 0, str.length(), fromIndex);
  1292     }
  1293 
  1294     /**
  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
  1299      * <blockquote><pre>
  1300      * this.toString().startsWith(str, k)
  1301      * </pre></blockquote>
  1302      * is true.
  1303      *
  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>.
  1311      */
  1312     public int lastIndexOf(String str) {
  1313         return lastIndexOf(str, count);
  1314     }
  1315 
  1316     /**
  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>
  1319      * such that:
  1320      * <blockquote><pre>
  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.
  1325      *
  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>.
  1332      */
  1333     public int lastIndexOf(String str, int fromIndex) {
  1334         return String.lastIndexOf(value, 0, count,
  1335                               str.toCharArray(), 0, str.length(), fromIndex);
  1336     }
  1337 
  1338     /**
  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.
  1344      *
  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.
  1351      *
  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      * "&#92;uDC00&#92;uD800" produces "&#92;uD800&#92;uDC00" which is
  1356      * a valid surrogate pair.
  1357      *
  1358      * @return  a reference to this object.
  1359      */
  1360     public AbstractStringBuilder reverse() {
  1361         boolean hasSurrogate = false;
  1362         int n = count - 1;
  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);
  1369             }
  1370             value[j] = temp2;
  1371             value[n - j] = temp;
  1372         }
  1373         if (hasSurrogate) {
  1374             // Reverse back all valid surrogate pairs
  1375             for (int i = 0; i < count - 1; i++) {
  1376                 char c2 = value[i];
  1377                 if (Character.isLowSurrogate(c2)) {
  1378                     char c1 = value[i + 1];
  1379                     if (Character.isHighSurrogate(c1)) {
  1380                         value[i++] = c1;
  1381                         value[i] = c2;
  1382                     }
  1383                 }
  1384             }
  1385         }
  1386         return this;
  1387     }
  1388 
  1389     /**
  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>.
  1396      *
  1397      * @return  a string representation of this sequence of characters.
  1398      */
  1399     public abstract String toString();
  1400 
  1401     /**
  1402      * Needed by <tt>String</tt> for the contentEquals method.
  1403      */
  1404     final char[] getValue() {
  1405         return value;
  1406     }
  1407 
  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);
  1412         }
  1413         char[] copy = new char[newLength];
  1414         arraycopy(original, from, copy, 0, Math.min(original.length - from, newLength));
  1415         return copy;
  1416     }
  1417 
  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];
  1422             }
  1423         } else {
  1424             while (count-- > 0) {
  1425                 dst[dstBegin++] = value[srcBegin++];
  1426             }
  1427         }
  1428     }
  1429 
  1430     // access system property
  1431     static String getProperty(String nm) {
  1432         return null;
  1433     }
  1434 
  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));
  1438         return copy;
  1439     }
  1440     
  1441 }