diff options
author | bryce <bryce@138bc75d-0d04-0410-961f-82ee72b054a4> | 2000-05-10 10:15:13 +0000 |
---|---|---|
committer | bryce <bryce@138bc75d-0d04-0410-961f-82ee72b054a4> | 2000-05-10 10:15:13 +0000 |
commit | 23cbbcf771dd276f571b1ae3bb4d0e5b96c99805 (patch) | |
tree | 2e85bf9857bc68b640764a1a390da11cbd147ccb /libjava/java | |
parent | 5641ddfa7c054b6ed30fba854ec07c618b05510e (diff) | |
download | gcc-23cbbcf771dd276f571b1ae3bb4d0e5b96c99805.tar.gz |
2000-05-10 Bryce McKinlay <bryce@albatross.co.nz>
* java/lang/StringBuffer.java (delete): Call arrayCopy() correctly.
Avoid arrayCopy() call where possible. Update `count' _after_ calling
arrayCopy().
(replace): Reimplemented. Fix javadoc.
(reverse): Call ensureCapacity_unsynchronized().
(StringBuffer (String)): Use DEFAULT_CAPACITY.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@33819 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libjava/java')
-rw-r--r-- | libjava/java/lang/StringBuffer.java | 35 |
1 files changed, 26 insertions, 9 deletions
diff --git a/libjava/java/lang/StringBuffer.java b/libjava/java/lang/StringBuffer.java index 15f6271ecf5..ed0e84c5395 100644 --- a/libjava/java/lang/StringBuffer.java +++ b/libjava/java/lang/StringBuffer.java @@ -227,8 +227,9 @@ public final class StringBuffer implements Serializable end = count; // This will unshare if required. ensureCapacity_unsynchronized (count); + if (count - end != 0) + System.arraycopy (value, end, value, start, count - end); count -= (end - start); - System.arraycopy (value, end - 1, value, start, end - start); return this; } @@ -498,17 +499,31 @@ public final class StringBuffer implements Serializable return count; } - /** Delete a character from this <code>StringBuffer</code>. - * @param index the index of the character to delete. + /** Replace characters between index <code>start</code> (inclusive) and + * <code>end</code> (exclusive) with <code>str</code>. If <code>end</code> + * is larger than the size of this StringBuffer, all characters after + * <code>start</code> are replaced. + * @param start the beginning index of characters to delete (inclusive). + * @param end the ending index of characters to delete (exclusive). + * @param str the new <code>String</code> to insert. * @return this <code>StringBuffer</code>. - * @exception StringIndexOutOfBoundsException if <code>index</code> - * is out of bounds. */ public synchronized StringBuffer replace (int start, int end, String str) { - // FIXME: this is inefficient. - delete (start, end); - return insert (start, str); + if (start < 0 || start > count || start > end) + throw new StringIndexOutOfBoundsException (start); + + int len = str.length(); + // Calculate the difference in 'count' after the replace. + int delta = len - ((end > count ? count : end) - start); + ensureCapacity_unsynchronized (count + delta); + + if (delta != 0 && end < count) + System.arraycopy(value, end, value, end + delta, count - start); + + str.getChars (0, len, value, start); + count += delta; + return this; } /** Reverse the characters in this StringBuffer. @@ -516,6 +531,8 @@ public final class StringBuffer implements Serializable */ public synchronized StringBuffer reverse () { + // Call ensureCapacity to enforce copy-on-write. + ensureCapacity_unsynchronized (count); for (int i = 0; i < count / 2; ++i) { char c = value[i]; @@ -594,7 +611,7 @@ public final class StringBuffer implements Serializable count = str.length(); // JLS: The initial capacity of the string buffer is 16 plus the // length of the argument string. - value = new char[count + 16]; + value = new char[count + DEFAULT_CAPACITY]; str.getChars(0, count, value, 0); shared = false; } |