2006-09-03 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mcs / class / corlib / System.Text / StringBuilder.cs
1 // -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 //
3 // System.Text.StringBuilder
4 //
5 // Authors: 
6 //   Marcin Szczepanski (marcins@zipworld.com.au)
7 //   Paolo Molaro (lupus@ximian.com)
8 //   Patrik Torstensson
9 //
10 // NOTE: In the case the buffer is only filled by 50% a new string
11 //       will be returned by ToString() is cached in the '_cached_str'
12 //               cache_string will also control if a string has been handed out
13 //               to via ToString(). If you are chaning the code make sure that
14 //               if you modify the string data set the cache_string to null.
15 //
16
17 //
18 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
19 //
20 // Permission is hereby granted, free of charge, to any person obtaining
21 // a copy of this software and associated documentation files (the
22 // "Software"), to deal in the Software without restriction, including
23 // without limitation the rights to use, copy, modify, merge, publish,
24 // distribute, sublicense, and/or sell copies of the Software, and to
25 // permit persons to whom the Software is furnished to do so, subject to
26 // the following conditions:
27 // 
28 // The above copyright notice and this permission notice shall be
29 // included in all copies or substantial portions of the Software.
30 // 
31 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 //
39 using System.Runtime.Serialization;
40 using System.Runtime.CompilerServices;
41 using System.Runtime.InteropServices;
42
43 namespace System.Text {
44         
45         [Serializable]
46 #if NET_2_0
47         [ComVisible (true)]
48 #endif
49         [MonoTODO ("Fix serialization compatibility with MS.NET")]
50         public sealed class StringBuilder
51 #if NET_2_0
52                 : ISerializable
53 #endif
54         {
55                 private int _length;
56                 private string _str;
57                 private string _cached_str;
58                 
59                 private int _maxCapacity = Int32.MaxValue;
60                 private const int constDefaultCapacity = 16;
61
62                 public StringBuilder(string value, int startIndex, int length, int capacity) 
63                 {
64                         // first, check the parameters and throw appropriate exceptions if needed
65                         if (null == value)
66                                 value = "";
67
68                         // make sure startIndex is zero or positive
69                         if (startIndex < 0)
70                                 throw new System.ArgumentOutOfRangeException ("startIndex", startIndex, "StartIndex cannot be less than zero.");
71
72                         // make sure length is zero or positive
73                         if(length < 0)
74                                 throw new System.ArgumentOutOfRangeException ("length", length, "Length cannot be less than zero.");
75
76                         if (capacity < 0)
77                                 throw new System.ArgumentOutOfRangeException ("capacity", capacity, "capacity must be greater than zero.");
78
79                         // make sure startIndex and length give a valid substring of value
80                         // re-ordered to avoid possible integer overflow
81                         if (startIndex > value.Length - length)
82                                 throw new System.ArgumentOutOfRangeException ("startIndex", startIndex, "StartIndex and length must refer to a location within the string.");
83
84                         if (capacity == 0)
85                                 capacity = constDefaultCapacity;
86
87                         _str = String.InternalAllocateStr ((length > capacity) ? length : capacity);
88                         if (length > 0)
89                                 String.InternalStrcpy(_str, 0, value, startIndex, length);
90                         
91                         _length = length;
92                 }
93
94                 public StringBuilder () : this (null) {}
95
96                 public StringBuilder(int capacity) : this (String.Empty, 0, 0, capacity) {}
97
98                 public StringBuilder(int capacity, int maxCapacity) : this (String.Empty, 0, 0, capacity) {
99                         if (maxCapacity < 1)
100                                 throw new System.ArgumentOutOfRangeException ("maxCapacity", "maxCapacity is less than one.");
101                         if (capacity > maxCapacity)
102                                 throw new System.ArgumentOutOfRangeException ("capacity", "Capacity exceeds maximum capacity.");
103
104                         _maxCapacity = maxCapacity;
105                 }
106
107                 public StringBuilder (string value)
108                 {
109                         /*
110                          * This is an optimization to avoid allocating the internal string
111                          * until the first Append () call.
112                          * The runtime pinvoke marshalling code needs to be aware of this.
113                          */
114                         if (null == value)
115                                 value = "";
116                         
117                         _length = value.Length;
118                         _str = _cached_str = value;
119                 }
120         
121                 public StringBuilder( string value, int capacity) : this(value, 0, value.Length, capacity) {}
122         
123                 public int MaxCapacity {
124                         get {
125                                 // MS runtime always returns Int32.MaxValue.
126                                 return _maxCapacity;
127                         }
128                 }
129
130                 public int Capacity {
131                         get {
132                                 if (_str.Length == 0)
133                                         return constDefaultCapacity;
134                                 
135                                 return _str.Length;
136                         }
137
138                         set {
139                                 if (value < _length)
140                                         throw new ArgumentException( "Capacity must be larger than length" );
141
142                                 InternalEnsureCapacity(value);
143                         }
144                 }
145
146                 public int Length {
147                         get {
148                                 return _length;
149                         }
150
151                         set {
152                                 if( value < 0 || value > _maxCapacity)
153                                         throw new ArgumentOutOfRangeException();
154
155                                 if (value == _length)
156                                         return;
157
158                                 if (value < _length) {
159                                         // LAMESPEC:  The spec is unclear as to what to do
160                                         // with the capacity when truncating the string.
161
162                                         // Do as MS, keep the capacity
163                                         
164                                         // Make sure that we invalidate any cached string.
165                                         InternalEnsureCapacity (value);
166                                         _length = value;
167                                 } else {
168                                         // Expand the capacity to the new length and
169                                         // pad the string with NULL characters.
170                                         Append('\0', value - _length);
171                                 }
172                         }
173                 }
174
175                 [IndexerName("Chars")]
176                 public char this [int index] {
177                         get {
178                                 if (index >= _length || index < 0)
179                                         throw new IndexOutOfRangeException();
180
181                                 return _str [index];
182                         } 
183
184                         set {
185                                 if (index >= _length || index < 0)
186                                         throw new IndexOutOfRangeException();
187
188                                 if (null != _cached_str)
189                                         InternalEnsureCapacity (_length);
190                                 
191                                 _str.InternalSetChar (index, value);
192                         }
193                 }
194
195                 public override string ToString () 
196                 {
197                         if (_length == 0)
198                                 return String.Empty;
199
200                         if (null != _cached_str)
201                                 return _cached_str;
202
203                         // If we only have a half-full buffer we return a new string.
204                         if (_length < (_str.Length >> 1)) 
205                         {
206                                 _cached_str = _str.Substring(0, _length);
207                                 return _cached_str;
208                         }
209
210                         _cached_str = _str;
211                         _str.InternalSetLength(_length);
212
213                         return _str;
214                 }
215
216                 public string ToString (int startIndex, int length) 
217                 {
218                         // re-ordered to avoid possible integer overflow
219                         if (startIndex < 0 || length < 0 || startIndex > _length - length)
220                                 throw new ArgumentOutOfRangeException();
221
222                         return _str.Substring (startIndex, length);
223                 }
224
225                 public int EnsureCapacity (int capacity) 
226                 {
227                         if (capacity < 0)
228                                 throw new ArgumentOutOfRangeException ("Capacity must be greater than 0." );
229
230                         if( capacity <= _str.Length )
231                                 return _str.Length;
232
233                         InternalEnsureCapacity (capacity);
234
235                         return _str.Length;
236                 }
237
238                 public bool Equals (StringBuilder sb) 
239                 {
240                         if (_length == sb.Length && _str == sb._str )
241                                 return true;
242
243                         return false;
244                 }
245
246                 public StringBuilder Remove (int startIndex, int length)
247                 {
248                         // re-ordered to avoid possible integer overflow
249                         if (startIndex < 0 || length < 0 || startIndex > _length - length)
250                                 throw new ArgumentOutOfRangeException();
251                         
252                         if (null != _cached_str)
253                                 InternalEnsureCapacity (_length);
254                         
255                         // Copy everything after the 'removed' part to the start 
256                         // of the removed part and truncate the sLength
257                         if (_length - (startIndex + length) > 0)
258                                 String.InternalStrcpy (_str, startIndex, _str, startIndex + length, _length - (startIndex + length));
259
260                         _length -= length;
261
262                         return this;
263                 }                              
264
265                 public StringBuilder Replace (char oldChar, char newChar) 
266                 {
267                         return Replace( oldChar, newChar, 0, _length);
268                 }
269
270                 public StringBuilder Replace (char oldChar, char newChar, int startIndex, int count) 
271                 {
272                         // re-ordered to avoid possible integer overflow
273                         if (startIndex > _length - count || startIndex < 0 || count < 0)
274                                 throw new ArgumentOutOfRangeException();
275
276                         if (null != _cached_str)
277                                 InternalEnsureCapacity (_str.Length);
278
279                         for (int replaceIterate = startIndex; replaceIterate < startIndex + count; replaceIterate++ ) {
280                                 if( _str [replaceIterate] == oldChar )
281                                         _str.InternalSetChar (replaceIterate, newChar);
282                         }
283
284                         return this;
285                 }
286
287                 public StringBuilder Replace( string oldValue, string newValue ) {
288                         return Replace (oldValue, newValue, 0, _length);
289                 }
290
291                 public StringBuilder Replace( string oldValue, string newValue, int startIndex, int count ) 
292                 {
293                         if (oldValue == null)
294                                 throw new ArgumentNullException ("The old value cannot be null.");
295
296                         if (startIndex < 0 || count < 0 || startIndex > _length - count)
297                                 throw new ArgumentOutOfRangeException ();
298
299                         if (oldValue.Length == 0)
300                                 throw new ArgumentException ("The old value cannot be zero length.");
301
302                         // TODO: OPTIMIZE!
303                         string replace = _str.Substring(startIndex, count).Replace(oldValue, newValue);
304
305                         InternalEnsureCapacity (replace.Length + (_length - count));
306
307                         string end = _str.Substring (startIndex + count, _length - startIndex - count );
308
309                         String.InternalStrcpy (_str, startIndex, replace);
310                         String.InternalStrcpy (_str, startIndex + replace.Length, end);
311                         
312                         _length = replace.Length + (_length - count);
313
314                         return this;
315                 }
316
317                       
318                 /* The Append Methods */
319                 public StringBuilder Append (char[] value) 
320                 {
321                         if (value == null)
322                                 return this;
323
324                         int needed_cap = _length + value.Length;
325                         if (null != _cached_str || _str.Length < needed_cap)
326                                 InternalEnsureCapacity (needed_cap);
327                         
328                         String.InternalStrcpy (_str, _length, value);
329                         _length = needed_cap;
330
331                         return this;
332                 } 
333                 
334                 public StringBuilder Append (string value) 
335                 {
336                         if (value == null)
337                                 return this;
338                         
339                         if (_length == 0 && value.Length < _maxCapacity && value.Length > _str.Length) {
340                                 _length = value.Length;
341                                 _str = _cached_str = value;
342                                 return this;
343                         }
344
345                         int needed_cap = _length + value.Length;
346                         if (null != _cached_str || _str.Length < needed_cap)
347                                 InternalEnsureCapacity (needed_cap);
348
349                         String.InternalStrcpy (_str, _length, value);
350                         _length = needed_cap;
351                         return this;
352                 }
353
354                 public StringBuilder Append (bool value) {
355                         return Append (value.ToString());
356                 }
357                 
358                 public StringBuilder Append (byte value) {
359                         return Append (value.ToString());
360                 }
361
362                 public StringBuilder Append (decimal value) {
363                         return Append (value.ToString());
364                 }
365
366                 public StringBuilder Append (double value) {
367                         return Append (value.ToString());
368                 }
369
370                 public StringBuilder Append (short value) {
371                         return Append (value.ToString());
372                 }
373
374                 public StringBuilder Append (int value) {
375                         return Append (value.ToString());
376                 }
377
378                 public StringBuilder Append (long value) {
379                         return Append (value.ToString());
380                 }
381
382                 public StringBuilder Append (object value) {
383                         if (value == null)
384                                 return this;
385
386                         return Append (value.ToString());
387                 }
388
389                 [CLSCompliant(false)]
390                 public StringBuilder Append (sbyte value) {
391                         return Append (value.ToString());
392                 }
393
394                 public StringBuilder Append (float value) {
395                         return Append (value.ToString());
396                 }
397
398                 [CLSCompliant(false)]
399                 public StringBuilder Append (ushort value) {
400                         return Append (value.ToString());
401                 }       
402                 
403                 [CLSCompliant(false)]
404                 public StringBuilder Append (uint value) {
405                         return Append (value.ToString());
406                 }
407
408                 [CLSCompliant(false)]
409                 public StringBuilder Append (ulong value) {
410                         return Append (value.ToString());
411                 }
412
413                 public StringBuilder Append (char value) 
414                 {
415                         int needed_cap = _length + 1;
416                         if (null != _cached_str || _str.Length < needed_cap)
417                                 InternalEnsureCapacity (needed_cap);
418
419                         _str.InternalSetChar(_length, value);
420                         _length = needed_cap;
421
422                         return this;
423                 }
424
425                 public StringBuilder Append (char value, int repeatCount) 
426                 {
427                         if( repeatCount < 0 )
428                                 throw new ArgumentOutOfRangeException();
429
430                         InternalEnsureCapacity (_length + repeatCount);
431                         
432                         for (int i = 0; i < repeatCount; i++)
433                                 _str.InternalSetChar (_length++, value);
434
435                         return this;
436                 }
437
438                 public StringBuilder Append( char[] value, int startIndex, int charCount ) 
439                 {
440                         if (value == null) {
441                                 if (!(startIndex == 0 && charCount == 0))
442                                         throw new ArgumentNullException ("value");
443
444                                 return this;
445                         }
446
447                         if ((charCount < 0 || startIndex < 0) || (startIndex > value.Length - charCount)) 
448                                 throw new ArgumentOutOfRangeException();
449                         
450                         int needed_cap = _length + charCount;
451                         InternalEnsureCapacity (needed_cap);
452
453                         String.InternalStrcpy (_str, _length, value, startIndex, charCount);
454                         _length = needed_cap;
455
456                         return this;
457                 }
458
459                 public StringBuilder Append (string value, int startIndex, int count) 
460                 {
461                         if (value == null) {
462                                 if (startIndex != 0 && count != 0)
463                                         throw new ArgumentNullException ("value");
464                                         
465                                 return this;
466                         }
467
468                         if ((count < 0 || startIndex < 0) || (startIndex > value.Length - count))
469                                 throw new ArgumentOutOfRangeException();
470                         
471                         int needed_cap = _length + count;
472                         if (null != _cached_str || _str.Length < needed_cap)
473                                 InternalEnsureCapacity (needed_cap);
474
475                         String.InternalStrcpy (_str, _length, value, startIndex, count);
476                         
477                         _length = needed_cap;
478
479                         return this;
480                 }
481
482 #if NET_2_0
483                 [ComVisible (false)]
484                 public StringBuilder AppendLine ()
485                 {
486                         return Append (System.Environment.NewLine);
487                 }
488
489                 [ComVisible (false)]
490                 public StringBuilder AppendLine (string value)
491                 {
492                         return Append (value).Append (System.Environment.NewLine);
493                 }
494 #endif
495
496                 public StringBuilder AppendFormat (string format, object arg0)
497                 {
498                         return AppendFormat (null, format, new object [] { arg0 });
499                 }
500
501                 public StringBuilder AppendFormat (string format, params object[] args)
502                 {
503                         return AppendFormat (null, format, args);
504                 }
505
506                 public StringBuilder AppendFormat (IFormatProvider provider,
507                                                    string format,
508                                                    params object[] args)
509                 {
510                         String.FormatHelper (this, provider, format, args);
511                         return this;
512                 }
513
514                 public StringBuilder AppendFormat (string format, object arg0, object arg1)
515                 {
516                         return AppendFormat (null, format, new object [] { arg0, arg1 });
517                 }
518
519                 public StringBuilder AppendFormat (string format, object arg0, object arg1, object arg2)
520                 {
521                         return AppendFormat (null, format, new object [] { arg0, arg1, arg2 });
522                 }
523
524                 /*  The Insert Functions */
525                 
526                 public StringBuilder Insert (int index, char[] value) 
527                 {
528                         return Insert (index, new string (value));
529                 }
530                                 
531                 public StringBuilder Insert (int index, string value) 
532                 {
533                         if( index > _length || index < 0)
534                                 throw new ArgumentOutOfRangeException();
535
536                         if (value == null || value.Length == 0)
537                                 return this;
538
539                         InternalEnsureCapacity (_length + value.Length);
540
541                         // Move everything to the right of the insert point across
542                         String.InternalStrcpy (_str, index + value.Length, _str, index, _length - index);
543                         
544                         // Copy in stuff from the insert buffer
545                         String.InternalStrcpy (_str, index, value);
546                         
547                         _length += value.Length;
548
549                         return this;
550                 }
551
552                 public StringBuilder Insert( int index, bool value ) {
553                         return Insert (index, value.ToString());
554                 }
555                 
556                 public StringBuilder Insert( int index, byte value ) {
557                         return Insert (index, value.ToString());
558                 }
559
560                 public StringBuilder Insert( int index, char value) 
561                 {
562                         if (index > _length || index < 0)
563                                 throw new ArgumentOutOfRangeException ("index");
564
565                         InternalEnsureCapacity (_length + 1);
566                         
567                         // Move everything to the right of the insert point across
568                         String.InternalStrcpy (_str, index + 1, _str, index, _length - index);
569                         
570                         _str.InternalSetChar (index, value);
571                         _length++;
572
573                         return this;
574                 }
575
576                 public StringBuilder Insert( int index, decimal value ) {
577                         return Insert (index, value.ToString());
578                 }
579
580                 public StringBuilder Insert( int index, double value ) {
581                         return Insert (index, value.ToString());
582                 }
583                 
584                 public StringBuilder Insert( int index, short value ) {
585                         return Insert (index, value.ToString());
586                 }
587
588                 public StringBuilder Insert( int index, int value ) {
589                         return Insert (index, value.ToString());
590                 }
591
592                 public StringBuilder Insert( int index, long value ) {
593                         return Insert (index, value.ToString());
594                 }
595         
596                 public StringBuilder Insert( int index, object value ) {
597                         return Insert (index, value.ToString());
598                 }
599                 
600                 [CLSCompliant(false)]
601                 public StringBuilder Insert( int index, sbyte value ) {
602                         return Insert (index, value.ToString() );
603                 }
604
605                 public StringBuilder Insert (int index, float value) {
606                         return Insert (index, value.ToString() );
607                 }
608
609                 [CLSCompliant(false)]
610                 public StringBuilder Insert (int index, ushort value) {
611                         return Insert (index, value.ToString() );
612                 }
613
614                 [CLSCompliant(false)]
615                 public StringBuilder Insert (int index, uint value) {
616                         return Insert ( index, value.ToString() );
617                 }
618                 
619                 [CLSCompliant(false)]
620                 public StringBuilder Insert (int index, ulong value) {
621                         return Insert ( index, value.ToString() );
622                 }
623
624                 public StringBuilder Insert (int index, string value, int count) 
625                 {
626                         // LAMESPEC: The spec says to throw an exception if 
627                         // count < 0, while MS throws even for count < 1!
628                         if ( count < 0 )
629                                 throw new ArgumentOutOfRangeException();
630
631                         if (value != null && value != String.Empty)
632                                 for (int insertCount = 0; insertCount < count; insertCount++)
633                                         Insert( index, value );
634
635                         return this;
636                 }
637
638                 public StringBuilder Insert (int index, char [] value, int startIndex, int charCount)
639                 {
640                         if (value == null) {
641                                 if (startIndex == 0 && charCount == 0)
642                                         return this;
643
644                                 throw new ArgumentNullException ("value");
645                         }
646
647                         if (charCount < 0 || startIndex < 0 || startIndex > value.Length - charCount)
648                                 throw new ArgumentOutOfRangeException ();
649
650                         return Insert (index, new String (value, startIndex, charCount));
651                 }
652         
653                 private void InternalEnsureCapacity (int size) 
654                 {
655                         if (size > _str.Length || (object) _cached_str == (object) _str) {
656                                 int capacity = _str.Length;
657
658                                 // Try double buffer, if that doesn't work, set the length as capacity
659                                 if (size > capacity) {
660                                         
661                                         // The first time a string is appended, we just set _cached_str
662                                         // and _str to it. This allows us to do some optimizations.
663                                         // Below, we take this into account.
664                                         if ((object) _cached_str == (object) _str && capacity < constDefaultCapacity)
665                                                 capacity = constDefaultCapacity;
666                                         
667                                         capacity = capacity << 1;
668                                         if (size > capacity)
669                                                 capacity = size;
670
671                                         if (capacity >= Int32.MaxValue || capacity < 0)
672                                                 capacity = Int32.MaxValue;
673
674                                         if (capacity > _maxCapacity && size <= _maxCapacity)
675                                                 capacity = _maxCapacity;
676                                         
677                                         if (capacity > _maxCapacity)
678                                                 throw new ArgumentOutOfRangeException ("size", "capacity was less than the current size.");
679                                 }
680
681                                 string tmp = String.InternalAllocateStr (capacity);
682                                 if (_length > 0)
683                                         String.InternalStrcpy (tmp, 0, _str, 0, _length);
684
685                                 _str = tmp;
686                         }
687
688                         _cached_str = null;
689                 }
690
691 #if NET_2_0
692                 [ComVisible (false)]
693                 public void CopyTo (int sourceIndex, char [] destination, int destinationIndex, int count)
694                 {
695                         if (destination == null)
696                                 throw new ArgumentNullException ("destination");
697                         if ((Length - count < sourceIndex) ||
698                             (destination.Length -count < destinationIndex) ||
699                             (sourceIndex < 0 || destinationIndex < 0 || count < 0))
700                                 throw new ArgumentOutOfRangeException ();
701
702                         for (int i = 0; i < count; i++)
703                                 destination [destinationIndex+i] = _str [sourceIndex+i];
704                 }
705
706                 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
707                 {
708                         info.AddValue ("m_MaxCapacity", _maxCapacity);
709                         info.AddValue ("Capacity", Capacity);
710                         info.AddValue ("m_StringValue", ToString ());
711                         info.AddValue ("m_currentThread", 0);
712                 }
713
714                 StringBuilder (SerializationInfo info, StreamingContext context)
715                 {
716                         string s = info.GetString ("m_StringValue");
717                         if (s == null)
718                                 s = "";
719                         _length = s.Length;
720                         _str = _cached_str = s;
721                         
722                         _maxCapacity = info.GetInt32 ("m_MaxCapacity");
723                         if (_maxCapacity < 0)
724                                 _maxCapacity = Int32.MaxValue;
725                         Capacity = info.GetInt32 ("Capacity");
726                 }
727 #endif
728         }
729 }