jaroslav@56: /* jaroslav@56: * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. jaroslav@56: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@56: * jaroslav@56: * This code is free software; you can redistribute it and/or modify it jaroslav@56: * under the terms of the GNU General Public License version 2 only, as jaroslav@56: * published by the Free Software Foundation. Oracle designates this jaroslav@56: * particular file as subject to the "Classpath" exception as provided jaroslav@56: * by Oracle in the LICENSE file that accompanied this code. jaroslav@56: * jaroslav@56: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@56: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@56: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@56: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@56: * accompanied this code). jaroslav@56: * jaroslav@56: * You should have received a copy of the GNU General Public License version jaroslav@56: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@56: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@56: * jaroslav@56: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@56: * or visit www.oracle.com if you need additional information or have any jaroslav@56: * questions. jaroslav@56: */ jaroslav@56: jaroslav@56: package java.lang; jaroslav@56: jaroslav@94: import org.apidesign.bck2brwsr.core.JavaScriptBody; jaroslav@94: jaroslav@56: /** jaroslav@56: * A mutable sequence of characters. jaroslav@56: *

jaroslav@56: * Implements a modifiable string. At any point in time it contains some jaroslav@56: * particular sequence of characters, but the length and content of the jaroslav@56: * sequence can be changed through certain method calls. jaroslav@56: * jaroslav@56: * @author Michael McCloskey jaroslav@56: * @author Martin Buchholz jaroslav@56: * @author Ulf Zibis jaroslav@56: * @since 1.5 jaroslav@56: */ jaroslav@56: abstract class AbstractStringBuilder implements Appendable, CharSequence { jaroslav@56: /** jaroslav@56: * The value is used for character storage. jaroslav@56: */ jaroslav@56: char[] value; jaroslav@56: jaroslav@56: /** jaroslav@56: * The count is the number of characters used. jaroslav@56: */ jaroslav@56: int count; jaroslav@56: jaroslav@56: /** jaroslav@56: * This no-arg constructor is necessary for serialization of subclasses. jaroslav@56: */ jaroslav@56: AbstractStringBuilder() { jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Creates an AbstractStringBuilder of the specified capacity. jaroslav@56: */ jaroslav@56: AbstractStringBuilder(int capacity) { jaroslav@56: value = new char[capacity]; jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Returns the length (character count). jaroslav@56: * jaroslav@56: * @return the length of the sequence of characters currently jaroslav@56: * represented by this object jaroslav@56: */ jaroslav@56: public int length() { jaroslav@56: return count; jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Returns the current capacity. The capacity is the amount of storage jaroslav@56: * available for newly inserted characters, beyond which an allocation jaroslav@56: * will occur. jaroslav@56: * jaroslav@56: * @return the current capacity jaroslav@56: */ jaroslav@56: public int capacity() { jaroslav@56: return value.length; jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Ensures that the capacity is at least equal to the specified minimum. jaroslav@56: * If the current capacity is less than the argument, then a new internal jaroslav@56: * array is allocated with greater capacity. The new capacity is the jaroslav@56: * larger of: jaroslav@56: *

jaroslav@56: * If the minimumCapacity argument is nonpositive, this jaroslav@56: * method takes no action and simply returns. jaroslav@56: * jaroslav@56: * @param minimumCapacity the minimum desired capacity. jaroslav@56: */ jaroslav@56: public void ensureCapacity(int minimumCapacity) { jaroslav@56: if (minimumCapacity > 0) jaroslav@56: ensureCapacityInternal(minimumCapacity); jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * This method has the same contract as ensureCapacity, but is jaroslav@56: * never synchronized. jaroslav@56: */ jaroslav@56: private void ensureCapacityInternal(int minimumCapacity) { jaroslav@56: // overflow-conscious code jaroslav@56: if (minimumCapacity - value.length > 0) jaroslav@56: expandCapacity(minimumCapacity); jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * This implements the expansion semantics of ensureCapacity with no jaroslav@56: * size check or synchronization. jaroslav@56: */ jaroslav@56: void expandCapacity(int minimumCapacity) { jaroslav@56: int newCapacity = value.length * 2 + 2; jaroslav@56: if (newCapacity - minimumCapacity < 0) jaroslav@56: newCapacity = minimumCapacity; jaroslav@56: if (newCapacity < 0) { jaroslav@56: if (minimumCapacity < 0) // overflow jaroslav@56: throw new OutOfMemoryError(); jaroslav@56: newCapacity = Integer.MAX_VALUE; jaroslav@56: } jaroslav@104: value = copyOf(value, newCapacity); jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Attempts to reduce storage used for the character sequence. jaroslav@56: * If the buffer is larger than necessary to hold its current sequence of jaroslav@56: * characters, then it may be resized to become more space efficient. jaroslav@56: * Calling this method may, but is not required to, affect the value jaroslav@56: * returned by a subsequent call to the {@link #capacity()} method. jaroslav@56: */ jaroslav@56: public void trimToSize() { jaroslav@56: if (count < value.length) { jaroslav@104: value = copyOf(value, count); jaroslav@56: } jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Sets the length of the character sequence. jaroslav@56: * The sequence is changed to a new character sequence jaroslav@56: * whose length is specified by the argument. For every nonnegative jaroslav@56: * index k less than newLength, the character at jaroslav@56: * index k in the new character sequence is the same as the jaroslav@56: * character at index k in the old sequence if k is less jaroslav@56: * than the length of the old character sequence; otherwise, it is the jaroslav@56: * null character '\u0000'. jaroslav@56: * jaroslav@56: * In other words, if the newLength argument is less than jaroslav@56: * the current length, the length is changed to the specified length. jaroslav@56: *

jaroslav@56: * If the newLength argument is greater than or equal jaroslav@56: * to the current length, sufficient null characters jaroslav@56: * ('\u0000') are appended so that jaroslav@56: * length becomes the newLength argument. jaroslav@56: *

jaroslav@56: * The newLength argument must be greater than or equal jaroslav@56: * to 0. jaroslav@56: * jaroslav@56: * @param newLength the new length jaroslav@56: * @throws IndexOutOfBoundsException if the jaroslav@56: * newLength argument is negative. jaroslav@56: */ jaroslav@56: public void setLength(int newLength) { jaroslav@56: if (newLength < 0) jaroslav@56: throw new StringIndexOutOfBoundsException(newLength); jaroslav@56: ensureCapacityInternal(newLength); jaroslav@56: jaroslav@56: if (count < newLength) { jaroslav@56: for (; count < newLength; count++) jaroslav@56: value[count] = '\0'; jaroslav@56: } else { jaroslav@56: count = newLength; jaroslav@56: } jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Returns the char value in this sequence at the specified index. jaroslav@56: * The first char value is at index 0, the next at index jaroslav@56: * 1, and so on, as in array indexing. jaroslav@56: *

jaroslav@56: * The index argument must be greater than or equal to jaroslav@56: * 0, and less than the length of this sequence. jaroslav@56: * jaroslav@56: *

If the char value specified by the index is a jaroslav@56: * surrogate, the surrogate jaroslav@56: * value is returned. jaroslav@56: * jaroslav@56: * @param index the index of the desired char value. jaroslav@56: * @return the char value at the specified index. jaroslav@56: * @throws IndexOutOfBoundsException if index is jaroslav@56: * negative or greater than or equal to length(). jaroslav@56: */ jaroslav@56: public char charAt(int index) { jaroslav@56: if ((index < 0) || (index >= count)) jaroslav@56: throw new StringIndexOutOfBoundsException(index); jaroslav@56: return value[index]; jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Returns the character (Unicode code point) at the specified jaroslav@56: * index. The index refers to char values jaroslav@56: * (Unicode code units) and ranges from 0 to jaroslav@56: * {@link #length()} - 1. jaroslav@56: * jaroslav@56: *

If the char value specified at the given index jaroslav@56: * is in the high-surrogate range, the following index is less jaroslav@56: * than the length of this sequence, and the jaroslav@56: * char value at the following index is in the jaroslav@56: * low-surrogate range, then the supplementary code point jaroslav@56: * corresponding to this surrogate pair is returned. Otherwise, jaroslav@56: * the char value at the given index is returned. jaroslav@56: * jaroslav@56: * @param index the index to the char values jaroslav@56: * @return the code point value of the character at the jaroslav@56: * index jaroslav@56: * @exception IndexOutOfBoundsException if the index jaroslav@56: * argument is negative or not less than the length of this jaroslav@56: * sequence. jaroslav@56: */ jaroslav@56: public int codePointAt(int index) { jaroslav@56: if ((index < 0) || (index >= count)) { jaroslav@56: throw new StringIndexOutOfBoundsException(index); jaroslav@56: } jaroslav@56: return Character.codePointAt(value, index); jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Returns the character (Unicode code point) before the specified jaroslav@56: * index. The index refers to char values jaroslav@56: * (Unicode code units) and ranges from 1 to {@link jaroslav@56: * #length()}. jaroslav@56: * jaroslav@56: *

If the char value at (index - 1) jaroslav@56: * is in the low-surrogate range, (index - 2) is not jaroslav@56: * negative, and the char value at (index - jaroslav@56: * 2) is in the high-surrogate range, then the jaroslav@56: * supplementary code point value of the surrogate pair is jaroslav@56: * returned. If the char value at index - jaroslav@56: * 1 is an unpaired low-surrogate or a high-surrogate, the jaroslav@56: * surrogate value is returned. jaroslav@56: * jaroslav@56: * @param index the index following the code point that should be returned jaroslav@56: * @return the Unicode code point value before the given index. jaroslav@56: * @exception IndexOutOfBoundsException if the index jaroslav@56: * argument is less than 1 or greater than the length jaroslav@56: * of this sequence. jaroslav@56: */ jaroslav@56: public int codePointBefore(int index) { jaroslav@56: int i = index - 1; jaroslav@56: if ((i < 0) || (i >= count)) { jaroslav@56: throw new StringIndexOutOfBoundsException(index); jaroslav@56: } jaroslav@56: return Character.codePointBefore(value, index); jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Returns the number of Unicode code points in the specified text jaroslav@56: * range of this sequence. The text range begins at the specified jaroslav@56: * beginIndex and extends to the char at jaroslav@56: * index endIndex - 1. Thus the length (in jaroslav@56: * chars) of the text range is jaroslav@56: * endIndex-beginIndex. Unpaired surrogates within jaroslav@56: * this sequence count as one code point each. jaroslav@56: * jaroslav@56: * @param beginIndex the index to the first char of jaroslav@56: * the text range. jaroslav@56: * @param endIndex the index after the last char of jaroslav@56: * the text range. jaroslav@56: * @return the number of Unicode code points in the specified text jaroslav@56: * range jaroslav@56: * @exception IndexOutOfBoundsException if the jaroslav@56: * beginIndex is negative, or endIndex jaroslav@56: * is larger than the length of this sequence, or jaroslav@56: * beginIndex is larger than endIndex. jaroslav@56: */ jaroslav@56: public int codePointCount(int beginIndex, int endIndex) { jaroslav@56: if (beginIndex < 0 || endIndex > count || beginIndex > endIndex) { jaroslav@56: throw new IndexOutOfBoundsException(); jaroslav@56: } jaroslav@56: return Character.codePointCountImpl(value, beginIndex, endIndex-beginIndex); jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Returns the index within this sequence that is offset from the jaroslav@56: * given index by codePointOffset code jaroslav@56: * points. Unpaired surrogates within the text range given by jaroslav@56: * index and codePointOffset count as jaroslav@56: * one code point each. jaroslav@56: * jaroslav@56: * @param index the index to be offset jaroslav@56: * @param codePointOffset the offset in code points jaroslav@56: * @return the index within this sequence jaroslav@56: * @exception IndexOutOfBoundsException if index jaroslav@56: * is negative or larger then the length of this sequence, jaroslav@56: * or if codePointOffset is positive and the subsequence jaroslav@56: * starting with index has fewer than jaroslav@56: * codePointOffset code points, jaroslav@56: * or if codePointOffset is negative and the subsequence jaroslav@56: * before index has fewer than the absolute value of jaroslav@56: * codePointOffset code points. jaroslav@56: */ jaroslav@56: public int offsetByCodePoints(int index, int codePointOffset) { jaroslav@56: if (index < 0 || index > count) { jaroslav@56: throw new IndexOutOfBoundsException(); jaroslav@56: } jaroslav@56: return Character.offsetByCodePointsImpl(value, 0, count, jaroslav@56: index, codePointOffset); jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Characters are copied from this sequence into the jaroslav@56: * destination character array dst. The first character to jaroslav@56: * be copied is at index srcBegin; the last character to jaroslav@56: * be copied is at index srcEnd-1. The total number of jaroslav@56: * characters to be copied is srcEnd-srcBegin. The jaroslav@56: * characters are copied into the subarray of dst starting jaroslav@56: * at index dstBegin and ending at index: jaroslav@56: *

jaroslav@56:      * dstbegin + (srcEnd-srcBegin) - 1
jaroslav@56:      * 
jaroslav@56: * jaroslav@56: * @param srcBegin start copying at this offset. jaroslav@56: * @param srcEnd stop copying at this offset. jaroslav@56: * @param dst the array to copy the data into. jaroslav@56: * @param dstBegin offset into dst. jaroslav@56: * @throws NullPointerException if dst is jaroslav@56: * null. jaroslav@56: * @throws IndexOutOfBoundsException if any of the following is true: jaroslav@56: * jaroslav@56: */ jaroslav@56: public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) jaroslav@56: { jaroslav@56: if (srcBegin < 0) jaroslav@56: throw new StringIndexOutOfBoundsException(srcBegin); jaroslav@56: if ((srcEnd < 0) || (srcEnd > count)) jaroslav@56: throw new StringIndexOutOfBoundsException(srcEnd); jaroslav@56: if (srcBegin > srcEnd) jaroslav@56: throw new StringIndexOutOfBoundsException("srcBegin > srcEnd"); jaroslav@104: arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin); jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * The character at the specified index is set to ch. This jaroslav@56: * sequence is altered to represent a new character sequence that is jaroslav@56: * identical to the old character sequence, except that it contains the jaroslav@56: * character ch at position index. jaroslav@56: *

jaroslav@56: * The index argument must be greater than or equal to jaroslav@56: * 0, and less than the length of this sequence. jaroslav@56: * jaroslav@56: * @param index the index of the character to modify. jaroslav@56: * @param ch the new character. jaroslav@56: * @throws IndexOutOfBoundsException if index is jaroslav@56: * negative or greater than or equal to length(). jaroslav@56: */ jaroslav@56: public void setCharAt(int index, char ch) { jaroslav@56: if ((index < 0) || (index >= count)) jaroslav@56: throw new StringIndexOutOfBoundsException(index); jaroslav@56: value[index] = ch; jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Appends the string representation of the {@code Object} argument. jaroslav@56: *

jaroslav@56: * The overall effect is exactly as if the argument were converted jaroslav@56: * to a string by the method {@link String#valueOf(Object)}, jaroslav@56: * and the characters of that string were then jaroslav@56: * {@link #append(String) appended} to this character sequence. jaroslav@56: * jaroslav@56: * @param obj an {@code Object}. jaroslav@56: * @return a reference to this object. jaroslav@56: */ jaroslav@56: public AbstractStringBuilder append(Object obj) { jaroslav@56: return append(String.valueOf(obj)); jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Appends the specified string to this character sequence. jaroslav@56: *

jaroslav@56: * The characters of the {@code String} argument are appended, in jaroslav@56: * order, increasing the length of this sequence by the length of the jaroslav@56: * argument. If {@code str} is {@code null}, then the four jaroslav@56: * characters {@code "null"} are appended. jaroslav@56: *

jaroslav@56: * Let n be the length of this character sequence just prior to jaroslav@56: * execution of the {@code append} method. Then the character at jaroslav@56: * index k in the new character sequence is equal to the character jaroslav@56: * at index k in the old character sequence, if k is less jaroslav@56: * than n; otherwise, it is equal to the character at index jaroslav@56: * k-n in the argument {@code str}. jaroslav@56: * jaroslav@56: * @param str a string. jaroslav@56: * @return a reference to this object. jaroslav@56: */ jaroslav@56: public AbstractStringBuilder append(String str) { jaroslav@56: if (str == null) str = "null"; jaroslav@56: int len = str.length(); jaroslav@56: ensureCapacityInternal(count + len); jaroslav@56: str.getChars(0, len, value, count); jaroslav@56: count += len; jaroslav@56: return this; jaroslav@56: } jaroslav@56: jaroslav@56: // Documentation in subclasses because of synchro difference jaroslav@56: public AbstractStringBuilder append(StringBuffer sb) { jaroslav@56: if (sb == null) jaroslav@56: return append("null"); jaroslav@56: int len = sb.length(); jaroslav@56: ensureCapacityInternal(count + len); jaroslav@56: sb.getChars(0, len, value, count); jaroslav@56: count += len; jaroslav@56: return this; jaroslav@56: } jaroslav@56: jaroslav@56: // Documentation in subclasses because of synchro difference jaroslav@56: public AbstractStringBuilder append(CharSequence s) { jaroslav@56: if (s == null) jaroslav@56: s = "null"; jaroslav@56: if (s instanceof String) jaroslav@56: return this.append((String)s); jaroslav@56: if (s instanceof StringBuffer) jaroslav@56: return this.append((StringBuffer)s); jaroslav@56: return this.append(s, 0, s.length()); jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Appends a subsequence of the specified {@code CharSequence} to this jaroslav@56: * sequence. jaroslav@56: *

jaroslav@56: * Characters of the argument {@code s}, starting at jaroslav@56: * index {@code start}, are appended, in order, to the contents of jaroslav@56: * this sequence up to the (exclusive) index {@code end}. The length jaroslav@56: * of this sequence is increased by the value of {@code end - start}. jaroslav@56: *

jaroslav@56: * Let n be the length of this character sequence just prior to jaroslav@56: * execution of the {@code append} method. Then the character at jaroslav@56: * index k in this character sequence becomes equal to the jaroslav@56: * character at index k in this sequence, if k is less than jaroslav@56: * n; otherwise, it is equal to the character at index jaroslav@56: * k+start-n in the argument {@code s}. jaroslav@56: *

jaroslav@56: * If {@code s} is {@code null}, then this method appends jaroslav@56: * characters as if the s parameter was a sequence containing the four jaroslav@56: * characters {@code "null"}. jaroslav@56: * jaroslav@56: * @param s the sequence to append. jaroslav@56: * @param start the starting index of the subsequence to be appended. jaroslav@56: * @param end the end index of the subsequence to be appended. jaroslav@56: * @return a reference to this object. jaroslav@56: * @throws IndexOutOfBoundsException if jaroslav@56: * {@code start} is negative, or jaroslav@56: * {@code start} is greater than {@code end} or jaroslav@56: * {@code end} is greater than {@code s.length()} jaroslav@56: */ jaroslav@56: public AbstractStringBuilder append(CharSequence s, int start, int end) { jaroslav@56: if (s == null) jaroslav@56: s = "null"; jaroslav@56: if ((start < 0) || (start > end) || (end > s.length())) jaroslav@56: throw new IndexOutOfBoundsException( jaroslav@56: "start " + start + ", end " + end + ", s.length() " jaroslav@56: + s.length()); jaroslav@56: int len = end - start; jaroslav@56: ensureCapacityInternal(count + len); jaroslav@56: for (int i = start, j = count; i < end; i++, j++) jaroslav@56: value[j] = s.charAt(i); jaroslav@56: count += len; jaroslav@56: return this; jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Appends the string representation of the {@code char} array jaroslav@56: * argument to this sequence. jaroslav@56: *

jaroslav@56: * The characters of the array argument are appended, in order, to jaroslav@56: * the contents of this sequence. The length of this sequence jaroslav@56: * increases by the length of the argument. jaroslav@56: *

jaroslav@56: * The overall effect is exactly as if the argument were converted jaroslav@56: * to a string by the method {@link String#valueOf(char[])}, jaroslav@56: * and the characters of that string were then jaroslav@56: * {@link #append(String) appended} to this character sequence. jaroslav@56: * jaroslav@56: * @param str the characters to be appended. jaroslav@56: * @return a reference to this object. jaroslav@56: */ jaroslav@56: public AbstractStringBuilder append(char[] str) { jaroslav@56: int len = str.length; jaroslav@56: ensureCapacityInternal(count + len); jaroslav@104: arraycopy(str, 0, value, count, len); jaroslav@56: count += len; jaroslav@56: return this; jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Appends the string representation of a subarray of the jaroslav@56: * {@code char} array argument to this sequence. jaroslav@56: *

jaroslav@56: * Characters of the {@code char} array {@code str}, starting at jaroslav@56: * index {@code offset}, are appended, in order, to the contents jaroslav@56: * of this sequence. The length of this sequence increases jaroslav@56: * by the value of {@code len}. jaroslav@56: *

jaroslav@56: * The overall effect is exactly as if the arguments were converted jaroslav@56: * to a string by the method {@link String#valueOf(char[],int,int)}, jaroslav@56: * and the characters of that string were then jaroslav@56: * {@link #append(String) appended} to this character sequence. jaroslav@56: * jaroslav@56: * @param str the characters to be appended. jaroslav@56: * @param offset the index of the first {@code char} to append. jaroslav@56: * @param len the number of {@code char}s to append. jaroslav@56: * @return a reference to this object. jaroslav@56: * @throws IndexOutOfBoundsException jaroslav@56: * if {@code offset < 0} or {@code len < 0} jaroslav@56: * or {@code offset+len > str.length} jaroslav@56: */ jaroslav@56: public AbstractStringBuilder append(char str[], int offset, int len) { jaroslav@56: if (len > 0) // let arraycopy report AIOOBE for len < 0 jaroslav@56: ensureCapacityInternal(count + len); jaroslav@104: arraycopy(str, offset, value, count, len); jaroslav@56: count += len; jaroslav@56: return this; jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Appends the string representation of the {@code boolean} jaroslav@56: * argument to the sequence. jaroslav@56: *

jaroslav@56: * The overall effect is exactly as if the argument were converted jaroslav@56: * to a string by the method {@link String#valueOf(boolean)}, jaroslav@56: * and the characters of that string were then jaroslav@56: * {@link #append(String) appended} to this character sequence. jaroslav@56: * jaroslav@56: * @param b a {@code boolean}. jaroslav@56: * @return a reference to this object. jaroslav@56: */ jaroslav@56: public AbstractStringBuilder append(boolean b) { jaroslav@56: if (b) { jaroslav@56: ensureCapacityInternal(count + 4); jaroslav@56: value[count++] = 't'; jaroslav@56: value[count++] = 'r'; jaroslav@56: value[count++] = 'u'; jaroslav@56: value[count++] = 'e'; jaroslav@56: } else { jaroslav@56: ensureCapacityInternal(count + 5); jaroslav@56: value[count++] = 'f'; jaroslav@56: value[count++] = 'a'; jaroslav@56: value[count++] = 'l'; jaroslav@56: value[count++] = 's'; jaroslav@56: value[count++] = 'e'; jaroslav@56: } jaroslav@56: return this; jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Appends the string representation of the {@code char} jaroslav@56: * argument to this sequence. jaroslav@56: *

jaroslav@56: * The argument is appended to the contents of this sequence. jaroslav@56: * The length of this sequence increases by {@code 1}. jaroslav@56: *

jaroslav@56: * The overall effect is exactly as if the argument were converted jaroslav@56: * to a string by the method {@link String#valueOf(char)}, jaroslav@56: * and the character in that string were then jaroslav@56: * {@link #append(String) appended} to this character sequence. jaroslav@56: * jaroslav@56: * @param c a {@code char}. jaroslav@56: * @return a reference to this object. jaroslav@56: */ jaroslav@56: public AbstractStringBuilder append(char c) { jaroslav@56: ensureCapacityInternal(count + 1); jaroslav@56: value[count++] = c; jaroslav@56: return this; jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Appends the string representation of the {@code int} jaroslav@56: * argument to this sequence. jaroslav@56: *

jaroslav@56: * The overall effect is exactly as if the argument were converted jaroslav@56: * to a string by the method {@link String#valueOf(int)}, jaroslav@56: * and the characters of that string were then jaroslav@56: * {@link #append(String) appended} to this character sequence. jaroslav@56: * jaroslav@56: * @param i an {@code int}. jaroslav@56: * @return a reference to this object. jaroslav@56: */ jaroslav@94: @JavaScriptBody( jaroslav@94: args={ "self", "i" }, jaroslav@94: body="return java_lang_AbstractStringBuilder_appendLjava_lang_AbstractStringBuilderLjava_lang_String(self,i.toString());" jaroslav@94: ) jaroslav@56: public AbstractStringBuilder append(int i) { jaroslav@56: if (i == Integer.MIN_VALUE) { jaroslav@56: append("-2147483648"); jaroslav@56: return this; jaroslav@56: } jaroslav@56: int appendedLength = (i < 0) ? Integer.stringSize(-i) + 1 jaroslav@56: : Integer.stringSize(i); jaroslav@56: int spaceNeeded = count + appendedLength; jaroslav@56: ensureCapacityInternal(spaceNeeded); jaroslav@56: Integer.getChars(i, spaceNeeded, value); jaroslav@56: count = spaceNeeded; jaroslav@56: return this; jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Appends the string representation of the {@code long} jaroslav@56: * argument to this sequence. jaroslav@56: *

jaroslav@56: * The overall effect is exactly as if the argument were converted jaroslav@56: * to a string by the method {@link String#valueOf(long)}, jaroslav@56: * and the characters of that string were then jaroslav@56: * {@link #append(String) appended} to this character sequence. jaroslav@56: * jaroslav@56: * @param l a {@code long}. jaroslav@56: * @return a reference to this object. jaroslav@56: */ jaroslav@56: public AbstractStringBuilder append(long l) { jaroslav@56: if (l == Long.MIN_VALUE) { jaroslav@56: append("-9223372036854775808"); jaroslav@56: return this; jaroslav@56: } jaroslav@56: int appendedLength = (l < 0) ? Long.stringSize(-l) + 1 jaroslav@56: : Long.stringSize(l); jaroslav@56: int spaceNeeded = count + appendedLength; jaroslav@56: ensureCapacityInternal(spaceNeeded); jaroslav@56: Long.getChars(l, spaceNeeded, value); jaroslav@56: count = spaceNeeded; jaroslav@56: return this; jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Appends the string representation of the {@code float} jaroslav@56: * argument to this sequence. jaroslav@56: *

jaroslav@56: * The overall effect is exactly as if the argument were converted jaroslav@56: * to a string by the method {@link String#valueOf(float)}, jaroslav@56: * and the characters of that string were then jaroslav@56: * {@link #append(String) appended} to this character sequence. jaroslav@56: * jaroslav@56: * @param f a {@code float}. jaroslav@56: * @return a reference to this object. jaroslav@56: */ jaroslav@56: public AbstractStringBuilder append(float f) { jaroslav@116: return append(Float.toString(f)); jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Appends the string representation of the {@code double} jaroslav@56: * argument to this sequence. jaroslav@56: *

jaroslav@56: * The overall effect is exactly as if the argument were converted jaroslav@56: * to a string by the method {@link String#valueOf(double)}, jaroslav@56: * and the characters of that string were then jaroslav@56: * {@link #append(String) appended} to this character sequence. jaroslav@56: * jaroslav@56: * @param d a {@code double}. jaroslav@56: * @return a reference to this object. jaroslav@56: */ jaroslav@56: public AbstractStringBuilder append(double d) { jaroslav@116: return append(Double.toString(d)); jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Removes the characters in a substring of this sequence. jaroslav@56: * The substring begins at the specified {@code start} and extends to jaroslav@56: * the character at index {@code end - 1} or to the end of the jaroslav@56: * sequence if no such character exists. If jaroslav@56: * {@code start} is equal to {@code end}, no changes are made. jaroslav@56: * jaroslav@56: * @param start The beginning index, inclusive. jaroslav@56: * @param end The ending index, exclusive. jaroslav@56: * @return This object. jaroslav@56: * @throws StringIndexOutOfBoundsException if {@code start} jaroslav@56: * is negative, greater than {@code length()}, or jaroslav@56: * greater than {@code end}. jaroslav@56: */ jaroslav@56: public AbstractStringBuilder delete(int start, int end) { jaroslav@56: if (start < 0) jaroslav@56: throw new StringIndexOutOfBoundsException(start); jaroslav@56: if (end > count) jaroslav@56: end = count; jaroslav@56: if (start > end) jaroslav@56: throw new StringIndexOutOfBoundsException(); jaroslav@56: int len = end - start; jaroslav@56: if (len > 0) { jaroslav@104: arraycopy(value, start+len, value, start, count-end); jaroslav@56: count -= len; jaroslav@56: } jaroslav@56: return this; jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Appends the string representation of the {@code codePoint} jaroslav@56: * argument to this sequence. jaroslav@56: * jaroslav@56: *

The argument is appended to the contents of this sequence. jaroslav@56: * The length of this sequence increases by jaroslav@56: * {@link Character#charCount(int) Character.charCount(codePoint)}. jaroslav@56: * jaroslav@56: *

The overall effect is exactly as if the argument were jaroslav@56: * converted to a {@code char} array by the method jaroslav@56: * {@link Character#toChars(int)} and the character in that array jaroslav@56: * were then {@link #append(char[]) appended} to this character jaroslav@56: * sequence. jaroslav@56: * jaroslav@56: * @param codePoint a Unicode code point jaroslav@56: * @return a reference to this object. jaroslav@56: * @exception IllegalArgumentException if the specified jaroslav@56: * {@code codePoint} isn't a valid Unicode code point jaroslav@56: */ jaroslav@56: public AbstractStringBuilder appendCodePoint(int codePoint) { jaroslav@56: final int count = this.count; jaroslav@56: jaroslav@56: if (Character.isBmpCodePoint(codePoint)) { jaroslav@56: ensureCapacityInternal(count + 1); jaroslav@56: value[count] = (char) codePoint; jaroslav@56: this.count = count + 1; jaroslav@56: } else if (Character.isValidCodePoint(codePoint)) { jaroslav@56: ensureCapacityInternal(count + 2); jaroslav@56: Character.toSurrogates(codePoint, value, count); jaroslav@56: this.count = count + 2; jaroslav@56: } else { jaroslav@56: throw new IllegalArgumentException(); jaroslav@56: } jaroslav@56: return this; jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Removes the char at the specified position in this jaroslav@56: * sequence. This sequence is shortened by one char. jaroslav@56: * jaroslav@56: *

Note: If the character at the given index is a supplementary jaroslav@56: * character, this method does not remove the entire character. If jaroslav@56: * correct handling of supplementary characters is required, jaroslav@56: * determine the number of chars to remove by calling jaroslav@56: * Character.charCount(thisSequence.codePointAt(index)), jaroslav@56: * where thisSequence is this sequence. jaroslav@56: * jaroslav@56: * @param index Index of char to remove jaroslav@56: * @return This object. jaroslav@56: * @throws StringIndexOutOfBoundsException if the index jaroslav@56: * is negative or greater than or equal to jaroslav@56: * length(). jaroslav@56: */ jaroslav@56: public AbstractStringBuilder deleteCharAt(int index) { jaroslav@56: if ((index < 0) || (index >= count)) jaroslav@56: throw new StringIndexOutOfBoundsException(index); jaroslav@104: arraycopy(value, index+1, value, index, count-index-1); jaroslav@56: count--; jaroslav@56: return this; jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Replaces the characters in a substring of this sequence jaroslav@56: * with characters in the specified String. The substring jaroslav@56: * begins at the specified start and extends to the character jaroslav@56: * at index end - 1 or to the end of the jaroslav@56: * sequence if no such character exists. First the jaroslav@56: * characters in the substring are removed and then the specified jaroslav@56: * String is inserted at start. (This jaroslav@56: * sequence will be lengthened to accommodate the jaroslav@56: * specified String if necessary.) jaroslav@56: * jaroslav@56: * @param start The beginning index, inclusive. jaroslav@56: * @param end The ending index, exclusive. jaroslav@56: * @param str String that will replace previous contents. jaroslav@56: * @return This object. jaroslav@56: * @throws StringIndexOutOfBoundsException if start jaroslav@56: * is negative, greater than length(), or jaroslav@56: * greater than end. jaroslav@56: */ jaroslav@56: public AbstractStringBuilder replace(int start, int end, String str) { jaroslav@56: if (start < 0) jaroslav@56: throw new StringIndexOutOfBoundsException(start); jaroslav@56: if (start > count) jaroslav@56: throw new StringIndexOutOfBoundsException("start > length()"); jaroslav@56: if (start > end) jaroslav@56: throw new StringIndexOutOfBoundsException("start > end"); jaroslav@56: jaroslav@56: if (end > count) jaroslav@56: end = count; jaroslav@56: int len = str.length(); jaroslav@56: int newCount = count + len - (end - start); jaroslav@56: ensureCapacityInternal(newCount); jaroslav@56: jaroslav@104: arraycopy(value, end, value, start + len, count - end); jaroslav@56: str.getChars(value, start); jaroslav@56: count = newCount; jaroslav@56: return this; jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Returns a new String that contains a subsequence of jaroslav@56: * characters currently contained in this character sequence. The jaroslav@56: * substring begins at the specified index and extends to the end of jaroslav@56: * this sequence. jaroslav@56: * jaroslav@56: * @param start The beginning index, inclusive. jaroslav@56: * @return The new string. jaroslav@56: * @throws StringIndexOutOfBoundsException if start is jaroslav@56: * less than zero, or greater than the length of this object. jaroslav@56: */ jaroslav@56: public String substring(int start) { jaroslav@56: return substring(start, count); jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Returns a new character sequence that is a subsequence of this sequence. jaroslav@56: * jaroslav@56: *

An invocation of this method of the form jaroslav@56: * jaroslav@56: *

jaroslav@56:      * sb.subSequence(begin, end)
jaroslav@56: * jaroslav@56: * behaves in exactly the same way as the invocation jaroslav@56: * jaroslav@56: *
jaroslav@56:      * sb.substring(begin, end)
jaroslav@56: * jaroslav@56: * This method is provided so that this class can jaroslav@56: * implement the {@link CharSequence} interface.

jaroslav@56: * jaroslav@56: * @param start the start index, inclusive. jaroslav@56: * @param end the end index, exclusive. jaroslav@56: * @return the specified subsequence. jaroslav@56: * jaroslav@56: * @throws IndexOutOfBoundsException jaroslav@56: * if start or end are negative, jaroslav@56: * if end is greater than length(), jaroslav@56: * or if start is greater than end jaroslav@56: * @spec JSR-51 jaroslav@56: */ jaroslav@56: public CharSequence subSequence(int start, int end) { jaroslav@56: return substring(start, end); jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Returns a new String that contains a subsequence of jaroslav@56: * characters currently contained in this sequence. The jaroslav@56: * substring begins at the specified start and jaroslav@56: * extends to the character at index end - 1. jaroslav@56: * jaroslav@56: * @param start The beginning index, inclusive. jaroslav@56: * @param end The ending index, exclusive. jaroslav@56: * @return The new string. jaroslav@56: * @throws StringIndexOutOfBoundsException if start jaroslav@56: * or end are negative or greater than jaroslav@56: * length(), or start is jaroslav@56: * greater than end. jaroslav@56: */ jaroslav@56: public String substring(int start, int end) { jaroslav@56: if (start < 0) jaroslav@56: throw new StringIndexOutOfBoundsException(start); jaroslav@56: if (end > count) jaroslav@56: throw new StringIndexOutOfBoundsException(end); jaroslav@56: if (start > end) jaroslav@56: throw new StringIndexOutOfBoundsException(end - start); jaroslav@56: return new String(value, start, end - start); jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Inserts the string representation of a subarray of the {@code str} jaroslav@56: * array argument into this sequence. The subarray begins at the jaroslav@56: * specified {@code offset} and extends {@code len} {@code char}s. jaroslav@56: * The characters of the subarray are inserted into this sequence at jaroslav@56: * the position indicated by {@code index}. The length of this jaroslav@56: * sequence increases by {@code len} {@code char}s. jaroslav@56: * jaroslav@56: * @param index position at which to insert subarray. jaroslav@56: * @param str A {@code char} array. jaroslav@56: * @param offset the index of the first {@code char} in subarray to jaroslav@56: * be inserted. jaroslav@56: * @param len the number of {@code char}s in the subarray to jaroslav@56: * be inserted. jaroslav@56: * @return This object jaroslav@56: * @throws StringIndexOutOfBoundsException if {@code index} jaroslav@56: * is negative or greater than {@code length()}, or jaroslav@56: * {@code offset} or {@code len} are negative, or jaroslav@56: * {@code (offset+len)} is greater than jaroslav@56: * {@code str.length}. jaroslav@56: */ jaroslav@56: public AbstractStringBuilder insert(int index, char[] str, int offset, jaroslav@56: int len) jaroslav@56: { jaroslav@56: if ((index < 0) || (index > length())) jaroslav@56: throw new StringIndexOutOfBoundsException(index); jaroslav@56: if ((offset < 0) || (len < 0) || (offset > str.length - len)) jaroslav@56: throw new StringIndexOutOfBoundsException( jaroslav@56: "offset " + offset + ", len " + len + ", str.length " jaroslav@56: + str.length); jaroslav@56: ensureCapacityInternal(count + len); jaroslav@104: arraycopy(value, index, value, index + len, count - index); jaroslav@104: arraycopy(str, offset, value, index, len); jaroslav@56: count += len; jaroslav@56: return this; jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Inserts the string representation of the {@code Object} jaroslav@56: * argument into this character sequence. jaroslav@56: *

jaroslav@56: * The overall effect is exactly as if the second argument were jaroslav@56: * converted to a string by the method {@link String#valueOf(Object)}, jaroslav@56: * and the characters of that string were then jaroslav@56: * {@link #insert(int,String) inserted} into this character jaroslav@56: * sequence at the indicated offset. jaroslav@56: *

jaroslav@56: * The {@code offset} argument must be greater than or equal to jaroslav@56: * {@code 0}, and less than or equal to the {@linkplain #length() length} jaroslav@56: * of this sequence. jaroslav@56: * jaroslav@56: * @param offset the offset. jaroslav@56: * @param obj an {@code Object}. jaroslav@56: * @return a reference to this object. jaroslav@56: * @throws StringIndexOutOfBoundsException if the offset is invalid. jaroslav@56: */ jaroslav@56: public AbstractStringBuilder insert(int offset, Object obj) { jaroslav@56: return insert(offset, String.valueOf(obj)); jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Inserts the string into this character sequence. jaroslav@56: *

jaroslav@56: * The characters of the {@code String} argument are inserted, in jaroslav@56: * order, into this sequence at the indicated offset, moving up any jaroslav@56: * characters originally above that position and increasing the length jaroslav@56: * of this sequence by the length of the argument. If jaroslav@56: * {@code str} is {@code null}, then the four characters jaroslav@56: * {@code "null"} are inserted into this sequence. jaroslav@56: *

jaroslav@56: * The character at index k in the new character sequence is jaroslav@56: * equal to: jaroslav@56: *

jaroslav@56: * The {@code offset} argument must be greater than or equal to jaroslav@56: * {@code 0}, and less than or equal to the {@linkplain #length() length} jaroslav@56: * of this sequence. jaroslav@56: * jaroslav@56: * @param offset the offset. jaroslav@56: * @param str a string. jaroslav@56: * @return a reference to this object. jaroslav@56: * @throws StringIndexOutOfBoundsException if the offset is invalid. jaroslav@56: */ jaroslav@56: public AbstractStringBuilder insert(int offset, String str) { jaroslav@56: if ((offset < 0) || (offset > length())) jaroslav@56: throw new StringIndexOutOfBoundsException(offset); jaroslav@56: if (str == null) jaroslav@56: str = "null"; jaroslav@56: int len = str.length(); jaroslav@56: ensureCapacityInternal(count + len); jaroslav@104: arraycopy(value, offset, value, offset + len, count - offset); jaroslav@56: str.getChars(value, offset); jaroslav@56: count += len; jaroslav@56: return this; jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Inserts the string representation of the {@code char} array jaroslav@56: * argument into this sequence. jaroslav@56: *

jaroslav@56: * The characters of the array argument are inserted into the jaroslav@56: * contents of this sequence at the position indicated by jaroslav@56: * {@code offset}. The length of this sequence increases by jaroslav@56: * the length of the argument. jaroslav@56: *

jaroslav@56: * The overall effect is exactly as if the second argument were jaroslav@56: * converted to a string by the method {@link String#valueOf(char[])}, jaroslav@56: * and the characters of that string were then jaroslav@56: * {@link #insert(int,String) inserted} into this character jaroslav@56: * sequence at the indicated offset. jaroslav@56: *

jaroslav@56: * The {@code offset} argument must be greater than or equal to jaroslav@56: * {@code 0}, and less than or equal to the {@linkplain #length() length} jaroslav@56: * of this sequence. jaroslav@56: * jaroslav@56: * @param offset the offset. jaroslav@56: * @param str a character array. jaroslav@56: * @return a reference to this object. jaroslav@56: * @throws StringIndexOutOfBoundsException if the offset is invalid. jaroslav@56: */ jaroslav@56: public AbstractStringBuilder insert(int offset, char[] str) { jaroslav@56: if ((offset < 0) || (offset > length())) jaroslav@56: throw new StringIndexOutOfBoundsException(offset); jaroslav@56: int len = str.length; jaroslav@56: ensureCapacityInternal(count + len); jaroslav@104: arraycopy(value, offset, value, offset + len, count - offset); jaroslav@104: arraycopy(str, 0, value, offset, len); jaroslav@56: count += len; jaroslav@56: return this; jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Inserts the specified {@code CharSequence} into this sequence. jaroslav@56: *

jaroslav@56: * The characters of the {@code CharSequence} argument are inserted, jaroslav@56: * in order, into this sequence at the indicated offset, moving up jaroslav@56: * any characters originally above that position and increasing the length jaroslav@56: * of this sequence by the length of the argument s. jaroslav@56: *

jaroslav@56: * The result of this method is exactly the same as if it were an jaroslav@56: * invocation of this object's jaroslav@56: * {@link #insert(int,CharSequence,int,int) insert}(dstOffset, s, 0, s.length()) jaroslav@56: * method. jaroslav@56: * jaroslav@56: *

If {@code s} is {@code null}, then the four characters jaroslav@56: * {@code "null"} are inserted into this sequence. jaroslav@56: * jaroslav@56: * @param dstOffset the offset. jaroslav@56: * @param s the sequence to be inserted jaroslav@56: * @return a reference to this object. jaroslav@56: * @throws IndexOutOfBoundsException if the offset is invalid. jaroslav@56: */ jaroslav@56: public AbstractStringBuilder insert(int dstOffset, CharSequence s) { jaroslav@56: if (s == null) jaroslav@56: s = "null"; jaroslav@56: if (s instanceof String) jaroslav@56: return this.insert(dstOffset, (String)s); jaroslav@56: return this.insert(dstOffset, s, 0, s.length()); jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Inserts a subsequence of the specified {@code CharSequence} into jaroslav@56: * this sequence. jaroslav@56: *

jaroslav@56: * The subsequence of the argument {@code s} specified by jaroslav@56: * {@code start} and {@code end} are inserted, jaroslav@56: * in order, into this sequence at the specified destination offset, moving jaroslav@56: * up any characters originally above that position. The length of this jaroslav@56: * sequence is increased by {@code end - start}. jaroslav@56: *

jaroslav@56: * The character at index k in this sequence becomes equal to: jaroslav@56: *

jaroslav@56: * The {@code dstOffset} argument must be greater than or equal to jaroslav@56: * {@code 0}, and less than or equal to the {@linkplain #length() length} jaroslav@56: * of this sequence. jaroslav@56: *

The start argument must be nonnegative, and not greater than jaroslav@56: * {@code end}. jaroslav@56: *

The end argument must be greater than or equal to jaroslav@56: * {@code start}, and less than or equal to the length of s. jaroslav@56: * jaroslav@56: *

If {@code s} is {@code null}, then this method inserts jaroslav@56: * characters as if the s parameter was a sequence containing the four jaroslav@56: * characters {@code "null"}. jaroslav@56: * jaroslav@56: * @param dstOffset the offset in this sequence. jaroslav@56: * @param s the sequence to be inserted. jaroslav@56: * @param start the starting index of the subsequence to be inserted. jaroslav@56: * @param end the end index of the subsequence to be inserted. jaroslav@56: * @return a reference to this object. jaroslav@56: * @throws IndexOutOfBoundsException if {@code dstOffset} jaroslav@56: * is negative or greater than {@code this.length()}, or jaroslav@56: * {@code start} or {@code end} are negative, or jaroslav@56: * {@code start} is greater than {@code end} or jaroslav@56: * {@code end} is greater than {@code s.length()} jaroslav@56: */ jaroslav@56: public AbstractStringBuilder insert(int dstOffset, CharSequence s, jaroslav@56: int start, int end) { jaroslav@56: if (s == null) jaroslav@56: s = "null"; jaroslav@56: if ((dstOffset < 0) || (dstOffset > this.length())) jaroslav@56: throw new IndexOutOfBoundsException("dstOffset "+dstOffset); jaroslav@56: if ((start < 0) || (end < 0) || (start > end) || (end > s.length())) jaroslav@56: throw new IndexOutOfBoundsException( jaroslav@56: "start " + start + ", end " + end + ", s.length() " jaroslav@56: + s.length()); jaroslav@56: int len = end - start; jaroslav@56: ensureCapacityInternal(count + len); jaroslav@104: arraycopy(value, dstOffset, value, dstOffset + len, jaroslav@56: count - dstOffset); jaroslav@56: for (int i=start; i jaroslav@56: * The overall effect is exactly as if the second argument were jaroslav@56: * converted to a string by the method {@link String#valueOf(boolean)}, jaroslav@56: * and the characters of that string were then jaroslav@56: * {@link #insert(int,String) inserted} into this character jaroslav@56: * sequence at the indicated offset. jaroslav@56: *

jaroslav@56: * The {@code offset} argument must be greater than or equal to jaroslav@56: * {@code 0}, and less than or equal to the {@linkplain #length() length} jaroslav@56: * of this sequence. jaroslav@56: * jaroslav@56: * @param offset the offset. jaroslav@56: * @param b a {@code boolean}. jaroslav@56: * @return a reference to this object. jaroslav@56: * @throws StringIndexOutOfBoundsException if the offset is invalid. jaroslav@56: */ jaroslav@56: public AbstractStringBuilder insert(int offset, boolean b) { jaroslav@56: return insert(offset, String.valueOf(b)); jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Inserts the string representation of the {@code char} jaroslav@56: * argument into this sequence. jaroslav@56: *

jaroslav@56: * The overall effect is exactly as if the second argument were jaroslav@56: * converted to a string by the method {@link String#valueOf(char)}, jaroslav@56: * and the character in that string were then jaroslav@56: * {@link #insert(int,String) inserted} into this character jaroslav@56: * sequence at the indicated offset. jaroslav@56: *

jaroslav@56: * The {@code offset} argument must be greater than or equal to jaroslav@56: * {@code 0}, and less than or equal to the {@linkplain #length() length} jaroslav@56: * of this sequence. jaroslav@56: * jaroslav@56: * @param offset the offset. jaroslav@56: * @param c a {@code char}. jaroslav@56: * @return a reference to this object. jaroslav@56: * @throws IndexOutOfBoundsException if the offset is invalid. jaroslav@56: */ jaroslav@56: public AbstractStringBuilder insert(int offset, char c) { jaroslav@56: ensureCapacityInternal(count + 1); jaroslav@104: arraycopy(value, offset, value, offset + 1, count - offset); jaroslav@56: value[offset] = c; jaroslav@56: count += 1; jaroslav@56: return this; jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Inserts the string representation of the second {@code int} jaroslav@56: * argument into this sequence. jaroslav@56: *

jaroslav@56: * The overall effect is exactly as if the second argument were jaroslav@56: * converted to a string by the method {@link String#valueOf(int)}, jaroslav@56: * and the characters of that string were then jaroslav@56: * {@link #insert(int,String) inserted} into this character jaroslav@56: * sequence at the indicated offset. jaroslav@56: *

jaroslav@56: * The {@code offset} argument must be greater than or equal to jaroslav@56: * {@code 0}, and less than or equal to the {@linkplain #length() length} jaroslav@56: * of this sequence. jaroslav@56: * jaroslav@56: * @param offset the offset. jaroslav@56: * @param i an {@code int}. jaroslav@56: * @return a reference to this object. jaroslav@56: * @throws StringIndexOutOfBoundsException if the offset is invalid. jaroslav@56: */ jaroslav@56: public AbstractStringBuilder insert(int offset, int i) { jaroslav@56: return insert(offset, String.valueOf(i)); jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Inserts the string representation of the {@code long} jaroslav@56: * argument into this sequence. jaroslav@56: *

jaroslav@56: * The overall effect is exactly as if the second argument were jaroslav@56: * converted to a string by the method {@link String#valueOf(long)}, jaroslav@56: * and the characters of that string were then jaroslav@56: * {@link #insert(int,String) inserted} into this character jaroslav@56: * sequence at the indicated offset. jaroslav@56: *

jaroslav@56: * The {@code offset} argument must be greater than or equal to jaroslav@56: * {@code 0}, and less than or equal to the {@linkplain #length() length} jaroslav@56: * of this sequence. jaroslav@56: * jaroslav@56: * @param offset the offset. jaroslav@56: * @param l a {@code long}. jaroslav@56: * @return a reference to this object. jaroslav@56: * @throws StringIndexOutOfBoundsException if the offset is invalid. jaroslav@56: */ jaroslav@56: public AbstractStringBuilder insert(int offset, long l) { jaroslav@56: return insert(offset, String.valueOf(l)); jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Inserts the string representation of the {@code float} jaroslav@56: * argument into this sequence. jaroslav@56: *

jaroslav@56: * The overall effect is exactly as if the second argument were jaroslav@56: * converted to a string by the method {@link String#valueOf(float)}, jaroslav@56: * and the characters of that string were then jaroslav@56: * {@link #insert(int,String) inserted} into this character jaroslav@56: * sequence at the indicated offset. jaroslav@56: *

jaroslav@56: * The {@code offset} argument must be greater than or equal to jaroslav@56: * {@code 0}, and less than or equal to the {@linkplain #length() length} jaroslav@56: * of this sequence. jaroslav@56: * jaroslav@56: * @param offset the offset. jaroslav@56: * @param f a {@code float}. jaroslav@56: * @return a reference to this object. jaroslav@56: * @throws StringIndexOutOfBoundsException if the offset is invalid. jaroslav@56: */ jaroslav@56: public AbstractStringBuilder insert(int offset, float f) { jaroslav@56: return insert(offset, String.valueOf(f)); jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Inserts the string representation of the {@code double} jaroslav@56: * argument into this sequence. jaroslav@56: *

jaroslav@56: * The overall effect is exactly as if the second argument were jaroslav@56: * converted to a string by the method {@link String#valueOf(double)}, jaroslav@56: * and the characters of that string were then jaroslav@56: * {@link #insert(int,String) inserted} into this character jaroslav@56: * sequence at the indicated offset. jaroslav@56: *

jaroslav@56: * The {@code offset} argument must be greater than or equal to jaroslav@56: * {@code 0}, and less than or equal to the {@linkplain #length() length} jaroslav@56: * of this sequence. jaroslav@56: * jaroslav@56: * @param offset the offset. jaroslav@56: * @param d a {@code double}. jaroslav@56: * @return a reference to this object. jaroslav@56: * @throws StringIndexOutOfBoundsException if the offset is invalid. jaroslav@56: */ jaroslav@56: public AbstractStringBuilder insert(int offset, double d) { jaroslav@56: return insert(offset, String.valueOf(d)); jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Returns the index within this string of the first occurrence of the jaroslav@56: * specified substring. The integer returned is the smallest value jaroslav@56: * k such that: jaroslav@56: *

jaroslav@56:      * this.toString().startsWith(str, k)
jaroslav@56:      * 
jaroslav@56: * is true. jaroslav@56: * jaroslav@56: * @param str any string. jaroslav@56: * @return if the string argument occurs as a substring within this jaroslav@56: * object, then the index of the first character of the first jaroslav@56: * such substring is returned; if it does not occur as a jaroslav@56: * substring, -1 is returned. jaroslav@56: * @throws java.lang.NullPointerException if str is jaroslav@56: * null. jaroslav@56: */ jaroslav@56: public int indexOf(String str) { jaroslav@56: return indexOf(str, 0); jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Returns the index within this string of the first occurrence of the jaroslav@56: * specified substring, starting at the specified index. The integer jaroslav@56: * returned is the smallest value k for which: jaroslav@56: *
jaroslav@56:      *     k >= Math.min(fromIndex, str.length()) &&
jaroslav@56:      *                   this.toString().startsWith(str, k)
jaroslav@56:      * 
jaroslav@56: * If no such value of k exists, then -1 is returned. jaroslav@56: * jaroslav@56: * @param str the substring for which to search. jaroslav@56: * @param fromIndex the index from which to start the search. jaroslav@56: * @return the index within this string of the first occurrence of the jaroslav@56: * specified substring, starting at the specified index. jaroslav@56: * @throws java.lang.NullPointerException if str is jaroslav@56: * null. jaroslav@56: */ jaroslav@56: public int indexOf(String str, int fromIndex) { jaroslav@56: return String.indexOf(value, 0, count, jaroslav@56: str.toCharArray(), 0, str.length(), fromIndex); jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Returns the index within this string of the rightmost occurrence jaroslav@56: * of the specified substring. The rightmost empty string "" is jaroslav@56: * considered to occur at the index value this.length(). jaroslav@56: * The returned index is the largest value k such that jaroslav@56: *
jaroslav@56:      * this.toString().startsWith(str, k)
jaroslav@56:      * 
jaroslav@56: * is true. jaroslav@56: * jaroslav@56: * @param str the substring to search for. jaroslav@56: * @return if the string argument occurs one or more times as a substring jaroslav@56: * within this object, then the index of the first character of jaroslav@56: * the last such substring is returned. If it does not occur as jaroslav@56: * a substring, -1 is returned. jaroslav@56: * @throws java.lang.NullPointerException if str is jaroslav@56: * null. jaroslav@56: */ jaroslav@56: public int lastIndexOf(String str) { jaroslav@56: return lastIndexOf(str, count); jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Returns the index within this string of the last occurrence of the jaroslav@56: * specified substring. The integer returned is the largest value k jaroslav@56: * such that: jaroslav@56: *
jaroslav@56:      *     k <= Math.min(fromIndex, str.length()) &&
jaroslav@56:      *                   this.toString().startsWith(str, k)
jaroslav@56:      * 
jaroslav@56: * If no such value of k exists, then -1 is returned. jaroslav@56: * jaroslav@56: * @param str the substring to search for. jaroslav@56: * @param fromIndex the index to start the search from. jaroslav@56: * @return the index within this sequence of the last occurrence of the jaroslav@56: * specified substring. jaroslav@56: * @throws java.lang.NullPointerException if str is jaroslav@56: * null. jaroslav@56: */ jaroslav@56: public int lastIndexOf(String str, int fromIndex) { jaroslav@56: return String.lastIndexOf(value, 0, count, jaroslav@56: str.toCharArray(), 0, str.length(), fromIndex); jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Causes this character sequence to be replaced by the reverse of jaroslav@56: * the sequence. If there are any surrogate pairs included in the jaroslav@56: * sequence, these are treated as single characters for the jaroslav@56: * reverse operation. Thus, the order of the high-low surrogates jaroslav@56: * is never reversed. jaroslav@56: * jaroslav@56: * Let n be the character length of this character sequence jaroslav@56: * (not the length in char values) just prior to jaroslav@56: * execution of the reverse method. Then the jaroslav@56: * character at index k in the new character sequence is jaroslav@56: * equal to the character at index n-k-1 in the old jaroslav@56: * character sequence. jaroslav@56: * jaroslav@56: *

Note that the reverse operation may result in producing jaroslav@56: * surrogate pairs that were unpaired low-surrogates and jaroslav@56: * high-surrogates before the operation. For example, reversing jaroslav@56: * "\uDC00\uD800" produces "\uD800\uDC00" which is jaroslav@56: * a valid surrogate pair. jaroslav@56: * jaroslav@56: * @return a reference to this object. jaroslav@56: */ jaroslav@56: public AbstractStringBuilder reverse() { jaroslav@56: boolean hasSurrogate = false; jaroslav@56: int n = count - 1; jaroslav@56: for (int j = (n-1) >> 1; j >= 0; --j) { jaroslav@56: char temp = value[j]; jaroslav@56: char temp2 = value[n - j]; jaroslav@56: if (!hasSurrogate) { jaroslav@56: hasSurrogate = (temp >= Character.MIN_SURROGATE && temp <= Character.MAX_SURROGATE) jaroslav@56: || (temp2 >= Character.MIN_SURROGATE && temp2 <= Character.MAX_SURROGATE); jaroslav@56: } jaroslav@56: value[j] = temp2; jaroslav@56: value[n - j] = temp; jaroslav@56: } jaroslav@56: if (hasSurrogate) { jaroslav@56: // Reverse back all valid surrogate pairs jaroslav@56: for (int i = 0; i < count - 1; i++) { jaroslav@56: char c2 = value[i]; jaroslav@56: if (Character.isLowSurrogate(c2)) { jaroslav@56: char c1 = value[i + 1]; jaroslav@56: if (Character.isHighSurrogate(c1)) { jaroslav@56: value[i++] = c1; jaroslav@56: value[i] = c2; jaroslav@56: } jaroslav@56: } jaroslav@56: } jaroslav@56: } jaroslav@56: return this; jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Returns a string representing the data in this sequence. jaroslav@56: * A new String object is allocated and initialized to jaroslav@56: * contain the character sequence currently represented by this jaroslav@56: * object. This String is then returned. Subsequent jaroslav@56: * changes to this sequence do not affect the contents of the jaroslav@56: * String. jaroslav@56: * jaroslav@56: * @return a string representation of this sequence of characters. jaroslav@56: */ jaroslav@56: public abstract String toString(); jaroslav@56: jaroslav@56: /** jaroslav@56: * Needed by String for the contentEquals method. jaroslav@56: */ jaroslav@56: final char[] getValue() { jaroslav@56: return value; jaroslav@56: } jaroslav@56: jaroslav@104: static char[] copyOfRange(char[] original, int from, int to) { jaroslav@104: int newLength = to - from; jaroslav@104: if (newLength < 0) { jaroslav@104: throw new IllegalArgumentException(from + " > " + to); jaroslav@104: } jaroslav@104: char[] copy = new char[newLength]; jaroslav@104: arraycopy(original, from, copy, 0, Math.min(original.length - from, newLength)); jaroslav@104: return copy; jaroslav@104: } jaroslav@104: jaroslav@104: static void arraycopy(char[] value, int srcBegin, char[] dst, int dstBegin, int count) { jaroslav@104: while (count-- > 0) { jaroslav@188: dst[dstBegin + count] = value[srcBegin + count]; jaroslav@104: } jaroslav@104: } jaroslav@104: jaroslav@104: // access system property jaroslav@104: static String getProperty(String nm) { jaroslav@104: return null; jaroslav@104: } jaroslav@104: jaroslav@104: static char[] copyOf(char[] original, int newLength) { jaroslav@104: char[] copy = new char[newLength]; jaroslav@104: arraycopy(original, 0, copy, 0, Math.min(original.length, newLength)); jaroslav@104: return copy; jaroslav@104: } jaroslav@104: jaroslav@56: }