1 /* 2 * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.lang; 27 28 import jdk.internal.math.FloatingDecimal; 29 import java.util.Arrays; 30 import java.util.Spliterator; 31 import java.util.stream.IntStream; 32 import java.util.stream.StreamSupport; 33 34 import static java.lang.String.COMPACT_STRINGS; 35 import static java.lang.String.UTF16; 36 import static java.lang.String.LATIN1; 37 import static java.lang.String.checkIndex; 38 import static java.lang.String.checkOffset; 39 40 /** 41 * A mutable sequence of characters. 42 * <p> 43 * Implements a modifiable string. At any point in time it contains some 44 * particular sequence of characters, but the length and content of the 45 * sequence can be changed through certain method calls. 46 * 47 * <p>Unless otherwise noted, passing a {@code null} argument to a constructor 48 * or method in this class will cause a {@link NullPointerException} to be 49 * thrown. 50 * 51 * @author Michael McCloskey 52 * @author Martin Buchholz 53 * @author Ulf Zibis 54 * @since 1.5 55 */ 56 abstract class AbstractStringBuilder implements Appendable, CharSequence { 57 /** 58 * The value is used for character storage. 59 */ 60 byte[] value; 61 62 /** 63 * The id of the encoding used to encode the bytes in {@code value}. 64 */ 65 byte coder; 66 67 /** 68 * The count is the number of characters used. 69 */ 70 int count; 71 72 /** 73 * This no-arg constructor is necessary for serialization of subclasses. 74 */ 75 AbstractStringBuilder() { 76 } 77 78 /** 79 * Creates an AbstractStringBuilder of the specified capacity. 80 */ 81 AbstractStringBuilder(int capacity) { 82 if (COMPACT_STRINGS) { 83 value = new byte[capacity]; 84 coder = LATIN1; 85 } else { 86 value = StringUTF16.newBytesFor(capacity); 87 coder = UTF16; 88 } 89 } 90 91 /** 92 * Returns the length (character count). 93 * 94 * @return the length of the sequence of characters currently 95 * represented by this object 96 */ 97 @Override 98 public int length() { 99 return count; 100 } 101 102 /** 103 * Returns the current capacity. The capacity is the amount of storage 104 * available for newly inserted characters, beyond which an allocation 105 * will occur. 106 * 107 * @return the current capacity 108 */ 109 public int capacity() { 110 return value.length >> coder; 111 } 112 113 /** 114 * Ensures that the capacity is at least equal to the specified minimum. 115 * If the current capacity is less than the argument, then a new internal 116 * array is allocated with greater capacity. The new capacity is the 117 * larger of: 118 * <ul> 119 * <li>The {@code minimumCapacity} argument. 120 * <li>Twice the old capacity, plus {@code 2}. 121 * </ul> 122 * If the {@code minimumCapacity} argument is nonpositive, this 123 * method takes no action and simply returns. 124 * Note that subsequent operations on this object can reduce the 125 * actual capacity below that requested here. 126 * 127 * @param minimumCapacity the minimum desired capacity. 128 */ 129 public void ensureCapacity(int minimumCapacity) { 130 if (minimumCapacity > 0) { 131 ensureCapacityInternal(minimumCapacity); 132 } 133 } 134 135 /** 136 * For positive values of {@code minimumCapacity}, this method 137 * behaves like {@code ensureCapacity}, however it is never 138 * synchronized. 139 * If {@code minimumCapacity} is non positive due to numeric 140 * overflow, this method throws {@code OutOfMemoryError}. 141 */ 142 private void ensureCapacityInternal(int minimumCapacity) { 143 // overflow-conscious code 144 int oldCapacity = value.length >> coder; 145 if (minimumCapacity - oldCapacity > 0) { 146 value = Arrays.copyOf(value, 147 newCapacity(minimumCapacity) << coder); 148 } 149 } 150 151 /** 152 * The maximum size of array to allocate (unless necessary). 153 * Some VMs reserve some header words in an array. 154 * Attempts to allocate larger arrays may result in 155 * OutOfMemoryError: Requested array size exceeds VM limit 156 */ 157 private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; 158 159 /** 160 * Returns a capacity at least as large as the given minimum capacity. 161 * Returns the current capacity increased by the same amount + 2 if 162 * that suffices. 163 * Will not return a capacity greater than 164 * {@code (MAX_ARRAY_SIZE >> coder)} unless the given minimum capacity 165 * is greater than that. 166 * 167 * @param minCapacity the desired minimum capacity 168 * @throws OutOfMemoryError if minCapacity is less than zero or 169 * greater than (Integer.MAX_VALUE >> coder) 170 */ 171 private int newCapacity(int minCapacity) { 172 // overflow-conscious code 173 int oldCapacity = value.length >> coder; 174 int newCapacity = (oldCapacity << 1) + 2; 175 if (newCapacity - minCapacity < 0) { 176 newCapacity = minCapacity; 177 } 178 int SAFE_BOUND = MAX_ARRAY_SIZE >> coder; 179 return (newCapacity <= 0 || SAFE_BOUND - newCapacity < 0) 180 ? hugeCapacity(minCapacity) 181 : newCapacity; 182 } 183 184 private int hugeCapacity(int minCapacity) { 185 int SAFE_BOUND = MAX_ARRAY_SIZE >> coder; 186 int UNSAFE_BOUND = Integer.MAX_VALUE >> coder; 187 if (UNSAFE_BOUND - minCapacity < 0) { // overflow 188 throw new OutOfMemoryError(); 189 } 190 return (minCapacity > SAFE_BOUND) 191 ? minCapacity : SAFE_BOUND; 192 } 193 194 /** 195 * If the coder is "isLatin1", this inflates the internal 8-bit storage 196 * to 16-bit <hi=0, low> pair storage. 197 */ 198 private void inflate() { 199 if (!isLatin1()) { 200 return; 201 } 202 byte[] buf = StringUTF16.newBytesFor(value.length); 203 StringLatin1.inflate(value, 0, buf, 0, count); 204 this.value = buf; 205 this.coder = UTF16; 206 } 207 208 /** 209 * Attempts to reduce storage used for the character sequence. 210 * If the buffer is larger than necessary to hold its current sequence of 211 * characters, then it may be resized to become more space efficient. 212 * Calling this method may, but is not required to, affect the value 213 * returned by a subsequent call to the {@link #capacity()} method. 214 */ 215 public void trimToSize() { 216 int length = count << coder; 217 if (length < value.length) { 218 value = Arrays.copyOf(value, length); 219 } 220 } 221 222 /** 223 * Sets the length of the character sequence. 224 * The sequence is changed to a new character sequence 225 * whose length is specified by the argument. For every nonnegative 226 * index <i>k</i> less than {@code newLength}, the character at 227 * index <i>k</i> in the new character sequence is the same as the 228 * character at index <i>k</i> in the old sequence if <i>k</i> is less 229 * than the length of the old character sequence; otherwise, it is the 230 * null character {@code '\u005Cu0000'}. 231 * 232 * In other words, if the {@code newLength} argument is less than 233 * the current length, the length is changed to the specified length. 234 * <p> 235 * If the {@code newLength} argument is greater than or equal 236 * to the current length, sufficient null characters 237 * ({@code '\u005Cu0000'}) are appended so that 238 * length becomes the {@code newLength} argument. 239 * <p> 240 * The {@code newLength} argument must be greater than or equal 241 * to {@code 0}. 242 * 243 * @param newLength the new length 244 * @throws IndexOutOfBoundsException if the 245 * {@code newLength} argument is negative. 246 */ 247 public void setLength(int newLength) { 248 if (newLength < 0) { 249 throw new StringIndexOutOfBoundsException(newLength); 250 } 251 ensureCapacityInternal(newLength); 252 if (count < newLength) { 253 if (isLatin1()) { 254 StringLatin1.fillNull(value, count, newLength); 255 } else { 256 StringUTF16.fillNull(value, count, newLength); 257 } 258 } 259 count = newLength; 260 } 261 262 /** 263 * Returns the {@code char} value in this sequence at the specified index. 264 * The first {@code char} value is at index {@code 0}, the next at index 265 * {@code 1}, and so on, as in array indexing. 266 * <p> 267 * The index argument must be greater than or equal to 268 * {@code 0}, and less than the length of this sequence. 269 * 270 * <p>If the {@code char} value specified by the index is a 271 * <a href="Character.html#unicode">surrogate</a>, the surrogate 272 * value is returned. 273 * 274 * @param index the index of the desired {@code char} value. 275 * @return the {@code char} value at the specified index. 276 * @throws IndexOutOfBoundsException if {@code index} is 277 * negative or greater than or equal to {@code length()}. 278 */ 279 @Override 280 public char charAt(int index) { 281 checkIndex(index, count); 282 if (isLatin1()) { 283 return (char)(value[index] & 0xff); 284 } 285 return StringUTF16.charAt(value, index); 286 } 287 288 /** 289 * Returns the character (Unicode code point) at the specified 290 * index. The index refers to {@code char} values 291 * (Unicode code units) and ranges from {@code 0} to 292 * {@link #length()}{@code - 1}. 293 * 294 * <p> If the {@code char} value specified at the given index 295 * is in the high-surrogate range, the following index is less 296 * than the length of this sequence, and the 297 * {@code char} value at the following index is in the 298 * low-surrogate range, then the supplementary code point 299 * corresponding to this surrogate pair is returned. Otherwise, 300 * the {@code char} value at the given index is returned. 301 * 302 * @param index the index to the {@code char} values 303 * @return the code point value of the character at the 304 * {@code index} 305 * @exception IndexOutOfBoundsException if the {@code index} 306 * argument is negative or not less than the length of this 307 * sequence. 308 */ 309 public int codePointAt(int index) { 310 checkIndex(index, count); 311 if (isLatin1()) { 312 return value[index] & 0xff; 313 } 314 return StringUTF16.codePointAtSB(value, index, count); 315 } 316 317 /** 318 * Returns the character (Unicode code point) before the specified 319 * index. The index refers to {@code char} values 320 * (Unicode code units) and ranges from {@code 1} to {@link 321 * #length()}. 322 * 323 * <p> If the {@code char} value at {@code (index - 1)} 324 * is in the low-surrogate range, {@code (index - 2)} is not 325 * negative, and the {@code char} value at {@code (index - 326 * 2)} is in the high-surrogate range, then the 327 * supplementary code point value of the surrogate pair is 328 * returned. If the {@code char} value at {@code index - 329 * 1} is an unpaired low-surrogate or a high-surrogate, the 330 * surrogate value is returned. 331 * 332 * @param index the index following the code point that should be returned 333 * @return the Unicode code point value before the given index. 334 * @exception IndexOutOfBoundsException if the {@code index} 335 * argument is less than 1 or greater than the length 336 * of this sequence. 337 */ 338 public int codePointBefore(int index) { 339 int i = index - 1; 340 if (i < 0 || i >= count) { 341 throw new StringIndexOutOfBoundsException(index); 342 } 343 if (isLatin1()) { 344 return value[i] & 0xff; 345 } 346 return StringUTF16.codePointBeforeSB(value, index); 347 } 348 349 /** 350 * Returns the number of Unicode code points in the specified text 351 * range of this sequence. The text range begins at the specified 352 * {@code beginIndex} and extends to the {@code char} at 353 * index {@code endIndex - 1}. Thus the length (in 354 * {@code char}s) of the text range is 355 * {@code endIndex-beginIndex}. Unpaired surrogates within 356 * this sequence count as one code point each. 357 * 358 * @param beginIndex the index to the first {@code char} of 359 * the text range. 360 * @param endIndex the index after the last {@code char} of 361 * the text range. 362 * @return the number of Unicode code points in the specified text 363 * range 364 * @exception IndexOutOfBoundsException if the 365 * {@code beginIndex} is negative, or {@code endIndex} 366 * is larger than the length of this sequence, or 367 * {@code beginIndex} is larger than {@code endIndex}. 368 */ 369 public int codePointCount(int beginIndex, int endIndex) { 370 if (beginIndex < 0 || endIndex > count || beginIndex > endIndex) { 371 throw new IndexOutOfBoundsException(); 372 } 373 if (isLatin1()) { 374 return endIndex - beginIndex; 375 } 376 return StringUTF16.codePointCountSB(value, beginIndex, endIndex); 377 } 378 379 /** 380 * Returns the index within this sequence that is offset from the 381 * given {@code index} by {@code codePointOffset} code 382 * points. Unpaired surrogates within the text range given by 383 * {@code index} and {@code codePointOffset} count as 384 * one code point each. 385 * 386 * @param index the index to be offset 387 * @param codePointOffset the offset in code points 388 * @return the index within this sequence 389 * @exception IndexOutOfBoundsException if {@code index} 390 * is negative or larger then the length of this sequence, 391 * or if {@code codePointOffset} is positive and the subsequence 392 * starting with {@code index} has fewer than 393 * {@code codePointOffset} code points, 394 * or if {@code codePointOffset} is negative and the subsequence 395 * before {@code index} has fewer than the absolute value of 396 * {@code codePointOffset} code points. 397 */ 398 public int offsetByCodePoints(int index, int codePointOffset) { 399 if (index < 0 || index > count) { 400 throw new IndexOutOfBoundsException(); 401 } 402 return Character.offsetByCodePoints(this, 403 index, codePointOffset); 404 } 405 406 /** 407 * Characters are copied from this sequence into the 408 * destination character array {@code dst}. The first character to 409 * be copied is at index {@code srcBegin}; the last character to 410 * be copied is at index {@code srcEnd-1}. The total number of 411 * characters to be copied is {@code srcEnd-srcBegin}. The 412 * characters are copied into the subarray of {@code dst} starting 413 * at index {@code dstBegin} and ending at index: 414 * <pre>{@code 415 * dstbegin + (srcEnd-srcBegin) - 1 416 * }</pre> 417 * 418 * @param srcBegin start copying at this offset. 419 * @param srcEnd stop copying at this offset. 420 * @param dst the array to copy the data into. 421 * @param dstBegin offset into {@code dst}. 422 * @throws IndexOutOfBoundsException if any of the following is true: 423 * <ul> 424 * <li>{@code srcBegin} is negative 425 * <li>{@code dstBegin} is negative 426 * <li>the {@code srcBegin} argument is greater than 427 * the {@code srcEnd} argument. 428 * <li>{@code srcEnd} is greater than 429 * {@code this.length()}. 430 * <li>{@code dstBegin+srcEnd-srcBegin} is greater than 431 * {@code dst.length} 432 * </ul> 433 */ 434 public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 435 { 436 checkRangeSIOOBE(srcBegin, srcEnd, count); // compatible to old version 437 int n = srcEnd - srcBegin; 438 checkRange(dstBegin, dstBegin + n, dst.length); 439 if (isLatin1()) { 440 StringLatin1.getChars(value, srcBegin, srcEnd, dst, dstBegin); 441 } else { 442 StringUTF16.getChars(value, srcBegin, srcEnd, dst, dstBegin); 443 } 444 } 445 446 /** 447 * The character at the specified index is set to {@code ch}. This 448 * sequence is altered to represent a new character sequence that is 449 * identical to the old character sequence, except that it contains the 450 * character {@code ch} at position {@code index}. 451 * <p> 452 * The index argument must be greater than or equal to 453 * {@code 0}, and less than the length of this sequence. 454 * 455 * @param index the index of the character to modify. 456 * @param ch the new character. 457 * @throws IndexOutOfBoundsException if {@code index} is 458 * negative or greater than or equal to {@code length()}. 459 */ 460 public void setCharAt(int index, char ch) { 461 checkIndex(index, count); 462 if (isLatin1() && StringLatin1.canEncode(ch)) { 463 value[index] = (byte)ch; 464 } else { 465 if (isLatin1()) { 466 inflate(); 467 } 468 StringUTF16.putCharSB(value, index, ch); 469 } 470 } 471 472 /** 473 * Appends the string representation of the {@code Object} argument. 474 * <p> 475 * The overall effect is exactly as if the argument were converted 476 * to a string by the method {@link String#valueOf(Object)}, 477 * and the characters of that string were then 478 * {@link #append(String) appended} to this character sequence. 479 * 480 * @param obj an {@code Object}. 481 * @return a reference to this object. 482 */ 483 public AbstractStringBuilder append(Object obj) { 484 return append(String.valueOf(obj)); 485 } 486 487 /** 488 * Appends the specified string to this character sequence. 489 * <p> 490 * The characters of the {@code String} argument are appended, in 491 * order, increasing the length of this sequence by the length of the 492 * argument. If {@code str} is {@code null}, then the four 493 * characters {@code "null"} are appended. 494 * <p> 495 * Let <i>n</i> be the length of this character sequence just prior to 496 * execution of the {@code append} method. Then the character at 497 * index <i>k</i> in the new character sequence is equal to the character 498 * at index <i>k</i> in the old character sequence, if <i>k</i> is less 499 * than <i>n</i>; otherwise, it is equal to the character at index 500 * <i>k-n</i> in the argument {@code str}. 501 * 502 * @param str a string. 503 * @return a reference to this object. 504 */ 505 public AbstractStringBuilder append(String str) { 506 if (str == null) { 507 return appendNull(); 508 } 509 int len = str.length(); 510 ensureCapacityInternal(count + len); 511 putStringAt(count, str); 512 count += len; 513 return this; 514 } 515 516 // Documentation in subclasses because of synchro difference 517 public AbstractStringBuilder append(StringBuffer sb) { 518 return this.append((AbstractStringBuilder)sb); 519 } 520 521 /** 522 * @since 1.8 523 */ 524 AbstractStringBuilder append(AbstractStringBuilder asb) { 525 if (asb == null) { 526 return appendNull(); 527 } 528 int len = asb.length(); 529 ensureCapacityInternal(count + len); 530 if (getCoder() != asb.getCoder()) { 531 inflate(); 532 } 533 asb.getBytes(value, count, coder); 534 count += len; 535 return this; 536 } 537 538 // Documentation in subclasses because of synchro difference 539 @Override 540 public AbstractStringBuilder append(CharSequence s) { 541 if (s == null) { 542 return appendNull(); 543 } 544 if (s instanceof String) { 545 return this.append((String)s); 546 } 547 if (s instanceof AbstractStringBuilder) { 548 return this.append((AbstractStringBuilder)s); 549 } 550 return this.append(s, 0, s.length()); 551 } 552 553 private AbstractStringBuilder appendNull() { 554 ensureCapacityInternal(count + 4); 555 int count = this.count; 556 byte[] val = this.value; 557 if (isLatin1()) { 558 val[count++] = 'n'; 559 val[count++] = 'u'; 560 val[count++] = 'l'; 561 val[count++] = 'l'; 562 } else { 563 checkOffset(count + 4, val.length >> 1); 564 StringUTF16.putChar(val, count++, 'n'); 565 StringUTF16.putChar(val, count++, 'u'); 566 StringUTF16.putChar(val, count++, 'l'); 567 StringUTF16.putChar(val, count++, 'l'); 568 } 569 this.count = count; 570 return this; 571 } 572 573 /** 574 * Appends a subsequence of the specified {@code CharSequence} to this 575 * sequence. 576 * <p> 577 * Characters of the argument {@code s}, starting at 578 * index {@code start}, are appended, in order, to the contents of 579 * this sequence up to the (exclusive) index {@code end}. The length 580 * of this sequence is increased by the value of {@code end - start}. 581 * <p> 582 * Let <i>n</i> be the length of this character sequence just prior to 583 * execution of the {@code append} method. Then the character at 584 * index <i>k</i> in this character sequence becomes equal to the 585 * character at index <i>k</i> in this sequence, if <i>k</i> is less than 586 * <i>n</i>; otherwise, it is equal to the character at index 587 * <i>k+start-n</i> in the argument {@code s}. 588 * <p> 589 * If {@code s} is {@code null}, then this method appends 590 * characters as if the s parameter was a sequence containing the four 591 * characters {@code "null"}. 592 * 593 * @param s the sequence to append. 594 * @param start the starting index of the subsequence to be appended. 595 * @param end the end index of the subsequence to be appended. 596 * @return a reference to this object. 597 * @throws IndexOutOfBoundsException if 598 * {@code start} is negative, or 599 * {@code start} is greater than {@code end} or 600 * {@code end} is greater than {@code s.length()} 601 */ 602 @Override 603 public AbstractStringBuilder append(CharSequence s, int start, int end) { 604 if (s == null) { 605 s = "null"; 606 } 607 checkRange(start, end, s.length()); 608 int len = end - start; 609 ensureCapacityInternal(count + len); 610 appendChars(s, start, end); 611 return this; 612 } 613 614 /** 615 * Appends the string representation of the {@code char} array 616 * argument to this sequence. 617 * <p> 618 * The characters of the array argument are appended, in order, to 619 * the contents of this sequence. The length of this sequence 620 * increases by the length of the argument. 621 * <p> 622 * The overall effect is exactly as if the argument were converted 623 * to a string by the method {@link String#valueOf(char[])}, 624 * and the characters of that string were then 625 * {@link #append(String) appended} to this character sequence. 626 * 627 * @param str the characters to be appended. 628 * @return a reference to this object. 629 */ 630 public AbstractStringBuilder append(char[] str) { 631 int len = str.length; 632 ensureCapacityInternal(count + len); 633 appendChars(str, 0, len); 634 return this; 635 } 636 637 /** 638 * Appends the string representation of a subarray of the 639 * {@code char} array argument to this sequence. 640 * <p> 641 * Characters of the {@code char} array {@code str}, starting at 642 * index {@code offset}, are appended, in order, to the contents 643 * of this sequence. The length of this sequence increases 644 * by the value of {@code len}. 645 * <p> 646 * The overall effect is exactly as if the arguments were converted 647 * to a string by the method {@link String#valueOf(char[],int,int)}, 648 * and the characters of that string were then 649 * {@link #append(String) appended} to this character sequence. 650 * 651 * @param str the characters to be appended. 652 * @param offset the index of the first {@code char} to append. 653 * @param len the number of {@code char}s to append. 654 * @return a reference to this object. 655 * @throws IndexOutOfBoundsException 656 * if {@code offset < 0} or {@code len < 0} 657 * or {@code offset+len > str.length} 658 */ 659 public AbstractStringBuilder append(char str[], int offset, int len) { 660 int end = offset + len; 661 checkRange(offset, end, str.length); 662 ensureCapacityInternal(count + len); 663 appendChars(str, offset, end); 664 return this; 665 } 666 667 /** 668 * Appends the string representation of the {@code boolean} 669 * argument to the sequence. 670 * <p> 671 * The overall effect is exactly as if the argument were converted 672 * to a string by the method {@link String#valueOf(boolean)}, 673 * and the characters of that string were then 674 * {@link #append(String) appended} to this character sequence. 675 * 676 * @param b a {@code boolean}. 677 * @return a reference to this object. 678 */ 679 public AbstractStringBuilder append(boolean b) { 680 ensureCapacityInternal(count + (b ? 4 : 5)); 681 int count = this.count; 682 byte[] val = this.value; 683 if (isLatin1()) { 684 if (b) { 685 val[count++] = 't'; 686 val[count++] = 'r'; 687 val[count++] = 'u'; 688 val[count++] = 'e'; 689 } else { 690 val[count++] = 'f'; 691 val[count++] = 'a'; 692 val[count++] = 'l'; 693 val[count++] = 's'; 694 val[count++] = 'e'; 695 } 696 } else { 697 if (b) { 698 checkOffset(count + 4, val.length >> 1); 699 StringUTF16.putChar(val, count++, 't'); 700 StringUTF16.putChar(val, count++, 'r'); 701 StringUTF16.putChar(val, count++, 'u'); 702 StringUTF16.putChar(val, count++, 'e'); 703 } else { 704 checkOffset(count + 5, val.length >> 1); 705 StringUTF16.putChar(val, count++, 'f'); 706 StringUTF16.putChar(val, count++, 'a'); 707 StringUTF16.putChar(val, count++, 'l'); 708 StringUTF16.putChar(val, count++, 's'); 709 StringUTF16.putChar(val, count++, 'e'); 710 } 711 } 712 this.count = count; 713 return this; 714 } 715 716 /** 717 * Appends the string representation of the {@code char} 718 * argument to this sequence. 719 * <p> 720 * The argument is appended to the contents of this sequence. 721 * The length of this sequence increases by {@code 1}. 722 * <p> 723 * The overall effect is exactly as if the argument were converted 724 * to a string by the method {@link String#valueOf(char)}, 725 * and the character in that string were then 726 * {@link #append(String) appended} to this character sequence. 727 * 728 * @param c a {@code char}. 729 * @return a reference to this object. 730 */ 731 @Override 732 public AbstractStringBuilder append(char c) { 733 ensureCapacityInternal(count + 1); 734 if (isLatin1() && StringLatin1.canEncode(c)) { 735 value[count++] = (byte)c; 736 } else { 737 if (isLatin1()) { 738 inflate(); 739 } 740 StringUTF16.putCharSB(value, count++, c); 741 } 742 return this; 743 } 744 745 /** 746 * Appends the string representation of the {@code int} 747 * argument to this sequence. 748 * <p> 749 * The overall effect is exactly as if the argument were converted 750 * to a string by the method {@link String#valueOf(int)}, 751 * and the characters of that string were then 752 * {@link #append(String) appended} to this character sequence. 753 * 754 * @param i an {@code int}. 755 * @return a reference to this object. 756 */ 757 public AbstractStringBuilder append(int i) { 758 int spaceNeeded = count + Integer.stringSize(i); 759 ensureCapacityInternal(spaceNeeded); 760 if (isLatin1()) { 761 Integer.getChars(i, spaceNeeded, value); 762 } else { 763 byte[] val = this.value; 764 checkOffset(spaceNeeded, val.length >> 1); 765 Integer.getCharsUTF16(i, spaceNeeded, val); 766 } 767 count = spaceNeeded; 768 return this; 769 } 770 771 /** 772 * Appends the string representation of the {@code long} 773 * argument to this sequence. 774 * <p> 775 * The overall effect is exactly as if the argument were converted 776 * to a string by the method {@link String#valueOf(long)}, 777 * and the characters of that string were then 778 * {@link #append(String) appended} to this character sequence. 779 * 780 * @param l a {@code long}. 781 * @return a reference to this object. 782 */ 783 public AbstractStringBuilder append(long l) { 784 int spaceNeeded = count + Long.stringSize(l); 785 ensureCapacityInternal(spaceNeeded); 786 if (isLatin1()) { 787 Long.getChars(l, spaceNeeded, value); 788 } else { 789 byte[] val = this.value; 790 checkOffset(spaceNeeded, val.length >> 1); 791 Long.getCharsUTF16(l, spaceNeeded, val); 792 } 793 count = spaceNeeded; 794 return this; 795 } 796 797 /** 798 * Appends the string representation of the {@code float} 799 * argument to this sequence. 800 * <p> 801 * The overall effect is exactly as if the argument were converted 802 * to a string by the method {@link String#valueOf(float)}, 803 * and the characters of that string were then 804 * {@link #append(String) appended} to this character sequence. 805 * 806 * @param f a {@code float}. 807 * @return a reference to this object. 808 */ 809 public AbstractStringBuilder append(float f) { 810 FloatingDecimal.appendTo(f,this); 811 return this; 812 } 813 814 /** 815 * Appends the string representation of the {@code double} 816 * argument to this sequence. 817 * <p> 818 * The overall effect is exactly as if the argument were converted 819 * to a string by the method {@link String#valueOf(double)}, 820 * and the characters of that string were then 821 * {@link #append(String) appended} to this character sequence. 822 * 823 * @param d a {@code double}. 824 * @return a reference to this object. 825 */ 826 public AbstractStringBuilder append(double d) { 827 FloatingDecimal.appendTo(d,this); 828 return this; 829 } 830 831 /** 832 * Removes the characters in a substring of this sequence. 833 * The substring begins at the specified {@code start} and extends to 834 * the character at index {@code end - 1} or to the end of the 835 * sequence if no such character exists. If 836 * {@code start} is equal to {@code end}, no changes are made. 837 * 838 * @param start The beginning index, inclusive. 839 * @param end The ending index, exclusive. 840 * @return This object. 841 * @throws StringIndexOutOfBoundsException if {@code start} 842 * is negative, greater than {@code length()}, or 843 * greater than {@code end}. 844 */ 845 public AbstractStringBuilder delete(int start, int end) { 846 if (end > count) { 847 end = count; 848 } 849 checkRangeSIOOBE(start, end, count); 850 int len = end - start; 851 if (len > 0) { 852 shift(end, -len); 853 count -= len; 854 } 855 return this; 856 } 857 858 /** 859 * Appends the string representation of the {@code codePoint} 860 * argument to this sequence. 861 * 862 * <p> The argument is appended to the contents of this sequence. 863 * The length of this sequence increases by 864 * {@link Character#charCount(int) Character.charCount(codePoint)}. 865 * 866 * <p> The overall effect is exactly as if the argument were 867 * converted to a {@code char} array by the method 868 * {@link Character#toChars(int)} and the character in that array 869 * were then {@link #append(char[]) appended} to this character 870 * sequence. 871 * 872 * @param codePoint a Unicode code point 873 * @return a reference to this object. 874 * @exception IllegalArgumentException if the specified 875 * {@code codePoint} isn't a valid Unicode code point 876 */ 877 public AbstractStringBuilder appendCodePoint(int codePoint) { 878 if (Character.isBmpCodePoint(codePoint)) { 879 return append((char)codePoint); 880 } 881 return append(Character.toChars(codePoint)); 882 } 883 884 /** 885 * Removes the {@code char} at the specified position in this 886 * sequence. This sequence is shortened by one {@code char}. 887 * 888 * <p>Note: If the character at the given index is a supplementary 889 * character, this method does not remove the entire character. If 890 * correct handling of supplementary characters is required, 891 * determine the number of {@code char}s to remove by calling 892 * {@code Character.charCount(thisSequence.codePointAt(index))}, 893 * where {@code thisSequence} is this sequence. 894 * 895 * @param index Index of {@code char} to remove 896 * @return This object. 897 * @throws StringIndexOutOfBoundsException if the {@code index} 898 * is negative or greater than or equal to 899 * {@code length()}. 900 */ 901 public AbstractStringBuilder deleteCharAt(int index) { 902 checkIndex(index, count); 903 shift(index + 1, -1); 904 count--; 905 return this; 906 } 907 908 /** 909 * Replaces the characters in a substring of this sequence 910 * with characters in the specified {@code String}. The substring 911 * begins at the specified {@code start} and extends to the character 912 * at index {@code end - 1} or to the end of the 913 * sequence if no such character exists. First the 914 * characters in the substring are removed and then the specified 915 * {@code String} is inserted at {@code start}. (This 916 * sequence will be lengthened to accommodate the 917 * specified String if necessary.) 918 * 919 * @param start The beginning index, inclusive. 920 * @param end The ending index, exclusive. 921 * @param str String that will replace previous contents. 922 * @return This object. 923 * @throws StringIndexOutOfBoundsException if {@code start} 924 * is negative, greater than {@code length()}, or 925 * greater than {@code end}. 926 */ 927 public AbstractStringBuilder replace(int start, int end, String str) { 928 if (end > count) { 929 end = count; 930 } 931 checkRangeSIOOBE(start, end, count); 932 int len = str.length(); 933 int newCount = count + len - (end - start); 934 ensureCapacityInternal(newCount); 935 shift(end, newCount - count); 936 count = newCount; 937 putStringAt(start, str); 938 return this; 939 } 940 941 /** 942 * Returns a new {@code String} that contains a subsequence of 943 * characters currently contained in this character sequence. The 944 * substring begins at the specified index and extends to the end of 945 * this sequence. 946 * 947 * @param start The beginning index, inclusive. 948 * @return The new string. 949 * @throws StringIndexOutOfBoundsException if {@code start} is 950 * less than zero, or greater than the length of this object. 951 */ 952 public String substring(int start) { 953 return substring(start, count); 954 } 955 956 /** 957 * Returns a new character sequence that is a subsequence of this sequence. 958 * 959 * <p> An invocation of this method of the form 960 * 961 * <pre>{@code 962 * sb.subSequence(begin, end)}</pre> 963 * 964 * behaves in exactly the same way as the invocation 965 * 966 * <pre>{@code 967 * sb.substring(begin, end)}</pre> 968 * 969 * This method is provided so that this class can 970 * implement the {@link CharSequence} interface. 971 * 972 * @param start the start index, inclusive. 973 * @param end the end index, exclusive. 974 * @return the specified subsequence. 975 * 976 * @throws IndexOutOfBoundsException 977 * if {@code start} or {@code end} are negative, 978 * if {@code end} is greater than {@code length()}, 979 * or if {@code start} is greater than {@code end} 980 * @spec JSR-51 981 */ 982 @Override 983 public CharSequence subSequence(int start, int end) { 984 return substring(start, end); 985 } 986 987 /** 988 * Returns a new {@code String} that contains a subsequence of 989 * characters currently contained in this sequence. The 990 * substring begins at the specified {@code start} and 991 * extends to the character at index {@code end - 1}. 992 * 993 * @param start The beginning index, inclusive. 994 * @param end The ending index, exclusive. 995 * @return The new string. 996 * @throws StringIndexOutOfBoundsException if {@code start} 997 * or {@code end} are negative or greater than 998 * {@code length()}, or {@code start} is 999 * greater than {@code end}. 1000 */ 1001 public String substring(int start, int end) { 1002 checkRangeSIOOBE(start, end, count); 1003 if (isLatin1()) { 1004 return StringLatin1.newString(value, start, end - start); 1005 } 1006 return StringUTF16.newString(value, start, end - start); 1007 } 1008 1009 private void shift(int offset, int n) { 1010 System.arraycopy(value, offset << coder, 1011 value, (offset + n) << coder, (count - offset) << coder); 1012 } 1013 1014 /** 1015 * Inserts the string representation of a subarray of the {@code str} 1016 * array argument into this sequence. The subarray begins at the 1017 * specified {@code offset} and extends {@code len} {@code char}s. 1018 * The characters of the subarray are inserted into this sequence at 1019 * the position indicated by {@code index}. The length of this 1020 * sequence increases by {@code len} {@code char}s. 1021 * 1022 * @param index position at which to insert subarray. 1023 * @param str A {@code char} array. 1024 * @param offset the index of the first {@code char} in subarray to 1025 * be inserted. 1026 * @param len the number of {@code char}s in the subarray to 1027 * be inserted. 1028 * @return This object 1029 * @throws StringIndexOutOfBoundsException if {@code index} 1030 * is negative or greater than {@code length()}, or 1031 * {@code offset} or {@code len} are negative, or 1032 * {@code (offset+len)} is greater than 1033 * {@code str.length}. 1034 */ 1035 public AbstractStringBuilder insert(int index, char[] str, int offset, 1036 int len) 1037 { 1038 checkOffset(index, count); 1039 checkRangeSIOOBE(offset, offset + len, str.length); 1040 ensureCapacityInternal(count + len); 1041 shift(index, len); 1042 count += len; 1043 putCharsAt(index, str, offset, offset + len); 1044 return this; 1045 } 1046 1047 /** 1048 * Inserts the string representation of the {@code Object} 1049 * argument into this character sequence. 1050 * <p> 1051 * The overall effect is exactly as if the second argument were 1052 * converted to a string by the method {@link String#valueOf(Object)}, 1053 * and the characters of that string were then 1054 * {@link #insert(int,String) inserted} into this character 1055 * sequence at the indicated offset. 1056 * <p> 1057 * The {@code offset} argument must be greater than or equal to 1058 * {@code 0}, and less than or equal to the {@linkplain #length() length} 1059 * of this sequence. 1060 * 1061 * @param offset the offset. 1062 * @param obj an {@code Object}. 1063 * @return a reference to this object. 1064 * @throws StringIndexOutOfBoundsException if the offset is invalid. 1065 */ 1066 public AbstractStringBuilder insert(int offset, Object obj) { 1067 return insert(offset, String.valueOf(obj)); 1068 } 1069 1070 /** 1071 * Inserts the string into this character sequence. 1072 * <p> 1073 * The characters of the {@code String} argument are inserted, in 1074 * order, into this sequence at the indicated offset, moving up any 1075 * characters originally above that position and increasing the length 1076 * of this sequence by the length of the argument. If 1077 * {@code str} is {@code null}, then the four characters 1078 * {@code "null"} are inserted into this sequence. 1079 * <p> 1080 * The character at index <i>k</i> in the new character sequence is 1081 * equal to: 1082 * <ul> 1083 * <li>the character at index <i>k</i> in the old character sequence, if 1084 * <i>k</i> is less than {@code offset} 1085 * <li>the character at index <i>k</i>{@code -offset} in the 1086 * argument {@code str}, if <i>k</i> is not less than 1087 * {@code offset} but is less than {@code offset+str.length()} 1088 * <li>the character at index <i>k</i>{@code -str.length()} in the 1089 * old character sequence, if <i>k</i> is not less than 1090 * {@code offset+str.length()} 1091 * </ul><p> 1092 * The {@code offset} argument must be greater than or equal to 1093 * {@code 0}, and less than or equal to the {@linkplain #length() length} 1094 * of this sequence. 1095 * 1096 * @param offset the offset. 1097 * @param str a string. 1098 * @return a reference to this object. 1099 * @throws StringIndexOutOfBoundsException if the offset is invalid. 1100 */ 1101 public AbstractStringBuilder insert(int offset, String str) { 1102 checkOffset(offset, count); 1103 if (str == null) { 1104 str = "null"; 1105 } 1106 int len = str.length(); 1107 ensureCapacityInternal(count + len); 1108 shift(offset, len); 1109 count += len; 1110 putStringAt(offset, str); 1111 return this; 1112 } 1113 1114 /** 1115 * Inserts the string representation of the {@code char} array 1116 * argument into this sequence. 1117 * <p> 1118 * The characters of the array argument are inserted into the 1119 * contents of this sequence at the position indicated by 1120 * {@code offset}. The length of this sequence increases by 1121 * the length of the argument. 1122 * <p> 1123 * The overall effect is exactly as if the second argument were 1124 * converted to a string by the method {@link String#valueOf(char[])}, 1125 * and the characters of that string were then 1126 * {@link #insert(int,String) inserted} into this character 1127 * sequence at the indicated offset. 1128 * <p> 1129 * The {@code offset} argument must be greater than or equal to 1130 * {@code 0}, and less than or equal to the {@linkplain #length() length} 1131 * of this sequence. 1132 * 1133 * @param offset the offset. 1134 * @param str a character array. 1135 * @return a reference to this object. 1136 * @throws StringIndexOutOfBoundsException if the offset is invalid. 1137 */ 1138 public AbstractStringBuilder insert(int offset, char[] str) { 1139 checkOffset(offset, count); 1140 int len = str.length; 1141 ensureCapacityInternal(count + len); 1142 shift(offset, len); 1143 count += len; 1144 putCharsAt(offset, str, 0, len); 1145 return this; 1146 } 1147 1148 /** 1149 * Inserts the specified {@code CharSequence} into this sequence. 1150 * <p> 1151 * The characters of the {@code CharSequence} argument are inserted, 1152 * in order, into this sequence at the indicated offset, moving up 1153 * any characters originally above that position and increasing the length 1154 * of this sequence by the length of the argument s. 1155 * <p> 1156 * The result of this method is exactly the same as if it were an 1157 * invocation of this object's 1158 * {@link #insert(int,CharSequence,int,int) insert}(dstOffset, s, 0, s.length()) 1159 * method. 1160 * 1161 * <p>If {@code s} is {@code null}, then the four characters 1162 * {@code "null"} are inserted into this sequence. 1163 * 1164 * @param dstOffset the offset. 1165 * @param s the sequence to be inserted 1166 * @return a reference to this object. 1167 * @throws IndexOutOfBoundsException if the offset is invalid. 1168 */ 1169 public AbstractStringBuilder insert(int dstOffset, CharSequence s) { 1170 if (s == null) { 1171 s = "null"; 1172 } 1173 if (s instanceof String) { 1174 return this.insert(dstOffset, (String)s); 1175 } 1176 return this.insert(dstOffset, s, 0, s.length()); 1177 } 1178 1179 /** 1180 * Inserts a subsequence of the specified {@code CharSequence} into 1181 * this sequence. 1182 * <p> 1183 * The subsequence of the argument {@code s} specified by 1184 * {@code start} and {@code end} are inserted, 1185 * in order, into this sequence at the specified destination offset, moving 1186 * up any characters originally above that position. The length of this 1187 * sequence is increased by {@code end - start}. 1188 * <p> 1189 * The character at index <i>k</i> in this sequence becomes equal to: 1190 * <ul> 1191 * <li>the character at index <i>k</i> in this sequence, if 1192 * <i>k</i> is less than {@code dstOffset} 1193 * <li>the character at index <i>k</i>{@code +start-dstOffset} in 1194 * the argument {@code s}, if <i>k</i> is greater than or equal to 1195 * {@code dstOffset} but is less than {@code dstOffset+end-start} 1196 * <li>the character at index <i>k</i>{@code -(end-start)} in this 1197 * sequence, if <i>k</i> is greater than or equal to 1198 * {@code dstOffset+end-start} 1199 * </ul><p> 1200 * The {@code dstOffset} argument must be greater than or equal to 1201 * {@code 0}, and less than or equal to the {@linkplain #length() length} 1202 * of this sequence. 1203 * <p>The start argument must be nonnegative, and not greater than 1204 * {@code end}. 1205 * <p>The end argument must be greater than or equal to 1206 * {@code start}, and less than or equal to the length of s. 1207 * 1208 * <p>If {@code s} is {@code null}, then this method inserts 1209 * characters as if the s parameter was a sequence containing the four 1210 * characters {@code "null"}. 1211 * 1212 * @param dstOffset the offset in this sequence. 1213 * @param s the sequence to be inserted. 1214 * @param start the starting index of the subsequence to be inserted. 1215 * @param end the end index of the subsequence to be inserted. 1216 * @return a reference to this object. 1217 * @throws IndexOutOfBoundsException if {@code dstOffset} 1218 * is negative or greater than {@code this.length()}, or 1219 * {@code start} or {@code end} are negative, or 1220 * {@code start} is greater than {@code end} or 1221 * {@code end} is greater than {@code s.length()} 1222 */ 1223 public AbstractStringBuilder insert(int dstOffset, CharSequence s, 1224 int start, int end) 1225 { 1226 if (s == null) { 1227 s = "null"; 1228 } 1229 checkOffset(dstOffset, count); 1230 checkRange(start, end, s.length()); 1231 int len = end - start; 1232 ensureCapacityInternal(count + len); 1233 shift(dstOffset, len); 1234 count += len; 1235 putCharsAt(dstOffset, s, start, end); 1236 return this; 1237 } 1238 1239 /** 1240 * Inserts the string representation of the {@code boolean} 1241 * argument into this sequence. 1242 * <p> 1243 * The overall effect is exactly as if the second argument were 1244 * converted to a string by the method {@link String#valueOf(boolean)}, 1245 * and the characters of that string were then 1246 * {@link #insert(int,String) inserted} into this character 1247 * sequence at the indicated offset. 1248 * <p> 1249 * The {@code offset} argument must be greater than or equal to 1250 * {@code 0}, and less than or equal to the {@linkplain #length() length} 1251 * of this sequence. 1252 * 1253 * @param offset the offset. 1254 * @param b a {@code boolean}. 1255 * @return a reference to this object. 1256 * @throws StringIndexOutOfBoundsException if the offset is invalid. 1257 */ 1258 public AbstractStringBuilder insert(int offset, boolean b) { 1259 return insert(offset, String.valueOf(b)); 1260 } 1261 1262 /** 1263 * Inserts the string representation of the {@code char} 1264 * argument into this sequence. 1265 * <p> 1266 * The overall effect is exactly as if the second argument were 1267 * converted to a string by the method {@link String#valueOf(char)}, 1268 * and the character in that string were then 1269 * {@link #insert(int,String) inserted} into this character 1270 * sequence at the indicated offset. 1271 * <p> 1272 * The {@code offset} argument must be greater than or equal to 1273 * {@code 0}, and less than or equal to the {@linkplain #length() length} 1274 * of this sequence. 1275 * 1276 * @param offset the offset. 1277 * @param c a {@code char}. 1278 * @return a reference to this object. 1279 * @throws IndexOutOfBoundsException if the offset is invalid. 1280 */ 1281 public AbstractStringBuilder insert(int offset, char c) { 1282 checkOffset(offset, count); 1283 ensureCapacityInternal(count + 1); 1284 shift(offset, 1); 1285 count += 1; 1286 if (isLatin1() && StringLatin1.canEncode(c)) { 1287 value[offset] = (byte)c; 1288 } else { 1289 if (isLatin1()) { 1290 inflate(); 1291 } 1292 StringUTF16.putCharSB(value, offset, c); 1293 } 1294 return this; 1295 } 1296 1297 /** 1298 * Inserts the string representation of the second {@code int} 1299 * argument into this sequence. 1300 * <p> 1301 * The overall effect is exactly as if the second argument were 1302 * converted to a string by the method {@link String#valueOf(int)}, 1303 * and the characters of that string were then 1304 * {@link #insert(int,String) inserted} into this character 1305 * sequence at the indicated offset. 1306 * <p> 1307 * The {@code offset} argument must be greater than or equal to 1308 * {@code 0}, and less than or equal to the {@linkplain #length() length} 1309 * of this sequence. 1310 * 1311 * @param offset the offset. 1312 * @param i an {@code int}. 1313 * @return a reference to this object. 1314 * @throws StringIndexOutOfBoundsException if the offset is invalid. 1315 */ 1316 public AbstractStringBuilder insert(int offset, int i) { 1317 return insert(offset, String.valueOf(i)); 1318 } 1319 1320 /** 1321 * Inserts the string representation of the {@code long} 1322 * argument into this sequence. 1323 * <p> 1324 * The overall effect is exactly as if the second argument were 1325 * converted to a string by the method {@link String#valueOf(long)}, 1326 * and the characters of that string were then 1327 * {@link #insert(int,String) inserted} into this character 1328 * sequence at the indicated offset. 1329 * <p> 1330 * The {@code offset} argument must be greater than or equal to 1331 * {@code 0}, and less than or equal to the {@linkplain #length() length} 1332 * of this sequence. 1333 * 1334 * @param offset the offset. 1335 * @param l a {@code long}. 1336 * @return a reference to this object. 1337 * @throws StringIndexOutOfBoundsException if the offset is invalid. 1338 */ 1339 public AbstractStringBuilder insert(int offset, long l) { 1340 return insert(offset, String.valueOf(l)); 1341 } 1342 1343 /** 1344 * Inserts the string representation of the {@code float} 1345 * argument into this sequence. 1346 * <p> 1347 * The overall effect is exactly as if the second argument were 1348 * converted to a string by the method {@link String#valueOf(float)}, 1349 * and the characters of that string were then 1350 * {@link #insert(int,String) inserted} into this character 1351 * sequence at the indicated offset. 1352 * <p> 1353 * The {@code offset} argument must be greater than or equal to 1354 * {@code 0}, and less than or equal to the {@linkplain #length() length} 1355 * of this sequence. 1356 * 1357 * @param offset the offset. 1358 * @param f a {@code float}. 1359 * @return a reference to this object. 1360 * @throws StringIndexOutOfBoundsException if the offset is invalid. 1361 */ 1362 public AbstractStringBuilder insert(int offset, float f) { 1363 return insert(offset, String.valueOf(f)); 1364 } 1365 1366 /** 1367 * Inserts the string representation of the {@code double} 1368 * argument into this sequence. 1369 * <p> 1370 * The overall effect is exactly as if the second argument were 1371 * converted to a string by the method {@link String#valueOf(double)}, 1372 * and the characters of that string were then 1373 * {@link #insert(int,String) inserted} into this character 1374 * sequence at the indicated offset. 1375 * <p> 1376 * The {@code offset} argument must be greater than or equal to 1377 * {@code 0}, and less than or equal to the {@linkplain #length() length} 1378 * of this sequence. 1379 * 1380 * @param offset the offset. 1381 * @param d a {@code double}. 1382 * @return a reference to this object. 1383 * @throws StringIndexOutOfBoundsException if the offset is invalid. 1384 */ 1385 public AbstractStringBuilder insert(int offset, double d) { 1386 return insert(offset, String.valueOf(d)); 1387 } 1388 1389 /** 1390 * Returns the index within this string of the first occurrence of the 1391 * specified substring. 1392 * 1393 * <p>The returned index is the smallest value {@code k} for which: 1394 * <pre>{@code 1395 * this.toString().startsWith(str, k) 1396 * }</pre> 1397 * If no such value of {@code k} exists, then {@code -1} is returned. 1398 * 1399 * @param str the substring to search for. 1400 * @return the index of the first occurrence of the specified substring, 1401 * or {@code -1} if there is no such occurrence. 1402 */ 1403 public int indexOf(String str) { 1404 return indexOf(str, 0); 1405 } 1406 1407 /** 1408 * Returns the index within this string of the first occurrence of the 1409 * specified substring, starting at the specified index. 1410 * 1411 * <p>The returned index is the smallest value {@code k} for which: 1412 * <pre>{@code 1413 * k >= Math.min(fromIndex, this.length()) && 1414 * this.toString().startsWith(str, k) 1415 * }</pre> 1416 * If no such value of {@code k} exists, then {@code -1} is returned. 1417 * 1418 * @param str the substring to search for. 1419 * @param fromIndex the index from which to start the search. 1420 * @return the index of the first occurrence of the specified substring, 1421 * starting at the specified index, 1422 * or {@code -1} if there is no such occurrence. 1423 */ 1424 public int indexOf(String str, int fromIndex) { 1425 return String.indexOf(value, coder, count, str, fromIndex); 1426 } 1427 1428 /** 1429 * Returns the index within this string of the last occurrence of the 1430 * specified substring. The last occurrence of the empty string "" is 1431 * considered to occur at the index value {@code this.length()}. 1432 * 1433 * <p>The returned index is the largest value {@code k} for which: 1434 * <pre>{@code 1435 * this.toString().startsWith(str, k) 1436 * }</pre> 1437 * If no such value of {@code k} exists, then {@code -1} is returned. 1438 * 1439 * @param str the substring to search for. 1440 * @return the index of the last occurrence of the specified substring, 1441 * or {@code -1} if there is no such occurrence. 1442 */ 1443 public int lastIndexOf(String str) { 1444 return lastIndexOf(str, count); 1445 } 1446 1447 /** 1448 * Returns the index within this string of the last occurrence of the 1449 * specified substring, searching backward starting at the specified index. 1450 * 1451 * <p>The returned index is the largest value {@code k} for which: 1452 * <pre>{@code 1453 * k <= Math.min(fromIndex, this.length()) && 1454 * this.toString().startsWith(str, k) 1455 * }</pre> 1456 * If no such value of {@code k} exists, then {@code -1} is returned. 1457 * 1458 * @param str the substring to search for. 1459 * @param fromIndex the index to start the search from. 1460 * @return the index of the last occurrence of the specified substring, 1461 * searching backward from the specified index, 1462 * or {@code -1} if there is no such occurrence. 1463 */ 1464 public int lastIndexOf(String str, int fromIndex) { 1465 return String.lastIndexOf(value, coder, count, str, fromIndex); 1466 } 1467 1468 /** 1469 * Causes this character sequence to be replaced by the reverse of 1470 * the sequence. If there are any surrogate pairs included in the 1471 * sequence, these are treated as single characters for the 1472 * reverse operation. Thus, the order of the high-low surrogates 1473 * is never reversed. 1474 * 1475 * Let <i>n</i> be the character length of this character sequence 1476 * (not the length in {@code char} values) just prior to 1477 * execution of the {@code reverse} method. Then the 1478 * character at index <i>k</i> in the new character sequence is 1479 * equal to the character at index <i>n-k-1</i> in the old 1480 * character sequence. 1481 * 1482 * <p>Note that the reverse operation may result in producing 1483 * surrogate pairs that were unpaired low-surrogates and 1484 * high-surrogates before the operation. For example, reversing 1485 * "\u005CuDC00\u005CuD800" produces "\u005CuD800\u005CuDC00" which is 1486 * a valid surrogate pair. 1487 * 1488 * @return a reference to this object. 1489 */ 1490 public AbstractStringBuilder reverse() { 1491 byte[] val = this.value; 1492 int count = this.count; 1493 int coder = this.coder; 1494 int n = count - 1; 1495 if (COMPACT_STRINGS && coder == LATIN1) { 1496 for (int j = (n-1) >> 1; j >= 0; j--) { 1497 int k = n - j; 1498 byte cj = val[j]; 1499 val[j] = val[k]; 1500 val[k] = cj; 1501 } 1502 } else { 1503 checkOffset(count, val.length >> 1); 1504 boolean hasSurrogates = false; 1505 for (int j = (n-1) >> 1; j >= 0; j--) { 1506 int k = n - j; 1507 char cj = StringUTF16.getChar(val, j); 1508 char ck = StringUTF16.getChar(val, k); 1509 StringUTF16.putChar(val, j, ck); 1510 StringUTF16.putChar(val, k, cj); 1511 if (Character.isSurrogate(cj) || 1512 Character.isSurrogate(ck)) { 1513 hasSurrogates = true; 1514 } 1515 } 1516 if (hasSurrogates) { 1517 reverseAllValidSurrogatePairs(val, count); 1518 } 1519 } 1520 return this; 1521 } 1522 1523 /** Outlined helper method for reverse() */ 1524 private void reverseAllValidSurrogatePairs(byte[] val, int count) { 1525 for (int i = 0; i < count - 1; i++) { 1526 char c2 = StringUTF16.getChar(val, i); 1527 if (Character.isLowSurrogate(c2)) { 1528 char c1 = StringUTF16.getChar(val, i + 1); 1529 if (Character.isHighSurrogate(c1)) { 1530 StringUTF16.putChar(val, i++, c1); 1531 StringUTF16.putChar(val, i, c2); 1532 } 1533 } 1534 } 1535 } 1536 1537 /** 1538 * Returns a string representing the data in this sequence. 1539 * A new {@code String} object is allocated and initialized to 1540 * contain the character sequence currently represented by this 1541 * object. This {@code String} is then returned. Subsequent 1542 * changes to this sequence do not affect the contents of the 1543 * {@code String}. 1544 * 1545 * @return a string representation of this sequence of characters. 1546 */ 1547 @Override 1548 public abstract String toString(); 1549 1550 /** 1551 * {@inheritDoc} 1552 * @since 9 1553 */ 1554 @Override 1555 public IntStream chars() { 1556 // Reuse String-based spliterator. This requires a supplier to 1557 // capture the value and count when the terminal operation is executed 1558 return StreamSupport.intStream( 1559 () -> { 1560 // The combined set of field reads are not atomic and thread 1561 // safe but bounds checks will ensure no unsafe reads from 1562 // the byte array 1563 byte[] val = this.value; 1564 int count = this.count; 1565 byte coder = this.coder; 1566 return coder == LATIN1 1567 ? new StringLatin1.CharsSpliterator(val, 0, count, 0) 1568 : new StringUTF16.CharsSpliterator(val, 0, count, 0); 1569 }, 1570 Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED, 1571 false); 1572 } 1573 1574 /** 1575 * {@inheritDoc} 1576 * @since 9 1577 */ 1578 @Override 1579 public IntStream codePoints() { 1580 // Reuse String-based spliterator. This requires a supplier to 1581 // capture the value and count when the terminal operation is executed 1582 return StreamSupport.intStream( 1583 () -> { 1584 // The combined set of field reads are not atomic and thread 1585 // safe but bounds checks will ensure no unsafe reads from 1586 // the byte array 1587 byte[] val = this.value; 1588 int count = this.count; 1589 byte coder = this.coder; 1590 return coder == LATIN1 1591 ? new StringLatin1.CharsSpliterator(val, 0, count, 0) 1592 : new StringUTF16.CodePointsSpliterator(val, 0, count, 0); 1593 }, 1594 Spliterator.ORDERED, 1595 false); 1596 } 1597 1598 /** 1599 * Needed by {@code String} for the contentEquals method. 1600 */ 1601 final byte[] getValue() { 1602 return value; 1603 } 1604 1605 /* 1606 * Invoker guarantees it is in UTF16 (inflate itself for asb), if two 1607 * coders are different and the dstBegin has enough space 1608 * 1609 * @param dstBegin the char index, not offset of byte[] 1610 * @param coder the coder of dst[] 1611 */ 1612 void getBytes(byte dst[], int dstBegin, byte coder) { 1613 if (this.coder == coder) { 1614 System.arraycopy(value, 0, dst, dstBegin << coder, count << coder); 1615 } else { // this.coder == LATIN && coder == UTF16 1616 StringLatin1.inflate(value, 0, dst, dstBegin, count); 1617 } 1618 } 1619 1620 /* for readObject() */ 1621 void initBytes(char[] value, int off, int len) { 1622 if (String.COMPACT_STRINGS) { 1623 this.value = StringUTF16.compress(value, off, len); 1624 if (this.value != null) { 1625 this.coder = LATIN1; 1626 return; 1627 } 1628 } 1629 this.coder = UTF16; 1630 this.value = StringUTF16.toBytes(value, off, len); 1631 } 1632 1633 final byte getCoder() { 1634 return COMPACT_STRINGS ? coder : UTF16; 1635 } 1636 1637 final boolean isLatin1() { 1638 return COMPACT_STRINGS && coder == LATIN1; 1639 } 1640 1641 private final void putCharsAt(int index, char[] s, int off, int end) { 1642 if (isLatin1()) { 1643 byte[] val = this.value; 1644 for (int i = off, j = index; i < end; i++) { 1645 char c = s[i]; 1646 if (StringLatin1.canEncode(c)) { 1647 val[j++] = (byte)c; 1648 } else { 1649 inflate(); 1650 StringUTF16.putCharsSB(this.value, j, s, i, end); 1651 return; 1652 } 1653 } 1654 } else { 1655 StringUTF16.putCharsSB(this.value, index, s, off, end); 1656 } 1657 } 1658 1659 private final void putCharsAt(int index, CharSequence s, int off, int end) { 1660 if (isLatin1()) { 1661 byte[] val = this.value; 1662 for (int i = off, j = index; i < end; i++) { 1663 char c = s.charAt(i); 1664 if (StringLatin1.canEncode(c)) { 1665 val[j++] = (byte)c; 1666 } else { 1667 inflate(); 1668 StringUTF16.putCharsSB(this.value, j, s, i, end); 1669 return; 1670 } 1671 } 1672 } else { 1673 StringUTF16.putCharsSB(this.value, index, s, off, end); 1674 } 1675 } 1676 1677 private final void putStringAt(int index, String str) { 1678 if (getCoder() != str.coder()) { 1679 inflate(); 1680 } 1681 str.getBytes(value, index, coder); 1682 } 1683 1684 private final void appendChars(char[] s, int off, int end) { 1685 if (isLatin1()) { 1686 byte[] val = this.value; 1687 for (int i = off, j = count; i < end; i++) { 1688 char c = s[i]; 1689 if (StringLatin1.canEncode(c)) { 1690 val[j++] = (byte)c; 1691 } else { 1692 count = j; 1693 inflate(); 1694 StringUTF16.putCharsSB(this.value, j, s, i, end); 1695 count += end - i; 1696 return; 1697 } 1698 } 1699 } else { 1700 StringUTF16.putCharsSB(this.value, count, s, off, end); 1701 } 1702 count += end - off; 1703 } 1704 1705 private final void appendChars(CharSequence s, int off, int end) { 1706 if (isLatin1()) { 1707 byte[] val = this.value; 1708 for (int i = off, j = count; i < end; i++) { 1709 char c = s.charAt(i); 1710 if (StringLatin1.canEncode(c)) { 1711 val[j++] = (byte)c; 1712 } else { 1713 count = j; 1714 inflate(); 1715 StringUTF16.putCharsSB(this.value, j, s, i, end); 1716 count += end - i; 1717 return; 1718 } 1719 } 1720 } else { 1721 StringUTF16.putCharsSB(this.value, count, s, off, end); 1722 } 1723 count += end - off; 1724 } 1725 1726 /* IndexOutOfBoundsException, if out of bounds */ 1727 private static void checkRange(int start, int end, int len) { 1728 if (start < 0 || start > end || end > len) { 1729 throw new IndexOutOfBoundsException( 1730 "start " + start + ", end " + end + ", length " + len); 1731 } 1732 } 1733 1734 /* StringIndexOutOfBoundsException, if out of bounds */ 1735 private static void checkRangeSIOOBE(int start, int end, int len) { 1736 if (start < 0 || start > end || end > len) { 1737 throw new StringIndexOutOfBoundsException( 1738 "start " + start + ", end " + end + ", length " + len); 1739 } 1740 } 1741 }