Commit bdf2ced9 by Anthony Green Committed by Anthony Green

StringBuffer.java (ensureCapacity): Don't call Math::max.

2000-04-08  Anthony Green  <green@cygnus.com>

	* java/lang/StringBuffer.java (ensureCapacity): Don't call Math::max.
	(ensureCapacity_unsynchronized): New private method.
	(append): Use ensureCapacity_unsynchronized.

From-SVN: r33036
parent 3dbc07b6
2000-04-08 Anthony Green <green@cygnus.com>
* java/lang/StringBuffer.java (ensureCapacity): Don't call Math::max.
(ensureCapacity_unsynchronized): New private method.
(append): Use ensureCapacity_unsynchronized.
2000-04-08 Tom Tromey <tromey@cygnus.com> 2000-04-08 Tom Tromey <tromey@cygnus.com>
* Makefile.in: Rebuilt. * Makefile.in: Rebuilt.
......
...@@ -28,7 +28,7 @@ public final class StringBuffer implements Serializable ...@@ -28,7 +28,7 @@ public final class StringBuffer implements Serializable
public synchronized StringBuffer append (char ch) public synchronized StringBuffer append (char ch)
{ {
ensureCapacity (count + 1); ensureCapacity_unsynchronized (count + 1);
value[count++] = ch; value[count++] = ch;
return this; return this;
} }
...@@ -63,7 +63,7 @@ public final class StringBuffer implements Serializable ...@@ -63,7 +63,7 @@ public final class StringBuffer implements Serializable
if (str == null) if (str == null)
str = "null"; str = "null";
int len = str.length(); int len = str.length();
ensureCapacity (count + len); ensureCapacity_unsynchronized (count + len);
str.getChars(0, len, value, count); str.getChars(0, len, value, count);
count += len; count += len;
return this; return this;
...@@ -76,7 +76,7 @@ public final class StringBuffer implements Serializable ...@@ -76,7 +76,7 @@ public final class StringBuffer implements Serializable
public synchronized StringBuffer append (char[] data, int offset, int count) public synchronized StringBuffer append (char[] data, int offset, int count)
{ {
ensureCapacity (this.count + count); ensureCapacity_unsynchronized (this.count + count);
System.arraycopy(data, offset, value, this.count, count); System.arraycopy(data, offset, value, this.count, count);
this.count += count; this.count += count;
return this; return this;
...@@ -104,7 +104,27 @@ public final class StringBuffer implements Serializable ...@@ -104,7 +104,27 @@ public final class StringBuffer implements Serializable
int max = (minimumCapacity > value.length int max = (minimumCapacity > value.length
? value.length*2+2 ? value.length*2+2
: value.length); : value.length);
minimumCapacity = Math.max(minimumCapacity, max); minimumCapacity = (minimumCapacity < max ? max : minimumCapacity);
char[] nb = new char[minimumCapacity];
System.arraycopy(value, 0, nb, 0, count);
value = nb;
shared = false;
}
}
// ensureCapacity is used by several synchronized methods in StringBuffer.
// There's no need to synchronize again.
private void ensureCapacity_unsynchronized (int minimumCapacity)
{
if (shared || minimumCapacity > value.length)
{
// We don't want to make a larger vector when `shared' is
// set. If we do, then setLength becomes very inefficient
// when repeatedly reusing a StringBuffer in a loop.
int max = (minimumCapacity > value.length
? value.length*2+2
: value.length);
minimumCapacity = (minimumCapacity < max ? max : minimumCapacity);
char[] nb = new char[minimumCapacity]; char[] nb = new char[minimumCapacity];
System.arraycopy(value, 0, nb, 0, count); System.arraycopy(value, 0, nb, 0, count);
value = nb; value = nb;
...@@ -132,7 +152,7 @@ public final class StringBuffer implements Serializable ...@@ -132,7 +152,7 @@ public final class StringBuffer implements Serializable
{ {
if (offset < 0 || offset > count) if (offset < 0 || offset > count)
throw new StringIndexOutOfBoundsException (offset); throw new StringIndexOutOfBoundsException (offset);
ensureCapacity (count+1); ensureCapacity_unsynchronized (count+1);
System.arraycopy(value, offset, value, offset+1, count-offset); System.arraycopy(value, offset, value, offset+1, count-offset);
value[offset] = ch; value[offset] = ch;
count++; count++;
...@@ -172,7 +192,7 @@ public final class StringBuffer implements Serializable ...@@ -172,7 +192,7 @@ public final class StringBuffer implements Serializable
if (str == null) if (str == null)
str = "null"; str = "null";
int len = str.length(); int len = str.length();
ensureCapacity(count+len); ensureCapacity_unsynchronized (count+len);
System.arraycopy(value, offset, value, offset+len, count-offset); System.arraycopy(value, offset, value, offset+len, count-offset);
str.getChars(0, len, value, offset); str.getChars(0, len, value, offset);
count += len; count += len;
...@@ -184,7 +204,7 @@ public final class StringBuffer implements Serializable ...@@ -184,7 +204,7 @@ public final class StringBuffer implements Serializable
if (offset < 0 || offset > count) if (offset < 0 || offset > count)
throw new StringIndexOutOfBoundsException (offset); throw new StringIndexOutOfBoundsException (offset);
int len = data.length; int len = data.length;
ensureCapacity (count+len); ensureCapacity_unsynchronized (count+len);
System.arraycopy(value, offset, value, offset+len, count-offset); System.arraycopy(value, offset, value, offset+len, count-offset);
System.arraycopy(data, 0, value, offset, len); System.arraycopy(data, 0, value, offset, len);
count += len; count += len;
...@@ -212,7 +232,7 @@ public final class StringBuffer implements Serializable ...@@ -212,7 +232,7 @@ public final class StringBuffer implements Serializable
if (index < 0 || index >= count) if (index < 0 || index >= count)
throw new StringIndexOutOfBoundsException (index); throw new StringIndexOutOfBoundsException (index);
// Call ensureCapacity to enforce copy-on-write. // Call ensureCapacity to enforce copy-on-write.
ensureCapacity (count); ensureCapacity_unsynchronized (count);
value[index] = ch; value[index] = ch;
} }
...@@ -221,7 +241,7 @@ public final class StringBuffer implements Serializable ...@@ -221,7 +241,7 @@ public final class StringBuffer implements Serializable
if (newLength < 0) if (newLength < 0)
throw new StringIndexOutOfBoundsException (newLength); throw new StringIndexOutOfBoundsException (newLength);
ensureCapacity (newLength); ensureCapacity_unsynchronized (newLength);
for (int i = count; i < newLength; ++i) for (int i = count; i < newLength; ++i)
value[i] = '\0'; value[i] = '\0';
count = newLength; count = newLength;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment