2002-06-26 Martin Baulig <martin@gnome.org>
[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                 public String ToLower(CultureInfo culture) {
548                         throw new NotImplementedException();
549                 }
550
551                 public String ToUpper() {
552                         return InternalToUpper();
553                 }
554
555                 public String ToUpper(CultureInfo culture) {
556                         throw new NotImplementedException();
557                 }
558
559                 public override String ToString() {
560                         return this;
561                 }
562
563                 public String ToString(IFormatProvider provider) {
564                         return this;
565                 }
566
567                 public String Trim() {
568                         return Trim(null);
569                 }
570
571                 public static String Format(String format, Object arg0) {
572                         return Format(null, format, new Object[] {arg0});
573                 }
574
575                 public static String Format(String format, Object arg0, Object arg1) {
576                         return Format(null, format, new Object[] {arg0, arg1});
577                 }
578
579                 public static String Format(String format, Object arg0, Object arg1, Object arg2) {
580                         return Format(null, format, new Object[] {arg0, arg1, arg2});
581                 }
582
583                 public static string Format (string format, params object[] args) {
584                         return Format (null, format, args);
585                 }
586         
587                 public static string Format (IFormatProvider provider, string format, params object[] args) {
588                         if (format == null || args == null)
589                                 throw new ArgumentNullException ();
590                 
591                         StringBuilder result = new StringBuilder ();
592
593                         int ptr = 0;
594                         int start = ptr;
595                         while (ptr < format.length) {
596                                 char c = format[ptr ++];
597
598                                 if (c == '{') {
599                                         result.Append (format, start, ptr - start - 1);
600
601                                         // check for escaped open bracket
602
603                                         if (format[ptr] == '{') {
604                                                 start = ptr ++;
605                                                 continue;
606                                         }
607
608                                         // parse specifier
609                                 
610                                         int n, width;
611                                         bool left_align;
612                                         string arg_format;
613
614                                         ParseFormatSpecifier (format, ref ptr, out n, out width, out left_align, out arg_format);
615                                         if (n >= args.Length)
616                                                 throw new FormatException ("Index (zero based) must be greater than or equal to zero and less than the size of the argument list.");
617
618                                         // format argument
619
620                                         object arg = args[n];
621
622                                         string str;
623                                         if (arg == null)
624                                                 str = "";
625                                         else if (arg is IFormattable)
626                                                 str = ((IFormattable)arg).ToString (arg_format, provider);
627                                         else
628                                                 str = arg.ToString ();
629
630                                         // pad formatted string and append to result
631
632                                         if (width > str.length) {
633                                                 string pad = new String (' ', width - str.length);
634
635                                                 if (left_align) {
636                                                         result.Append (str);
637                                                         result.Append (pad);
638                                                 }
639                                                 else {
640                                                         result.Append (pad);
641                                                         result.Append (str);
642                                                 }
643                                         }
644                                         else
645                                                 result.Append (str);
646
647                                         start = ptr;
648                                 }
649                                 else if (c == '}' && format[ptr] == '}') {
650                                         result.Append (format, start, ptr - start - 1);
651                                         start = ptr ++;
652                                 }
653                         }
654
655                         if (start < format.length)
656                                 result.Append (format.Substring (start));
657
658                         return result.ToString ();
659                 }
660
661                 public static String Copy (String str) {
662                         if (str == null)
663                                 throw new ArgumentNullException ();
664
665                         int length = str.length;
666
667                         String tmp = InternalAllocateStr(length);
668                         InternalStrcpy(tmp, 0, str);
669                         return tmp;
670                 }
671
672                 public static String Concat(Object obj) {
673                         if (null == obj)
674                                 return String.Empty;
675
676                         return obj.ToString();
677                 }
678
679                 public static String Concat(Object obj1, Object obj2) {
680                         if (null == obj1)
681                                 obj1 = String.Empty;
682     
683                         if (null == obj2)
684                                 obj2 = String.Empty;
685
686                         return Concat(obj1.ToString(), obj2.ToString());
687                 }
688
689                 public static String Concat(Object obj1, Object obj2, Object obj3) {
690                         if (null == obj1)
691                                 obj1 = String.Empty;
692     
693                         if (null == obj2)
694                                 obj2 = String.Empty;
695     
696                         if (null == obj3)
697                                 obj3 = String.Empty;
698     
699                         return Concat(obj1.ToString(), obj2.ToString(), obj3.ToString());
700                 }
701
702                 public static String Concat(String s1, String s2) {
703                         if (null == s1) {
704                                 if (null == s2) { return String.Empty; }
705                                 return s2;
706                         }
707
708                         if (null == s2) { return s1; }
709
710                         String tmp = InternalAllocateStr(s1.length + s2.length);
711             
712                         InternalStrcpy(tmp, 0, s1);
713                         InternalStrcpy(tmp, s1.length, s2);
714             
715                         return tmp;
716                 }
717
718                 public static String Concat(String s1, String s2, String s3) {
719                         if (null == s1 && null == s2 && null == s3) {
720                                 return String.Empty;
721                         }
722
723                         if (null == s1) { s1 = String.Empty; }
724                         if (null == s2) { s2 = String.Empty; }
725                         if (null == s3) { s3 = String.Empty; }
726
727                         String tmp = InternalAllocateStr(s1.length + s2.length + s3.length);
728
729                         InternalStrcpy(tmp, 0, s1);
730                         InternalStrcpy(tmp, s1.length, s2);
731                         InternalStrcpy(tmp, s1.length + s2.length, s3);
732
733                         return tmp;
734                 }
735
736                 public static String Concat(String s1, String s2, String s3, String s4) {
737                         if (null == s1 && null == s2 && null == s3 && null == s4) {
738                                 return String.Empty;
739                         }
740
741                         if (null == s1) { s1 = String.Empty; }
742                         if (null == s2) { s2 = String.Empty; }
743                         if (null == s3) { s3 = String.Empty; }
744                         if (null == s4) { s4 = String.Empty; }
745
746                         String tmp = InternalAllocateStr(s1.length + s2.length + s3.length + s4.length);
747
748                         InternalStrcpy(tmp, 0, s1);
749                         InternalStrcpy(tmp, s1.length, s2);
750                         InternalStrcpy(tmp, s1.length + s2.length, s3);
751                         InternalStrcpy(tmp, s1.length + s2.length + s3.length, s4);
752
753                         return tmp;
754                 }
755
756                 public static String Concat(params Object[] args) {
757                         string [] strings;
758                         int len, i, currentpos;
759
760                         if (null == args)
761                                 throw new ArgumentNullException ();
762
763                         strings = new string [args.Length];
764                         len = 0;
765                         i = 0;
766                         foreach (object arg in args) {
767                                 /* use Empty for each null argument */
768                                 if (arg == null)
769                                         strings[i] = String.Empty;
770                                 else
771                                         strings[i] = arg.ToString ();
772                                 len += strings[i].length;
773                                 i++;
774                         }
775
776                         if (len == 0)
777                                 return String.Empty;
778
779                         currentpos = 0;
780
781                         String tmp = InternalAllocateStr(len);
782                         for (i = 0; i < strings.Length; i++) {
783                                 InternalStrcpy(tmp, currentpos, strings[i]);
784                                 currentpos += strings[i].length;
785                         }
786
787                         return tmp;
788                 }
789
790                 public static String Concat(params String[] values) {
791                         int len, i, currentpos;
792
793                         if (values == null)
794                                 throw new ArgumentNullException ();
795
796                         len = 0;
797                         foreach (string value in values)
798                                 len += value != null ? value.length : 0;
799
800                         if (len == 0)
801                                 return String.Empty;
802
803                         currentpos = 0;
804
805                         String tmp = InternalAllocateStr(len);
806                         for (i = 0; i < values.Length; i++) {
807                                 if (values[i] == null)
808                                         continue;
809
810                                 InternalStrcpy(tmp, currentpos, values[i]);
811                                 currentpos += values[i].length;
812                         }       
813         
814                         return tmp;
815                 }
816
817                 public String Insert(int sindex, String value) {
818                         if (null == value)
819                                 throw new ArgumentNullException();
820
821                         if (sindex < 0 || sindex > this.length)
822                                 throw new ArgumentOutOfRangeException();
823         
824                         return InternalInsert(sindex, value);
825                 }
826
827
828                 public static string Intern (string str) {
829                         if (null == str)
830                                 throw new ArgumentNullException ();
831
832                         return InternalIntern(str);
833                 }
834
835                 public static string IsInterned (string str) {
836                         if (null == str)
837                                 throw new ArgumentNullException();
838
839                         return InternalIsInterned(str);
840                 }
841         
842                 public static string Join (string separator, string [] value) {
843                         if (value == null)
844                                 throw new ArgumentNullException ();
845
846                         return Join(separator, value, 0, value.Length);
847                 }
848
849                 public static string Join(string separator, string[] value, int sindex, int count) {
850                         if (value == null)
851                                 throw new ArgumentNullException ();
852
853                         if (sindex + count > value.Length)
854                                 throw new ArgumentOutOfRangeException ();
855
856                         if (sindex == value.Length)
857                                 return String.Empty;
858
859                         return InternalJoin(separator, value, sindex, count);
860                 }
861
862                 bool IConvertible.ToBoolean (IFormatProvider provider) {
863                         return Convert.ToBoolean (this);
864                 }
865                 
866                 byte IConvertible.ToByte (IFormatProvider provider) {
867                         return Convert.ToByte (this);
868                 }
869                 
870                 char IConvertible.ToChar (IFormatProvider provider) {
871                         return Convert.ToChar (this);
872                 }
873
874                 DateTime IConvertible.ToDateTime (IFormatProvider provider) {
875                         return Convert.ToDateTime (this);
876                 }
877
878                 decimal IConvertible.ToDecimal (IFormatProvider provider) {
879                         return Convert.ToDecimal (this);
880                 }
881
882                 double IConvertible.ToDouble (IFormatProvider provider) {
883                         return Convert.ToDouble (this);
884                 }
885
886                 short IConvertible.ToInt16 (IFormatProvider provider) {
887                         return Convert.ToInt16 (this);
888                 }
889
890                 int IConvertible.ToInt32 (IFormatProvider provider) {
891                         return Convert.ToInt32 (this);
892                 }
893
894                 long IConvertible.ToInt64 (IFormatProvider provider) {
895                         return Convert.ToInt64 (this);
896                 }
897         
898                 [CLSCompliant(false)]
899                 sbyte IConvertible.ToSByte (IFormatProvider provider) {
900                         return Convert.ToSByte (this);
901                 }
902
903                 float IConvertible.ToSingle (IFormatProvider provider) {
904                         return Convert.ToSingle (this);
905                 }
906                 string IConvertible.ToString (IFormatProvider format) {
907                         return this;
908                 }
909
910                 object IConvertible.ToType (Type conversionType, IFormatProvider provider) {
911                         return Convert.ToType (this, conversionType,  provider);
912                 }
913
914                 [CLSCompliant(false)]
915                 ushort IConvertible.ToUInt16 (IFormatProvider provider) {
916                         return Convert.ToUInt16 (this);
917                 }
918
919                 [CLSCompliant(false)]
920                 uint IConvertible.ToUInt32 (IFormatProvider provider) {
921                         return Convert.ToUInt32 (this);
922                 }
923
924                 [CLSCompliant(false)]
925                 ulong IConvertible.ToUInt64 (IFormatProvider provider) {
926                         return Convert.ToUInt64 (this);
927                 }
928
929                 TypeCode IConvertible.GetTypeCode () {
930                         return TypeCode.String;
931                 }
932
933                 public int Length {
934                         get {
935                                 return length;
936                         }
937                 }
938
939                 public CharEnumerator GetEnumerator () {
940                         return new CharEnumerator (this);
941                 }
942                 
943                 IEnumerator IEnumerable.GetEnumerator () {
944                         return new CharEnumerator (this);
945                 }
946
947                 private static void ParseFormatSpecifier (string str, ref int ptr, out int n, out int width, out bool left_align, out string format) {
948                         // parses format specifier of form:
949                         //   N,[[-]M][:F]}
950                         //
951                         // where:
952
953                         try {
954                                 // N = argument number (non-negative integer)
955                         
956                                 n = ParseDecimal (str, ref ptr);
957                                 if (n < 0)
958                                         throw new FormatException ("Input string was not in correct format.");
959                                 
960                                 // M = width (non-negative integer)
961
962                                 if (str[ptr] == ',') {
963                                         left_align = (str[++ ptr] == '-');
964                                         if (left_align)
965                                                 ++ ptr;
966
967                                         width = ParseDecimal (str, ref ptr);
968                                         if (width < 0)
969                                                 throw new FormatException ("Input string was not in correct format.");
970                                 }
971                                 else {
972                                         width = 0;
973                                         left_align = false;
974                                 }
975
976                                 // F = argument format (string)
977
978                                 if (str[ptr] == ':') {
979                                         int start = ++ ptr;
980                                         while (str[ptr] != '}')
981                                                 ++ ptr;
982
983                                         format = str.Substring (start, ptr - start);
984                                 }
985                                 else
986                                         format = null;
987
988                                 if (str[ptr ++] != '}')
989                                         throw new FormatException ("Input string was not in correct format.");
990                         }
991                         catch (IndexOutOfRangeException) {
992                                 throw new FormatException ("Input string was not in correct format.");
993                         }
994                 }
995
996                 private static int ParseDecimal (string str, ref int ptr) {
997                         int p = ptr;
998                         int n = 0;
999                         while (true) {
1000                                 char c = str[p];
1001                                 if (c < '0' || '9' < c)
1002                                         break;
1003
1004                                 n = n * 10 + c - '0';
1005                                 ++ p;
1006                         }
1007
1008                         if (p == ptr)
1009                                 return -1;
1010                         
1011                         ptr = p;
1012                         return n;
1013                 }
1014                 
1015                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1016                 private extern static string InternalJoin(string separator, string[] value, int sindex, int count);
1017                 
1018                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1019                 private extern String InternalInsert(int sindex, String value);
1020
1021                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1022                 private extern String InternalReplace(char oldChar, char newChar);
1023
1024                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1025                 private extern String InternalReplace(String oldValue, String newValue);
1026     
1027                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1028                 private extern String InternalRemove(int sindex, int count);
1029                 
1030                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1031                 private extern void InternalCopyTo(int sindex, char[] dest, int dindex, int count);
1032
1033                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1034                 private extern String[] InternalSplit(char[] separator, int count);
1035
1036                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1037                 private extern String InternalTrim(char[] chars, int typ);
1038
1039                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1040                 private extern int InternalIndexOf(char value, int sindex, int count);
1041
1042                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1043                 private extern int InternalIndexOf(string value, int sindex, int count);
1044
1045                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1046                 private extern int InternalIndexOfAny(char [] arr, int sindex, int count);
1047
1048                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1049                 private extern int InternalLastIndexOf(char value, int sindex, int count);
1050
1051                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1052                 private extern int InternalLastIndexOf(String value, int sindex, int count);
1053
1054                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1055                 private extern int InternalLastIndexOfAny(char [] anyOf, int sindex, int count);
1056
1057                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1058                 private extern String InternalPad(int width, char chr, bool right);
1059
1060                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1061                 private extern String InternalToLower();
1062
1063                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1064                 private extern String InternalToUpper();
1065
1066                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1067                 private extern static String InternalAllocateStr(int length);
1068
1069                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1070                 private extern static void InternalStrcpy(String dest, int destPos, String src);
1071
1072                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1073                 private extern static void InternalStrcpy(String dest, int destPos, String src, int startPos, int count);
1074
1075                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1076                 private extern static string InternalIntern(string str);
1077
1078                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1079                 private extern static string InternalIsInterned(string str);
1080
1081                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1082                 private extern static int InternalCompare(String s1, int i1, String s2, int i2, int length, bool inCase);
1083
1084                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1085                 private extern static bool InternalEquals(String s1, String s2);
1086         }
1087 }