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