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