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