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