< prev index next >

src/java.base/share/classes/java/lang/AbstractStringBuilder.java

Print this page


   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.


 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


 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}.


 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.


 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 


 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.


 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     /**


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


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 }
   1 /*
   2  * Copyright (c) 2003, 2017, 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 import static java.lang.String.checkBoundsBeginEnd;
  40 
  41 /**
  42  * A mutable sequence of characters.
  43  * <p>
  44  * Implements a modifiable string. At any point in time it contains some
  45  * particular sequence of characters, but the length and content of the
  46  * sequence can be changed through certain method calls.
  47  *
  48  * <p>Unless otherwise noted, passing a {@code null} argument to a constructor
  49  * or method in this class will cause a {@link NullPointerException} to be
  50  * thrown.
  51  *
  52  * @author      Michael McCloskey
  53  * @author      Martin Buchholz
  54  * @author      Ulf Zibis
  55  * @since       1.5
  56  */
  57 abstract class AbstractStringBuilder implements Appendable, CharSequence {
  58     /**
  59      * The value is used for character storage.


 291      * index. The index refers to {@code char} values
 292      * (Unicode code units) and ranges from {@code 0} to
 293      * {@link #length()}{@code  - 1}.
 294      *
 295      * <p> If the {@code char} value specified at the given index
 296      * is in the high-surrogate range, the following index is less
 297      * than the length of this sequence, and the
 298      * {@code char} value at the following index is in the
 299      * low-surrogate range, then the supplementary code point
 300      * corresponding to this surrogate pair is returned. Otherwise,
 301      * the {@code char} value at the given index is returned.
 302      *
 303      * @param      index the index to the {@code char} values
 304      * @return     the code point value of the character at the
 305      *             {@code index}
 306      * @exception  IndexOutOfBoundsException  if the {@code index}
 307      *             argument is negative or not less than the length of this
 308      *             sequence.
 309      */
 310     public int codePointAt(int index) {
 311         int count = this.count;
 312         byte[] value = this.value;
 313         checkIndex(index, count);
 314         if (isLatin1()) {
 315             return value[index] & 0xff;
 316         }
 317         return StringUTF16.codePointAtSB(value, index, count);
 318     }
 319 
 320     /**
 321      * Returns the character (Unicode code point) before the specified
 322      * index. The index refers to {@code char} values
 323      * (Unicode code units) and ranges from {@code 1} to {@link
 324      * #length()}.
 325      *
 326      * <p> If the {@code char} value at {@code (index - 1)}
 327      * is in the low-surrogate range, {@code (index - 2)} is not
 328      * negative, and the {@code char} value at {@code (index -
 329      * 2)} is in the high-surrogate range, then the
 330      * supplementary code point value of the surrogate pair is
 331      * returned. If the {@code char} value at {@code index -
 332      * 1} is an unpaired low-surrogate or a high-surrogate, the


 546         }
 547         if (s instanceof String) {
 548             return this.append((String)s);
 549         }
 550         if (s instanceof AbstractStringBuilder) {
 551             return this.append((AbstractStringBuilder)s);
 552         }
 553         return this.append(s, 0, s.length());
 554     }
 555 
 556     private AbstractStringBuilder appendNull() {
 557         ensureCapacityInternal(count + 4);
 558         int count = this.count;
 559         byte[] val = this.value;
 560         if (isLatin1()) {
 561             val[count++] = 'n';
 562             val[count++] = 'u';
 563             val[count++] = 'l';
 564             val[count++] = 'l';
 565         } else {
 566             count = StringUTF16.putCharsAt(val, count, 'n', 'u', 'l', 'l');




 567         }
 568         this.count = count;
 569         return this;
 570     }
 571 
 572     /**
 573      * Appends a subsequence of the specified {@code CharSequence} to this
 574      * sequence.
 575      * <p>
 576      * Characters of the argument {@code s}, starting at
 577      * index {@code start}, are appended, in order, to the contents of
 578      * this sequence up to the (exclusive) index {@code end}. The length
 579      * of this sequence is increased by the value of {@code end - start}.
 580      * <p>
 581      * Let <i>n</i> be the length of this character sequence just prior to
 582      * execution of the {@code append} method. Then the character at
 583      * index <i>k</i> in this character sequence becomes equal to the
 584      * character at index <i>k</i> in this sequence, if <i>k</i> is less than
 585      * <i>n</i>; otherwise, it is equal to the character at index
 586      * <i>k+start-n</i> in the argument {@code s}.


 677      */
 678     public AbstractStringBuilder append(boolean b) {
 679         ensureCapacityInternal(count + (b ? 4 : 5));
 680         int count = this.count;
 681         byte[] val = this.value;
 682         if (isLatin1()) {
 683             if (b) {
 684                 val[count++] = 't';
 685                 val[count++] = 'r';
 686                 val[count++] = 'u';
 687                 val[count++] = 'e';
 688             } else {
 689                 val[count++] = 'f';
 690                 val[count++] = 'a';
 691                 val[count++] = 'l';
 692                 val[count++] = 's';
 693                 val[count++] = 'e';
 694             }
 695         } else {
 696             if (b) {
 697                 count = StringUTF16.putCharsAt(val, count, 't', 'r', 'u', 'e');
 698             } else {
 699                 count = StringUTF16.putCharsAt(val, count, 'f', 'a', 'l', 's', 'e');









 700             }
 701         }
 702         this.count = count;
 703         return this;
 704     }
 705 
 706     /**
 707      * Appends the string representation of the {@code char}
 708      * argument to this sequence.
 709      * <p>
 710      * The argument is appended to the contents of this sequence.
 711      * The length of this sequence increases by {@code 1}.
 712      * <p>
 713      * The overall effect is exactly as if the argument were converted
 714      * to a string by the method {@link String#valueOf(char)},
 715      * and the character in that string were then
 716      * {@link #append(String) appended} to this character sequence.
 717      *
 718      * @param   c   a {@code char}.
 719      * @return  a reference to this object.


 728                 inflate();
 729             }
 730             StringUTF16.putCharSB(value, count++, c);
 731         }
 732         return this;
 733     }
 734 
 735     /**
 736      * Appends the string representation of the {@code int}
 737      * argument to this sequence.
 738      * <p>
 739      * The overall effect is exactly as if the argument were converted
 740      * to a string by the method {@link String#valueOf(int)},
 741      * and the characters of that string were then
 742      * {@link #append(String) appended} to this character sequence.
 743      *
 744      * @param   i   an {@code int}.
 745      * @return  a reference to this object.
 746      */
 747     public AbstractStringBuilder append(int i) {
 748         int count = this.count;
 749         int spaceNeeded = count + Integer.stringSize(i);
 750         ensureCapacityInternal(spaceNeeded);
 751         if (isLatin1()) {
 752             Integer.getChars(i, spaceNeeded, value);
 753         } else {
 754             StringUTF16.getChars(i, count, spaceNeeded, value);


 755         }
 756         this.count = spaceNeeded;
 757         return this;
 758     }
 759 
 760     /**
 761      * Appends the string representation of the {@code long}
 762      * argument to this sequence.
 763      * <p>
 764      * The overall effect is exactly as if the argument were converted
 765      * to a string by the method {@link String#valueOf(long)},
 766      * and the characters of that string were then
 767      * {@link #append(String) appended} to this character sequence.
 768      *
 769      * @param   l   a {@code long}.
 770      * @return  a reference to this object.
 771      */
 772     public AbstractStringBuilder append(long l) {
 773         int count = this.count;
 774         int spaceNeeded = count + Long.stringSize(l);
 775         ensureCapacityInternal(spaceNeeded);
 776         if (isLatin1()) {
 777             Long.getChars(l, spaceNeeded, value);
 778         } else {
 779             StringUTF16.getChars(l, count, spaceNeeded, value);


 780         }
 781         this.count = spaceNeeded;
 782         return this;
 783     }
 784 
 785     /**
 786      * Appends the string representation of the {@code float}
 787      * argument to this sequence.
 788      * <p>
 789      * The overall effect is exactly as if the argument were converted
 790      * to a string by the method {@link String#valueOf(float)},
 791      * and the characters of that string were then
 792      * {@link #append(String) appended} to this character sequence.
 793      *
 794      * @param   f   a {@code float}.
 795      * @return  a reference to this object.
 796      */
 797     public AbstractStringBuilder append(float f) {
 798         FloatingDecimal.appendTo(f,this);
 799         return this;
 800     }
 801 


 814     public AbstractStringBuilder append(double d) {
 815         FloatingDecimal.appendTo(d,this);
 816         return this;
 817     }
 818 
 819     /**
 820      * Removes the characters in a substring of this sequence.
 821      * The substring begins at the specified {@code start} and extends to
 822      * the character at index {@code end - 1} or to the end of the
 823      * sequence if no such character exists. If
 824      * {@code start} is equal to {@code end}, no changes are made.
 825      *
 826      * @param      start  The beginning index, inclusive.
 827      * @param      end    The ending index, exclusive.
 828      * @return     This object.
 829      * @throws     StringIndexOutOfBoundsException  if {@code start}
 830      *             is negative, greater than {@code length()}, or
 831      *             greater than {@code end}.
 832      */
 833     public AbstractStringBuilder delete(int start, int end) {
 834         int count = this.count;
 835         if (end > count) {
 836             end = count;
 837         }
 838         checkRangeSIOOBE(start, end, count);
 839         int len = end - start;
 840         if (len > 0) {
 841             shift(end, -len);
 842             this.count = count - len;
 843         }
 844         return this;
 845     }
 846 
 847     /**
 848      * Appends the string representation of the {@code codePoint}
 849      * argument to this sequence.
 850      *
 851      * <p> The argument is appended to the contents of this sequence.
 852      * The length of this sequence increases by
 853      * {@link Character#charCount(int) Character.charCount(codePoint)}.
 854      *
 855      * <p> The overall effect is exactly as if the argument were
 856      * converted to a {@code char} array by the method
 857      * {@link Character#toChars(int)} and the character in that array
 858      * were then {@link #append(char[]) appended} to this character
 859      * sequence.
 860      *
 861      * @param   codePoint   a Unicode code point
 862      * @return  a reference to this object.


 897     /**
 898      * Replaces the characters in a substring of this sequence
 899      * with characters in the specified {@code String}. The substring
 900      * begins at the specified {@code start} and extends to the character
 901      * at index {@code end - 1} or to the end of the
 902      * sequence if no such character exists. First the
 903      * characters in the substring are removed and then the specified
 904      * {@code String} is inserted at {@code start}. (This
 905      * sequence will be lengthened to accommodate the
 906      * specified String if necessary.)
 907      *
 908      * @param      start    The beginning index, inclusive.
 909      * @param      end      The ending index, exclusive.
 910      * @param      str   String that will replace previous contents.
 911      * @return     This object.
 912      * @throws     StringIndexOutOfBoundsException  if {@code start}
 913      *             is negative, greater than {@code length()}, or
 914      *             greater than {@code end}.
 915      */
 916     public AbstractStringBuilder replace(int start, int end, String str) {
 917         int count = this.count;
 918         if (end > count) {
 919             end = count;
 920         }
 921         checkRangeSIOOBE(start, end, count);
 922         int len = str.length();
 923         int newCount = count + len - (end - start);
 924         ensureCapacityInternal(newCount);
 925         shift(end, newCount - count);
 926         this.count = newCount;
 927         putStringAt(start, str);
 928         return this;
 929     }
 930 
 931     /**
 932      * Returns a new {@code String} that contains a subsequence of
 933      * characters currently contained in this character sequence. The
 934      * substring begins at the specified index and extends to the end of
 935      * this sequence.
 936      *
 937      * @param      start    The beginning index, inclusive.
 938      * @return     The new string.
 939      * @throws     StringIndexOutOfBoundsException  if {@code start} is
 940      *             less than zero, or greater than the length of this object.
 941      */
 942     public String substring(int start) {
 943         return substring(start, count);
 944     }
 945 
 946     /**


1473      * surrogate pairs that were unpaired low-surrogates and
1474      * high-surrogates before the operation. For example, reversing
1475      * "\u005CuDC00\u005CuD800" produces "\u005CuD800\u005CuDC00" which is
1476      * a valid surrogate pair.
1477      *
1478      * @return  a reference to this object.
1479      */
1480     public AbstractStringBuilder reverse() {
1481         byte[] val = this.value;
1482         int count = this.count;
1483         int coder = this.coder;
1484         int n = count - 1;
1485         if (COMPACT_STRINGS && coder == LATIN1) {
1486             for (int j = (n-1) >> 1; j >= 0; j--) {
1487                 int k = n - j;
1488                 byte cj = val[j];
1489                 val[j] = val[k];
1490                 val[k] = cj;
1491             }
1492         } else {
1493             StringUTF16.reverse(val, count);















1494         }
1495         return this;
1496     }
1497 














1498     /**
1499      * Returns a string representing the data in this sequence.
1500      * A new {@code String} object is allocated and initialized to
1501      * contain the character sequence currently represented by this
1502      * object. This {@code String} is then returned. Subsequent
1503      * changes to this sequence do not affect the contents of the
1504      * {@code String}.
1505      *
1506      * @return  a string representation of this sequence of characters.
1507      */
1508     @Override
1509     public abstract String toString();
1510 
1511     /**
1512      * {@inheritDoc}
1513      * @since 9
1514      */
1515     @Override
1516     public IntStream chars() {
1517         // Reuse String-based spliterator. This requires a supplier to


1626                     val[j++] = (byte)c;
1627                 } else {
1628                     inflate();
1629                     StringUTF16.putCharsSB(this.value, j, s, i, end);
1630                     return;
1631                 }
1632             }
1633         } else {
1634             StringUTF16.putCharsSB(this.value, index, s, off, end);
1635         }
1636     }
1637 
1638     private final void putStringAt(int index, String str) {
1639         if (getCoder() != str.coder()) {
1640             inflate();
1641         }
1642         str.getBytes(value, index, coder);
1643     }
1644 
1645     private final void appendChars(char[] s, int off, int end) {
1646         int count = this.count;
1647         if (isLatin1()) {
1648             byte[] val = this.value;
1649             for (int i = off, j = count; i < end; i++) {
1650                 char c = s[i];
1651                 if (StringLatin1.canEncode(c)) {
1652                     val[j++] = (byte)c;
1653                 } else {
1654                     this.count = count = j;
1655                     inflate();
1656                     StringUTF16.putCharsSB(this.value, j, s, i, end);
1657                     this.count = count + end - i;
1658                     return;
1659                 }
1660             }
1661         } else {
1662             StringUTF16.putCharsSB(this.value, count, s, off, end);
1663         }
1664         this.count = count + end - off;
1665     }
1666 
1667     private final void appendChars(CharSequence s, int off, int end) {
1668         if (isLatin1()) {
1669             byte[] val = this.value;
1670             for (int i = off, j = count; i < end; i++) {
1671                 char c = s.charAt(i);
1672                 if (StringLatin1.canEncode(c)) {
1673                     val[j++] = (byte)c;
1674                 } else {
1675                     count = j;
1676                     inflate();
1677                     StringUTF16.putCharsSB(this.value, j, s, i, end);
1678                     count += end - i;
1679                     return;
1680                 }
1681             }
1682         } else {
1683             StringUTF16.putCharsSB(this.value, count, s, off, end);
1684         }
1685         count += end - off;
1686     }
1687 
1688     /* IndexOutOfBoundsException, if out of bounds */
1689     private static void checkRange(int start, int end, int len) {
1690         if (start < 0 || start > end || end > len) {
1691             throw new IndexOutOfBoundsException(
1692                 "start " + start + ", end " + end + ", length " + len);
1693         }
1694     }
1695 
1696     /* StringIndexOutOfBoundsException, if out of bounds */
1697     private static void checkRangeSIOOBE(int start, int end, int len) {
1698         if (start < 0 || start > end || end > len) {
1699             throw new StringIndexOutOfBoundsException(
1700                 "start " + start + ", end " + end + ", length " + len);
1701         }
1702     }
1703 
1704 }
< prev index next >