2009-07-26 Gonzalo Paniagua Javier <gonzalo@novell.com>
authorGonzalo Paniagua Javier <gonzalo.mono@gmail.com>
Sun, 26 Jul 2009 17:08:10 +0000 (17:08 -0000)
committerGonzalo Paniagua Javier <gonzalo.mono@gmail.com>
Sun, 26 Jul 2009 17:08:10 +0000 (17:08 -0000)
* StringBuilder.cs: don't throw on null values in ctor(string, int).
Unify another ctor into the main one to account for MaxCapacity.

svn path=/trunk/mcs/; revision=138692

mcs/class/corlib/System.Text/ChangeLog
mcs/class/corlib/System.Text/StringBuilder.cs

index 7a09757931abee300e52b03a4038b1a6ca790629..144686053c477757687392a39273e2cb16a6c6f7 100644 (file)
@@ -1,3 +1,8 @@
+2009-07-26 Gonzalo Paniagua Javier <gonzalo@novell.com>
+
+       * StringBuilder.cs: don't throw on null values in ctor(string, int).
+       Unify another ctor into the main one to account for MaxCapacity.
+
 2009-07-26 Gonzalo Paniagua Javier <gonzalo@novell.com>
 
        * StringBuilder.cs: small fixes dealing with _maxCapacity.
index f87aab6771f396e0169ce22bb97b4dcf550516e2..abd8e12587466a5998ba7b4a9d2ac770ef3e802d 100644 (file)
@@ -56,10 +56,15 @@ namespace System.Text {
                private string _str;
                private string _cached_str;
                
-               private int _maxCapacity = Int32.MaxValue;
+               private int _maxCapacity;
                private const int constDefaultCapacity = 16;
 
                public StringBuilder(string value, int startIndex, int length, int capacity) 
+                       : this (value, startIndex, length, capacity, Int32.MaxValue)
+               {
+               }
+
+               private StringBuilder(string value, int startIndex, int length, int capacity, int maxCapacity)
                {
                        // first, check the parameters and throw appropriate exceptions if needed
                        if (null == value)
@@ -76,15 +81,26 @@ namespace System.Text {
                        if (capacity < 0)
                                throw new System.ArgumentOutOfRangeException ("capacity", capacity, "capacity must be greater than zero.");
 
+                       if (maxCapacity < 1)
+                               throw new System.ArgumentOutOfRangeException ("maxCapacity", "maxCapacity is less than one.");
+                       if (capacity > maxCapacity)
+                               throw new System.ArgumentOutOfRangeException ("capacity", "Capacity exceeds maximum capacity.");
+
                        // make sure startIndex and length give a valid substring of value
                        // re-ordered to avoid possible integer overflow
                        if (startIndex > value.Length - length)
                                throw new System.ArgumentOutOfRangeException ("startIndex", startIndex, "StartIndex and length must refer to a location within the string.");
 
-                       if (capacity == 0)
-                               capacity = constDefaultCapacity;
+                       if (capacity == 0) {
+                               if (maxCapacity > constDefaultCapacity)
+                                       capacity = constDefaultCapacity;
+                               else
+                                       _str = _cached_str = String.Empty;
+                       }
+                       _maxCapacity = maxCapacity;
 
-                       _str = String.InternalAllocateStr ((length > capacity) ? length : capacity);
+                       if (_str == null)
+                               _str = String.InternalAllocateStr ((length > capacity) ? length : capacity);
                        if (length > 0)
                                String.CharCopy (_str, 0, value, startIndex, length);
                        
@@ -95,14 +111,7 @@ namespace System.Text {
 
                public StringBuilder(int capacity) : this (String.Empty, 0, 0, capacity) {}
 
-               public StringBuilder(int capacity, int maxCapacity) : this (String.Empty, 0, 0, capacity) {
-                       if (maxCapacity < 1)
-                               throw new System.ArgumentOutOfRangeException ("maxCapacity", "maxCapacity is less than one.");
-                       if (capacity > maxCapacity)
-                               throw new System.ArgumentOutOfRangeException ("capacity", "Capacity exceeds maximum capacity.");
-
-                       _maxCapacity = maxCapacity;
-               }
+               public StringBuilder(int capacity, int maxCapacity) : this (String.Empty, 0, 0, capacity, maxCapacity) { }
 
                public StringBuilder (string value)
                {
@@ -116,9 +125,10 @@ namespace System.Text {
                        
                        _length = value.Length;
                        _str = _cached_str = value;
+                       _maxCapacity = Int32.MaxValue;
                }
        
-               public StringBuilder( string value, int capacity) : this(value, 0, value.Length, capacity) {}
+               public StringBuilder( string value, int capacity) : this(value == null ? "" : value, 0, value == null ? 0 : value.Length, capacity) {}
        
                public int MaxCapacity {
                        get {