Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/lang/AbstractStringBuilder.java
38829 views
/*1* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.lang;2627import sun.misc.FloatingDecimal;28import java.util.Arrays;2930/**31* A mutable sequence of characters.32* <p>33* Implements a modifiable string. At any point in time it contains some34* particular sequence of characters, but the length and content of the35* sequence can be changed through certain method calls.36*37* <p>Unless otherwise noted, passing a {@code null} argument to a constructor38* or method in this class will cause a {@link NullPointerException} to be39* thrown.40*41* @author Michael McCloskey42* @author Martin Buchholz43* @author Ulf Zibis44* @since 1.545*/46abstract class AbstractStringBuilder implements Appendable, CharSequence {47/**48* The value is used for character storage.49*/50char[] value;5152/**53* The count is the number of characters used.54*/55int count;5657/**58* This no-arg constructor is necessary for serialization of subclasses.59*/60AbstractStringBuilder() {61}6263/**64* Creates an AbstractStringBuilder of the specified capacity.65*/66AbstractStringBuilder(int capacity) {67value = new char[capacity];68}6970/**71* Returns the length (character count).72*73* @return the length of the sequence of characters currently74* represented by this object75*/76@Override77public int length() {78return count;79}8081/**82* Returns the current capacity. The capacity is the amount of storage83* available for newly inserted characters, beyond which an allocation84* will occur.85*86* @return the current capacity87*/88public int capacity() {89return value.length;90}9192/**93* Ensures that the capacity is at least equal to the specified minimum.94* If the current capacity is less than the argument, then a new internal95* array is allocated with greater capacity. The new capacity is the96* larger of:97* <ul>98* <li>The {@code minimumCapacity} argument.99* <li>Twice the old capacity, plus {@code 2}.100* </ul>101* If the {@code minimumCapacity} argument is nonpositive, this102* method takes no action and simply returns.103* Note that subsequent operations on this object can reduce the104* actual capacity below that requested here.105*106* @param minimumCapacity the minimum desired capacity.107*/108public void ensureCapacity(int minimumCapacity) {109if (minimumCapacity > 0)110ensureCapacityInternal(minimumCapacity);111}112113/**114* For positive values of {@code minimumCapacity}, this method115* behaves like {@code ensureCapacity}, however it is never116* synchronized.117* If {@code minimumCapacity} is non positive due to numeric118* overflow, this method throws {@code OutOfMemoryError}.119*/120private void ensureCapacityInternal(int minimumCapacity) {121// overflow-conscious code122if (minimumCapacity - value.length > 0) {123value = Arrays.copyOf(value,124newCapacity(minimumCapacity));125}126}127128/**129* The maximum size of array to allocate (unless necessary).130* Some VMs reserve some header words in an array.131* Attempts to allocate larger arrays may result in132* OutOfMemoryError: Requested array size exceeds VM limit133*/134private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;135136/**137* Returns a capacity at least as large as the given minimum capacity.138* Returns the current capacity increased by the same amount + 2 if139* that suffices.140* Will not return a capacity greater than {@code MAX_ARRAY_SIZE}141* unless the given minimum capacity is greater than that.142*143* @param minCapacity the desired minimum capacity144* @throws OutOfMemoryError if minCapacity is less than zero or145* greater than Integer.MAX_VALUE146*/147private int newCapacity(int minCapacity) {148// overflow-conscious code149int newCapacity = (value.length << 1) + 2;150if (newCapacity - minCapacity < 0) {151newCapacity = minCapacity;152}153return (newCapacity <= 0 || MAX_ARRAY_SIZE - newCapacity < 0)154? hugeCapacity(minCapacity)155: newCapacity;156}157158private int hugeCapacity(int minCapacity) {159if (Integer.MAX_VALUE - minCapacity < 0) { // overflow160throw new OutOfMemoryError();161}162return (minCapacity > MAX_ARRAY_SIZE)163? minCapacity : MAX_ARRAY_SIZE;164}165166/**167* Attempts to reduce storage used for the character sequence.168* If the buffer is larger than necessary to hold its current sequence of169* characters, then it may be resized to become more space efficient.170* Calling this method may, but is not required to, affect the value171* returned by a subsequent call to the {@link #capacity()} method.172*/173public void trimToSize() {174if (count < value.length) {175value = Arrays.copyOf(value, count);176}177}178179/**180* Sets the length of the character sequence.181* The sequence is changed to a new character sequence182* whose length is specified by the argument. For every nonnegative183* index <i>k</i> less than {@code newLength}, the character at184* index <i>k</i> in the new character sequence is the same as the185* character at index <i>k</i> in the old sequence if <i>k</i> is less186* than the length of the old character sequence; otherwise, it is the187* null character {@code '\u005Cu0000'}.188*189* In other words, if the {@code newLength} argument is less than190* the current length, the length is changed to the specified length.191* <p>192* If the {@code newLength} argument is greater than or equal193* to the current length, sufficient null characters194* ({@code '\u005Cu0000'}) are appended so that195* length becomes the {@code newLength} argument.196* <p>197* The {@code newLength} argument must be greater than or equal198* to {@code 0}.199*200* @param newLength the new length201* @throws IndexOutOfBoundsException if the202* {@code newLength} argument is negative.203*/204public void setLength(int newLength) {205if (newLength < 0)206throw new StringIndexOutOfBoundsException(newLength);207ensureCapacityInternal(newLength);208209if (count < newLength) {210Arrays.fill(value, count, newLength, '\0');211}212213count = newLength;214}215216/**217* Returns the {@code char} value in this sequence at the specified index.218* The first {@code char} value is at index {@code 0}, the next at index219* {@code 1}, and so on, as in array indexing.220* <p>221* The index argument must be greater than or equal to222* {@code 0}, and less than the length of this sequence.223*224* <p>If the {@code char} value specified by the index is a225* <a href="Character.html#unicode">surrogate</a>, the surrogate226* value is returned.227*228* @param index the index of the desired {@code char} value.229* @return the {@code char} value at the specified index.230* @throws IndexOutOfBoundsException if {@code index} is231* negative or greater than or equal to {@code length()}.232*/233@Override234public char charAt(int index) {235if ((index < 0) || (index >= count))236throw new StringIndexOutOfBoundsException(index);237return value[index];238}239240/**241* Returns the character (Unicode code point) at the specified242* index. The index refers to {@code char} values243* (Unicode code units) and ranges from {@code 0} to244* {@link #length()}{@code - 1}.245*246* <p> If the {@code char} value specified at the given index247* is in the high-surrogate range, the following index is less248* than the length of this sequence, and the249* {@code char} value at the following index is in the250* low-surrogate range, then the supplementary code point251* corresponding to this surrogate pair is returned. Otherwise,252* the {@code char} value at the given index is returned.253*254* @param index the index to the {@code char} values255* @return the code point value of the character at the256* {@code index}257* @exception IndexOutOfBoundsException if the {@code index}258* argument is negative or not less than the length of this259* sequence.260*/261public int codePointAt(int index) {262if ((index < 0) || (index >= count)) {263throw new StringIndexOutOfBoundsException(index);264}265return Character.codePointAtImpl(value, index, count);266}267268/**269* Returns the character (Unicode code point) before the specified270* index. The index refers to {@code char} values271* (Unicode code units) and ranges from {@code 1} to {@link272* #length()}.273*274* <p> If the {@code char} value at {@code (index - 1)}275* is in the low-surrogate range, {@code (index - 2)} is not276* negative, and the {@code char} value at {@code (index -277* 2)} is in the high-surrogate range, then the278* supplementary code point value of the surrogate pair is279* returned. If the {@code char} value at {@code index -280* 1} is an unpaired low-surrogate or a high-surrogate, the281* surrogate value is returned.282*283* @param index the index following the code point that should be returned284* @return the Unicode code point value before the given index.285* @exception IndexOutOfBoundsException if the {@code index}286* argument is less than 1 or greater than the length287* of this sequence.288*/289public int codePointBefore(int index) {290int i = index - 1;291if ((i < 0) || (i >= count)) {292throw new StringIndexOutOfBoundsException(index);293}294return Character.codePointBeforeImpl(value, index, 0);295}296297/**298* Returns the number of Unicode code points in the specified text299* range of this sequence. The text range begins at the specified300* {@code beginIndex} and extends to the {@code char} at301* index {@code endIndex - 1}. Thus the length (in302* {@code char}s) of the text range is303* {@code endIndex-beginIndex}. Unpaired surrogates within304* this sequence count as one code point each.305*306* @param beginIndex the index to the first {@code char} of307* the text range.308* @param endIndex the index after the last {@code char} of309* the text range.310* @return the number of Unicode code points in the specified text311* range312* @exception IndexOutOfBoundsException if the313* {@code beginIndex} is negative, or {@code endIndex}314* is larger than the length of this sequence, or315* {@code beginIndex} is larger than {@code endIndex}.316*/317public int codePointCount(int beginIndex, int endIndex) {318if (beginIndex < 0 || endIndex > count || beginIndex > endIndex) {319throw new IndexOutOfBoundsException();320}321return Character.codePointCountImpl(value, beginIndex, endIndex-beginIndex);322}323324/**325* Returns the index within this sequence that is offset from the326* given {@code index} by {@code codePointOffset} code327* points. Unpaired surrogates within the text range given by328* {@code index} and {@code codePointOffset} count as329* one code point each.330*331* @param index the index to be offset332* @param codePointOffset the offset in code points333* @return the index within this sequence334* @exception IndexOutOfBoundsException if {@code index}335* is negative or larger then the length of this sequence,336* or if {@code codePointOffset} is positive and the subsequence337* starting with {@code index} has fewer than338* {@code codePointOffset} code points,339* or if {@code codePointOffset} is negative and the subsequence340* before {@code index} has fewer than the absolute value of341* {@code codePointOffset} code points.342*/343public int offsetByCodePoints(int index, int codePointOffset) {344if (index < 0 || index > count) {345throw new IndexOutOfBoundsException();346}347return Character.offsetByCodePointsImpl(value, 0, count,348index, codePointOffset);349}350351/**352* Characters are copied from this sequence into the353* destination character array {@code dst}. The first character to354* be copied is at index {@code srcBegin}; the last character to355* be copied is at index {@code srcEnd-1}. The total number of356* characters to be copied is {@code srcEnd-srcBegin}. The357* characters are copied into the subarray of {@code dst} starting358* at index {@code dstBegin} and ending at index:359* <pre>{@code360* dstbegin + (srcEnd-srcBegin) - 1361* }</pre>362*363* @param srcBegin start copying at this offset.364* @param srcEnd stop copying at this offset.365* @param dst the array to copy the data into.366* @param dstBegin offset into {@code dst}.367* @throws IndexOutOfBoundsException if any of the following is true:368* <ul>369* <li>{@code srcBegin} is negative370* <li>{@code dstBegin} is negative371* <li>the {@code srcBegin} argument is greater than372* the {@code srcEnd} argument.373* <li>{@code srcEnd} is greater than374* {@code this.length()}.375* <li>{@code dstBegin+srcEnd-srcBegin} is greater than376* {@code dst.length}377* </ul>378*/379public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)380{381if (srcBegin < 0)382throw new StringIndexOutOfBoundsException(srcBegin);383if ((srcEnd < 0) || (srcEnd > count))384throw new StringIndexOutOfBoundsException(srcEnd);385if (srcBegin > srcEnd)386throw new StringIndexOutOfBoundsException("srcBegin > srcEnd");387System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);388}389390/**391* The character at the specified index is set to {@code ch}. This392* sequence is altered to represent a new character sequence that is393* identical to the old character sequence, except that it contains the394* character {@code ch} at position {@code index}.395* <p>396* The index argument must be greater than or equal to397* {@code 0}, and less than the length of this sequence.398*399* @param index the index of the character to modify.400* @param ch the new character.401* @throws IndexOutOfBoundsException if {@code index} is402* negative or greater than or equal to {@code length()}.403*/404public void setCharAt(int index, char ch) {405if ((index < 0) || (index >= count))406throw new StringIndexOutOfBoundsException(index);407value[index] = ch;408}409410/**411* Appends the string representation of the {@code Object} argument.412* <p>413* The overall effect is exactly as if the argument were converted414* to a string by the method {@link String#valueOf(Object)},415* and the characters of that string were then416* {@link #append(String) appended} to this character sequence.417*418* @param obj an {@code Object}.419* @return a reference to this object.420*/421public AbstractStringBuilder append(Object obj) {422return append(String.valueOf(obj));423}424425/**426* Appends the specified string to this character sequence.427* <p>428* The characters of the {@code String} argument are appended, in429* order, increasing the length of this sequence by the length of the430* argument. If {@code str} is {@code null}, then the four431* characters {@code "null"} are appended.432* <p>433* Let <i>n</i> be the length of this character sequence just prior to434* execution of the {@code append} method. Then the character at435* index <i>k</i> in the new character sequence is equal to the character436* at index <i>k</i> in the old character sequence, if <i>k</i> is less437* than <i>n</i>; otherwise, it is equal to the character at index438* <i>k-n</i> in the argument {@code str}.439*440* @param str a string.441* @return a reference to this object.442*/443public AbstractStringBuilder append(String str) {444if (str == null)445return appendNull();446int len = str.length();447ensureCapacityInternal(count + len);448str.getChars(0, len, value, count);449count += len;450return this;451}452453// Documentation in subclasses because of synchro difference454public AbstractStringBuilder append(StringBuffer sb) {455if (sb == null)456return appendNull();457int len = sb.length();458ensureCapacityInternal(count + len);459sb.getChars(0, len, value, count);460count += len;461return this;462}463464/**465* @since 1.8466*/467AbstractStringBuilder append(AbstractStringBuilder asb) {468if (asb == null)469return appendNull();470int len = asb.length();471ensureCapacityInternal(count + len);472asb.getChars(0, len, value, count);473count += len;474return this;475}476477// Documentation in subclasses because of synchro difference478@Override479public AbstractStringBuilder append(CharSequence s) {480if (s == null)481return appendNull();482if (s instanceof String)483return this.append((String)s);484if (s instanceof AbstractStringBuilder)485return this.append((AbstractStringBuilder)s);486487return this.append(s, 0, s.length());488}489490private AbstractStringBuilder appendNull() {491int c = count;492ensureCapacityInternal(c + 4);493final char[] value = this.value;494value[c++] = 'n';495value[c++] = 'u';496value[c++] = 'l';497value[c++] = 'l';498count = c;499return this;500}501502/**503* Appends a subsequence of the specified {@code CharSequence} to this504* sequence.505* <p>506* Characters of the argument {@code s}, starting at507* index {@code start}, are appended, in order, to the contents of508* this sequence up to the (exclusive) index {@code end}. The length509* of this sequence is increased by the value of {@code end - start}.510* <p>511* Let <i>n</i> be the length of this character sequence just prior to512* execution of the {@code append} method. Then the character at513* index <i>k</i> in this character sequence becomes equal to the514* character at index <i>k</i> in this sequence, if <i>k</i> is less than515* <i>n</i>; otherwise, it is equal to the character at index516* <i>k+start-n</i> in the argument {@code s}.517* <p>518* If {@code s} is {@code null}, then this method appends519* characters as if the s parameter was a sequence containing the four520* characters {@code "null"}.521*522* @param s the sequence to append.523* @param start the starting index of the subsequence to be appended.524* @param end the end index of the subsequence to be appended.525* @return a reference to this object.526* @throws IndexOutOfBoundsException if527* {@code start} is negative, or528* {@code start} is greater than {@code end} or529* {@code end} is greater than {@code s.length()}530*/531@Override532public AbstractStringBuilder append(CharSequence s, int start, int end) {533if (s == null)534s = "null";535if ((start < 0) || (start > end) || (end > s.length()))536throw new IndexOutOfBoundsException(537"start " + start + ", end " + end + ", s.length() "538+ s.length());539int len = end - start;540ensureCapacityInternal(count + len);541for (int i = start, j = count; i < end; i++, j++)542value[j] = s.charAt(i);543count += len;544return this;545}546547/**548* Appends the string representation of the {@code char} array549* argument to this sequence.550* <p>551* The characters of the array argument are appended, in order, to552* the contents of this sequence. The length of this sequence553* increases by the length of the argument.554* <p>555* The overall effect is exactly as if the argument were converted556* to a string by the method {@link String#valueOf(char[])},557* and the characters of that string were then558* {@link #append(String) appended} to this character sequence.559*560* @param str the characters to be appended.561* @return a reference to this object.562*/563public AbstractStringBuilder append(char[] str) {564int len = str.length;565ensureCapacityInternal(count + len);566System.arraycopy(str, 0, value, count, len);567count += len;568return this;569}570571/**572* Appends the string representation of a subarray of the573* {@code char} array argument to this sequence.574* <p>575* Characters of the {@code char} array {@code str}, starting at576* index {@code offset}, are appended, in order, to the contents577* of this sequence. The length of this sequence increases578* by the value of {@code len}.579* <p>580* The overall effect is exactly as if the arguments were converted581* to a string by the method {@link String#valueOf(char[],int,int)},582* and the characters of that string were then583* {@link #append(String) appended} to this character sequence.584*585* @param str the characters to be appended.586* @param offset the index of the first {@code char} to append.587* @param len the number of {@code char}s to append.588* @return a reference to this object.589* @throws IndexOutOfBoundsException590* if {@code offset < 0} or {@code len < 0}591* or {@code offset+len > str.length}592*/593public AbstractStringBuilder append(char str[], int offset, int len) {594if (len > 0) // let arraycopy report AIOOBE for len < 0595ensureCapacityInternal(count + len);596System.arraycopy(str, offset, value, count, len);597count += len;598return this;599}600601/**602* Appends the string representation of the {@code boolean}603* argument to the sequence.604* <p>605* The overall effect is exactly as if the argument were converted606* to a string by the method {@link String#valueOf(boolean)},607* and the characters of that string were then608* {@link #append(String) appended} to this character sequence.609*610* @param b a {@code boolean}.611* @return a reference to this object.612*/613public AbstractStringBuilder append(boolean b) {614if (b) {615ensureCapacityInternal(count + 4);616value[count++] = 't';617value[count++] = 'r';618value[count++] = 'u';619value[count++] = 'e';620} else {621ensureCapacityInternal(count + 5);622value[count++] = 'f';623value[count++] = 'a';624value[count++] = 'l';625value[count++] = 's';626value[count++] = 'e';627}628return this;629}630631/**632* Appends the string representation of the {@code char}633* argument to this sequence.634* <p>635* The argument is appended to the contents of this sequence.636* The length of this sequence increases by {@code 1}.637* <p>638* The overall effect is exactly as if the argument were converted639* to a string by the method {@link String#valueOf(char)},640* and the character in that string were then641* {@link #append(String) appended} to this character sequence.642*643* @param c a {@code char}.644* @return a reference to this object.645*/646@Override647public AbstractStringBuilder append(char c) {648ensureCapacityInternal(count + 1);649value[count++] = c;650return this;651}652653/**654* Appends the string representation of the {@code int}655* argument to this sequence.656* <p>657* The overall effect is exactly as if the argument were converted658* to a string by the method {@link String#valueOf(int)},659* and the characters of that string were then660* {@link #append(String) appended} to this character sequence.661*662* @param i an {@code int}.663* @return a reference to this object.664*/665public AbstractStringBuilder append(int i) {666if (i == Integer.MIN_VALUE) {667append("-2147483648");668return this;669}670int appendedLength = (i < 0) ? Integer.stringSize(-i) + 1671: Integer.stringSize(i);672int spaceNeeded = count + appendedLength;673ensureCapacityInternal(spaceNeeded);674Integer.getChars(i, spaceNeeded, value);675count = spaceNeeded;676return this;677}678679/**680* Appends the string representation of the {@code long}681* argument to this sequence.682* <p>683* The overall effect is exactly as if the argument were converted684* to a string by the method {@link String#valueOf(long)},685* and the characters of that string were then686* {@link #append(String) appended} to this character sequence.687*688* @param l a {@code long}.689* @return a reference to this object.690*/691public AbstractStringBuilder append(long l) {692if (l == Long.MIN_VALUE) {693append("-9223372036854775808");694return this;695}696int appendedLength = (l < 0) ? Long.stringSize(-l) + 1697: Long.stringSize(l);698int spaceNeeded = count + appendedLength;699ensureCapacityInternal(spaceNeeded);700Long.getChars(l, spaceNeeded, value);701count = spaceNeeded;702return this;703}704705/**706* Appends the string representation of the {@code float}707* argument to this sequence.708* <p>709* The overall effect is exactly as if the argument were converted710* to a string by the method {@link String#valueOf(float)},711* and the characters of that string were then712* {@link #append(String) appended} to this character sequence.713*714* @param f a {@code float}.715* @return a reference to this object.716*/717public AbstractStringBuilder append(float f) {718FloatingDecimal.appendTo(f,this);719return this;720}721722/**723* Appends the string representation of the {@code double}724* argument to this sequence.725* <p>726* The overall effect is exactly as if the argument were converted727* to a string by the method {@link String#valueOf(double)},728* and the characters of that string were then729* {@link #append(String) appended} to this character sequence.730*731* @param d a {@code double}.732* @return a reference to this object.733*/734public AbstractStringBuilder append(double d) {735FloatingDecimal.appendTo(d,this);736return this;737}738739/**740* Removes the characters in a substring of this sequence.741* The substring begins at the specified {@code start} and extends to742* the character at index {@code end - 1} or to the end of the743* sequence if no such character exists. If744* {@code start} is equal to {@code end}, no changes are made.745*746* @param start The beginning index, inclusive.747* @param end The ending index, exclusive.748* @return This object.749* @throws StringIndexOutOfBoundsException if {@code start}750* is negative, greater than {@code length()}, or751* greater than {@code end}.752*/753public AbstractStringBuilder delete(int start, int end) {754if (start < 0)755throw new StringIndexOutOfBoundsException(start);756if (end > count)757end = count;758if (start > end)759throw new StringIndexOutOfBoundsException();760int len = end - start;761if (len > 0) {762System.arraycopy(value, start+len, value, start, count-end);763count -= len;764}765return this;766}767768/**769* Appends the string representation of the {@code codePoint}770* argument to this sequence.771*772* <p> The argument is appended to the contents of this sequence.773* The length of this sequence increases by774* {@link Character#charCount(int) Character.charCount(codePoint)}.775*776* <p> The overall effect is exactly as if the argument were777* converted to a {@code char} array by the method778* {@link Character#toChars(int)} and the character in that array779* were then {@link #append(char[]) appended} to this character780* sequence.781*782* @param codePoint a Unicode code point783* @return a reference to this object.784* @exception IllegalArgumentException if the specified785* {@code codePoint} isn't a valid Unicode code point786*/787public AbstractStringBuilder appendCodePoint(int codePoint) {788final int count = this.count;789790if (Character.isBmpCodePoint(codePoint)) {791ensureCapacityInternal(count + 1);792value[count] = (char) codePoint;793this.count = count + 1;794} else if (Character.isValidCodePoint(codePoint)) {795ensureCapacityInternal(count + 2);796Character.toSurrogates(codePoint, value, count);797this.count = count + 2;798} else {799throw new IllegalArgumentException();800}801return this;802}803804/**805* Removes the {@code char} at the specified position in this806* sequence. This sequence is shortened by one {@code char}.807*808* <p>Note: If the character at the given index is a supplementary809* character, this method does not remove the entire character. If810* correct handling of supplementary characters is required,811* determine the number of {@code char}s to remove by calling812* {@code Character.charCount(thisSequence.codePointAt(index))},813* where {@code thisSequence} is this sequence.814*815* @param index Index of {@code char} to remove816* @return This object.817* @throws StringIndexOutOfBoundsException if the {@code index}818* is negative or greater than or equal to819* {@code length()}.820*/821public AbstractStringBuilder deleteCharAt(int index) {822if ((index < 0) || (index >= count))823throw new StringIndexOutOfBoundsException(index);824System.arraycopy(value, index+1, value, index, count-index-1);825count--;826return this;827}828829/**830* Replaces the characters in a substring of this sequence831* with characters in the specified {@code String}. The substring832* begins at the specified {@code start} and extends to the character833* at index {@code end - 1} or to the end of the834* sequence if no such character exists. First the835* characters in the substring are removed and then the specified836* {@code String} is inserted at {@code start}. (This837* sequence will be lengthened to accommodate the838* specified String if necessary.)839*840* @param start The beginning index, inclusive.841* @param end The ending index, exclusive.842* @param str String that will replace previous contents.843* @return This object.844* @throws StringIndexOutOfBoundsException if {@code start}845* is negative, greater than {@code length()}, or846* greater than {@code end}.847*/848public AbstractStringBuilder replace(int start, int end, String str) {849if (start < 0)850throw new StringIndexOutOfBoundsException(start);851if (start > count)852throw new StringIndexOutOfBoundsException("start > length()");853if (start > end)854throw new StringIndexOutOfBoundsException("start > end");855856if (end > count)857end = count;858int len = str.length();859int newCount = count + len - (end - start);860ensureCapacityInternal(newCount);861862System.arraycopy(value, end, value, start + len, count - end);863str.getChars(value, start);864count = newCount;865return this;866}867868/**869* Returns a new {@code String} that contains a subsequence of870* characters currently contained in this character sequence. The871* substring begins at the specified index and extends to the end of872* this sequence.873*874* @param start The beginning index, inclusive.875* @return The new string.876* @throws StringIndexOutOfBoundsException if {@code start} is877* less than zero, or greater than the length of this object.878*/879public String substring(int start) {880return substring(start, count);881}882883/**884* Returns a new character sequence that is a subsequence of this sequence.885*886* <p> An invocation of this method of the form887*888* <pre>{@code889* sb.subSequence(begin, end)}</pre>890*891* behaves in exactly the same way as the invocation892*893* <pre>{@code894* sb.substring(begin, end)}</pre>895*896* This method is provided so that this class can897* implement the {@link CharSequence} interface.898*899* @param start the start index, inclusive.900* @param end the end index, exclusive.901* @return the specified subsequence.902*903* @throws IndexOutOfBoundsException904* if {@code start} or {@code end} are negative,905* if {@code end} is greater than {@code length()},906* or if {@code start} is greater than {@code end}907* @spec JSR-51908*/909@Override910public CharSequence subSequence(int start, int end) {911return substring(start, end);912}913914/**915* Returns a new {@code String} that contains a subsequence of916* characters currently contained in this sequence. The917* substring begins at the specified {@code start} and918* extends to the character at index {@code end - 1}.919*920* @param start The beginning index, inclusive.921* @param end The ending index, exclusive.922* @return The new string.923* @throws StringIndexOutOfBoundsException if {@code start}924* or {@code end} are negative or greater than925* {@code length()}, or {@code start} is926* greater than {@code end}.927*/928public String substring(int start, int end) {929if (start < 0)930throw new StringIndexOutOfBoundsException(start);931if (end > count)932throw new StringIndexOutOfBoundsException(end);933if (start > end)934throw new StringIndexOutOfBoundsException(end - start);935return new String(value, start, end - start);936}937938/**939* Inserts the string representation of a subarray of the {@code str}940* array argument into this sequence. The subarray begins at the941* specified {@code offset} and extends {@code len} {@code char}s.942* The characters of the subarray are inserted into this sequence at943* the position indicated by {@code index}. The length of this944* sequence increases by {@code len} {@code char}s.945*946* @param index position at which to insert subarray.947* @param str A {@code char} array.948* @param offset the index of the first {@code char} in subarray to949* be inserted.950* @param len the number of {@code char}s in the subarray to951* be inserted.952* @return This object953* @throws StringIndexOutOfBoundsException if {@code index}954* is negative or greater than {@code length()}, or955* {@code offset} or {@code len} are negative, or956* {@code (offset+len)} is greater than957* {@code str.length}.958*/959public AbstractStringBuilder insert(int index, char[] str, int offset,960int len)961{962if ((index < 0) || (index > length()))963throw new StringIndexOutOfBoundsException(index);964if ((offset < 0) || (len < 0) || (offset > str.length - len))965throw new StringIndexOutOfBoundsException(966"offset " + offset + ", len " + len + ", str.length "967+ str.length);968ensureCapacityInternal(count + len);969System.arraycopy(value, index, value, index + len, count - index);970System.arraycopy(str, offset, value, index, len);971count += len;972return this;973}974975/**976* Inserts the string representation of the {@code Object}977* argument into this character sequence.978* <p>979* The overall effect is exactly as if the second argument were980* converted to a string by the method {@link String#valueOf(Object)},981* and the characters of that string were then982* {@link #insert(int,String) inserted} into this character983* sequence at the indicated offset.984* <p>985* The {@code offset} argument must be greater than or equal to986* {@code 0}, and less than or equal to the {@linkplain #length() length}987* of this sequence.988*989* @param offset the offset.990* @param obj an {@code Object}.991* @return a reference to this object.992* @throws StringIndexOutOfBoundsException if the offset is invalid.993*/994public AbstractStringBuilder insert(int offset, Object obj) {995return insert(offset, String.valueOf(obj));996}997998/**999* Inserts the string into this character sequence.1000* <p>1001* The characters of the {@code String} argument are inserted, in1002* order, into this sequence at the indicated offset, moving up any1003* characters originally above that position and increasing the length1004* of this sequence by the length of the argument. If1005* {@code str} is {@code null}, then the four characters1006* {@code "null"} are inserted into this sequence.1007* <p>1008* The character at index <i>k</i> in the new character sequence is1009* equal to:1010* <ul>1011* <li>the character at index <i>k</i> in the old character sequence, if1012* <i>k</i> is less than {@code offset}1013* <li>the character at index <i>k</i>{@code -offset} in the1014* argument {@code str}, if <i>k</i> is not less than1015* {@code offset} but is less than {@code offset+str.length()}1016* <li>the character at index <i>k</i>{@code -str.length()} in the1017* old character sequence, if <i>k</i> is not less than1018* {@code offset+str.length()}1019* </ul><p>1020* The {@code offset} argument must be greater than or equal to1021* {@code 0}, and less than or equal to the {@linkplain #length() length}1022* of this sequence.1023*1024* @param offset the offset.1025* @param str a string.1026* @return a reference to this object.1027* @throws StringIndexOutOfBoundsException if the offset is invalid.1028*/1029public AbstractStringBuilder insert(int offset, String str) {1030if ((offset < 0) || (offset > length()))1031throw new StringIndexOutOfBoundsException(offset);1032if (str == null)1033str = "null";1034int len = str.length();1035ensureCapacityInternal(count + len);1036System.arraycopy(value, offset, value, offset + len, count - offset);1037str.getChars(value, offset);1038count += len;1039return this;1040}10411042/**1043* Inserts the string representation of the {@code char} array1044* argument into this sequence.1045* <p>1046* The characters of the array argument are inserted into the1047* contents of this sequence at the position indicated by1048* {@code offset}. The length of this sequence increases by1049* the length of the argument.1050* <p>1051* The overall effect is exactly as if the second argument were1052* converted to a string by the method {@link String#valueOf(char[])},1053* and the characters of that string were then1054* {@link #insert(int,String) inserted} into this character1055* sequence at the indicated offset.1056* <p>1057* The {@code offset} argument must be greater than or equal to1058* {@code 0}, and less than or equal to the {@linkplain #length() length}1059* of this sequence.1060*1061* @param offset the offset.1062* @param str a character array.1063* @return a reference to this object.1064* @throws StringIndexOutOfBoundsException if the offset is invalid.1065*/1066public AbstractStringBuilder insert(int offset, char[] str) {1067if ((offset < 0) || (offset > length()))1068throw new StringIndexOutOfBoundsException(offset);1069int len = str.length;1070ensureCapacityInternal(count + len);1071System.arraycopy(value, offset, value, offset + len, count - offset);1072System.arraycopy(str, 0, value, offset, len);1073count += len;1074return this;1075}10761077/**1078* Inserts the specified {@code CharSequence} into this sequence.1079* <p>1080* The characters of the {@code CharSequence} argument are inserted,1081* in order, into this sequence at the indicated offset, moving up1082* any characters originally above that position and increasing the length1083* of this sequence by the length of the argument s.1084* <p>1085* The result of this method is exactly the same as if it were an1086* invocation of this object's1087* {@link #insert(int,CharSequence,int,int) insert}(dstOffset, s, 0, s.length())1088* method.1089*1090* <p>If {@code s} is {@code null}, then the four characters1091* {@code "null"} are inserted into this sequence.1092*1093* @param dstOffset the offset.1094* @param s the sequence to be inserted1095* @return a reference to this object.1096* @throws IndexOutOfBoundsException if the offset is invalid.1097*/1098public AbstractStringBuilder insert(int dstOffset, CharSequence s) {1099if (s == null)1100s = "null";1101if (s instanceof String)1102return this.insert(dstOffset, (String)s);1103return this.insert(dstOffset, s, 0, s.length());1104}11051106/**1107* Inserts a subsequence of the specified {@code CharSequence} into1108* this sequence.1109* <p>1110* The subsequence of the argument {@code s} specified by1111* {@code start} and {@code end} are inserted,1112* in order, into this sequence at the specified destination offset, moving1113* up any characters originally above that position. The length of this1114* sequence is increased by {@code end - start}.1115* <p>1116* The character at index <i>k</i> in this sequence becomes equal to:1117* <ul>1118* <li>the character at index <i>k</i> in this sequence, if1119* <i>k</i> is less than {@code dstOffset}1120* <li>the character at index <i>k</i>{@code +start-dstOffset} in1121* the argument {@code s}, if <i>k</i> is greater than or equal to1122* {@code dstOffset} but is less than {@code dstOffset+end-start}1123* <li>the character at index <i>k</i>{@code -(end-start)} in this1124* sequence, if <i>k</i> is greater than or equal to1125* {@code dstOffset+end-start}1126* </ul><p>1127* The {@code dstOffset} argument must be greater than or equal to1128* {@code 0}, and less than or equal to the {@linkplain #length() length}1129* of this sequence.1130* <p>The start argument must be nonnegative, and not greater than1131* {@code end}.1132* <p>The end argument must be greater than or equal to1133* {@code start}, and less than or equal to the length of s.1134*1135* <p>If {@code s} is {@code null}, then this method inserts1136* characters as if the s parameter was a sequence containing the four1137* characters {@code "null"}.1138*1139* @param dstOffset the offset in this sequence.1140* @param s the sequence to be inserted.1141* @param start the starting index of the subsequence to be inserted.1142* @param end the end index of the subsequence to be inserted.1143* @return a reference to this object.1144* @throws IndexOutOfBoundsException if {@code dstOffset}1145* is negative or greater than {@code this.length()}, or1146* {@code start} or {@code end} are negative, or1147* {@code start} is greater than {@code end} or1148* {@code end} is greater than {@code s.length()}1149*/1150public AbstractStringBuilder insert(int dstOffset, CharSequence s,1151int start, int end) {1152if (s == null)1153s = "null";1154if ((dstOffset < 0) || (dstOffset > this.length()))1155throw new IndexOutOfBoundsException("dstOffset "+dstOffset);1156if ((start < 0) || (end < 0) || (start > end) || (end > s.length()))1157throw new IndexOutOfBoundsException(1158"start " + start + ", end " + end + ", s.length() "1159+ s.length());1160int len = end - start;1161ensureCapacityInternal(count + len);1162System.arraycopy(value, dstOffset, value, dstOffset + len,1163count - dstOffset);1164for (int i=start; i<end; i++)1165value[dstOffset++] = s.charAt(i);1166count += len;1167return this;1168}11691170/**1171* Inserts the string representation of the {@code boolean}1172* argument into this sequence.1173* <p>1174* The overall effect is exactly as if the second argument were1175* converted to a string by the method {@link String#valueOf(boolean)},1176* and the characters of that string were then1177* {@link #insert(int,String) inserted} into this character1178* sequence at the indicated offset.1179* <p>1180* The {@code offset} argument must be greater than or equal to1181* {@code 0}, and less than or equal to the {@linkplain #length() length}1182* of this sequence.1183*1184* @param offset the offset.1185* @param b a {@code boolean}.1186* @return a reference to this object.1187* @throws StringIndexOutOfBoundsException if the offset is invalid.1188*/1189public AbstractStringBuilder insert(int offset, boolean b) {1190return insert(offset, String.valueOf(b));1191}11921193/**1194* Inserts the string representation of the {@code char}1195* argument into this sequence.1196* <p>1197* The overall effect is exactly as if the second argument were1198* converted to a string by the method {@link String#valueOf(char)},1199* and the character in that string were then1200* {@link #insert(int,String) inserted} into this character1201* sequence at the indicated offset.1202* <p>1203* The {@code offset} argument must be greater than or equal to1204* {@code 0}, and less than or equal to the {@linkplain #length() length}1205* of this sequence.1206*1207* @param offset the offset.1208* @param c a {@code char}.1209* @return a reference to this object.1210* @throws IndexOutOfBoundsException if the offset is invalid.1211*/1212public AbstractStringBuilder insert(int offset, char c) {1213ensureCapacityInternal(count + 1);1214System.arraycopy(value, offset, value, offset + 1, count - offset);1215value[offset] = c;1216count += 1;1217return this;1218}12191220/**1221* Inserts the string representation of the second {@code int}1222* argument into this sequence.1223* <p>1224* The overall effect is exactly as if the second argument were1225* converted to a string by the method {@link String#valueOf(int)},1226* and the characters of that string were then1227* {@link #insert(int,String) inserted} into this character1228* sequence at the indicated offset.1229* <p>1230* The {@code offset} argument must be greater than or equal to1231* {@code 0}, and less than or equal to the {@linkplain #length() length}1232* of this sequence.1233*1234* @param offset the offset.1235* @param i an {@code int}.1236* @return a reference to this object.1237* @throws StringIndexOutOfBoundsException if the offset is invalid.1238*/1239public AbstractStringBuilder insert(int offset, int i) {1240return insert(offset, String.valueOf(i));1241}12421243/**1244* Inserts the string representation of the {@code long}1245* argument into this sequence.1246* <p>1247* The overall effect is exactly as if the second argument were1248* converted to a string by the method {@link String#valueOf(long)},1249* and the characters of that string were then1250* {@link #insert(int,String) inserted} into this character1251* sequence at the indicated offset.1252* <p>1253* The {@code offset} argument must be greater than or equal to1254* {@code 0}, and less than or equal to the {@linkplain #length() length}1255* of this sequence.1256*1257* @param offset the offset.1258* @param l a {@code long}.1259* @return a reference to this object.1260* @throws StringIndexOutOfBoundsException if the offset is invalid.1261*/1262public AbstractStringBuilder insert(int offset, long l) {1263return insert(offset, String.valueOf(l));1264}12651266/**1267* Inserts the string representation of the {@code float}1268* argument into this sequence.1269* <p>1270* The overall effect is exactly as if the second argument were1271* converted to a string by the method {@link String#valueOf(float)},1272* and the characters of that string were then1273* {@link #insert(int,String) inserted} into this character1274* sequence at the indicated offset.1275* <p>1276* The {@code offset} argument must be greater than or equal to1277* {@code 0}, and less than or equal to the {@linkplain #length() length}1278* of this sequence.1279*1280* @param offset the offset.1281* @param f a {@code float}.1282* @return a reference to this object.1283* @throws StringIndexOutOfBoundsException if the offset is invalid.1284*/1285public AbstractStringBuilder insert(int offset, float f) {1286return insert(offset, String.valueOf(f));1287}12881289/**1290* Inserts the string representation of the {@code double}1291* argument into this sequence.1292* <p>1293* The overall effect is exactly as if the second argument were1294* converted to a string by the method {@link String#valueOf(double)},1295* and the characters of that string were then1296* {@link #insert(int,String) inserted} into this character1297* sequence at the indicated offset.1298* <p>1299* The {@code offset} argument must be greater than or equal to1300* {@code 0}, and less than or equal to the {@linkplain #length() length}1301* of this sequence.1302*1303* @param offset the offset.1304* @param d a {@code double}.1305* @return a reference to this object.1306* @throws StringIndexOutOfBoundsException if the offset is invalid.1307*/1308public AbstractStringBuilder insert(int offset, double d) {1309return insert(offset, String.valueOf(d));1310}13111312/**1313* Returns the index within this string of the first occurrence of the1314* specified substring. The integer returned is the smallest value1315* <i>k</i> such that:1316* <pre>{@code1317* this.toString().startsWith(str, <i>k</i>)1318* }</pre>1319* is {@code true}.1320*1321* @param str any string.1322* @return if the string argument occurs as a substring within this1323* object, then the index of the first character of the first1324* such substring is returned; if it does not occur as a1325* substring, {@code -1} is returned.1326*/1327public int indexOf(String str) {1328return indexOf(str, 0);1329}13301331/**1332* Returns the index within this string of the first occurrence of the1333* specified substring, starting at the specified index. The integer1334* returned is the smallest value {@code k} for which:1335* <pre>{@code1336* k >= Math.min(fromIndex, this.length()) &&1337* this.toString().startsWith(str, k)1338* }</pre>1339* If no such value of <i>k</i> exists, then -1 is returned.1340*1341* @param str the substring for which to search.1342* @param fromIndex the index from which to start the search.1343* @return the index within this string of the first occurrence of the1344* specified substring, starting at the specified index.1345*/1346public int indexOf(String str, int fromIndex) {1347return String.indexOf(value, 0, count, str, fromIndex);1348}13491350/**1351* Returns the index within this string of the rightmost occurrence1352* of the specified substring. The rightmost empty string "" is1353* considered to occur at the index value {@code this.length()}.1354* The returned index is the largest value <i>k</i> such that1355* <pre>{@code1356* this.toString().startsWith(str, k)1357* }</pre>1358* is true.1359*1360* @param str the substring to search for.1361* @return if the string argument occurs one or more times as a substring1362* within this object, then the index of the first character of1363* the last such substring is returned. If it does not occur as1364* a substring, {@code -1} is returned.1365*/1366public int lastIndexOf(String str) {1367return lastIndexOf(str, count);1368}13691370/**1371* Returns the index within this string of the last occurrence of the1372* specified substring. The integer returned is the largest value <i>k</i>1373* such that:1374* <pre>{@code1375* k <= Math.min(fromIndex, this.length()) &&1376* this.toString().startsWith(str, k)1377* }</pre>1378* If no such value of <i>k</i> exists, then -1 is returned.1379*1380* @param str the substring to search for.1381* @param fromIndex the index to start the search from.1382* @return the index within this sequence of the last occurrence of the1383* specified substring.1384*/1385public int lastIndexOf(String str, int fromIndex) {1386return String.lastIndexOf(value, 0, count, str, fromIndex);1387}13881389/**1390* Causes this character sequence to be replaced by the reverse of1391* the sequence. If there are any surrogate pairs included in the1392* sequence, these are treated as single characters for the1393* reverse operation. Thus, the order of the high-low surrogates1394* is never reversed.1395*1396* Let <i>n</i> be the character length of this character sequence1397* (not the length in {@code char} values) just prior to1398* execution of the {@code reverse} method. Then the1399* character at index <i>k</i> in the new character sequence is1400* equal to the character at index <i>n-k-1</i> in the old1401* character sequence.1402*1403* <p>Note that the reverse operation may result in producing1404* surrogate pairs that were unpaired low-surrogates and1405* high-surrogates before the operation. For example, reversing1406* "\u005CuDC00\u005CuD800" produces "\u005CuD800\u005CuDC00" which is1407* a valid surrogate pair.1408*1409* @return a reference to this object.1410*/1411public AbstractStringBuilder reverse() {1412boolean hasSurrogates = false;1413int n = count - 1;1414for (int j = (n-1) >> 1; j >= 0; j--) {1415int k = n - j;1416char cj = value[j];1417char ck = value[k];1418value[j] = ck;1419value[k] = cj;1420if (Character.isSurrogate(cj) ||1421Character.isSurrogate(ck)) {1422hasSurrogates = true;1423}1424}1425if (hasSurrogates) {1426reverseAllValidSurrogatePairs();1427}1428return this;1429}14301431/** Outlined helper method for reverse() */1432private void reverseAllValidSurrogatePairs() {1433for (int i = 0; i < count - 1; i++) {1434char c2 = value[i];1435if (Character.isLowSurrogate(c2)) {1436char c1 = value[i + 1];1437if (Character.isHighSurrogate(c1)) {1438value[i++] = c1;1439value[i] = c2;1440}1441}1442}1443}14441445/**1446* Returns a string representing the data in this sequence.1447* A new {@code String} object is allocated and initialized to1448* contain the character sequence currently represented by this1449* object. This {@code String} is then returned. Subsequent1450* changes to this sequence do not affect the contents of the1451* {@code String}.1452*1453* @return a string representation of this sequence of characters.1454*/1455@Override1456public abstract String toString();14571458/**1459* Needed by {@code String} for the contentEquals method.1460*/1461final char[] getValue() {1462return value;1463}14641465}146614671468