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