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