From fe517fb2c9ddd0300d5ecad7658ac8e4554069bb Mon Sep 17 00:00:00 2001 From: Bryce McKinlay Date: Wed, 10 May 2000 10:15:13 +0000 Subject: [PATCH] StringBuffer.java (delete): Call arrayCopy() correctly. 2000-05-10 Bryce McKinlay * 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. From-SVN: r33819 --- libjava/ChangeLog | 9 ++++++++ libjava/java/lang/StringBuffer.java | 35 +++++++++++++++++++++-------- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/libjava/ChangeLog b/libjava/ChangeLog index 040fd2f701b..dd8abaa6ba9 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -1,3 +1,12 @@ +2000-05-10 Bryce McKinlay + + * 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. + 2000-05-09 Tom Tromey * java/lang/StringBuffer.java (toString): Don't mark buffer as 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 StringBuffer. - * @param index the index of the character to delete. + /** Replace characters between index start (inclusive) and + * end (exclusive) with str. If end + * is larger than the size of this StringBuffer, all characters after + * start 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 String to insert. * @return this StringBuffer. - * @exception StringIndexOutOfBoundsException if index - * 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; }