[Mono-list] Constructors for StringBuilder

Nick Drochak ndrochak@gol.com
Tue, 23 Oct 2001 18:38:18 +0900


This is a multi-part message in MIME format.

------=_NextPart_000_0018_01C15BF1.E28641E0
Content-Type: text/plain;
	charset="utf-8"
Content-Transfer-Encoding: quoted-printable

All,

I had some time today, so I put in the missing constructors for =
StringBuilder.  I also refactored the code into one constructor to =
simplify things.

On my machine this fixes the broken build we got recently.

If Marcin (the author of StringBuilder) or someone else can look over my =
patch I can commit it.

Regards,
Nick D.

------=_NextPart_000_0018_01C15BF1.E28641E0
Content-Type: application/octet-stream;
	name="stringbuilder.diff"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="stringbuilder.diff"

? stringbuilder.diff=0A=
Index: ChangeLog=0A=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A=
RCS file: /cvs/public/mcs/class/corlib/System.Text/ChangeLog,v=0A=
retrieving revision 1.5=0A=
diff -r1.5 ChangeLog=0A=
0a1,9=0A=
> 2001-10-23  Nick Drochak  <ndrochak@gol.com>=0A=
> =0A=
> 	* StringBuilder.cs: Refactored constructor code into just one=0A=
> 	constructor. All the other construtors call it. Also supplied missing=0A=
> 	constructors so the class has all those in the spec.=0A=
> =0A=
> 	Added the MaxCapacity property as well, however this needs to be =0A=
> 	completed to return a value is related to the available system memory.=0A=
> =0A=
Index: StringBuilder.cs=0A=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A=
RCS file: /cvs/public/mcs/class/corlib/System.Text/StringBuilder.cs,v=0A=
retrieving revision 1.4=0A=
diff -r1.4 StringBuilder.cs=0A=
15d14=0A=
<=20
19c18=0A=
< 		const int defaultCapacity =3D 16;
---=0A=
> 		private const int defaultCapacity =3D 16;
23a23=0A=
> 		private int sMaxCapacity =3D Int32.MaxValue;
25c25,47=0A=
< 		public StringBuilder() {
---=0A=
> 		public StringBuilder(string str, int startIndex, int length, int =
capacity) {
> 			// the capacity must be at least as big as the default capacity
> 			// LAMESPEC: what to do if capacity is too small to hold the =
substring?
> 			// For now, truncate the substring to "capacity" characters
> 			sCapacity =3D Math.Max(capacity, defaultCapacity);
> 			sString =3D new char[sCapacity];
>=20
> 			// LAMESPEC: what to do if startIndex is too big?  Throw an =
exception?  Which one?
> 			// For now, if the startIndex is beyond the end of the string, =
create an empty StringBuilder
> 			// Also, if str is null, then create an empty StringBuilder
> 			if (null =3D=3D str || startIndex > str.Length - 1)=20
> 			{
> 				sLength =3D 0;
> 			}
> 			else
> 			{
> 				// LAMESPEC: what if the length specified would take us past the =
end of the string?
> 				// For now, copy to the end of the string
> 				// First find out how many characters we can actually copy
> 				sLength =3D Math.Min(length, str.Length - startIndex);
> 				// Then limit the length to the capacity if necessary.  See =
LAMESPEC above.
> 				sLength =3D Math.Min(sLength, capacity);
> 			}
27,34c49,54=0A=
< 			// The MS Implementation uses the default
< 			// capacity for a StringBuilder.  The spec
< 			// says it's up to the implementer, but=20
< 			// we'll do it the MS way just in case.
< 			=09
< 			sString =3D new char[ defaultCapacity ];
< 			sCapacity =3D defaultCapacity;
< 			sLength =3D 0;
---=0A=
> 			// if the length is not going to be zero, then we have to copy some =
characters
> 			if (sLength > 0) {
> 				// Copy the correct number of characters into the internal array
> 				char[] tString =3D str.ToCharArray(startIndex, sLength);
> 				Array.Copy( tString, sString, sLength);
> 			}
37,42c57=0A=
< 		public StringBuilder( int capacity ) {
< 			if( capacity < defaultCapacity ) {
< 					// The spec says that the capacity
< 					// has to be at least the default capacity
< 				capacity =3D defaultCapacity;
< 			}
---=0A=
> 		public StringBuilder() : this(null, 0, 0, 0) {}
44,46c59,62=0A=
< 			sString =3D new char[capacity];
< 			sCapacity =3D capacity;
< 			sLength =3D 0;
---=0A=
> 		public StringBuilder( int capacity ) : this(null, 0, 0, capacity) {}
>=20
> 		public StringBuilder( int capacity, int maxCapacity ) : this(null, =
0, 0, capacity) {
> 			sMaxCapacity =3D maxCapacity;
49,60c65,74=0A=
< 		public StringBuilder( string str ) {
< 	=09
< 			if( str.Length < defaultCapacity ) {   =20
< 				char[] tString =3D str.ToCharArray();
< 				sString =3D new char[ defaultCapacity ];
< 				Array.Copy( tString, sString, str.Length );
< 				sLength =3D str.Length;
< 				sCapacity =3D defaultCapacity;
< 			} else {
< 				sString =3D str.ToCharArray();
< 				sCapacity =3D sString.Length;
< 				sLength =3D sString.Length;
---=0A=
> 		public StringBuilder( string str ) : this(str, 0, str.Length, =
str.Length) {}
> =09
> 		public StringBuilder( string str, int capacity) : this(str, 0, =
str.Length, capacity) {}
> =09
> 		public int MaxCapacity=20
> 		{
> 			get=20
> 			{
> 				// TODO: Need to look at the memory of the system to return a =
useful value here
> 				return sMaxCapacity;
63c77=0A=
< =09
---=0A=
>=20

------=_NextPart_000_0018_01C15BF1.E28641E0--