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