2004-02-23 Ben Maurer <bmaurer@users.sourceforge.net>
[mono.git] / mcs / class / corlib / System / String.cs
1 //
2 // System.String.cs
3 //
4 // Authors:
5 //       Patrik Torstensson
6 //   Jeffrey Stedfast (fejj@ximian.com)
7 //   Dan Lewis (dihlewis@yahoo.co.uk)
8 //
9 // (C) 2001 Ximian, Inc.  http://www.ximian.com
10 //
11
12 using System;
13 using System.Text;
14 using System.Collections;
15 using System.Globalization;
16 using System.Runtime.CompilerServices;
17
18 namespace System {
19         [Serializable]
20         public sealed class String : IConvertible, IComparable, ICloneable, IEnumerable {
21                 [NonSerialized] private int length;
22                 [NonSerialized] private char start_char;
23
24                 private const int COMPARE_CASE = 0;
25                 private const int COMPARE_INCASE = 1;
26                 private const int COMPARE_ORDINAL = 2;
27
28                 public static readonly String Empty = "";
29
30                 public static unsafe bool Equals (string str1, string str2)
31                 {
32                         if ((str1 as object) == (str2 as object))
33                                 return true;
34             
35                         if (null == str1 || null == str2)
36                                 return false;
37
38                         int len = str1.length;
39                         
40                         if (len != str2.length)
41                                 return false;
42
43                         if (len == 0)
44                                 return true;
45                         
46                         fixed (char * s1 = &str1.start_char, s2 = &str2.start_char) {
47                                 // it must be one char, because 0 len is done above
48                                 if (len < 2)
49                                         return *s1 == *s2;
50                                 
51                                 // check by twos
52                                 int * sint1 = (int *) s1, sint2 = (int *) s2;
53                                 int n2 = len >> 1;
54                                 do {
55                                         if (*sint1++ != *sint2++)
56                                                 return false;
57                                 } while (--n2 != 0);
58                                 
59                                 // nothing left
60                                 if ((len & 1) == 0)
61                                         return true;
62                                 
63                                 // check the last one
64                                 return *(char *) sint1 == *(char *) sint2;
65                         }
66                 }
67
68                 public static bool operator == (String str1, String str2) {
69                         return Equals(str1, str2);
70                 }
71
72                 public static bool operator != (String str1, String str2) {
73                         return !Equals(str1, str2);
74                 }
75
76                 public override bool Equals(Object obj) {
77                         return Equals (this, obj as String);
78                 }
79
80                 public bool Equals(String value) {
81                         return Equals (this, value);
82                 }
83
84                 [IndexerName("Chars")]
85                 public extern char this[int index] {
86                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
87                         get;
88                 }
89
90                 public Object Clone() {
91                         return this;
92                 }
93
94                 public TypeCode GetTypeCode () {
95                         return TypeCode.String;
96                 }
97
98                 public void CopyTo(int sindex, char[] dest, int dindex, int count) {
99                         // LAMESPEC: should I null-terminate?
100                         
101                         if (dest == null)
102                                 throw new ArgumentNullException();
103
104                         if (sindex < 0 || dindex < 0 || count < 0)
105                                 throw new ArgumentOutOfRangeException (); 
106
107                         if (sindex + count > Length)
108                                 throw new ArgumentOutOfRangeException ();
109
110                         if (dindex + count > dest.Length)
111                                 throw new ArgumentOutOfRangeException ();
112
113                         InternalCopyTo(sindex, dest, dindex, count);
114                 }
115
116                 public char[] ToCharArray() {
117                         return ToCharArray(0, length);
118                 }
119
120                 public char[] ToCharArray(int sindex, int length) {
121                         if (sindex < 0 || length < 0 || sindex + length > this.length)
122                                 throw new ArgumentOutOfRangeException (); 
123
124                         char [] tmp = new char[length];
125
126                         InternalCopyTo(sindex, tmp, 0, length);
127
128                         return tmp;
129                 }
130                 
131                 public String [] Split(params char [] separator) {
132                         return Split(separator, Int32.MaxValue);
133                 }
134
135                 public String[] Split(char[] separator, int count) {
136                         if (null == separator || separator.Length == 0) {
137                                 separator = WhiteChars;
138                         }
139
140                         if (count < 0)
141                                 throw new ArgumentOutOfRangeException ();
142
143                         if (count == 0) 
144                                 return new String[0];
145
146                         if (count == 1) 
147                                 return new String[1] { ToString() };
148
149                         return InternalSplit(separator, count);
150                 }
151
152                 public String Substring (int sindex) {
153                         if (sindex < 0 || sindex > this.length) {
154                                 throw new ArgumentOutOfRangeException();
155                         }
156
157                         string tmp = InternalAllocateStr(this.length - sindex);
158                         InternalStrcpy(tmp, 0, this, sindex, length - sindex);
159                         
160                         return tmp;
161                 }
162
163                 public String Substring (int sindex, int length) {
164                         if (length < 0 || sindex < 0 || sindex + length > this.length) {
165                                 throw new ArgumentOutOfRangeException();
166                         }
167
168                         if (length == 0)
169                                 return String.Empty;
170
171                         string tmp = InternalAllocateStr(length);
172                         InternalStrcpy(tmp, 0, this, sindex, length);
173
174                         return tmp;
175                 }       
176
177                 private static readonly char[] WhiteChars = {  (char) 0x9, (char) 0xA, (char) 0xB, (char) 0xC, (char) 0xD, (char) 0x20, (char) 0xA0, (char) 0x2000, (char) 0x2001, (char) 0x2002, (char) 0x2003, (char) 0x2004, (char) 0x2005,
178                                                                                                                                           (char) 0x2006, (char) 0x2007, (char) 0x2008, (char) 0x2009, (char) 0x200A, (char) 0x200B, (char) 0x3000, (char) 0xFEFF };
179
180                 public String Trim(params char[] chars) {
181                         if (null == chars || chars.Length == 0)
182                                 chars = WhiteChars;
183
184                         return InternalTrim(chars, 0);
185                 }
186
187                 public String TrimStart(params char[] chars) {
188                         if (null == chars || chars.Length == 0)
189                                 chars = WhiteChars;
190
191                         return InternalTrim(chars, 1);
192                 }
193
194                 public String TrimEnd(params char[] chars) {
195                         if (null == chars || chars.Length == 0)
196                                 chars = WhiteChars;
197
198                         return InternalTrim(chars, 2);
199                 }
200
201                 public static int Compare(String s1, String s2) {
202                         return(Compare(s1, s2, false,
203                                        CultureInfo.CurrentCulture));
204                 }
205
206                 public static int Compare(String s1, String s2, bool inCase) {
207                         return(Compare (s1, s2, inCase,
208                                         CultureInfo.CurrentCulture));
209                 }
210                 
211                 public static int Compare(String s1, String s2, bool inCase,
212                                           CultureInfo culture) {
213                         if (culture == null) {
214                                 throw new ArgumentNullException ("culture");
215                         }
216                         
217                         if (s1 == null) {
218                                 if (s2 == null) {
219                                         return(0);
220                                 } else {
221                                         return(-1);
222                                 }
223                         } else if (s2 == null) {
224                                 return(1);
225                         }
226
227                         CompareOptions compopts;
228                         
229                         if(inCase) {
230                                 compopts=CompareOptions.IgnoreCase;
231                         } else {
232                                 compopts=CompareOptions.None;
233                         }
234                         
235                         return(culture.CompareInfo.Compare (s1, s2, compopts));
236                 }
237
238                 public static int Compare(String s1, int i1, String s2, int i2,
239                                           int length) {
240                         return(Compare(s1, i1, s2, i2, length, false,
241                                        CultureInfo.CurrentCulture));
242                 }
243
244                 public static int Compare(String s1, int i1, String s2, int i2,
245                                           int length, bool inCase) {
246                         return(Compare (s1, i1, s2, i2, length, inCase,
247                                         CultureInfo.CurrentCulture));
248                 }
249                 
250                 public static int Compare(String s1, int i1, String s2, int i2,
251                                           int length, bool inCase,
252                                           CultureInfo culture) {
253                         if(culture==null) {
254                                 throw new ArgumentNullException ("culture");
255                         }
256
257                         if((i1 > s1.Length) ||
258                            (i2 > s2.Length) ||
259                            (i1 < 0) || (i2 < 0) || (length < 0)) {
260                                 throw new ArgumentOutOfRangeException ();
261                         }
262
263                         if (length==0) {
264                                 return(0);
265                         }
266                         
267                         if (s1 == null) {
268                                 if (s2 == null) {
269                                         return(0);
270                                 } else {
271                                         return(-1);
272                                 }
273                         } else if (s2 == null) {
274                                 return(1);
275                         }
276                         
277                         CompareOptions compopts;
278                         
279                         if(inCase) {
280                                 compopts=CompareOptions.IgnoreCase;
281                         } else {
282                                 compopts=CompareOptions.None;
283                         }
284                         
285                         /* Need to cap the requested length to the
286                          * length of the string, because
287                          * CompareInfo.Compare will insist that length
288                          * <= (string.Length - offset)
289                          */
290                         int len1=length;
291                         int len2=length;
292                         
293                         if(length > (s1.Length - i1)) {
294                                 len1=s1.Length - i1;
295                         }
296
297                         if(length > (s2.Length - i2)) {
298                                 len2=s2.Length - i2;
299                         }
300
301                         return(culture.CompareInfo.Compare(s1, i1, len1,
302                                                            s2, i2, len2,
303                                                            compopts));
304                 }
305
306                 public int CompareTo(Object value) {
307                         if (null == value)
308                                 return 1;
309             
310                         if (!(value is String))
311                                 throw new ArgumentException();
312
313                         return String.Compare(this, (String) value, false);
314                 }
315
316                 public int CompareTo(String str) {
317                         if (null == str)
318                                 return 1;
319
320                         return Compare(this, str, false);
321                 }
322
323                 public static int CompareOrdinal(String s1, String s2) {
324                         if (s1 == null) {
325                                 if (s2 == null) {
326                                         return(0);
327                                 } else {
328                                         return(-1);
329                                 }
330                         } else if (s2 == null) {
331                                 return(1);
332                         }
333
334                         /* Invariant, because that is cheaper to
335                          * instantiate (and chances are it already has
336                          * been.)
337                          */
338                         return(CultureInfo.InvariantCulture.CompareInfo.Compare (s1, s2, CompareOptions.Ordinal));
339                 }
340
341                 public static int CompareOrdinal(String s1, int i1, String s2,
342                                                  int i2, int length)
343                 {
344                         if ((i1 > s1.Length) ||
345                             (i2 > s2.Length) ||
346                             (i1 < 0) || (i2 < 0) || (length < 0)) {
347                                 throw new ArgumentOutOfRangeException ();
348                         }
349
350                         if (s1 == null) {
351                                 if (s2 == null) {
352                                         return(0);
353                                 } else {
354                                         return(-1);
355                                 }
356                         } else if (s2 == null) {
357                                 return(1);
358                         }
359
360                         /* Need to cap the requested length to the
361                          * length of the string, because
362                          * CompareInfo.Compare will insist that length
363                          * <= (string.Length - offset)
364                          */
365                         int len1=length;
366                         int len2=length;
367                         
368                         if(length > (s1.Length - i1)) {
369                                 len1=s1.Length - i1;
370                         }
371
372                         if(length > (s2.Length - i2)) {
373                                 len2=s2.Length - i2;
374                         }
375
376                         return(CultureInfo.InvariantCulture.CompareInfo.Compare(s1, i1, len1, s2, i2, len2, CompareOptions.Ordinal));
377                 }
378
379                 public bool EndsWith(String value) {
380                         if (null == value)
381                                 throw new ArgumentNullException();
382
383                         if (value == String.Empty) {
384                                 return(true);
385                         }
386                         
387                         if (value.length > this.length) {
388                                 return false;
389                         }
390
391                         return (0 == Compare(this, length - value.length, value, 0, value.length));
392                 }
393         
394                 public int IndexOfAny(char [] arr) {
395                         if (null == arr)
396                                 throw new ArgumentNullException();
397
398                         return InternalIndexOfAny(arr, 0, this.length);
399                 }
400
401                 public int IndexOfAny(char [] arr, int sindex) {
402                         if (null == arr)
403                                 throw new ArgumentNullException();
404                         if (sindex < 0 || sindex >= this.length)
405                                 throw new ArgumentOutOfRangeException();
406
407                         return InternalIndexOfAny(arr, sindex, this.length - sindex);
408                 }
409
410                 public int IndexOfAny(char [] arr, int sindex, int count) {
411                         if (null == arr)
412                                 throw new ArgumentNullException();
413                         if (sindex < 0 || count < 0 || sindex + count > this.length)
414                                 throw new ArgumentOutOfRangeException ();
415
416                         return InternalIndexOfAny(arr, sindex, count);
417                 }
418
419                 public int IndexOf(char value) {
420                         return(IndexOf(value, 0, this.length));
421                 }
422
423                 public int IndexOf(String value) {
424                         return(IndexOf(value, 0, this.length));
425                 }
426
427                 public int IndexOf(char value, int sindex) {
428                         return(IndexOf(value, sindex, this.length - sindex));
429                 }
430
431                 public int IndexOf(String value, int sindex) {
432                         return(IndexOf(value, sindex, this.length - sindex));
433                 }
434
435                 /* This method is culture-insensitive */
436                 public int IndexOf(char value, int sindex, int count) {
437                         if (sindex < 0 || count < 0 ||
438                             sindex + count > this.length) {
439                                 throw new ArgumentOutOfRangeException ();
440                         }
441
442                         if ((sindex == 0 && this.length == 0) ||
443                             (sindex == this.length) ||
444                             (count == 0)) {
445                                 return(-1);
446                         }
447                         
448                         for(int pos=sindex; pos < sindex + count; pos++) {
449                                 if(this[pos]==value) {
450                                         return(pos);
451                                 }
452                         }
453
454                         return(-1);
455                 }
456                 
457                 /* But this one is culture-sensitive */
458                 public int IndexOf(String value, int sindex, int count) {
459                         if (value == null) {
460                                 throw new ArgumentNullException();
461                         }
462
463                         if (sindex < 0 || count < 0 ||
464                             sindex + count > this.length) {
465                                 throw new ArgumentOutOfRangeException ();
466                         }
467
468                         if (value.length == 0) {
469                                 return sindex;
470                         }
471                         
472                         if (sindex == 0 && this.length == 0) {
473                                 return -1;
474                         }
475
476                         if (count == 0) {
477                                 return(-1);
478                         }
479                         
480                         return(CultureInfo.CurrentCulture.CompareInfo.IndexOf (this, value, sindex, count));
481                 }
482
483                 public int LastIndexOfAny(char [] arr) {
484                         if (null == arr) 
485                                 throw new ArgumentNullException();
486
487                         return InternalLastIndexOfAny(arr, this.length - 1, this.length);
488                 }
489
490                 public int LastIndexOfAny(char [] arr, int sindex) {
491                         if (null == arr) 
492                                 throw new ArgumentNullException();
493
494                         if (sindex < 0 || sindex > this.length)
495                                 throw new ArgumentOutOfRangeException();
496
497                         if (this.length == 0)
498                                 return -1;
499
500                         return InternalLastIndexOfAny(arr, sindex, sindex + 1);
501                 }
502
503                 public int LastIndexOfAny(char [] arr, int sindex, int count) {
504                         if (null == arr) 
505                                 throw new ArgumentNullException();
506
507                         if (sindex < 0 || count < 0 || sindex > this.length || sindex - count < -1)
508                                 throw new ArgumentOutOfRangeException();
509
510                         if (this.length == 0)
511                                 return -1;
512
513                         return InternalLastIndexOfAny(arr, sindex, count);
514                 }
515
516                 public int LastIndexOf(char value) {
517                         if(this.length==0) {
518                                 return(-1);
519                         } else {
520                                 return(LastIndexOf(value, this.length - 1,
521                                                    this.length));
522                         }
523                         
524                 }
525
526                 public int LastIndexOf(String value) {
527                         if(this.length==0) {
528                                 /* This overload does additional checking */
529                                 return(LastIndexOf(value, 0, 0));
530                         } else {
531                                 return(LastIndexOf(value, this.length - 1,
532                                                    this.length));
533                         }
534                 }
535
536                 public int LastIndexOf(char value, int sindex){
537                         return(LastIndexOf(value, sindex, sindex + 1));
538                 }
539
540                 public int LastIndexOf(String value, int sindex) {
541                         return(LastIndexOf(value, sindex, sindex + 1));
542                 }
543
544                 /* This method is culture-insensitive */
545                 public int LastIndexOf(char value, int sindex, int count) {
546                         if (sindex == 0 && this.length == 0) {
547                                 return -1;
548                         }
549
550                         if (sindex < 0 || count < 0) {
551                                 throw new ArgumentOutOfRangeException ();
552                         }
553
554                         if (sindex >= this.length || sindex - count + 1 < 0) {
555                                 throw new ArgumentOutOfRangeException ();
556                         }
557                         
558                         for(int pos=sindex; pos > sindex - count; pos--) {
559                                 if(this[pos]==value) {
560                                         return(pos);
561                                 }
562                         }
563
564                         return(-1);
565                 }
566
567                 /* But this one is culture-sensitive */
568                 public int LastIndexOf(String value, int sindex, int count) {
569                         if (null == value) {
570                                 throw new ArgumentNullException();
571                         }
572
573                         if (value == String.Empty) {
574                                 return(0);
575                         }
576
577                         if (sindex == 0 && this.length == 0) {
578                                 return -1;
579                         }
580
581                         // This check is needed to match undocumented MS behaviour
582                         if (this.length == 0 && value.length > 0) {
583                                 return(-1);
584                         }
585
586                         if (value.length > sindex) {
587                                 return -1;
588                         }
589
590                         if (count == 0) {
591                                 return(-1);
592                         }
593
594                         if (sindex < 0 || sindex > this.length) {
595                                 throw new ArgumentOutOfRangeException ();
596                         }
597
598                         if (count < 0 || sindex - count + 1 < 0) {
599                                 throw new ArgumentOutOfRangeException ();
600                         }
601                         
602                         return(CultureInfo.CurrentCulture.CompareInfo.LastIndexOf (this, value, sindex, count));
603                 }
604
605                 public String PadLeft(int width) {
606                         return PadLeft(width, ' ');
607                 }
608
609                 public String PadLeft(int width, char chr) {
610                         if (width < 0)
611                                 throw new ArgumentException();
612
613                         if (width < this.length)
614                                 return String.Copy(this);
615
616                         return InternalPad(width, chr, false);
617                 }
618
619                 public String PadRight(int width) {
620                         return PadRight(width, ' ');
621                 }
622
623                 public String PadRight(int width, char chr) {
624                         if (width < 0)
625                                 throw new ArgumentException();
626
627                         if (width < this.length)
628                                 return String.Copy(this);
629
630                         return InternalPad(width, chr, true);
631                 }
632
633                 public bool StartsWith(String value) {
634                         if (value == null) {
635                                 throw new ArgumentNullException("value");
636                         }
637                         
638                         if (value == String.Empty) {
639                                 return(true);
640                         }
641
642                         if (this.length < value.length) {
643                                 return(false);
644                         }
645
646                         return (0 == Compare(this, 0, value, 0 , value.length));
647                 }
648         
649     
650                 /* This method is culture insensitive */
651                 public String Replace (char oldChar, char newChar) {
652                         return(InternalReplace(oldChar, newChar));
653                 }
654
655                 /* This method is culture sensitive */
656                 public String Replace(String oldValue, String newValue) {
657                         if(oldValue==null) {
658                                 throw new ArgumentNullException ("oldValue");
659                         }
660                         if(oldValue==String.Empty) {
661                                 throw new ArgumentException ("oldValue is the empty string.");
662                         }
663
664                         if(this==String.Empty) {
665                                 return(this);
666                         }
667
668                         if(newValue==null) {
669                                 newValue=String.Empty;
670                         }
671                         
672                         return(InternalReplace (oldValue, newValue, CultureInfo.CurrentCulture.CompareInfo));
673                 }
674
675                 public String Remove(int sindex, int count) {
676                         if (sindex < 0 || count < 0 || sindex + count > this.length)
677                                 throw new ArgumentOutOfRangeException ();
678
679                         return InternalRemove(sindex, count);
680                 }
681
682                 public String ToLower() {
683                         return(InternalToLower(CultureInfo.CurrentCulture));
684                 }
685
686                 public String ToLower(CultureInfo culture) {
687                         return(InternalToLower(culture));
688                 }
689
690                 public String ToUpper() {
691                         return(InternalToUpper(CultureInfo.CurrentCulture));
692                 }
693
694                 public String ToUpper(CultureInfo culture) {
695                         return(InternalToUpper(culture));
696                 }
697
698                 public override String ToString() {
699                         return this;
700                 }
701
702                 public String ToString(IFormatProvider provider) {
703                         return this;
704                 }
705
706                 public String Trim() {
707                         return Trim(null);
708                 }
709
710                 public static String Format(String format, Object arg0) {
711                         return Format(null, format, new Object[] {arg0});
712                 }
713
714                 public static String Format(String format, Object arg0, Object arg1) {
715                         return Format(null, format, new Object[] {arg0, arg1});
716                 }
717
718                 public static String Format(String format, Object arg0, Object arg1, Object arg2) {
719                         return Format(null, format, new Object[] {arg0, arg1, arg2});
720                 }
721
722                 public static string Format (string format, params object[] args) {
723                         return Format (null, format, args);
724                 }
725         
726                 public static string Format (IFormatProvider provider, string format, params object[] args) {
727                         StringBuilder b = new StringBuilder ();
728                         FormatHelper (b, provider, format, args);
729                         return b.ToString ();
730                 }
731                 
732                 internal static void FormatHelper (StringBuilder result, IFormatProvider provider, string format, params object[] args) {
733                         if (format == null || args == null)
734                                 throw new ArgumentNullException ();
735                         
736                         int ptr = 0;
737                         int start = ptr;
738                         while (ptr < format.length) {
739                                 char c = format[ptr ++];
740
741                                 if (c == '{') {
742                                         result.Append (format, start, ptr - start - 1);
743
744                                         // check for escaped open bracket
745
746                                         if (format[ptr] == '{') {
747                                                 start = ptr ++;
748                                                 continue;
749                                         }
750
751                                         // parse specifier
752                                 
753                                         int n, width;
754                                         bool left_align;
755                                         string arg_format;
756
757                                         ParseFormatSpecifier (format, ref ptr, out n, out width, out left_align, out arg_format);
758                                         if (n >= args.Length)
759                                                 throw new FormatException ("Index (zero based) must be greater than or equal to zero and less than the size of the argument list.");
760
761                                         // format argument
762
763                                         object arg = args[n];
764
765                                         string str;
766                                         if (arg == null)
767                                                 str = "";
768                                         else if (arg is IFormattable)
769                                                 str = ((IFormattable)arg).ToString (arg_format, provider);
770                                         else
771                                                 str = arg.ToString ();
772
773                                         // pad formatted string and append to result
774
775                                         if (width > str.length) {
776                                                 string pad = new String (' ', width - str.length);
777
778                                                 if (left_align) {
779                                                         result.Append (str);
780                                                         result.Append (pad);
781                                                 }
782                                                 else {
783                                                         result.Append (pad);
784                                                         result.Append (str);
785                                                 }
786                                         }
787                                         else
788                                                 result.Append (str);
789
790                                         start = ptr;
791                                 }
792                                 else if (c == '}' && ptr < format.length && format[ptr] == '}') {
793                                         result.Append (format, start, ptr - start - 1);
794                                         start = ptr ++;
795                                 }
796                                 else if (c == '}') {
797                                         throw new FormatException ("Input string was not in a correct format.");
798                                 }
799                         }
800
801                         if (start < format.length)
802                                 result.Append (format.Substring (start));
803                 }
804
805                 public static String Copy (String str) {
806                         if (str == null)
807                                 throw new ArgumentNullException ();
808
809                         int length = str.length;
810
811                         String tmp = InternalAllocateStr(length);
812                         InternalStrcpy(tmp, 0, str);
813                         return tmp;
814                 }
815
816                 public static String Concat(Object obj) {
817                         if (null == obj)
818                                 return String.Empty;
819
820                         return obj.ToString();
821                 }
822
823                 public static String Concat(Object obj1, Object obj2)
824                 {
825                         string s1, s2;
826
827                         if (obj1 == null){
828                                 if (obj2 == null)
829                                         return String.Empty;
830                                 else
831                                         return obj2.ToString ();
832                         } else if (obj2 == null)
833                                 return obj1.ToString ();
834
835                         s1 = obj1.ToString ();
836                         s2 = obj2.ToString ();
837                         String tmp = InternalAllocateStr (s1.Length + s2.Length);
838                         InternalStrcpy (tmp, 0, s1);
839                         InternalStrcpy (tmp, s1.length, s2);
840
841                         return tmp;
842                 }
843
844                 public static String Concat(Object obj1, Object obj2, Object obj3)
845                 {
846                         string s1, s2, s3;
847                         if (obj1 == null)
848                                 s1 = String.Empty;
849                         else
850                                 s1 = obj1.ToString ();
851     
852                         if (obj2 == null)
853                                 s2 = String.Empty;
854                         else
855                                 s2 = obj2.ToString ();
856     
857                         if (obj3 == null)
858                                 s3 = String.Empty;
859                         else
860                                 s3 = obj3.ToString ();
861     
862                         return Concat (s1, s2, s3);
863                 }
864
865                 public static String Concat (Object obj1, Object obj2, Object obj3, Object obj4)
866                 {
867                         string s1, s2, s3, s4;
868
869                         if (obj1 == null)
870                                 s1 = String.Empty;
871                         else
872                                 s1 = obj1.ToString ();
873     
874                         if (obj2 == null)
875                                 s2 = String.Empty;
876                         else
877                                 s2 = obj2.ToString ();
878     
879                         if (obj3 == null)
880                                 s3 = String.Empty;
881                         else
882                                 s3 = obj3.ToString ();
883
884                         if (obj4 == null)
885                                 s4 = String.Empty;
886                         else
887                                 s4 = obj4.ToString ();
888                         
889                         return Concat (s1, s2, s3, s4);
890                         
891                 }
892
893                 public static String Concat(String s1, String s2)
894                 {
895                         if (s1 == null) {
896                                 if (s2 == null)
897                                         return String.Empty;
898                                 return s2;
899                         }
900
901                         if (s2 == null)
902                                 return s1; 
903
904                         String tmp = InternalAllocateStr(s1.length + s2.length);
905             
906                         InternalStrcpy(tmp, 0, s1);
907                         InternalStrcpy(tmp, s1.length, s2);
908             
909                         return tmp;
910                 }
911
912                 public static String Concat(String s1, String s2, String s3)
913                 {
914                         if (s1 == null){
915                                 if (s2 == null){
916                                         if (s3 == null)
917                                                 return String.Empty;
918                                         return s3;
919                                 } else {
920                                         if (s3 == null)
921                                                 return s2;
922                                 }
923                                 s1 = String.Empty;
924                         } else {
925                                 if (s2 == null){
926                                         if (s3 == null)
927                                                 return s1;
928                                         else
929                                                 s2 = String.Empty;
930                                 } else {
931                                         if (s3 == null)
932                                                 s3 = String.Empty;
933                                 }
934                         }
935
936                         //return InternalConcat (s1, s2, s3);
937                         String tmp = InternalAllocateStr(s1.length + s2.length + s3.length);
938
939                         InternalStrcpy(tmp, 0, s1);
940                         InternalStrcpy(tmp, s1.length, s2);
941                         InternalStrcpy(tmp, s1.length + s2.length, s3);
942
943                         return tmp;
944                 }
945
946                 public static String Concat(String s1, String s2, String s3, String s4) {
947                         if (null == s1 && null == s2 && null == s3 && null == s4) {
948                                 return String.Empty;
949                         }
950
951                         if (null == s1) { s1 = String.Empty; }
952                         if (null == s2) { s2 = String.Empty; }
953                         if (null == s3) { s3 = String.Empty; }
954                         if (null == s4) { s4 = String.Empty; }
955
956                         String tmp = InternalAllocateStr(s1.length + s2.length + s3.length + s4.length);
957
958                         InternalStrcpy(tmp, 0, s1);
959                         InternalStrcpy(tmp, s1.length, s2);
960                         InternalStrcpy(tmp, s1.length + s2.length, s3);
961                         InternalStrcpy(tmp, s1.length + s2.length + s3.length, s4);
962
963                         return tmp;
964                 }
965
966                 public static String Concat(params Object[] args) {
967                         string [] strings;
968                         int len, i, currentpos;
969
970                         if (null == args)
971                                 throw new ArgumentNullException ();
972
973                         strings = new string [args.Length];
974                         len = 0;
975                         i = 0;
976                         foreach (object arg in args) {
977                                 /* use Empty for each null argument */
978                                 if (arg == null)
979                                         strings[i] = String.Empty;
980                                 else
981                                         strings[i] = arg.ToString ();
982                                 len += strings[i].length;
983                                 i++;
984                         }
985
986                         if (len == 0)
987                                 return String.Empty;
988
989                         currentpos = 0;
990
991                         String tmp = InternalAllocateStr(len);
992                         for (i = 0; i < strings.Length; i++) {
993                                 InternalStrcpy(tmp, currentpos, strings[i]);
994                                 currentpos += strings[i].length;
995                         }
996
997                         return tmp;
998                 }
999
1000                 public static String Concat(params String[] values) {
1001                         int len, i, currentpos;
1002
1003                         if (values == null)
1004                                 throw new ArgumentNullException ();
1005
1006                         len = 0;
1007                         foreach (string value in values)
1008                                 len += value != null ? value.length : 0;
1009
1010                         if (len == 0)
1011                                 return String.Empty;
1012
1013                         currentpos = 0;
1014
1015                         String tmp = InternalAllocateStr(len);
1016                         for (i = 0; i < values.Length; i++) {
1017                                 if (values[i] == null)
1018                                         continue;
1019
1020                                 InternalStrcpy(tmp, currentpos, values[i]);
1021                                 currentpos += values[i].length;
1022                         }       
1023         
1024                         return tmp;
1025                 }
1026
1027                 public String Insert(int sindex, String value) {
1028                         if (null == value)
1029                                 throw new ArgumentNullException();
1030
1031                         if (sindex < 0 || sindex > this.length)
1032                                 throw new ArgumentOutOfRangeException();
1033         
1034                         return InternalInsert(sindex, value);
1035                 }
1036
1037
1038                 public static string Intern (string str) {
1039                         if (null == str)
1040                                 throw new ArgumentNullException ();
1041
1042                         return InternalIntern(str);
1043                 }
1044
1045                 public static string IsInterned (string str) {
1046                         if (null == str)
1047                                 throw new ArgumentNullException();
1048
1049                         return InternalIsInterned(str);
1050                 }
1051         
1052                 public static string Join (string separator, string [] value) {
1053                         if (value == null)
1054                                 throw new ArgumentNullException ();
1055
1056                         return Join(separator, value, 0, value.Length);
1057                 }
1058
1059                 public static string Join(string separator, string[] value, int sindex, int count) {
1060                         if (value == null)
1061                                 throw new ArgumentNullException ();
1062
1063                         if (sindex + count > value.Length)
1064                                 throw new ArgumentOutOfRangeException ();
1065
1066                         if (sindex == value.Length)
1067                                 return String.Empty;
1068
1069                         return InternalJoin(separator, value, sindex, count);
1070                 }
1071
1072                 bool IConvertible.ToBoolean (IFormatProvider provider) {
1073                         return Convert.ToBoolean (this);
1074                 }
1075                 
1076                 byte IConvertible.ToByte (IFormatProvider provider) {
1077                         return Convert.ToByte (this);
1078                 }
1079                 
1080                 char IConvertible.ToChar (IFormatProvider provider) {
1081                         return Convert.ToChar (this);
1082                 }
1083
1084                 DateTime IConvertible.ToDateTime (IFormatProvider provider) {
1085                         return Convert.ToDateTime (this);
1086                 }
1087
1088                 decimal IConvertible.ToDecimal (IFormatProvider provider) {
1089                         return Convert.ToDecimal (this);
1090                 }
1091
1092                 double IConvertible.ToDouble (IFormatProvider provider) {
1093                         return Convert.ToDouble (this);
1094                 }
1095
1096                 short IConvertible.ToInt16 (IFormatProvider provider) {
1097                         return Convert.ToInt16 (this);
1098                 }
1099
1100                 int IConvertible.ToInt32 (IFormatProvider provider) {
1101                         return Convert.ToInt32 (this);
1102                 }
1103
1104                 long IConvertible.ToInt64 (IFormatProvider provider) {
1105                         return Convert.ToInt64 (this);
1106                 }
1107         
1108                 [CLSCompliant(false)]
1109                 sbyte IConvertible.ToSByte (IFormatProvider provider) {
1110                         return Convert.ToSByte (this);
1111                 }
1112
1113                 float IConvertible.ToSingle (IFormatProvider provider) {
1114                         return Convert.ToSingle (this);
1115                 }
1116                 string IConvertible.ToString (IFormatProvider format) {
1117                         return this;
1118                 }
1119
1120                 object IConvertible.ToType (Type conversionType, IFormatProvider provider) {
1121                         return Convert.ToType (this, conversionType,  provider);
1122                 }
1123
1124                 [CLSCompliant(false)]
1125                 ushort IConvertible.ToUInt16 (IFormatProvider provider) {
1126                         return Convert.ToUInt16 (this);
1127                 }
1128
1129                 [CLSCompliant(false)]
1130                 uint IConvertible.ToUInt32 (IFormatProvider provider) {
1131                         return Convert.ToUInt32 (this);
1132                 }
1133
1134                 [CLSCompliant(false)]
1135                 ulong IConvertible.ToUInt64 (IFormatProvider provider) {
1136                         return Convert.ToUInt64 (this);
1137                 }
1138
1139                 TypeCode IConvertible.GetTypeCode () {
1140                         return TypeCode.String;
1141                 }
1142
1143                 public int Length {
1144                         get {
1145                                 return length;
1146                         }
1147                 }
1148
1149                 public CharEnumerator GetEnumerator () {
1150                         return new CharEnumerator (this);
1151                 }
1152                 
1153                 IEnumerator IEnumerable.GetEnumerator () {
1154                         return new CharEnumerator (this);
1155                 }
1156
1157                 private static void ParseFormatSpecifier (string str, ref int ptr, out int n, out int width, out bool left_align, out string format) {
1158                         // parses format specifier of form:
1159                         //   N,[\ +[-]M][:F]}
1160                         //
1161                         // where:
1162
1163                         try {
1164                                 // N = argument number (non-negative integer)
1165                         
1166                                 n = ParseDecimal (str, ref ptr);
1167                                 if (n < 0)
1168                                         throw new FormatException ("Input string was not in a correct format.");
1169                                 
1170                                 // M = width (non-negative integer)
1171
1172                                 if (str[ptr] == ',') {
1173                                         // White space between ',' and number or sign.
1174                                         int start = ++ptr;
1175                                         while (Char.IsWhiteSpace (str [ptr]))
1176                                                 ++ptr;
1177
1178                                         format = str.Substring (start, ptr - start);
1179
1180                                         left_align = (str [ptr] == '-');
1181                                         if (left_align)
1182                                                 ++ ptr;
1183
1184                                         width = ParseDecimal (str, ref ptr);
1185                                         if (width < 0)
1186                                                 throw new FormatException ("Input string was not in a correct format.");
1187                                 }
1188                                 else {
1189                                         width = 0;
1190                                         left_align = false;
1191                                         format = "";
1192                                 }
1193
1194                                 // F = argument format (string)
1195
1196                                 if (str[ptr] == ':') {
1197                                         int start = ++ ptr;
1198                                         while (str[ptr] != '}')
1199                                                 ++ ptr;
1200
1201                                         format += str.Substring (start, ptr - start);
1202                                 }
1203                                 else
1204                                         format = null;
1205
1206                                 if (str[ptr ++] != '}')
1207                                         throw new FormatException ("Input string was not in a correct format.");
1208                         }
1209                         catch (IndexOutOfRangeException) {
1210                                 throw new FormatException ("Input string was not in a correct format.");
1211                         }
1212                 }
1213
1214                 private static int ParseDecimal (string str, ref int ptr) {
1215                         int p = ptr;
1216                         int n = 0;
1217                         while (true) {
1218                                 char c = str[p];
1219                                 if (c < '0' || '9' < c)
1220                                         break;
1221
1222                                 n = n * 10 + c - '0';
1223                                 ++ p;
1224                         }
1225
1226                         if (p == ptr)
1227                                 return -1;
1228                         
1229                         ptr = p;
1230                         return n;
1231                 }
1232
1233                 internal unsafe void InternalSetChar(int idx, char val) 
1234                 {
1235                         if ((uint) idx >= (uint) Length)
1236                                 throw new ArgumentOutOfRangeException("idx");
1237
1238                         fixed (char * pStr = &start_char) 
1239                         {
1240                                 pStr [idx] = val;
1241                         }
1242                 }
1243
1244                 internal unsafe void InternalSetLength(int newLength) 
1245                 {
1246                         if (newLength > length)
1247                                 throw new ArgumentOutOfRangeException("newLength > length");
1248                         
1249                         length = newLength;
1250                         
1251                         // zero terminate, we can pass string objects directly via pinvoke
1252                         fixed (char * pStr = &start_char) {
1253                                 pStr [length] = '\0';
1254                         }
1255                 }
1256
1257                 [CLSCompliant(false), MethodImplAttribute(MethodImplOptions.InternalCall)]
1258                 unsafe public extern String(char *value);
1259
1260                 [CLSCompliant(false), MethodImplAttribute(MethodImplOptions.InternalCall)]
1261                 unsafe public extern String(char *value, int sindex, int length);
1262     
1263                 [CLSCompliant(false), MethodImplAttribute(MethodImplOptions.InternalCall)]
1264                 unsafe public extern String(sbyte *value);
1265
1266                 [CLSCompliant(false), MethodImplAttribute(MethodImplOptions.InternalCall)]
1267                 unsafe public extern String(sbyte *value, int sindex, int length);
1268
1269                 [CLSCompliant(false), MethodImplAttribute(MethodImplOptions.InternalCall)]
1270                 unsafe public extern String(sbyte *value, int sindex, int length, Encoding enc);
1271
1272                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1273                 public extern String(char [] val, int sindex, int length);
1274                 
1275                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1276                 public extern String(char [] val);
1277
1278                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1279                 public extern String(char c, int count);
1280         
1281                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1282                 public extern override int GetHashCode();
1283
1284                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1285                 private extern static string InternalJoin(string separator, string[] value, int sindex, int count);
1286                 
1287                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1288                 private extern String InternalInsert(int sindex, String value);
1289
1290                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1291                 private extern String InternalReplace(char oldChar, char newChar);
1292
1293                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1294                 private extern String InternalReplace(String oldValue, string newValue, CompareInfo comp);
1295                 
1296                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1297                 private extern String InternalRemove(int sindex, int count);
1298                 
1299                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1300                 private extern void InternalCopyTo(int sindex, char[] dest, int dindex, int count);
1301
1302                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1303                 private extern String[] InternalSplit(char[] separator, int count);
1304
1305                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1306                 private extern String InternalTrim(char[] chars, int typ);
1307
1308                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1309                 private extern int InternalIndexOfAny(char [] arr, int sindex, int count);
1310
1311                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1312                 private extern int InternalLastIndexOfAny(char [] anyOf, int sindex, int count);
1313
1314                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1315                 private extern String InternalPad(int width, char chr, bool right);
1316
1317                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1318                 private extern String InternalToLower(CultureInfo culture);
1319
1320                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1321                 private extern String InternalToUpper(CultureInfo culture);
1322
1323                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1324                 internal extern static String InternalAllocateStr(int length);
1325
1326                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1327                 internal extern static void InternalStrcpy(String dest, int destPos, String src);
1328
1329                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1330                 internal extern static void InternalStrcpy(String dest, int destPos, String src, int startPos, int count);
1331
1332                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1333                 private extern static string InternalIntern(string str);
1334
1335                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1336                 private extern static string InternalIsInterned(string str);
1337         }
1338 }