/[classpath]/classpath/java/lang/StringBuilder.java
ViewVC logotype

Diff of /classpath/java/lang/StringBuilder.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1.2.3 by tromey, Fri Oct 15 00:55:26 2004 UTC revision 1.1.2.4 by tromey, Sun Dec 19 06:32:51 2004 UTC
# Line 103  public final class StringBuilder Line 103  public final class StringBuilder
103    char[] value;    char[] value;
104    
105    /**    /**
    * True if the buffer is shared with another object (StringBuilder or  
    * String); this means the buffer must be copied before writing to it again.  
    * Note that this has permissions set this way so that String can get the  
    * value.  
    *  
    * @serial whether the buffer is shared  
    */  
   boolean shared;  
   
   /**  
106     * The default capacity of a buffer.     * The default capacity of a buffer.
107     */     */
108    private final static int DEFAULT_CAPACITY = 16;    private final static int DEFAULT_CAPACITY = 16;
# Line 211  public final class StringBuilder Line 201  public final class StringBuilder
201     */     */
202    public void ensureCapacity(int minimumCapacity)    public void ensureCapacity(int minimumCapacity)
203    {    {
204      if (shared || minimumCapacity > value.length)      if (minimumCapacity > value.length)
205        {        {
206          // We don't want to make a larger vector when `shared' is          int max = value.length * 2 + 2;
         // set.  If we do, then setLength becomes very inefficient  
         // when repeatedly reusing a StringBuilder in a loop.  
         int max = (minimumCapacity > value.length  
                    ? value.length * 2 + 2  
                    : value.length);  
207          minimumCapacity = (minimumCapacity < max ? max : minimumCapacity);          minimumCapacity = (minimumCapacity < max ? max : minimumCapacity);
208          char[] nb = new char[minimumCapacity];          char[] nb = new char[minimumCapacity];
209          System.arraycopy(value, 0, nb, 0, count);          System.arraycopy(value, 0, nb, 0, count);
210          value = nb;          value = nb;
         shared = false;  
211        }        }
212    }    }
213    
# Line 661  public final class StringBuilder Line 645  public final class StringBuilder
645        throw new StringIndexOutOfBoundsException();        throw new StringIndexOutOfBoundsException();
646      if (len == 0)      if (len == 0)
647        return "";        return "";
648      // Don't copy unless substring is smaller than 1/4 of the buffer.      return new String(value, beginIndex, len);
     boolean share_buffer = ((len << 2) >= value.length);  
     if (share_buffer)  
       this.shared = true;  
     // Package constructor avoids an array copy.  
     return new String(value, beginIndex, len, share_buffer);  
649    }    }
650    
651    /**    /**
# Line 951  public final class StringBuilder Line 930  public final class StringBuilder
930     */     */
931    public String toString()    public String toString()
932    {    {
     // The string will set this.shared = true.  
933      return new String(this);      return new String(this);
934    }    }
935    

Legend:
Removed from v.1.1.2.3  
changed lines
  Added in v.1.1.2.4

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26