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

Print this page
rev 6546 : 7197183: Alternate implementation of String.subSequence which uses shared backing array.
Reviewed-by: duke


 482      *
 483      * @param   s the sequence to append.
 484      * @param   start   the starting index of the subsequence to be appended.
 485      * @param   end     the end index of the subsequence to be appended.
 486      * @return  a reference to this object.
 487      * @throws     IndexOutOfBoundsException if
 488      *             {@code start} is negative, or
 489      *             {@code start} is greater than {@code end} or
 490      *             {@code end} is greater than {@code s.length()}
 491      */
 492     @Override
 493     public AbstractStringBuilder append(CharSequence s, int start, int end) {
 494         if (s == null)
 495             s = "null";
 496         if ((start < 0) || (start > end) || (end > s.length()))
 497             throw new IndexOutOfBoundsException(
 498                 "start " + start + ", end " + end + ", s.length() "
 499                 + s.length());
 500         int len = end - start;
 501         ensureCapacityInternal(count + len);
 502         for (int i = start, j = count; i < end; i++, j++)







 503             value[j] = s.charAt(i);


 504         count += len;
 505         return this;
 506     }
 507 
 508     /**
 509      * Appends the string representation of the {@code char} array
 510      * argument to this sequence.
 511      * <p>
 512      * The characters of the array argument are appended, in order, to
 513      * the contents of this sequence. The length of this sequence
 514      * increases by the length of the argument.
 515      * <p>
 516      * The overall effect is exactly as if the argument were converted
 517      * to a string by the method {@link String#valueOf(char[])},
 518      * and the characters of that string were then
 519      * {@link #append(String) appended} to this character sequence.
 520      *
 521      * @param   str   the characters to be appended.
 522      * @return  a reference to this object.
 523      */


1105      * @throws     IndexOutOfBoundsException  if {@code dstOffset}
1106      *             is negative or greater than {@code this.length()}, or
1107      *              {@code start} or {@code end} are negative, or
1108      *              {@code start} is greater than {@code end} or
1109      *              {@code end} is greater than {@code s.length()}
1110      */
1111      public AbstractStringBuilder insert(int dstOffset, CharSequence s,
1112                                          int start, int end) {
1113         if (s == null)
1114             s = "null";
1115         if ((dstOffset < 0) || (dstOffset > this.length()))
1116             throw new IndexOutOfBoundsException("dstOffset "+dstOffset);
1117         if ((start < 0) || (end < 0) || (start > end) || (end > s.length()))
1118             throw new IndexOutOfBoundsException(
1119                 "start " + start + ", end " + end + ", s.length() "
1120                 + s.length());
1121         int len = end - start;
1122         ensureCapacityInternal(count + len);
1123         System.arraycopy(value, dstOffset, value, dstOffset + len,
1124                          count - dstOffset);







1125         for (int i=start; i<end; i++)
1126             value[dstOffset++] = s.charAt(i);

1127         count += len;
1128         return this;
1129     }
1130 
1131     /**
1132      * Inserts the string representation of the {@code boolean}
1133      * argument into this sequence.
1134      * <p>
1135      * The overall effect is exactly as if the second argument were
1136      * converted to a string by the method {@link String#valueOf(boolean)},
1137      * and the characters of that string were then
1138      * {@link #insert(int,String) inserted} into this character
1139      * sequence at the indicated offset.
1140      * <p>
1141      * The {@code offset} argument must be greater than or equal to
1142      * {@code 0}, and less than or equal to the {@linkplain #length() length}
1143      * of this sequence.
1144      *
1145      * @param      offset   the offset.
1146      * @param      b        a {@code boolean}.




 482      *
 483      * @param   s the sequence to append.
 484      * @param   start   the starting index of the subsequence to be appended.
 485      * @param   end     the end index of the subsequence to be appended.
 486      * @return  a reference to this object.
 487      * @throws     IndexOutOfBoundsException if
 488      *             {@code start} is negative, or
 489      *             {@code start} is greater than {@code end} or
 490      *             {@code end} is greater than {@code s.length()}
 491      */
 492     @Override
 493     public AbstractStringBuilder append(CharSequence s, int start, int end) {
 494         if (s == null)
 495             s = "null";
 496         if ((start < 0) || (start > end) || (end > s.length()))
 497             throw new IndexOutOfBoundsException(
 498                 "start " + start + ", end " + end + ", s.length() "
 499                 + s.length());
 500         int len = end - start;
 501         ensureCapacityInternal(count + len);
 502         if (s instanceof String) {
 503             System.arraycopy(((String)s).value, start, value, count, len);
 504         } else if ((s instanceof StringBuilder) || (s instanceof StringBuffer)) {
 505             // two instanceof on leaf class is faster than instanceof check on AbstractStringBuilder.
 506             System.arraycopy(((AbstractStringBuilder)s).value, start, value, count, len);
 507         } else {
 508             // unspecialized path
 509             for (int i = start, j = count; i < end; i++, j++) {
 510                 value[j] = s.charAt(i);
 511             }
 512         }
 513         count += len;
 514         return this;
 515     }
 516 
 517     /**
 518      * Appends the string representation of the {@code char} array
 519      * argument to this sequence.
 520      * <p>
 521      * The characters of the array argument are appended, in order, to
 522      * the contents of this sequence. The length of this sequence
 523      * increases by the length of the argument.
 524      * <p>
 525      * The overall effect is exactly as if the argument were converted
 526      * to a string by the method {@link String#valueOf(char[])},
 527      * and the characters of that string were then
 528      * {@link #append(String) appended} to this character sequence.
 529      *
 530      * @param   str   the characters to be appended.
 531      * @return  a reference to this object.
 532      */


1114      * @throws     IndexOutOfBoundsException  if {@code dstOffset}
1115      *             is negative or greater than {@code this.length()}, or
1116      *              {@code start} or {@code end} are negative, or
1117      *              {@code start} is greater than {@code end} or
1118      *              {@code end} is greater than {@code s.length()}
1119      */
1120      public AbstractStringBuilder insert(int dstOffset, CharSequence s,
1121                                          int start, int end) {
1122         if (s == null)
1123             s = "null";
1124         if ((dstOffset < 0) || (dstOffset > this.length()))
1125             throw new IndexOutOfBoundsException("dstOffset "+dstOffset);
1126         if ((start < 0) || (end < 0) || (start > end) || (end > s.length()))
1127             throw new IndexOutOfBoundsException(
1128                 "start " + start + ", end " + end + ", s.length() "
1129                 + s.length());
1130         int len = end - start;
1131         ensureCapacityInternal(count + len);
1132         System.arraycopy(value, dstOffset, value, dstOffset + len,
1133                          count - dstOffset);
1134         if (s instanceof String) {
1135             System.arraycopy(((String)s).value, start, value, dstOffset, len);
1136         } else if ((s instanceof StringBuilder) || (s instanceof StringBuffer)) {
1137             // two instanceof on leaf class is faster than instanceof check on AbstractStringBuilder.
1138             System.arraycopy(((AbstractStringBuilder)s).value, start, value, dstOffset, len);
1139         } else {
1140             // unspecialized path
1141             for (int i=start; i<end; i++)
1142                 value[dstOffset++] = s.charAt(i);
1143         }
1144         count += len;
1145         return this;
1146     }
1147 
1148     /**
1149      * Inserts the string representation of the {@code boolean}
1150      * argument into this sequence.
1151      * <p>
1152      * The overall effect is exactly as if the second argument were
1153      * converted to a string by the method {@link String#valueOf(boolean)},
1154      * and the characters of that string were then
1155      * {@link #insert(int,String) inserted} into this character
1156      * sequence at the indicated offset.
1157      * <p>
1158      * The {@code offset} argument must be greater than or equal to
1159      * {@code 0}, and less than or equal to the {@linkplain #length() length}
1160      * of this sequence.
1161      *
1162      * @param      offset   the offset.
1163      * @param      b        a {@code boolean}.