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