1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/emul/mini/src/main/java/java/lang/AbstractStringBuilder.java Wed Jan 23 20:39:23 2013 +0100
1.3 @@ -0,0 +1,1424 @@
1.4 +/*
1.5 + * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 + *
1.8 + * This code is free software; you can redistribute it and/or modify it
1.9 + * under the terms of the GNU General Public License version 2 only, as
1.10 + * published by the Free Software Foundation. Oracle designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Oracle in the LICENSE file that accompanied this code.
1.13 + *
1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 + * version 2 for more details (a copy is included in the LICENSE file that
1.18 + * accompanied this code).
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License version
1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 + *
1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 + * or visit www.oracle.com if you need additional information or have any
1.26 + * questions.
1.27 + */
1.28 +
1.29 +package java.lang;
1.30 +
1.31 +/**
1.32 + * A mutable sequence of characters.
1.33 + * <p>
1.34 + * Implements a modifiable string. At any point in time it contains some
1.35 + * particular sequence of characters, but the length and content of the
1.36 + * sequence can be changed through certain method calls.
1.37 + *
1.38 + * @author Michael McCloskey
1.39 + * @author Martin Buchholz
1.40 + * @author Ulf Zibis
1.41 + * @since 1.5
1.42 + */
1.43 +abstract class AbstractStringBuilder implements Appendable, CharSequence {
1.44 + /**
1.45 + * The value is used for character storage.
1.46 + */
1.47 + char[] value;
1.48 +
1.49 + /**
1.50 + * The count is the number of characters used.
1.51 + */
1.52 + int count;
1.53 +
1.54 + /**
1.55 + * This no-arg constructor is necessary for serialization of subclasses.
1.56 + */
1.57 + AbstractStringBuilder() {
1.58 + }
1.59 +
1.60 + /**
1.61 + * Creates an AbstractStringBuilder of the specified capacity.
1.62 + */
1.63 + AbstractStringBuilder(int capacity) {
1.64 + value = new char[capacity];
1.65 + }
1.66 +
1.67 + /**
1.68 + * Returns the length (character count).
1.69 + *
1.70 + * @return the length of the sequence of characters currently
1.71 + * represented by this object
1.72 + */
1.73 + public int length() {
1.74 + return count;
1.75 + }
1.76 +
1.77 + /**
1.78 + * Returns the current capacity. The capacity is the amount of storage
1.79 + * available for newly inserted characters, beyond which an allocation
1.80 + * will occur.
1.81 + *
1.82 + * @return the current capacity
1.83 + */
1.84 + public int capacity() {
1.85 + return value.length;
1.86 + }
1.87 +
1.88 + /**
1.89 + * Ensures that the capacity is at least equal to the specified minimum.
1.90 + * If the current capacity is less than the argument, then a new internal
1.91 + * array is allocated with greater capacity. The new capacity is the
1.92 + * larger of:
1.93 + * <ul>
1.94 + * <li>The <code>minimumCapacity</code> argument.
1.95 + * <li>Twice the old capacity, plus <code>2</code>.
1.96 + * </ul>
1.97 + * If the <code>minimumCapacity</code> argument is nonpositive, this
1.98 + * method takes no action and simply returns.
1.99 + *
1.100 + * @param minimumCapacity the minimum desired capacity.
1.101 + */
1.102 + public void ensureCapacity(int minimumCapacity) {
1.103 + if (minimumCapacity > 0)
1.104 + ensureCapacityInternal(minimumCapacity);
1.105 + }
1.106 +
1.107 + /**
1.108 + * This method has the same contract as ensureCapacity, but is
1.109 + * never synchronized.
1.110 + */
1.111 + private void ensureCapacityInternal(int minimumCapacity) {
1.112 + // overflow-conscious code
1.113 + if (minimumCapacity - value.length > 0)
1.114 + expandCapacity(minimumCapacity);
1.115 + }
1.116 +
1.117 + /**
1.118 + * This implements the expansion semantics of ensureCapacity with no
1.119 + * size check or synchronization.
1.120 + */
1.121 + void expandCapacity(int minimumCapacity) {
1.122 + int newCapacity = value.length * 2 + 2;
1.123 + if (newCapacity - minimumCapacity < 0)
1.124 + newCapacity = minimumCapacity;
1.125 + if (newCapacity < 0) {
1.126 + if (minimumCapacity < 0) // overflow
1.127 + throw new OutOfMemoryError();
1.128 + newCapacity = Integer.MAX_VALUE;
1.129 + }
1.130 + value = copyOf(value, newCapacity);
1.131 + }
1.132 +
1.133 + /**
1.134 + * Attempts to reduce storage used for the character sequence.
1.135 + * If the buffer is larger than necessary to hold its current sequence of
1.136 + * characters, then it may be resized to become more space efficient.
1.137 + * Calling this method may, but is not required to, affect the value
1.138 + * returned by a subsequent call to the {@link #capacity()} method.
1.139 + */
1.140 + public void trimToSize() {
1.141 + if (count < value.length) {
1.142 + value = copyOf(value, count);
1.143 + }
1.144 + }
1.145 +
1.146 + /**
1.147 + * Sets the length of the character sequence.
1.148 + * The sequence is changed to a new character sequence
1.149 + * whose length is specified by the argument. For every nonnegative
1.150 + * index <i>k</i> less than <code>newLength</code>, the character at
1.151 + * index <i>k</i> in the new character sequence is the same as the
1.152 + * character at index <i>k</i> in the old sequence if <i>k</i> is less
1.153 + * than the length of the old character sequence; otherwise, it is the
1.154 + * null character <code>'\u0000'</code>.
1.155 + *
1.156 + * In other words, if the <code>newLength</code> argument is less than
1.157 + * the current length, the length is changed to the specified length.
1.158 + * <p>
1.159 + * If the <code>newLength</code> argument is greater than or equal
1.160 + * to the current length, sufficient null characters
1.161 + * (<code>'\u0000'</code>) are appended so that
1.162 + * length becomes the <code>newLength</code> argument.
1.163 + * <p>
1.164 + * The <code>newLength</code> argument must be greater than or equal
1.165 + * to <code>0</code>.
1.166 + *
1.167 + * @param newLength the new length
1.168 + * @throws IndexOutOfBoundsException if the
1.169 + * <code>newLength</code> argument is negative.
1.170 + */
1.171 + public void setLength(int newLength) {
1.172 + if (newLength < 0)
1.173 + throw new StringIndexOutOfBoundsException(newLength);
1.174 + ensureCapacityInternal(newLength);
1.175 +
1.176 + if (count < newLength) {
1.177 + for (; count < newLength; count++)
1.178 + value[count] = '\0';
1.179 + } else {
1.180 + count = newLength;
1.181 + }
1.182 + }
1.183 +
1.184 + /**
1.185 + * Returns the <code>char</code> value in this sequence at the specified index.
1.186 + * The first <code>char</code> value is at index <code>0</code>, the next at index
1.187 + * <code>1</code>, and so on, as in array indexing.
1.188 + * <p>
1.189 + * The index argument must be greater than or equal to
1.190 + * <code>0</code>, and less than the length of this sequence.
1.191 + *
1.192 + * <p>If the <code>char</code> value specified by the index is a
1.193 + * <a href="Character.html#unicode">surrogate</a>, the surrogate
1.194 + * value is returned.
1.195 + *
1.196 + * @param index the index of the desired <code>char</code> value.
1.197 + * @return the <code>char</code> value at the specified index.
1.198 + * @throws IndexOutOfBoundsException if <code>index</code> is
1.199 + * negative or greater than or equal to <code>length()</code>.
1.200 + */
1.201 + public char charAt(int index) {
1.202 + if ((index < 0) || (index >= count))
1.203 + throw new StringIndexOutOfBoundsException(index);
1.204 + return value[index];
1.205 + }
1.206 +
1.207 + /**
1.208 + * Returns the character (Unicode code point) at the specified
1.209 + * index. The index refers to <code>char</code> values
1.210 + * (Unicode code units) and ranges from <code>0</code> to
1.211 + * {@link #length()}<code> - 1</code>.
1.212 + *
1.213 + * <p> If the <code>char</code> value specified at the given index
1.214 + * is in the high-surrogate range, the following index is less
1.215 + * than the length of this sequence, and the
1.216 + * <code>char</code> value at the following index is in the
1.217 + * low-surrogate range, then the supplementary code point
1.218 + * corresponding to this surrogate pair is returned. Otherwise,
1.219 + * the <code>char</code> value at the given index is returned.
1.220 + *
1.221 + * @param index the index to the <code>char</code> values
1.222 + * @return the code point value of the character at the
1.223 + * <code>index</code>
1.224 + * @exception IndexOutOfBoundsException if the <code>index</code>
1.225 + * argument is negative or not less than the length of this
1.226 + * sequence.
1.227 + */
1.228 + public int codePointAt(int index) {
1.229 + if ((index < 0) || (index >= count)) {
1.230 + throw new StringIndexOutOfBoundsException(index);
1.231 + }
1.232 + return Character.codePointAt(value, index);
1.233 + }
1.234 +
1.235 + /**
1.236 + * Returns the character (Unicode code point) before the specified
1.237 + * index. The index refers to <code>char</code> values
1.238 + * (Unicode code units) and ranges from <code>1</code> to {@link
1.239 + * #length()}.
1.240 + *
1.241 + * <p> If the <code>char</code> value at <code>(index - 1)</code>
1.242 + * is in the low-surrogate range, <code>(index - 2)</code> is not
1.243 + * negative, and the <code>char</code> value at <code>(index -
1.244 + * 2)</code> is in the high-surrogate range, then the
1.245 + * supplementary code point value of the surrogate pair is
1.246 + * returned. If the <code>char</code> value at <code>index -
1.247 + * 1</code> is an unpaired low-surrogate or a high-surrogate, the
1.248 + * surrogate value is returned.
1.249 + *
1.250 + * @param index the index following the code point that should be returned
1.251 + * @return the Unicode code point value before the given index.
1.252 + * @exception IndexOutOfBoundsException if the <code>index</code>
1.253 + * argument is less than 1 or greater than the length
1.254 + * of this sequence.
1.255 + */
1.256 + public int codePointBefore(int index) {
1.257 + int i = index - 1;
1.258 + if ((i < 0) || (i >= count)) {
1.259 + throw new StringIndexOutOfBoundsException(index);
1.260 + }
1.261 + return Character.codePointBefore(value, index);
1.262 + }
1.263 +
1.264 + /**
1.265 + * Returns the number of Unicode code points in the specified text
1.266 + * range of this sequence. The text range begins at the specified
1.267 + * <code>beginIndex</code> and extends to the <code>char</code> at
1.268 + * index <code>endIndex - 1</code>. Thus the length (in
1.269 + * <code>char</code>s) of the text range is
1.270 + * <code>endIndex-beginIndex</code>. Unpaired surrogates within
1.271 + * this sequence count as one code point each.
1.272 + *
1.273 + * @param beginIndex the index to the first <code>char</code> of
1.274 + * the text range.
1.275 + * @param endIndex the index after the last <code>char</code> of
1.276 + * the text range.
1.277 + * @return the number of Unicode code points in the specified text
1.278 + * range
1.279 + * @exception IndexOutOfBoundsException if the
1.280 + * <code>beginIndex</code> is negative, or <code>endIndex</code>
1.281 + * is larger than the length of this sequence, or
1.282 + * <code>beginIndex</code> is larger than <code>endIndex</code>.
1.283 + */
1.284 + public int codePointCount(int beginIndex, int endIndex) {
1.285 + if (beginIndex < 0 || endIndex > count || beginIndex > endIndex) {
1.286 + throw new IndexOutOfBoundsException();
1.287 + }
1.288 + return Character.codePointCountImpl(value, beginIndex, endIndex-beginIndex);
1.289 + }
1.290 +
1.291 + /**
1.292 + * Returns the index within this sequence that is offset from the
1.293 + * given <code>index</code> by <code>codePointOffset</code> code
1.294 + * points. Unpaired surrogates within the text range given by
1.295 + * <code>index</code> and <code>codePointOffset</code> count as
1.296 + * one code point each.
1.297 + *
1.298 + * @param index the index to be offset
1.299 + * @param codePointOffset the offset in code points
1.300 + * @return the index within this sequence
1.301 + * @exception IndexOutOfBoundsException if <code>index</code>
1.302 + * is negative or larger then the length of this sequence,
1.303 + * or if <code>codePointOffset</code> is positive and the subsequence
1.304 + * starting with <code>index</code> has fewer than
1.305 + * <code>codePointOffset</code> code points,
1.306 + * or if <code>codePointOffset</code> is negative and the subsequence
1.307 + * before <code>index</code> has fewer than the absolute value of
1.308 + * <code>codePointOffset</code> code points.
1.309 + */
1.310 + public int offsetByCodePoints(int index, int codePointOffset) {
1.311 + if (index < 0 || index > count) {
1.312 + throw new IndexOutOfBoundsException();
1.313 + }
1.314 + return Character.offsetByCodePointsImpl(value, 0, count,
1.315 + index, codePointOffset);
1.316 + }
1.317 +
1.318 + /**
1.319 + * Characters are copied from this sequence into the
1.320 + * destination character array <code>dst</code>. The first character to
1.321 + * be copied is at index <code>srcBegin</code>; the last character to
1.322 + * be copied is at index <code>srcEnd-1</code>. The total number of
1.323 + * characters to be copied is <code>srcEnd-srcBegin</code>. The
1.324 + * characters are copied into the subarray of <code>dst</code> starting
1.325 + * at index <code>dstBegin</code> and ending at index:
1.326 + * <p><blockquote><pre>
1.327 + * dstbegin + (srcEnd-srcBegin) - 1
1.328 + * </pre></blockquote>
1.329 + *
1.330 + * @param srcBegin start copying at this offset.
1.331 + * @param srcEnd stop copying at this offset.
1.332 + * @param dst the array to copy the data into.
1.333 + * @param dstBegin offset into <code>dst</code>.
1.334 + * @throws NullPointerException if <code>dst</code> is
1.335 + * <code>null</code>.
1.336 + * @throws IndexOutOfBoundsException if any of the following is true:
1.337 + * <ul>
1.338 + * <li><code>srcBegin</code> is negative
1.339 + * <li><code>dstBegin</code> is negative
1.340 + * <li>the <code>srcBegin</code> argument is greater than
1.341 + * the <code>srcEnd</code> argument.
1.342 + * <li><code>srcEnd</code> is greater than
1.343 + * <code>this.length()</code>.
1.344 + * <li><code>dstBegin+srcEnd-srcBegin</code> is greater than
1.345 + * <code>dst.length</code>
1.346 + * </ul>
1.347 + */
1.348 + public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
1.349 + {
1.350 + if (srcBegin < 0)
1.351 + throw new StringIndexOutOfBoundsException(srcBegin);
1.352 + if ((srcEnd < 0) || (srcEnd > count))
1.353 + throw new StringIndexOutOfBoundsException(srcEnd);
1.354 + if (srcBegin > srcEnd)
1.355 + throw new StringIndexOutOfBoundsException("srcBegin > srcEnd");
1.356 + arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
1.357 + }
1.358 +
1.359 + /**
1.360 + * The character at the specified index is set to <code>ch</code>. This
1.361 + * sequence is altered to represent a new character sequence that is
1.362 + * identical to the old character sequence, except that it contains the
1.363 + * character <code>ch</code> at position <code>index</code>.
1.364 + * <p>
1.365 + * The index argument must be greater than or equal to
1.366 + * <code>0</code>, and less than the length of this sequence.
1.367 + *
1.368 + * @param index the index of the character to modify.
1.369 + * @param ch the new character.
1.370 + * @throws IndexOutOfBoundsException if <code>index</code> is
1.371 + * negative or greater than or equal to <code>length()</code>.
1.372 + */
1.373 + public void setCharAt(int index, char ch) {
1.374 + if ((index < 0) || (index >= count))
1.375 + throw new StringIndexOutOfBoundsException(index);
1.376 + value[index] = ch;
1.377 + }
1.378 +
1.379 + /**
1.380 + * Appends the string representation of the {@code Object} argument.
1.381 + * <p>
1.382 + * The overall effect is exactly as if the argument were converted
1.383 + * to a string by the method {@link String#valueOf(Object)},
1.384 + * and the characters of that string were then
1.385 + * {@link #append(String) appended} to this character sequence.
1.386 + *
1.387 + * @param obj an {@code Object}.
1.388 + * @return a reference to this object.
1.389 + */
1.390 + public AbstractStringBuilder append(Object obj) {
1.391 + return append(String.valueOf(obj));
1.392 + }
1.393 +
1.394 + /**
1.395 + * Appends the specified string to this character sequence.
1.396 + * <p>
1.397 + * The characters of the {@code String} argument are appended, in
1.398 + * order, increasing the length of this sequence by the length of the
1.399 + * argument. If {@code str} is {@code null}, then the four
1.400 + * characters {@code "null"} are appended.
1.401 + * <p>
1.402 + * Let <i>n</i> be the length of this character sequence just prior to
1.403 + * execution of the {@code append} method. Then the character at
1.404 + * index <i>k</i> in the new character sequence is equal to the character
1.405 + * at index <i>k</i> in the old character sequence, if <i>k</i> is less
1.406 + * than <i>n</i>; otherwise, it is equal to the character at index
1.407 + * <i>k-n</i> in the argument {@code str}.
1.408 + *
1.409 + * @param str a string.
1.410 + * @return a reference to this object.
1.411 + */
1.412 + public AbstractStringBuilder append(String str) {
1.413 + if (str == null) str = "null";
1.414 + int len = str.length();
1.415 + ensureCapacityInternal(count + len);
1.416 + str.getChars(0, len, value, count);
1.417 + count += len;
1.418 + return this;
1.419 + }
1.420 +
1.421 + // Documentation in subclasses because of synchro difference
1.422 + public AbstractStringBuilder append(StringBuffer sb) {
1.423 + if (sb == null)
1.424 + return append("null");
1.425 + int len = sb.length();
1.426 + ensureCapacityInternal(count + len);
1.427 + sb.getChars(0, len, value, count);
1.428 + count += len;
1.429 + return this;
1.430 + }
1.431 +
1.432 + // Documentation in subclasses because of synchro difference
1.433 + public AbstractStringBuilder append(CharSequence s) {
1.434 + if (s == null)
1.435 + s = "null";
1.436 + if (s instanceof String)
1.437 + return this.append((String)s);
1.438 + if (s instanceof StringBuffer)
1.439 + return this.append((StringBuffer)s);
1.440 + return this.append(s, 0, s.length());
1.441 + }
1.442 +
1.443 + /**
1.444 + * Appends a subsequence of the specified {@code CharSequence} to this
1.445 + * sequence.
1.446 + * <p>
1.447 + * Characters of the argument {@code s}, starting at
1.448 + * index {@code start}, are appended, in order, to the contents of
1.449 + * this sequence up to the (exclusive) index {@code end}. The length
1.450 + * of this sequence is increased by the value of {@code end - start}.
1.451 + * <p>
1.452 + * Let <i>n</i> be the length of this character sequence just prior to
1.453 + * execution of the {@code append} method. Then the character at
1.454 + * index <i>k</i> in this character sequence becomes equal to the
1.455 + * character at index <i>k</i> in this sequence, if <i>k</i> is less than
1.456 + * <i>n</i>; otherwise, it is equal to the character at index
1.457 + * <i>k+start-n</i> in the argument {@code s}.
1.458 + * <p>
1.459 + * If {@code s} is {@code null}, then this method appends
1.460 + * characters as if the s parameter was a sequence containing the four
1.461 + * characters {@code "null"}.
1.462 + *
1.463 + * @param s the sequence to append.
1.464 + * @param start the starting index of the subsequence to be appended.
1.465 + * @param end the end index of the subsequence to be appended.
1.466 + * @return a reference to this object.
1.467 + * @throws IndexOutOfBoundsException if
1.468 + * {@code start} is negative, or
1.469 + * {@code start} is greater than {@code end} or
1.470 + * {@code end} is greater than {@code s.length()}
1.471 + */
1.472 + public AbstractStringBuilder append(CharSequence s, int start, int end) {
1.473 + if (s == null)
1.474 + s = "null";
1.475 + if ((start < 0) || (start > end) || (end > s.length()))
1.476 + throw new IndexOutOfBoundsException(
1.477 + "start " + start + ", end " + end + ", s.length() "
1.478 + + s.length());
1.479 + int len = end - start;
1.480 + ensureCapacityInternal(count + len);
1.481 + for (int i = start, j = count; i < end; i++, j++)
1.482 + value[j] = s.charAt(i);
1.483 + count += len;
1.484 + return this;
1.485 + }
1.486 +
1.487 + /**
1.488 + * Appends the string representation of the {@code char} array
1.489 + * argument to this sequence.
1.490 + * <p>
1.491 + * The characters of the array argument are appended, in order, to
1.492 + * the contents of this sequence. The length of this sequence
1.493 + * increases by the length of the argument.
1.494 + * <p>
1.495 + * The overall effect is exactly as if the argument were converted
1.496 + * to a string by the method {@link String#valueOf(char[])},
1.497 + * and the characters of that string were then
1.498 + * {@link #append(String) appended} to this character sequence.
1.499 + *
1.500 + * @param str the characters to be appended.
1.501 + * @return a reference to this object.
1.502 + */
1.503 + public AbstractStringBuilder append(char[] str) {
1.504 + int len = str.length;
1.505 + ensureCapacityInternal(count + len);
1.506 + arraycopy(str, 0, value, count, len);
1.507 + count += len;
1.508 + return this;
1.509 + }
1.510 +
1.511 + /**
1.512 + * Appends the string representation of a subarray of the
1.513 + * {@code char} array argument to this sequence.
1.514 + * <p>
1.515 + * Characters of the {@code char} array {@code str}, starting at
1.516 + * index {@code offset}, are appended, in order, to the contents
1.517 + * of this sequence. The length of this sequence increases
1.518 + * by the value of {@code len}.
1.519 + * <p>
1.520 + * The overall effect is exactly as if the arguments were converted
1.521 + * to a string by the method {@link String#valueOf(char[],int,int)},
1.522 + * and the characters of that string were then
1.523 + * {@link #append(String) appended} to this character sequence.
1.524 + *
1.525 + * @param str the characters to be appended.
1.526 + * @param offset the index of the first {@code char} to append.
1.527 + * @param len the number of {@code char}s to append.
1.528 + * @return a reference to this object.
1.529 + * @throws IndexOutOfBoundsException
1.530 + * if {@code offset < 0} or {@code len < 0}
1.531 + * or {@code offset+len > str.length}
1.532 + */
1.533 + public AbstractStringBuilder append(char str[], int offset, int len) {
1.534 + if (len > 0) // let arraycopy report AIOOBE for len < 0
1.535 + ensureCapacityInternal(count + len);
1.536 + arraycopy(str, offset, value, count, len);
1.537 + count += len;
1.538 + return this;
1.539 + }
1.540 +
1.541 + /**
1.542 + * Appends the string representation of the {@code boolean}
1.543 + * argument to the sequence.
1.544 + * <p>
1.545 + * The overall effect is exactly as if the argument were converted
1.546 + * to a string by the method {@link String#valueOf(boolean)},
1.547 + * and the characters of that string were then
1.548 + * {@link #append(String) appended} to this character sequence.
1.549 + *
1.550 + * @param b a {@code boolean}.
1.551 + * @return a reference to this object.
1.552 + */
1.553 + public AbstractStringBuilder append(boolean b) {
1.554 + if (b) {
1.555 + ensureCapacityInternal(count + 4);
1.556 + value[count++] = 't';
1.557 + value[count++] = 'r';
1.558 + value[count++] = 'u';
1.559 + value[count++] = 'e';
1.560 + } else {
1.561 + ensureCapacityInternal(count + 5);
1.562 + value[count++] = 'f';
1.563 + value[count++] = 'a';
1.564 + value[count++] = 'l';
1.565 + value[count++] = 's';
1.566 + value[count++] = 'e';
1.567 + }
1.568 + return this;
1.569 + }
1.570 +
1.571 + /**
1.572 + * Appends the string representation of the {@code char}
1.573 + * argument to this sequence.
1.574 + * <p>
1.575 + * The argument is appended to the contents of this sequence.
1.576 + * The length of this sequence increases by {@code 1}.
1.577 + * <p>
1.578 + * The overall effect is exactly as if the argument were converted
1.579 + * to a string by the method {@link String#valueOf(char)},
1.580 + * and the character in that string were then
1.581 + * {@link #append(String) appended} to this character sequence.
1.582 + *
1.583 + * @param c a {@code char}.
1.584 + * @return a reference to this object.
1.585 + */
1.586 + public AbstractStringBuilder append(char c) {
1.587 + ensureCapacityInternal(count + 1);
1.588 + value[count++] = c;
1.589 + return this;
1.590 + }
1.591 +
1.592 + /**
1.593 + * Appends the string representation of the {@code int}
1.594 + * argument to this sequence.
1.595 + * <p>
1.596 + * The overall effect is exactly as if the argument were converted
1.597 + * to a string by the method {@link String#valueOf(int)},
1.598 + * and the characters of that string were then
1.599 + * {@link #append(String) appended} to this character sequence.
1.600 + *
1.601 + * @param i an {@code int}.
1.602 + * @return a reference to this object.
1.603 + */
1.604 + public AbstractStringBuilder append(int i) {
1.605 + return append(Integer.toString(i));
1.606 + }
1.607 +
1.608 + /**
1.609 + * Appends the string representation of the {@code long}
1.610 + * argument to this sequence.
1.611 + * <p>
1.612 + * The overall effect is exactly as if the argument were converted
1.613 + * to a string by the method {@link String#valueOf(long)},
1.614 + * and the characters of that string were then
1.615 + * {@link #append(String) appended} to this character sequence.
1.616 + *
1.617 + * @param l a {@code long}.
1.618 + * @return a reference to this object.
1.619 + */
1.620 + public AbstractStringBuilder append(long l) {
1.621 + if (l == Long.MIN_VALUE) {
1.622 + append("-9223372036854775808");
1.623 + return this;
1.624 + }
1.625 + int appendedLength = (l < 0) ? Long.stringSize(-l) + 1
1.626 + : Long.stringSize(l);
1.627 + int spaceNeeded = count + appendedLength;
1.628 + ensureCapacityInternal(spaceNeeded);
1.629 + Long.getChars(l, spaceNeeded, value);
1.630 + count = spaceNeeded;
1.631 + return this;
1.632 + }
1.633 +
1.634 + /**
1.635 + * Appends the string representation of the {@code float}
1.636 + * argument to this sequence.
1.637 + * <p>
1.638 + * The overall effect is exactly as if the argument were converted
1.639 + * to a string by the method {@link String#valueOf(float)},
1.640 + * and the characters of that string were then
1.641 + * {@link #append(String) appended} to this character sequence.
1.642 + *
1.643 + * @param f a {@code float}.
1.644 + * @return a reference to this object.
1.645 + */
1.646 + public AbstractStringBuilder append(float f) {
1.647 + return append(Float.toString(f));
1.648 + }
1.649 +
1.650 + /**
1.651 + * Appends the string representation of the {@code double}
1.652 + * argument to this sequence.
1.653 + * <p>
1.654 + * The overall effect is exactly as if the argument were converted
1.655 + * to a string by the method {@link String#valueOf(double)},
1.656 + * and the characters of that string were then
1.657 + * {@link #append(String) appended} to this character sequence.
1.658 + *
1.659 + * @param d a {@code double}.
1.660 + * @return a reference to this object.
1.661 + */
1.662 + public AbstractStringBuilder append(double d) {
1.663 + return append(Double.toString(d));
1.664 + }
1.665 +
1.666 + /**
1.667 + * Removes the characters in a substring of this sequence.
1.668 + * The substring begins at the specified {@code start} and extends to
1.669 + * the character at index {@code end - 1} or to the end of the
1.670 + * sequence if no such character exists. If
1.671 + * {@code start} is equal to {@code end}, no changes are made.
1.672 + *
1.673 + * @param start The beginning index, inclusive.
1.674 + * @param end The ending index, exclusive.
1.675 + * @return This object.
1.676 + * @throws StringIndexOutOfBoundsException if {@code start}
1.677 + * is negative, greater than {@code length()}, or
1.678 + * greater than {@code end}.
1.679 + */
1.680 + public AbstractStringBuilder delete(int start, int end) {
1.681 + if (start < 0)
1.682 + throw new StringIndexOutOfBoundsException(start);
1.683 + if (end > count)
1.684 + end = count;
1.685 + if (start > end)
1.686 + throw new StringIndexOutOfBoundsException();
1.687 + int len = end - start;
1.688 + if (len > 0) {
1.689 + arraycopy(value, start+len, value, start, count-end);
1.690 + count -= len;
1.691 + }
1.692 + return this;
1.693 + }
1.694 +
1.695 + /**
1.696 + * Appends the string representation of the {@code codePoint}
1.697 + * argument to this sequence.
1.698 + *
1.699 + * <p> The argument is appended to the contents of this sequence.
1.700 + * The length of this sequence increases by
1.701 + * {@link Character#charCount(int) Character.charCount(codePoint)}.
1.702 + *
1.703 + * <p> The overall effect is exactly as if the argument were
1.704 + * converted to a {@code char} array by the method
1.705 + * {@link Character#toChars(int)} and the character in that array
1.706 + * were then {@link #append(char[]) appended} to this character
1.707 + * sequence.
1.708 + *
1.709 + * @param codePoint a Unicode code point
1.710 + * @return a reference to this object.
1.711 + * @exception IllegalArgumentException if the specified
1.712 + * {@code codePoint} isn't a valid Unicode code point
1.713 + */
1.714 + public AbstractStringBuilder appendCodePoint(int codePoint) {
1.715 + final int count = this.count;
1.716 +
1.717 + if (Character.isBmpCodePoint(codePoint)) {
1.718 + ensureCapacityInternal(count + 1);
1.719 + value[count] = (char) codePoint;
1.720 + this.count = count + 1;
1.721 + } else if (Character.isValidCodePoint(codePoint)) {
1.722 + ensureCapacityInternal(count + 2);
1.723 + Character.toSurrogates(codePoint, value, count);
1.724 + this.count = count + 2;
1.725 + } else {
1.726 + throw new IllegalArgumentException();
1.727 + }
1.728 + return this;
1.729 + }
1.730 +
1.731 + /**
1.732 + * Removes the <code>char</code> at the specified position in this
1.733 + * sequence. This sequence is shortened by one <code>char</code>.
1.734 + *
1.735 + * <p>Note: If the character at the given index is a supplementary
1.736 + * character, this method does not remove the entire character. If
1.737 + * correct handling of supplementary characters is required,
1.738 + * determine the number of <code>char</code>s to remove by calling
1.739 + * <code>Character.charCount(thisSequence.codePointAt(index))</code>,
1.740 + * where <code>thisSequence</code> is this sequence.
1.741 + *
1.742 + * @param index Index of <code>char</code> to remove
1.743 + * @return This object.
1.744 + * @throws StringIndexOutOfBoundsException if the <code>index</code>
1.745 + * is negative or greater than or equal to
1.746 + * <code>length()</code>.
1.747 + */
1.748 + public AbstractStringBuilder deleteCharAt(int index) {
1.749 + if ((index < 0) || (index >= count))
1.750 + throw new StringIndexOutOfBoundsException(index);
1.751 + arraycopy(value, index+1, value, index, count-index-1);
1.752 + count--;
1.753 + return this;
1.754 + }
1.755 +
1.756 + /**
1.757 + * Replaces the characters in a substring of this sequence
1.758 + * with characters in the specified <code>String</code>. The substring
1.759 + * begins at the specified <code>start</code> and extends to the character
1.760 + * at index <code>end - 1</code> or to the end of the
1.761 + * sequence if no such character exists. First the
1.762 + * characters in the substring are removed and then the specified
1.763 + * <code>String</code> is inserted at <code>start</code>. (This
1.764 + * sequence will be lengthened to accommodate the
1.765 + * specified String if necessary.)
1.766 + *
1.767 + * @param start The beginning index, inclusive.
1.768 + * @param end The ending index, exclusive.
1.769 + * @param str String that will replace previous contents.
1.770 + * @return This object.
1.771 + * @throws StringIndexOutOfBoundsException if <code>start</code>
1.772 + * is negative, greater than <code>length()</code>, or
1.773 + * greater than <code>end</code>.
1.774 + */
1.775 + public AbstractStringBuilder replace(int start, int end, String str) {
1.776 + if (start < 0)
1.777 + throw new StringIndexOutOfBoundsException(start);
1.778 + if (start > count)
1.779 + throw new StringIndexOutOfBoundsException("start > length()");
1.780 + if (start > end)
1.781 + throw new StringIndexOutOfBoundsException("start > end");
1.782 +
1.783 + if (end > count)
1.784 + end = count;
1.785 + int len = str.length();
1.786 + int newCount = count + len - (end - start);
1.787 + ensureCapacityInternal(newCount);
1.788 +
1.789 + arraycopy(value, end, value, start + len, count - end);
1.790 + str.getChars(value, start);
1.791 + count = newCount;
1.792 + return this;
1.793 + }
1.794 +
1.795 + /**
1.796 + * Returns a new <code>String</code> that contains a subsequence of
1.797 + * characters currently contained in this character sequence. The
1.798 + * substring begins at the specified index and extends to the end of
1.799 + * this sequence.
1.800 + *
1.801 + * @param start The beginning index, inclusive.
1.802 + * @return The new string.
1.803 + * @throws StringIndexOutOfBoundsException if <code>start</code> is
1.804 + * less than zero, or greater than the length of this object.
1.805 + */
1.806 + public String substring(int start) {
1.807 + return substring(start, count);
1.808 + }
1.809 +
1.810 + /**
1.811 + * Returns a new character sequence that is a subsequence of this sequence.
1.812 + *
1.813 + * <p> An invocation of this method of the form
1.814 + *
1.815 + * <blockquote><pre>
1.816 + * sb.subSequence(begin, end)</pre></blockquote>
1.817 + *
1.818 + * behaves in exactly the same way as the invocation
1.819 + *
1.820 + * <blockquote><pre>
1.821 + * sb.substring(begin, end)</pre></blockquote>
1.822 + *
1.823 + * This method is provided so that this class can
1.824 + * implement the {@link CharSequence} interface. </p>
1.825 + *
1.826 + * @param start the start index, inclusive.
1.827 + * @param end the end index, exclusive.
1.828 + * @return the specified subsequence.
1.829 + *
1.830 + * @throws IndexOutOfBoundsException
1.831 + * if <tt>start</tt> or <tt>end</tt> are negative,
1.832 + * if <tt>end</tt> is greater than <tt>length()</tt>,
1.833 + * or if <tt>start</tt> is greater than <tt>end</tt>
1.834 + * @spec JSR-51
1.835 + */
1.836 + public CharSequence subSequence(int start, int end) {
1.837 + return substring(start, end);
1.838 + }
1.839 +
1.840 + /**
1.841 + * Returns a new <code>String</code> that contains a subsequence of
1.842 + * characters currently contained in this sequence. The
1.843 + * substring begins at the specified <code>start</code> and
1.844 + * extends to the character at index <code>end - 1</code>.
1.845 + *
1.846 + * @param start The beginning index, inclusive.
1.847 + * @param end The ending index, exclusive.
1.848 + * @return The new string.
1.849 + * @throws StringIndexOutOfBoundsException if <code>start</code>
1.850 + * or <code>end</code> are negative or greater than
1.851 + * <code>length()</code>, or <code>start</code> is
1.852 + * greater than <code>end</code>.
1.853 + */
1.854 + public String substring(int start, int end) {
1.855 + if (start < 0)
1.856 + throw new StringIndexOutOfBoundsException(start);
1.857 + if (end > count)
1.858 + throw new StringIndexOutOfBoundsException(end);
1.859 + if (start > end)
1.860 + throw new StringIndexOutOfBoundsException(end - start);
1.861 + return new String(value, start, end - start);
1.862 + }
1.863 +
1.864 + /**
1.865 + * Inserts the string representation of a subarray of the {@code str}
1.866 + * array argument into this sequence. The subarray begins at the
1.867 + * specified {@code offset} and extends {@code len} {@code char}s.
1.868 + * The characters of the subarray are inserted into this sequence at
1.869 + * the position indicated by {@code index}. The length of this
1.870 + * sequence increases by {@code len} {@code char}s.
1.871 + *
1.872 + * @param index position at which to insert subarray.
1.873 + * @param str A {@code char} array.
1.874 + * @param offset the index of the first {@code char} in subarray to
1.875 + * be inserted.
1.876 + * @param len the number of {@code char}s in the subarray to
1.877 + * be inserted.
1.878 + * @return This object
1.879 + * @throws StringIndexOutOfBoundsException if {@code index}
1.880 + * is negative or greater than {@code length()}, or
1.881 + * {@code offset} or {@code len} are negative, or
1.882 + * {@code (offset+len)} is greater than
1.883 + * {@code str.length}.
1.884 + */
1.885 + public AbstractStringBuilder insert(int index, char[] str, int offset,
1.886 + int len)
1.887 + {
1.888 + if ((index < 0) || (index > length()))
1.889 + throw new StringIndexOutOfBoundsException(index);
1.890 + if ((offset < 0) || (len < 0) || (offset > str.length - len))
1.891 + throw new StringIndexOutOfBoundsException(
1.892 + "offset " + offset + ", len " + len + ", str.length "
1.893 + + str.length);
1.894 + ensureCapacityInternal(count + len);
1.895 + arraycopy(value, index, value, index + len, count - index);
1.896 + arraycopy(str, offset, value, index, len);
1.897 + count += len;
1.898 + return this;
1.899 + }
1.900 +
1.901 + /**
1.902 + * Inserts the string representation of the {@code Object}
1.903 + * argument into this character sequence.
1.904 + * <p>
1.905 + * The overall effect is exactly as if the second argument were
1.906 + * converted to a string by the method {@link String#valueOf(Object)},
1.907 + * and the characters of that string were then
1.908 + * {@link #insert(int,String) inserted} into this character
1.909 + * sequence at the indicated offset.
1.910 + * <p>
1.911 + * The {@code offset} argument must be greater than or equal to
1.912 + * {@code 0}, and less than or equal to the {@linkplain #length() length}
1.913 + * of this sequence.
1.914 + *
1.915 + * @param offset the offset.
1.916 + * @param obj an {@code Object}.
1.917 + * @return a reference to this object.
1.918 + * @throws StringIndexOutOfBoundsException if the offset is invalid.
1.919 + */
1.920 + public AbstractStringBuilder insert(int offset, Object obj) {
1.921 + return insert(offset, String.valueOf(obj));
1.922 + }
1.923 +
1.924 + /**
1.925 + * Inserts the string into this character sequence.
1.926 + * <p>
1.927 + * The characters of the {@code String} argument are inserted, in
1.928 + * order, into this sequence at the indicated offset, moving up any
1.929 + * characters originally above that position and increasing the length
1.930 + * of this sequence by the length of the argument. If
1.931 + * {@code str} is {@code null}, then the four characters
1.932 + * {@code "null"} are inserted into this sequence.
1.933 + * <p>
1.934 + * The character at index <i>k</i> in the new character sequence is
1.935 + * equal to:
1.936 + * <ul>
1.937 + * <li>the character at index <i>k</i> in the old character sequence, if
1.938 + * <i>k</i> is less than {@code offset}
1.939 + * <li>the character at index <i>k</i>{@code -offset} in the
1.940 + * argument {@code str}, if <i>k</i> is not less than
1.941 + * {@code offset} but is less than {@code offset+str.length()}
1.942 + * <li>the character at index <i>k</i>{@code -str.length()} in the
1.943 + * old character sequence, if <i>k</i> is not less than
1.944 + * {@code offset+str.length()}
1.945 + * </ul><p>
1.946 + * The {@code offset} argument must be greater than or equal to
1.947 + * {@code 0}, and less than or equal to the {@linkplain #length() length}
1.948 + * of this sequence.
1.949 + *
1.950 + * @param offset the offset.
1.951 + * @param str a string.
1.952 + * @return a reference to this object.
1.953 + * @throws StringIndexOutOfBoundsException if the offset is invalid.
1.954 + */
1.955 + public AbstractStringBuilder insert(int offset, String str) {
1.956 + if ((offset < 0) || (offset > length()))
1.957 + throw new StringIndexOutOfBoundsException(offset);
1.958 + if (str == null)
1.959 + str = "null";
1.960 + int len = str.length();
1.961 + ensureCapacityInternal(count + len);
1.962 + arraycopy(value, offset, value, offset + len, count - offset);
1.963 + str.getChars(value, offset);
1.964 + count += len;
1.965 + return this;
1.966 + }
1.967 +
1.968 + /**
1.969 + * Inserts the string representation of the {@code char} array
1.970 + * argument into this sequence.
1.971 + * <p>
1.972 + * The characters of the array argument are inserted into the
1.973 + * contents of this sequence at the position indicated by
1.974 + * {@code offset}. The length of this sequence increases by
1.975 + * the length of the argument.
1.976 + * <p>
1.977 + * The overall effect is exactly as if the second argument were
1.978 + * converted to a string by the method {@link String#valueOf(char[])},
1.979 + * and the characters of that string were then
1.980 + * {@link #insert(int,String) inserted} into this character
1.981 + * sequence at the indicated offset.
1.982 + * <p>
1.983 + * The {@code offset} argument must be greater than or equal to
1.984 + * {@code 0}, and less than or equal to the {@linkplain #length() length}
1.985 + * of this sequence.
1.986 + *
1.987 + * @param offset the offset.
1.988 + * @param str a character array.
1.989 + * @return a reference to this object.
1.990 + * @throws StringIndexOutOfBoundsException if the offset is invalid.
1.991 + */
1.992 + public AbstractStringBuilder insert(int offset, char[] str) {
1.993 + if ((offset < 0) || (offset > length()))
1.994 + throw new StringIndexOutOfBoundsException(offset);
1.995 + int len = str.length;
1.996 + ensureCapacityInternal(count + len);
1.997 + arraycopy(value, offset, value, offset + len, count - offset);
1.998 + arraycopy(str, 0, value, offset, len);
1.999 + count += len;
1.1000 + return this;
1.1001 + }
1.1002 +
1.1003 + /**
1.1004 + * Inserts the specified {@code CharSequence} into this sequence.
1.1005 + * <p>
1.1006 + * The characters of the {@code CharSequence} argument are inserted,
1.1007 + * in order, into this sequence at the indicated offset, moving up
1.1008 + * any characters originally above that position and increasing the length
1.1009 + * of this sequence by the length of the argument s.
1.1010 + * <p>
1.1011 + * The result of this method is exactly the same as if it were an
1.1012 + * invocation of this object's
1.1013 + * {@link #insert(int,CharSequence,int,int) insert}(dstOffset, s, 0, s.length())
1.1014 + * method.
1.1015 + *
1.1016 + * <p>If {@code s} is {@code null}, then the four characters
1.1017 + * {@code "null"} are inserted into this sequence.
1.1018 + *
1.1019 + * @param dstOffset the offset.
1.1020 + * @param s the sequence to be inserted
1.1021 + * @return a reference to this object.
1.1022 + * @throws IndexOutOfBoundsException if the offset is invalid.
1.1023 + */
1.1024 + public AbstractStringBuilder insert(int dstOffset, CharSequence s) {
1.1025 + if (s == null)
1.1026 + s = "null";
1.1027 + if (s instanceof String)
1.1028 + return this.insert(dstOffset, (String)s);
1.1029 + return this.insert(dstOffset, s, 0, s.length());
1.1030 + }
1.1031 +
1.1032 + /**
1.1033 + * Inserts a subsequence of the specified {@code CharSequence} into
1.1034 + * this sequence.
1.1035 + * <p>
1.1036 + * The subsequence of the argument {@code s} specified by
1.1037 + * {@code start} and {@code end} are inserted,
1.1038 + * in order, into this sequence at the specified destination offset, moving
1.1039 + * up any characters originally above that position. The length of this
1.1040 + * sequence is increased by {@code end - start}.
1.1041 + * <p>
1.1042 + * The character at index <i>k</i> in this sequence becomes equal to:
1.1043 + * <ul>
1.1044 + * <li>the character at index <i>k</i> in this sequence, if
1.1045 + * <i>k</i> is less than {@code dstOffset}
1.1046 + * <li>the character at index <i>k</i>{@code +start-dstOffset} in
1.1047 + * the argument {@code s}, if <i>k</i> is greater than or equal to
1.1048 + * {@code dstOffset} but is less than {@code dstOffset+end-start}
1.1049 + * <li>the character at index <i>k</i>{@code -(end-start)} in this
1.1050 + * sequence, if <i>k</i> is greater than or equal to
1.1051 + * {@code dstOffset+end-start}
1.1052 + * </ul><p>
1.1053 + * The {@code dstOffset} argument must be greater than or equal to
1.1054 + * {@code 0}, and less than or equal to the {@linkplain #length() length}
1.1055 + * of this sequence.
1.1056 + * <p>The start argument must be nonnegative, and not greater than
1.1057 + * {@code end}.
1.1058 + * <p>The end argument must be greater than or equal to
1.1059 + * {@code start}, and less than or equal to the length of s.
1.1060 + *
1.1061 + * <p>If {@code s} is {@code null}, then this method inserts
1.1062 + * characters as if the s parameter was a sequence containing the four
1.1063 + * characters {@code "null"}.
1.1064 + *
1.1065 + * @param dstOffset the offset in this sequence.
1.1066 + * @param s the sequence to be inserted.
1.1067 + * @param start the starting index of the subsequence to be inserted.
1.1068 + * @param end the end index of the subsequence to be inserted.
1.1069 + * @return a reference to this object.
1.1070 + * @throws IndexOutOfBoundsException if {@code dstOffset}
1.1071 + * is negative or greater than {@code this.length()}, or
1.1072 + * {@code start} or {@code end} are negative, or
1.1073 + * {@code start} is greater than {@code end} or
1.1074 + * {@code end} is greater than {@code s.length()}
1.1075 + */
1.1076 + public AbstractStringBuilder insert(int dstOffset, CharSequence s,
1.1077 + int start, int end) {
1.1078 + if (s == null)
1.1079 + s = "null";
1.1080 + if ((dstOffset < 0) || (dstOffset > this.length()))
1.1081 + throw new IndexOutOfBoundsException("dstOffset "+dstOffset);
1.1082 + if ((start < 0) || (end < 0) || (start > end) || (end > s.length()))
1.1083 + throw new IndexOutOfBoundsException(
1.1084 + "start " + start + ", end " + end + ", s.length() "
1.1085 + + s.length());
1.1086 + int len = end - start;
1.1087 + ensureCapacityInternal(count + len);
1.1088 + arraycopy(value, dstOffset, value, dstOffset + len,
1.1089 + count - dstOffset);
1.1090 + for (int i=start; i<end; i++)
1.1091 + value[dstOffset++] = s.charAt(i);
1.1092 + count += len;
1.1093 + return this;
1.1094 + }
1.1095 +
1.1096 + /**
1.1097 + * Inserts the string representation of the {@code boolean}
1.1098 + * argument into this sequence.
1.1099 + * <p>
1.1100 + * The overall effect is exactly as if the second argument were
1.1101 + * converted to a string by the method {@link String#valueOf(boolean)},
1.1102 + * and the characters of that string were then
1.1103 + * {@link #insert(int,String) inserted} into this character
1.1104 + * sequence at the indicated offset.
1.1105 + * <p>
1.1106 + * The {@code offset} argument must be greater than or equal to
1.1107 + * {@code 0}, and less than or equal to the {@linkplain #length() length}
1.1108 + * of this sequence.
1.1109 + *
1.1110 + * @param offset the offset.
1.1111 + * @param b a {@code boolean}.
1.1112 + * @return a reference to this object.
1.1113 + * @throws StringIndexOutOfBoundsException if the offset is invalid.
1.1114 + */
1.1115 + public AbstractStringBuilder insert(int offset, boolean b) {
1.1116 + return insert(offset, String.valueOf(b));
1.1117 + }
1.1118 +
1.1119 + /**
1.1120 + * Inserts the string representation of the {@code char}
1.1121 + * argument into this sequence.
1.1122 + * <p>
1.1123 + * The overall effect is exactly as if the second argument were
1.1124 + * converted to a string by the method {@link String#valueOf(char)},
1.1125 + * and the character in that string were then
1.1126 + * {@link #insert(int,String) inserted} into this character
1.1127 + * sequence at the indicated offset.
1.1128 + * <p>
1.1129 + * The {@code offset} argument must be greater than or equal to
1.1130 + * {@code 0}, and less than or equal to the {@linkplain #length() length}
1.1131 + * of this sequence.
1.1132 + *
1.1133 + * @param offset the offset.
1.1134 + * @param c a {@code char}.
1.1135 + * @return a reference to this object.
1.1136 + * @throws IndexOutOfBoundsException if the offset is invalid.
1.1137 + */
1.1138 + public AbstractStringBuilder insert(int offset, char c) {
1.1139 + ensureCapacityInternal(count + 1);
1.1140 + arraycopy(value, offset, value, offset + 1, count - offset);
1.1141 + value[offset] = c;
1.1142 + count += 1;
1.1143 + return this;
1.1144 + }
1.1145 +
1.1146 + /**
1.1147 + * Inserts the string representation of the second {@code int}
1.1148 + * argument into this sequence.
1.1149 + * <p>
1.1150 + * The overall effect is exactly as if the second argument were
1.1151 + * converted to a string by the method {@link String#valueOf(int)},
1.1152 + * and the characters of that string were then
1.1153 + * {@link #insert(int,String) inserted} into this character
1.1154 + * sequence at the indicated offset.
1.1155 + * <p>
1.1156 + * The {@code offset} argument must be greater than or equal to
1.1157 + * {@code 0}, and less than or equal to the {@linkplain #length() length}
1.1158 + * of this sequence.
1.1159 + *
1.1160 + * @param offset the offset.
1.1161 + * @param i an {@code int}.
1.1162 + * @return a reference to this object.
1.1163 + * @throws StringIndexOutOfBoundsException if the offset is invalid.
1.1164 + */
1.1165 + public AbstractStringBuilder insert(int offset, int i) {
1.1166 + return insert(offset, String.valueOf(i));
1.1167 + }
1.1168 +
1.1169 + /**
1.1170 + * Inserts the string representation of the {@code long}
1.1171 + * argument into this sequence.
1.1172 + * <p>
1.1173 + * The overall effect is exactly as if the second argument were
1.1174 + * converted to a string by the method {@link String#valueOf(long)},
1.1175 + * and the characters of that string were then
1.1176 + * {@link #insert(int,String) inserted} into this character
1.1177 + * sequence at the indicated offset.
1.1178 + * <p>
1.1179 + * The {@code offset} argument must be greater than or equal to
1.1180 + * {@code 0}, and less than or equal to the {@linkplain #length() length}
1.1181 + * of this sequence.
1.1182 + *
1.1183 + * @param offset the offset.
1.1184 + * @param l a {@code long}.
1.1185 + * @return a reference to this object.
1.1186 + * @throws StringIndexOutOfBoundsException if the offset is invalid.
1.1187 + */
1.1188 + public AbstractStringBuilder insert(int offset, long l) {
1.1189 + return insert(offset, String.valueOf(l));
1.1190 + }
1.1191 +
1.1192 + /**
1.1193 + * Inserts the string representation of the {@code float}
1.1194 + * argument into this sequence.
1.1195 + * <p>
1.1196 + * The overall effect is exactly as if the second argument were
1.1197 + * converted to a string by the method {@link String#valueOf(float)},
1.1198 + * and the characters of that string were then
1.1199 + * {@link #insert(int,String) inserted} into this character
1.1200 + * sequence at the indicated offset.
1.1201 + * <p>
1.1202 + * The {@code offset} argument must be greater than or equal to
1.1203 + * {@code 0}, and less than or equal to the {@linkplain #length() length}
1.1204 + * of this sequence.
1.1205 + *
1.1206 + * @param offset the offset.
1.1207 + * @param f a {@code float}.
1.1208 + * @return a reference to this object.
1.1209 + * @throws StringIndexOutOfBoundsException if the offset is invalid.
1.1210 + */
1.1211 + public AbstractStringBuilder insert(int offset, float f) {
1.1212 + return insert(offset, String.valueOf(f));
1.1213 + }
1.1214 +
1.1215 + /**
1.1216 + * Inserts the string representation of the {@code double}
1.1217 + * argument into this sequence.
1.1218 + * <p>
1.1219 + * The overall effect is exactly as if the second argument were
1.1220 + * converted to a string by the method {@link String#valueOf(double)},
1.1221 + * and the characters of that string were then
1.1222 + * {@link #insert(int,String) inserted} into this character
1.1223 + * sequence at the indicated offset.
1.1224 + * <p>
1.1225 + * The {@code offset} argument must be greater than or equal to
1.1226 + * {@code 0}, and less than or equal to the {@linkplain #length() length}
1.1227 + * of this sequence.
1.1228 + *
1.1229 + * @param offset the offset.
1.1230 + * @param d a {@code double}.
1.1231 + * @return a reference to this object.
1.1232 + * @throws StringIndexOutOfBoundsException if the offset is invalid.
1.1233 + */
1.1234 + public AbstractStringBuilder insert(int offset, double d) {
1.1235 + return insert(offset, String.valueOf(d));
1.1236 + }
1.1237 +
1.1238 + /**
1.1239 + * Returns the index within this string of the first occurrence of the
1.1240 + * specified substring. The integer returned is the smallest value
1.1241 + * <i>k</i> such that:
1.1242 + * <blockquote><pre>
1.1243 + * this.toString().startsWith(str, <i>k</i>)
1.1244 + * </pre></blockquote>
1.1245 + * is <code>true</code>.
1.1246 + *
1.1247 + * @param str any string.
1.1248 + * @return if the string argument occurs as a substring within this
1.1249 + * object, then the index of the first character of the first
1.1250 + * such substring is returned; if it does not occur as a
1.1251 + * substring, <code>-1</code> is returned.
1.1252 + * @throws java.lang.NullPointerException if <code>str</code> is
1.1253 + * <code>null</code>.
1.1254 + */
1.1255 + public int indexOf(String str) {
1.1256 + return indexOf(str, 0);
1.1257 + }
1.1258 +
1.1259 + /**
1.1260 + * Returns the index within this string of the first occurrence of the
1.1261 + * specified substring, starting at the specified index. The integer
1.1262 + * returned is the smallest value <tt>k</tt> for which:
1.1263 + * <blockquote><pre>
1.1264 + * k >= Math.min(fromIndex, str.length()) &&
1.1265 + * this.toString().startsWith(str, k)
1.1266 + * </pre></blockquote>
1.1267 + * If no such value of <i>k</i> exists, then -1 is returned.
1.1268 + *
1.1269 + * @param str the substring for which to search.
1.1270 + * @param fromIndex the index from which to start the search.
1.1271 + * @return the index within this string of the first occurrence of the
1.1272 + * specified substring, starting at the specified index.
1.1273 + * @throws java.lang.NullPointerException if <code>str</code> is
1.1274 + * <code>null</code>.
1.1275 + */
1.1276 + public int indexOf(String str, int fromIndex) {
1.1277 + return toString().indexOf(str, fromIndex);
1.1278 + }
1.1279 +
1.1280 + /**
1.1281 + * Returns the index within this string of the rightmost occurrence
1.1282 + * of the specified substring. The rightmost empty string "" is
1.1283 + * considered to occur at the index value <code>this.length()</code>.
1.1284 + * The returned index is the largest value <i>k</i> such that
1.1285 + * <blockquote><pre>
1.1286 + * this.toString().startsWith(str, k)
1.1287 + * </pre></blockquote>
1.1288 + * is true.
1.1289 + *
1.1290 + * @param str the substring to search for.
1.1291 + * @return if the string argument occurs one or more times as a substring
1.1292 + * within this object, then the index of the first character of
1.1293 + * the last such substring is returned. If it does not occur as
1.1294 + * a substring, <code>-1</code> is returned.
1.1295 + * @throws java.lang.NullPointerException if <code>str</code> is
1.1296 + * <code>null</code>.
1.1297 + */
1.1298 + public int lastIndexOf(String str) {
1.1299 + return lastIndexOf(str, count);
1.1300 + }
1.1301 +
1.1302 + /**
1.1303 + * Returns the index within this string of the last occurrence of the
1.1304 + * specified substring. The integer returned is the largest value <i>k</i>
1.1305 + * such that:
1.1306 + * <blockquote><pre>
1.1307 + * k <= Math.min(fromIndex, str.length()) &&
1.1308 + * this.toString().startsWith(str, k)
1.1309 + * </pre></blockquote>
1.1310 + * If no such value of <i>k</i> exists, then -1 is returned.
1.1311 + *
1.1312 + * @param str the substring to search for.
1.1313 + * @param fromIndex the index to start the search from.
1.1314 + * @return the index within this sequence of the last occurrence of the
1.1315 + * specified substring.
1.1316 + * @throws java.lang.NullPointerException if <code>str</code> is
1.1317 + * <code>null</code>.
1.1318 + */
1.1319 + public int lastIndexOf(String str, int fromIndex) {
1.1320 + return String.lastIndexOf(value, 0, count,
1.1321 + str.toCharArray(), 0, str.length(), fromIndex);
1.1322 + }
1.1323 +
1.1324 + /**
1.1325 + * Causes this character sequence to be replaced by the reverse of
1.1326 + * the sequence. If there are any surrogate pairs included in the
1.1327 + * sequence, these are treated as single characters for the
1.1328 + * reverse operation. Thus, the order of the high-low surrogates
1.1329 + * is never reversed.
1.1330 + *
1.1331 + * Let <i>n</i> be the character length of this character sequence
1.1332 + * (not the length in <code>char</code> values) just prior to
1.1333 + * execution of the <code>reverse</code> method. Then the
1.1334 + * character at index <i>k</i> in the new character sequence is
1.1335 + * equal to the character at index <i>n-k-1</i> in the old
1.1336 + * character sequence.
1.1337 + *
1.1338 + * <p>Note that the reverse operation may result in producing
1.1339 + * surrogate pairs that were unpaired low-surrogates and
1.1340 + * high-surrogates before the operation. For example, reversing
1.1341 + * "\uDC00\uD800" produces "\uD800\uDC00" which is
1.1342 + * a valid surrogate pair.
1.1343 + *
1.1344 + * @return a reference to this object.
1.1345 + */
1.1346 + public AbstractStringBuilder reverse() {
1.1347 + boolean hasSurrogate = false;
1.1348 + int n = count - 1;
1.1349 + for (int j = (n-1) >> 1; j >= 0; --j) {
1.1350 + char temp = value[j];
1.1351 + char temp2 = value[n - j];
1.1352 + if (!hasSurrogate) {
1.1353 + hasSurrogate = (temp >= Character.MIN_SURROGATE && temp <= Character.MAX_SURROGATE)
1.1354 + || (temp2 >= Character.MIN_SURROGATE && temp2 <= Character.MAX_SURROGATE);
1.1355 + }
1.1356 + value[j] = temp2;
1.1357 + value[n - j] = temp;
1.1358 + }
1.1359 + if (hasSurrogate) {
1.1360 + // Reverse back all valid surrogate pairs
1.1361 + for (int i = 0; i < count - 1; i++) {
1.1362 + char c2 = value[i];
1.1363 + if (Character.isLowSurrogate(c2)) {
1.1364 + char c1 = value[i + 1];
1.1365 + if (Character.isHighSurrogate(c1)) {
1.1366 + value[i++] = c1;
1.1367 + value[i] = c2;
1.1368 + }
1.1369 + }
1.1370 + }
1.1371 + }
1.1372 + return this;
1.1373 + }
1.1374 +
1.1375 + /**
1.1376 + * Returns a string representing the data in this sequence.
1.1377 + * A new <code>String</code> object is allocated and initialized to
1.1378 + * contain the character sequence currently represented by this
1.1379 + * object. This <code>String</code> is then returned. Subsequent
1.1380 + * changes to this sequence do not affect the contents of the
1.1381 + * <code>String</code>.
1.1382 + *
1.1383 + * @return a string representation of this sequence of characters.
1.1384 + */
1.1385 + public abstract String toString();
1.1386 +
1.1387 + /**
1.1388 + * Needed by <tt>String</tt> for the contentEquals method.
1.1389 + */
1.1390 + final char[] getValue() {
1.1391 + return value;
1.1392 + }
1.1393 +
1.1394 + static char[] copyOfRange(char[] original, int from, int to) {
1.1395 + int newLength = to - from;
1.1396 + if (newLength < 0) {
1.1397 + throw new IllegalArgumentException(from + " > " + to);
1.1398 + }
1.1399 + char[] copy = new char[newLength];
1.1400 + arraycopy(original, from, copy, 0, Math.min(original.length - from, newLength));
1.1401 + return copy;
1.1402 + }
1.1403 +
1.1404 + static void arraycopy(char[] value, int srcBegin, char[] dst, int dstBegin, int count) {
1.1405 + if (srcBegin < dstBegin) {
1.1406 + while (count-- > 0) {
1.1407 + dst[dstBegin + count] = value[srcBegin + count];
1.1408 + }
1.1409 + } else {
1.1410 + while (count-- > 0) {
1.1411 + dst[dstBegin++] = value[srcBegin++];
1.1412 + }
1.1413 + }
1.1414 + }
1.1415 +
1.1416 + // access system property
1.1417 + static String getProperty(String nm) {
1.1418 + return null;
1.1419 + }
1.1420 +
1.1421 + static char[] copyOf(char[] original, int newLength) {
1.1422 + char[] copy = new char[newLength];
1.1423 + arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
1.1424 + return copy;
1.1425 + }
1.1426 +
1.1427 +}