< prev index next >

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

Print this page




   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 
  30 import java.util.Arrays;
  31 import java.util.Spliterator;
  32 import java.util.stream.IntStream;
  33 import java.util.stream.StreamSupport;
  34 
  35 import static java.lang.String.COMPACT_STRINGS;
  36 import static java.lang.String.UTF16;
  37 import static java.lang.String.LATIN1;
  38 import static java.lang.String.checkIndex;
  39 import static java.lang.String.checkOffset;
  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


 669      * characters as if the s parameter was a sequence containing the four
 670      * characters {@code "null"}.
 671      *
 672      * @param   s the sequence to append.
 673      * @param   start   the starting index of the subsequence to be appended.
 674      * @param   end     the end index of the subsequence to be appended.
 675      * @return  a reference to this object.
 676      * @throws     IndexOutOfBoundsException if
 677      *             {@code start} is negative, or
 678      *             {@code start} is greater than {@code end} or
 679      *             {@code end} is greater than {@code s.length()}
 680      */
 681     @Override
 682     public AbstractStringBuilder append(CharSequence s, int start, int end) {
 683         if (s == null) {
 684             s = "null";
 685         }
 686         checkRange(start, end, s.length());
 687         int len = end - start;
 688         ensureCapacityInternal(count + len);
 689         if (s instanceof String) {
 690             appendChars((String)s, start, end);
 691         } else {
 692             appendChars(s, start, end);
 693         }
 694         return this;
 695     }
 696 
 697 
 698     /**
 699      * Appends the string representation of the {@code char} array
 700      * argument to this sequence.
 701      * <p>
 702      * The characters of the array argument are appended, in order, to
 703      * the contents of this sequence. The length of this sequence
 704      * increases by the length of the argument.
 705      * <p>
 706      * The overall effect is exactly as if the argument were converted
 707      * to a string by the method {@link String#valueOf(char[])},
 708      * and the characters of that string were then
 709      * {@link #append(String) appended} to this character sequence.
 710      *
 711      * @param   str   the characters to be appended.
 712      * @return  a reference to this object.
 713      */
 714     public AbstractStringBuilder append(char[] str) {
 715         int len = str.length;
 716         ensureCapacityInternal(count + len);
 717         appendChars(str, 0, len);


1730     private final void appendChars(char[] s, int off, int end) {
1731         int count = this.count;
1732         if (isLatin1()) {
1733             byte[] val = this.value;
1734             for (int i = off, j = count; i < end; i++) {
1735                 char c = s[i];
1736                 if (StringLatin1.canEncode(c)) {
1737                     val[j++] = (byte)c;
1738                 } else {
1739                     this.count = count = j;
1740                     inflate();
1741                     StringUTF16.putCharsSB(this.value, j, s, i, end);
1742                     this.count = count + end - i;
1743                     return;
1744                 }
1745             }
1746         } else {
1747             StringUTF16.putCharsSB(this.value, count, s, off, end);
1748         }
1749         this.count = count + end - off;
1750     }
1751 
1752     private final void appendChars(String s, int off, int end) {
1753         if (isLatin1()) {
1754             if (s.isLatin1()) {
1755                 System.arraycopy(s.value(), off, this.value, this.count, end - off);
1756             } else {
1757                 // We might need to inflate, but do it as late as possible since
1758                 // the range of characters we're copying might all be latin1
1759                 byte[] val = this.value;
1760                 for (int i = off, j = count; i < end; i++) {
1761                     char c = s.charAt(i);
1762                     if (StringLatin1.canEncode(c)) {
1763                         val[j++] = (byte) c;
1764                     } else {
1765                         count = j;
1766                         inflate();
1767                         System.arraycopy(s.value(), i << UTF16, this.value, j << UTF16, (end - i) << UTF16);
1768                         count += end - i;
1769                         return;
1770                     }
1771                 }
1772             }
1773         } else if (s.isLatin1()) {
1774             StringUTF16.putCharsSB(this.value, this.count, s, off, end);
1775         } else { // both UTF16
1776             System.arraycopy(s.value(), off << UTF16, this.value, this.count << UTF16, (end - off) << UTF16);
1777         }
1778         count += end - off;
1779     }
1780 
1781     private final void appendChars(CharSequence s, int off, int end) {
1782         if (isLatin1()) {
1783             byte[] val = this.value;
1784             for (int i = off, j = count; i < end; i++) {
1785                 char c = s.charAt(i);
1786                 if (StringLatin1.canEncode(c)) {
1787                     val[j++] = (byte)c;
1788                 } else {
1789                     count = j;
1790                     inflate();
1791                     StringUTF16.putCharsSB(this.value, j, s, i, end);
1792                     count += end - i;
1793                     return;
1794                 }
1795             }
1796         } else {
1797             StringUTF16.putCharsSB(this.value, count, s, off, end);
1798         }


   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


 668      * characters as if the s parameter was a sequence containing the four
 669      * characters {@code "null"}.
 670      *
 671      * @param   s the sequence to append.
 672      * @param   start   the starting index of the subsequence to be appended.
 673      * @param   end     the end index of the subsequence to be appended.
 674      * @return  a reference to this object.
 675      * @throws     IndexOutOfBoundsException if
 676      *             {@code start} is negative, or
 677      *             {@code start} is greater than {@code end} or
 678      *             {@code end} is greater than {@code s.length()}
 679      */
 680     @Override
 681     public AbstractStringBuilder append(CharSequence s, int start, int end) {
 682         if (s == null) {
 683             s = "null";
 684         }
 685         checkRange(start, end, s.length());
 686         int len = end - start;
 687         ensureCapacityInternal(count + len);
 688         appendChars(s, start, end);




 689         return this;
 690     }
 691 

 692     /**
 693      * Appends the string representation of the {@code char} array
 694      * argument to this sequence.
 695      * <p>
 696      * The characters of the array argument are appended, in order, to
 697      * the contents of this sequence. The length of this sequence
 698      * increases by the length of the argument.
 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 characters of that string were then
 703      * {@link #append(String) appended} to this character sequence.
 704      *
 705      * @param   str   the characters to be appended.
 706      * @return  a reference to this object.
 707      */
 708     public AbstractStringBuilder append(char[] str) {
 709         int len = str.length;
 710         ensureCapacityInternal(count + len);
 711         appendChars(str, 0, len);


1724     private final void appendChars(char[] s, int off, int end) {
1725         int count = this.count;
1726         if (isLatin1()) {
1727             byte[] val = this.value;
1728             for (int i = off, j = count; i < end; i++) {
1729                 char c = s[i];
1730                 if (StringLatin1.canEncode(c)) {
1731                     val[j++] = (byte)c;
1732                 } else {
1733                     this.count = count = j;
1734                     inflate();
1735                     StringUTF16.putCharsSB(this.value, j, s, i, end);
1736                     this.count = count + end - i;
1737                     return;
1738                 }
1739             }
1740         } else {
1741             StringUTF16.putCharsSB(this.value, count, s, off, end);
1742         }
1743         this.count = count + end - off;





























1744     }
1745 
1746     private final void appendChars(CharSequence s, int off, int end) {
1747         if (isLatin1()) {
1748             byte[] val = this.value;
1749             for (int i = off, j = count; i < end; i++) {
1750                 char c = s.charAt(i);
1751                 if (StringLatin1.canEncode(c)) {
1752                     val[j++] = (byte)c;
1753                 } else {
1754                     count = j;
1755                     inflate();
1756                     StringUTF16.putCharsSB(this.value, j, s, i, end);
1757                     count += end - i;
1758                     return;
1759                 }
1760             }
1761         } else {
1762             StringUTF16.putCharsSB(this.value, count, s, off, end);
1763         }
< prev index next >