257 |
* @param index the index of the character to get, starting at 0 |
* @param index the index of the character to get, starting at 0 |
258 |
* @return the character at the specified index |
* @return the character at the specified index |
259 |
* @throws IndexOutOfBoundsException if index is negative or >= length() |
* @throws IndexOutOfBoundsException if index is negative or >= length() |
|
* (while unspecified, this is a StringIndexOutOfBoundsException) |
|
260 |
*/ |
*/ |
261 |
public synchronized char charAt(int index) |
public synchronized char charAt(int index) |
262 |
{ |
{ |
266 |
} |
} |
267 |
|
|
268 |
/** |
/** |
269 |
|
* Get the code point at the specified index. This is like #charAt(int), |
270 |
|
* but if the character is the start of a surrogate pair, and the |
271 |
|
* following character completes the pair, then the corresponding |
272 |
|
* supplementary code point is returned. |
273 |
|
* @param index the index of the codepoint to get, starting at 0 |
274 |
|
* @return the codepoint at the specified index |
275 |
|
* @throws IndexOutOfBoundsException if index is negative or >= length() |
276 |
|
* @since 1.5 |
277 |
|
*/ |
278 |
|
public synchronized int codePointAt(int index) |
279 |
|
{ |
280 |
|
return Character.codePointAt(value, index, count); |
281 |
|
} |
282 |
|
|
283 |
|
/** |
284 |
|
* Get the code point before the specified index. This is like |
285 |
|
* #codePointAt(int), but checks the characters at <code>index-1</code> and |
286 |
|
* <code>index-2</code> to see if they form a supplementary code point. |
287 |
|
* @param index the index just past the codepoint to get, starting at 0 |
288 |
|
* @return the codepoint at the specified index |
289 |
|
* @throws IndexOutOfBoundsException if index is negative or >= length() |
290 |
|
* @since 1.5 |
291 |
|
*/ |
292 |
|
public synchronized int codePointBefore(int index) |
293 |
|
{ |
294 |
|
// Character.codePointBefore() doesn't perform this check. We |
295 |
|
// could use the CharSequence overload, but this is just as easy. |
296 |
|
if (index >= count) |
297 |
|
throw new IndexOutOfBoundsException(); |
298 |
|
return Character.codePointBefore(value, index, 1); |
299 |
|
} |
300 |
|
|
301 |
|
/** |
302 |
* Get the specified array of characters. <code>srcOffset - srcEnd</code> |
* Get the specified array of characters. <code>srcOffset - srcEnd</code> |
303 |
* characters will be copied into the array you pass in. |
* characters will be copied into the array you pass in. |
304 |
* |
* |
435 |
} |
} |
436 |
|
|
437 |
/** |
/** |
438 |
|
* Append the code point to this <code>StringBuffer</code>. |
439 |
|
* This is like #append(char), but will append two characters |
440 |
|
* if a supplementary code point is given. |
441 |
|
* |
442 |
|
* @param code the code point to append |
443 |
|
* @return this <code>StringBuffer</code> |
444 |
|
* @see Character#toChars(int, char[], int) |
445 |
|
*/ |
446 |
|
public synchronized StringBuffer appendCodePoint(int code) |
447 |
|
{ |
448 |
|
int len = Character.charCount(code); |
449 |
|
ensureCapacity_unsynchronized(count + len); |
450 |
|
Character.toChars(code, value, count); |
451 |
|
count += len; |
452 |
|
return this; |
453 |
|
} |
454 |
|
|
455 |
|
/** |
456 |
* Append the <code>String</code> value of the argument to this |
* Append the <code>String</code> value of the argument to this |
457 |
* <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert |
* <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert |
458 |
* to <code>String</code>. |
* to <code>String</code>. |
770 |
} |
} |
771 |
|
|
772 |
/** |
/** |
773 |
|
* Insert the <code>CharSequence</code> argument into this |
774 |
|
* <code>StringBuffer</code>. If the sequence is null, the String |
775 |
|
* "null" is used instead. |
776 |
|
* |
777 |
|
* @param offset the place to insert in this buffer |
778 |
|
* @param sequence the <code>CharSequence</code> to insert |
779 |
|
* @return this <code>StringBuffer</code> |
780 |
|
* @throws IndexOutOfBoundsException if offset is out of bounds |
781 |
|
* @since 1.5 |
782 |
|
*/ |
783 |
|
public synchronized StringBuffer insert(int offset, CharSequence sequence) |
784 |
|
{ |
785 |
|
if (sequence == null) |
786 |
|
sequence = "null"; |
787 |
|
return insert(offset, sequence, 0, sequence.length()); |
788 |
|
} |
789 |
|
|
790 |
|
/** |
791 |
|
* Insert a subsequence of the <code>CharSequence</code> argument into this |
792 |
|
* <code>StringBuffer</code>. If the sequence is null, the String |
793 |
|
* "null" is used instead. |
794 |
|
* |
795 |
|
* @param offset the place to insert in this buffer |
796 |
|
* @param sequence the <code>CharSequence</code> to insert |
797 |
|
* @param start the starting index of the subsequence |
798 |
|
* @param end one past the ending index of the subsequence |
799 |
|
* @return this <code>StringBuffer</code> |
800 |
|
* @throws IndexOutOfBoundsException if offset, start, |
801 |
|
* or end are out of bounds |
802 |
|
* @since 1.5 |
803 |
|
*/ |
804 |
|
public synchronized StringBuffer insert(int offset, CharSequence sequence, |
805 |
|
int start, int end) |
806 |
|
{ |
807 |
|
if (sequence == null) |
808 |
|
sequence = "null"; |
809 |
|
if (start < 0 || end < 0 || start > end || end > sequence.length()) |
810 |
|
throw new IndexOutOfBoundsException(); |
811 |
|
int len = end - start; |
812 |
|
ensureCapacity_unsynchronized(count + len); |
813 |
|
VMSystem.arraycopy(value, offset, value, offset + len, count - offset); |
814 |
|
for (int i = start; i < end; ++i) |
815 |
|
value[offset++] = sequence.charAt(i); |
816 |
|
count += len; |
817 |
|
return this; |
818 |
|
} |
819 |
|
|
820 |
|
/** |
821 |
* Insert the <code>char[]</code> argument into this |
* Insert the <code>char[]</code> argument into this |
822 |
* <code>StringBuffer</code>. |
* <code>StringBuffer</code>. |
823 |
* |
* |
1038 |
} |
} |
1039 |
|
|
1040 |
/** |
/** |
1041 |
|
* This may reduce the amount of memory used by the StringBuffer, |
1042 |
|
* by resizing the internal array to remove unused space. However, |
1043 |
|
* this method is not required to resize, so this behavior cannot |
1044 |
|
* be relied upon. |
1045 |
|
* @since 1.5 |
1046 |
|
*/ |
1047 |
|
public synchronized void trimToSize() |
1048 |
|
{ |
1049 |
|
int wouldSave = value.length - count; |
1050 |
|
// Some random heuristics: if we save less than 20 characters, who |
1051 |
|
// cares. |
1052 |
|
if (wouldSave < 20) |
1053 |
|
return; |
1054 |
|
// If we save more than 200 characters, shrink. |
1055 |
|
// If we save more than 1/4 of the buffer, shrink. |
1056 |
|
if (wouldSave > 200 || wouldSave * 4 > value.length) |
1057 |
|
{ |
1058 |
|
char[] newValue = new char[count]; |
1059 |
|
VMSystem.arraycopy(value, 0, newValue, 0, count); |
1060 |
|
value = newValue; |
1061 |
|
} |
1062 |
|
} |
1063 |
|
|
1064 |
|
/** |
1065 |
|
* Return the number of code points between two indices in the |
1066 |
|
* <code>StringBuffer</code>. An unpaired surrogate counts as a |
1067 |
|
* code point for this purpose. Characters outside the indicated |
1068 |
|
* range are not examined, even if the range ends in the middle of a |
1069 |
|
* surrogate pair. |
1070 |
|
* |
1071 |
|
* @param start the starting index |
1072 |
|
* @param end one past the ending index |
1073 |
|
* @return the number of code points |
1074 |
|
* @since 1.5 |
1075 |
|
*/ |
1076 |
|
public synchronized int codePointCount(int start, int end) |
1077 |
|
{ |
1078 |
|
if (start < 0 || end >= count || start > end) |
1079 |
|
throw new StringIndexOutOfBoundsException(); |
1080 |
|
|
1081 |
|
int count = 0; |
1082 |
|
while (start < end) |
1083 |
|
{ |
1084 |
|
char base = value[start]; |
1085 |
|
if (base < Character.MIN_HIGH_SURROGATE |
1086 |
|
|| base > Character.MAX_HIGH_SURROGATE |
1087 |
|
|| start == end |
1088 |
|
|| start == count |
1089 |
|
|| value[start + 1] < Character.MIN_LOW_SURROGATE |
1090 |
|
|| value[start + 1] > Character.MAX_LOW_SURROGATE) |
1091 |
|
{ |
1092 |
|
// Nothing. |
1093 |
|
} |
1094 |
|
else |
1095 |
|
{ |
1096 |
|
// Surrogate pair. |
1097 |
|
++start; |
1098 |
|
} |
1099 |
|
++start; |
1100 |
|
++count; |
1101 |
|
} |
1102 |
|
return count; |
1103 |
|
} |
1104 |
|
|
1105 |
|
/** |
1106 |
|
* Starting at the given index, this counts forward by the indicated |
1107 |
|
* number of code points, and then returns the resulting index. An |
1108 |
|
* unpaired surrogate counts as a single code point for this |
1109 |
|
* purpose. |
1110 |
|
* |
1111 |
|
* @param start the starting index |
1112 |
|
* @param codePoints the number of code points |
1113 |
|
* @return the resulting index |
1114 |
|
* @since 1.5 |
1115 |
|
*/ |
1116 |
|
public synchronized int offsetByCodePoints(int start, int codePoints) |
1117 |
|
{ |
1118 |
|
while (codePoints > 0) |
1119 |
|
{ |
1120 |
|
char base = value[start]; |
1121 |
|
if (base < Character.MIN_HIGH_SURROGATE |
1122 |
|
|| base > Character.MAX_HIGH_SURROGATE |
1123 |
|
|| start == count |
1124 |
|
|| value[start + 1] < Character.MIN_LOW_SURROGATE |
1125 |
|
|| value[start + 1] > Character.MAX_LOW_SURROGATE) |
1126 |
|
{ |
1127 |
|
// Nothing. |
1128 |
|
} |
1129 |
|
else |
1130 |
|
{ |
1131 |
|
// Surrogate pair. |
1132 |
|
++start; |
1133 |
|
} |
1134 |
|
++start; |
1135 |
|
--codePoints; |
1136 |
|
} |
1137 |
|
return start; |
1138 |
|
} |
1139 |
|
|
1140 |
|
/** |
1141 |
* An unsynchronized version of ensureCapacity, used internally to avoid |
* An unsynchronized version of ensureCapacity, used internally to avoid |
1142 |
* the cost of a second lock on the same object. This also has the side |
* the cost of a second lock on the same object. This also has the side |
1143 |
* effect of duplicating the array, if it was shared (to form copy-on-write |
* effect of duplicating the array, if it was shared (to form copy-on-write |