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