2002-01-05 Nick Drochak <ndrochak@gol.com>
[mono.git] / mcs / class / corlib / System / String.cs
1 // -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 //
3 // System.String.cs
4 //
5 // Author:
6 //   Jeffrey Stedfast (fejj@ximian.com)
7 //
8 // (C) 2001 Ximian, Inc.  http://www.ximian.com
9 //
10
11 // FIXME: from what I gather from msdn, when a function is to return an empty string
12 //        we should be returning this.Empty - some methods do this and others don't.
13
14 // FIXME: I didn't realise until later that `string' has a .Length method and so
15 //        I am missing some proper bounds-checking in some methods. Find these
16 //        instances and throw the ArgumentOutOfBoundsException at the programmer.
17 //        I like pelting programmers with ArgumentOutOfBoundsException's :-)
18
19 // FIXME: The ToLower(), ToUpper(), and Compare(..., bool ignoreCase) methods
20 //        need to be made unicode aware.
21
22 // FIXME: when you have a char carr[], does carr.Length include the terminating null char?
23
24 using System;
25 using System.Text;
26 using System.Collections;
27 using System.Globalization;
28 using System.Runtime.CompilerServices;
29
30 namespace System {
31
32         public sealed class String : IComparable, ICloneable, IConvertible, IEnumerable {
33                 public static readonly string Empty = "";
34                 private char[] c_str;
35                 private int length;
36
37                 // Constructors
38
39                 internal String (int storage)
40                 {
41                         length = storage;
42                         c_str = new char [storage];
43                 }
44                 
45                 [CLSCompliant(false)]
46                 unsafe public String (char *value)
47                 {
48                         int i;
49
50                         // FIXME: can I do value.Length here?
51                         if (value == null) {
52                                 this.length = 0;
53                         } else {
54                                 for (i = 0; *(value + i) != '\0'; i++);
55                                 this.length = i;
56                         }
57
58                         this.c_str = new char [this.length + 1];
59                         for (i = 0; i < this.length; i++)
60                                 this.c_str[i] = *(value + i);
61                 }
62
63                 public String (char[] value)
64                 {
65                         int i;
66
67                         // FIXME: value.Length includes the terminating null char?
68                         this.length = value != null ? strlen (value): 0;
69                         this.c_str = new char [this.length + 1];
70                         for (i = 0; i < this.length; i++)
71                                 this.c_str[i] = value[i];
72                 }
73
74                 [CLSCompliant(false)]
75                 unsafe public String (sbyte *value)
76                 {
77                         // FIXME: consider unicode?
78                         int i;
79
80                         // FIXME: can I do value.Length here? */
81                         if (value == null) {
82                                 this.length = 0;
83                         } else {
84                                 for (i = 0; *(value + i) != '\0'; i++);
85                                 this.length = i;
86                         }
87
88                         this.c_str = new char [this.length + 1];
89                         for (i = 0; i < this.length; i++)
90                                 this.c_str[i] = (char) *(value + i);
91                 }
92
93                 public String (char c, int count)
94                 {
95                         int i;
96
97                         this.length = count;
98                         this.c_str = new char [count + 1];
99                         for (i = 0; i < count; i++)
100                                 this.c_str[i] = c;
101                 }
102
103                 [CLSCompliant(false)]
104                 unsafe public String (char *value, int startIndex, int length)
105                 {
106                         int i;
107
108                         if (value == null && startIndex != 0 && length != 0)
109                                 throw new ArgumentNullException ();
110
111                         if (startIndex < 0 || length < 0)
112                                 throw new ArgumentOutOfRangeException ();
113
114                         this.length = length;
115                         this.c_str = new char [length + 1];
116                         for (i = 0; i < length; i++)
117                                 this.c_str[i] = *(value + startIndex + i);
118                 }
119
120                 public String (char[] value, int startIndex, int length)
121                 {
122                         int i;
123
124                         if (value == null && startIndex != 0 && length != 0)
125                                 throw new ArgumentNullException ();
126
127                         if (startIndex < 0 || length < 0)
128                                 throw new ArgumentOutOfRangeException ();
129
130                         this.length = length;
131                         this.c_str = new char [length + 1];
132                         for (i = 0; i < length; i++)
133                                 this.c_str[i] = value[startIndex + i];
134                 }
135
136                 [CLSCompliant(false)]
137                 unsafe public String (sbyte *value, int startIndex, int length)
138                 {
139                         // FIXME: consider unicode?
140                         int i;
141
142                         if (value == null && startIndex != 0 && length != 0)
143                                 throw new ArgumentNullException ();
144
145                         if (startIndex < 0 || length < 0)
146                                 throw new ArgumentOutOfRangeException ();
147
148                         this.length = length;
149                         this.c_str = new char [length + 1];
150                         for (i = 0; i < length; i++)
151                                 this.c_str[i] = (char) *(value + startIndex + i);
152                 }
153
154                 [CLSCompliant(false)][MonoTODO]
155                 unsafe public String (sbyte *value, int startIndex, int length, Encoding enc)
156                 {
157                         // FIXME: implement me
158                 }
159
160                 ~String ()
161                 {
162                         // FIXME: is there anything we need to do here?
163                         /*base.Finalize ();*/
164                 }
165
166                 // Properties
167                 public int Length {
168                         get {
169                                 return this.length;
170                         }
171                 }
172
173                 // FIXME: is this correct syntax??
174                 public char this [int index] {
175                         get {
176                                 if (index >= this.length)
177                                         throw new ArgumentOutOfRangeException ();
178
179                                 return this.c_str[index];
180                         }
181                 }
182
183                 // Private helper methods
184                 private static int strlen (char[] str)
185                 {
186                         // FIXME: if str.Length includes terminating null char, then return (str.Length - 1)
187                         return str.Length;
188                 }
189
190                 [MonoTODO]
191                 private static char tolowerordinal (char c)
192                 {
193                         // FIXME: implement me
194                         return c;
195                 }
196
197                 private static bool is_lwsp (char c)
198                 {
199                         /* this comes from the msdn docs for String.Trim() */
200                         if ((c >= '\x9' && c <= '\xD') || c == '\x20' || c == '\xA0' ||
201                             (c >= '\x2000' && c <= '\x200B') || c == '\x3000' || c == '\xFEFF')
202                                 return true;
203                         else
204                                 return false;
205                 }
206
207                 private static int BoyerMoore (char[] haystack, string needle, int startIndex, int count)
208                 {
209                         /* (hopefully) Unicode-safe Boyer-Moore implementation */
210                         int[] skiptable = new int[65536];  /* our unicode-safe skip-table */
211                         int h, n, he, ne, hc, nc, i;
212
213                         if (haystack == null || needle == null)
214                                 throw new ArgumentNullException ();
215
216                         /* if the search buffer is shorter than the pattern buffer, we can't match */
217                         if (count < needle.Length)
218                                 return -1;
219
220                         /* return an instant match if the pattern is 0-length */
221                         if (needle.Length == 0)
222                                 return startIndex;
223
224                         /* set a pointer at the end of each string */
225                         ne = needle.Length - 1;      /* position of char before '\0' */
226                         he = startIndex + count;     /* position of last valid char */
227
228                         /* init the skip table with the pattern length */
229                         for (i = 0; i < 65536; i++)
230                                 skiptable[i] = needle.Length;
231
232                         /* set the skip value for the chars that *do* appear in the
233                          * pattern buffer (needle) to the distance from the index to
234                          * the end of the pattern buffer. */
235                         for (nc = 0; nc < ne; nc++)
236                                 skiptable[(int) needle[nc]] = ne - nc;
237
238                         h = startIndex;
239                         while (count >= needle.Length) {
240                                 hc = h + needle.Length - 1;  /* set the haystack compare pointer */
241                                 nc = ne;                     /* set the needle compare pointer */
242
243                                 /* work our way backwards until they don't match */
244                                 for (i = 0; nc > 0; nc--, hc--, i++)
245                                         if (needle[nc] != haystack[hc])
246                                                 break;
247
248                                 if (needle[nc] != haystack[hc]) {
249                                         n = skiptable[(int) haystack[hc]] - i;
250                                         h += n;
251                                         count -= n;
252                                 } else
253                                         return h;
254                         }
255
256                         return -1;
257                 }
258
259                 // Methods
260                 [MonoTODO]
261                 public object Clone ()
262                 {
263                         // FIXME: implement me
264                         return null;
265                 }
266
267                 public static int Compare (string strA, string strB)
268                 {
269                         int min, i;
270
271                         if (strA == null) {
272                                 if (strB == null)
273                                         return 0;
274                                 else
275                                         return -1;
276                         } else if (strB == null)
277                                 return 1;
278
279                         min = strA.length < strB.length ? strA.length : strB.length;
280
281                         for (i = 0; i < min; i++) {
282                                 if (strA.c_str[i] != strB.c_str[i]) {
283                                         return (int)strA.c_str[i] - (int)strB.c_str[i];
284                                 }
285                         }
286                         if (strA.length == strB.length) {
287                                 return 0;
288                         }
289                         if (strA.length > min) {
290                                 return 1;
291                         } else {
292                                 return -1;
293                         }
294                 }
295
296                 public static int Compare (string strA, string strB, bool ignoreCase)
297                 {
298                         int min, i;
299
300                         if (!ignoreCase)
301                                 return Compare (strA, strB);
302
303                         /*
304                          * And here I thought Eazel developers were on crack...
305                          * if a string is null it should pelt the programmer with
306                          * ArgumentNullExceptions, damnit!
307                          */
308                         if (strA == null) {
309                                 if (strB == null)
310                                         return 0;
311                                 else
312                                         return -1;
313                         } else if (strB == null)
314                                 return 1;
315
316                         min = strA.length < strB.length ? strA.length : strB.length;
317
318                         for (i = 0; i < min; i++) {
319                                 if (Char.ToLower (strA.c_str[i]) != Char.ToLower (strB.c_str[i]))
320                                         break;
321                         }
322
323                         return ((int) (strA.c_str[i] - strB.c_str[i]));
324                 }
325
326                 [MonoTODO]
327                 public static int Compare (string strA, string strB, bool ignoreCase, CultureInfo culture)
328                 {
329                         // FIXME: implement me
330                         return 0;
331                 }
332
333                 public static int Compare (string strA, int indexA, string strB, int indexB, int length)
334                 {
335                         int i;
336
337                         if (length < 0 || indexA < 0 || indexB < 0)
338                                 throw new ArgumentOutOfRangeException ();
339
340                         if (indexA > strA.Length || indexB > strB.Length)
341                                 throw new ArgumentOutOfRangeException ();
342
343                         /* And again with the ("" > null) logic... lord have mercy! */
344                         if (strA == null) {
345                                 if (strB == null)
346                                         return 0;
347                                 else
348                                         return -1;
349                         } else if (strB == null)
350                                 return 1;
351
352                         for (i = 0; i < length - 1; i++) {
353                                 if (strA[indexA + i] != strB[indexB + i])
354                                         break;
355                         }
356
357                         return ((int) (strA[indexA + i] - strB[indexB + i]));
358                 }
359
360                 public static int Compare (string strA, int indexA, string strB, int indexB,
361                                            int length, bool ignoreCase)
362                 {
363                         int i;
364
365                         if (!ignoreCase)
366                                 return Compare (strA, indexA, strB, indexB, length);
367
368                         if (length < 0 || indexA < 0 || indexB < 0)
369                                 throw new ArgumentOutOfRangeException ();
370
371                         if (indexA > strA.Length || indexB > strB.Length)
372                                 throw new ArgumentOutOfRangeException ();
373
374                         /* When will the hurting stop!?!? */
375                         if (strA == null) {
376                                 if (strB == null)
377                                         return 0;
378                                 else
379                                         return -1;
380                         } else if (strB == null)
381                                 return 1;
382
383                         for (i = 0; i < length - 1; i++) {
384                                 if (Char.ToLower (strA[indexA + i]) != Char.ToLower (strB[indexB + i]))
385                                         break;
386                         }
387
388                         return ((int) (strA[indexA + i] - strB[indexB + i]));
389                 }
390
391                 [MonoTODO]
392                 public static int Compare (string strA, int indexA, string strB, int indexB,
393                                            int length, bool ignoreCase, CultureInfo culture)
394                 {
395                         if (culture == null)
396                                 throw new ArgumentNullException ();
397
398                         if (length < 0 || indexA < 0 || indexB < 0)
399                                 throw new ArgumentOutOfRangeException ();
400
401                         if (indexA > strA.Length || indexB > strB.Length)
402                                 throw new ArgumentOutOfRangeException ();
403
404                         /* I can't take it anymore! */
405                         if (strA == null) {
406                                 if (strB == null)
407                                         return 0;
408                                 else
409                                         return -1;
410                         } else if (strB == null)
411                                 return 1;
412
413                         // FIXME: implement me
414                         return 0;
415                 }
416
417                 public static int CompareOrdinal (string strA, string strB)
418                 {
419                         int i;
420
421                         /* Please God, make it stop! */
422                         if (strA == null) {
423                                 if (strB == null)
424                                         return 0;
425                                 else
426                                         return -1;
427                         } else if (strB == null)
428                                 return 1;
429
430                         for (i = 0; i < strA.Length && i < strB.Length; i++) {
431                                 char cA, cB;
432
433                                 cA = tolowerordinal (strA[i]);
434                                 cB = tolowerordinal (strB[i]);
435
436                                 if (cA != cB)
437                                         break;
438                         }
439
440                         return ((int) (strA[i] - strB[i]));
441                 }
442
443                 public static int CompareOrdinal (string strA, int indexA, string strB, int indexB,
444                                                   int length)
445                 {
446                         int i;
447
448                         if (length < 0 || indexA < 0 || indexB < 0)
449                                 throw new ArgumentOutOfRangeException ();
450
451                         if (strA == null) {
452                                 if (strB == null)
453                                         return 0;
454                                 else
455                                         return -1;
456                         } else if (strB == null)
457                                 return 1;
458
459                         for (i = 0; i < length; i++) {
460                                 char cA, cB;
461
462                                 cA = tolowerordinal (strA[indexA + i]);
463                                 cB = tolowerordinal (strB[indexB + i]);
464
465                                 if (cA != cB)
466                                         break;
467                         }
468
469                         return ((int) (strA[indexA + i] - strB[indexB + i]));
470                 }
471
472                 public int CompareTo (object obj)
473                 {
474                         return Compare (this, obj == null ? null : obj.ToString ());
475                 }
476
477                 public int CompareTo (string str)
478                 {
479                         return Compare (this, str);
480                 }
481
482                 public static string Concat (object arg)
483                 {
484                         return arg != null ? arg.ToString () : String.Empty;
485                 }
486
487                 public static string Concat (params object[] args)
488                 {
489                         string[] strings;
490                         char[] str;
491                         int len, i;
492
493                         if (args == null)
494                                 throw new ArgumentNullException ();
495
496                         strings = new string [args.Length];
497                         len = 0;
498                         i = 0;
499                         foreach (object arg in args) {
500                                 /* use Empty for each null argument */
501                                 if (arg == null)
502                                         strings[i] = String.Empty;
503                                 else
504                                         strings[i] = arg.ToString ();
505                                 len += strings[i].length;
506                                 i++;
507                         }
508
509                         if (len == 0)
510                                 return String.Empty;
511
512                         String res = new String (len);
513                         str = res.c_str;
514                         i = 0;
515                         for (int j = 0; j < strings.Length; j++)
516                                 for (int k = 0; k < strings[j].length; k++)
517                                         str[i++] = strings[j].c_str[k];
518
519                         return res;
520                 }
521
522                 public static string Concat (params string[] values)
523                 {
524                         int len, i;
525                         char[] str;
526
527                         if (values == null)
528                                 throw new ArgumentNullException ();
529
530                         len = 0;
531                         foreach (string value in values)
532                                 len += value != null ? value.Length : 0;
533
534                         if (len == 0)
535                                 return String.Empty;
536
537                         String res = new String (len);
538                         str = res.c_str;
539                         i = 0;
540                         foreach (string value in values) {
541                                 if (value == null)
542                                         continue;
543
544                                 for (int j = 0; j < value.length; j++)
545                                         str[i++] = value.c_str[j];
546                         }
547
548                         return res;
549                 }
550
551                 public static string Concat (object arg0, object arg1)
552                 {
553                         string str0 = arg0 != null ? arg0.ToString () : String.Empty;
554                         string str1 = arg1 != null ? arg1.ToString () : String.Empty;
555
556                         return Concat (str0, str1);
557                 }
558
559                 public static string Concat (string str0, string str1)
560                 {
561                         char[] concat;
562                         int i, j, len;
563
564                         if (str0 == null)
565                                 str0 = String.Empty;
566                         if (str1 == null)
567                                 str1 = String.Empty;
568
569                         len = str0.length + str1.length;
570                         if (len == 0)
571                                 return String.Empty;
572
573                         String res = new String (len);
574
575                         concat = res.c_str;
576                         for (i = 0; i < str0.length; i++)
577                                 concat[i] = str0.c_str[i];
578                         for (j = 0 ; j < str1.length; j++)
579                                 concat[i + j] = str1.c_str[j];
580
581                         return res;
582                 }
583
584                 public static string Concat (object arg0, object arg1, object arg2)
585                 {
586                         string str0 = arg0 != null ? arg0.ToString () : String.Empty;
587                         string str1 = arg1 != null ? arg1.ToString () : String.Empty;
588                         string str2 = arg2 != null ? arg2.ToString () : String.Empty;
589
590                         return Concat (str0, str1, str2);
591                 }
592
593                 public static string Concat (string str0, string str1, string str2)
594                 {
595                         char[] concat;
596                         int i, j, k, len;
597
598                         if (str0 == null)
599                                 str0 = String.Empty;
600                         if (str1 == null)
601                                 str1 = String.Empty;
602                         if (str2 == null)
603                                 str2 = String.Empty;
604
605                         len = str0.length + str1.length + str2.length;
606                         if (len == 0)
607                                 return String.Empty;
608
609                         String res = new String (len);
610
611                         concat = res.c_str;
612                         for (i = 0; i < str0.length; i++)
613                                 concat[i] = str0.c_str[i];
614                         for (j = 0; j < str1.length; j++)
615                                 concat[i + j] = str1.c_str[j];
616                         for (k = 0; k < str2.length; k++)
617                                 concat[i + j + k] = str2.c_str[k];
618
619                         return res;
620                 }
621
622                 public static string Concat (string str0, string str1, string str2, string str3)
623                 {
624                         char[] concat;
625                         int i, j, k, l, len;
626
627                         if (str0 == null)
628                                 str0 = String.Empty;
629                         if (str1 == null)
630                                 str1 = String.Empty;
631                         if (str2 == null)
632                                 str2 = String.Empty;
633                         if (str3 == null)
634                                 str3 = String.Empty;
635
636                         len = str0.length + str1.length + str2.length + str3.length;
637                         if (len == 0)
638                                 return String.Empty;
639                         String res = new String (len);
640
641                         concat = res.c_str;
642                         for (i = 0; i < str0.length; i++)
643                                 concat[i] = str0.c_str[i];
644                         for (j = 0; j < str1.length; j++)
645                                 concat[i + j] = str1.c_str[j];
646                         for (k = 0; k < str2.length; k++)
647                                 concat[i + j + k] = str2.c_str[k];
648                         for (l = 0; l < str3.length; l++)
649                                 concat[i + j + k + l] = str3.c_str[l];
650
651                         return res;
652                 }
653
654                 public static string Copy (string str)
655                 {
656                         // FIXME: how do I *copy* a string if I can only have 1 of each?
657                         if (str == null)
658                                 throw new ArgumentNullException ();
659
660                         return str;
661                 }
662
663                 public void CopyTo (int sourceIndex, char[] destination, int destinationIndex, int count)
664                 {
665                         // LAMESPEC: should I null-terminate?
666                         int i;
667
668                         if (destination == null)
669                                 throw new ArgumentNullException ();
670
671                         if (sourceIndex < 0 || destinationIndex < 0 || count < 0)
672                                 throw new ArgumentOutOfRangeException ();
673
674                         if (sourceIndex + count > this.length)
675                                 throw new ArgumentOutOfRangeException ();
676
677                         if (destinationIndex + count > destination.Length)
678                                 throw new ArgumentOutOfRangeException ();
679
680                         for (i = 0; i < count; i++)
681                                 destination[destinationIndex + i] = this.c_str[sourceIndex + i];
682                 }
683
684                 public bool EndsWith (string value)
685                 {
686                         bool endswith = true;
687                         int start, i;
688
689                         if (value == null)
690                                 throw new ArgumentNullException ();
691
692                         start = this.length - value.length;
693                         if (start < 0)
694                                 return false;
695
696                         for (i = start; i < this.length && endswith; i++)
697                                 endswith = this.c_str[i] == value.c_str[i - start];
698
699                         return endswith;
700                 }
701
702                 public override bool Equals (object obj)
703                 {
704                         if (!(obj is String))
705                                 return false;
706
707                         return this == (String) obj;
708                 }
709
710                 public bool Equals (string value)
711                 {
712                         return this == value;
713                 }
714
715                 public static bool Equals (string a, string b)
716                 {
717                         return a == b;
718                 }
719
720                 [MonoTODO]
721                 public static string Format (string format, object arg0)
722                 {
723                         // FIXME: implement me
724                         return null;
725                 }
726
727                 [MonoTODO]
728                 public static string Format (string format, params object[] args)
729                 {
730                         // FIXME: implement me
731                         return null;
732                 }
733                 
734                 [MonoTODO]
735                 public static string Format (IFormatProvider provider, string format, params object[] args)
736                 {
737                         // FIXME: implement me
738                         return null;
739                 }
740                 
741                 [MonoTODO]
742                 public static string Format (string format, object arg0, object arg1)
743                 {
744                         // FIXME: implement me
745                         return null;
746                 }
747
748                 [MonoTODO]
749                 public static string Format (string format, object arg0, object arg1, object arg2)
750                 {
751                         // FIXME: implement me
752                         return null;
753                 }
754
755                 //public CharEnumerator GetEnumerator ()
756                 [MonoTODO]
757                 public IEnumerator GetEnumerator ()
758                 {
759                         // FIXME: implement me
760                         return null;
761                 }
762
763                 public override int GetHashCode ()
764                 {
765                         int h = 0;
766                         int i;
767                         for (i = 0; i < length; ++i)
768                                 h = (h << 5) - h + c_str [i];
769                         return h;
770                 }
771
772                 public TypeCode GetTypeCode ()
773                 {
774                         return TypeCode.String;
775                 }
776
777                 public int IndexOf (char value)
778                 {
779                         return IndexOf (value, 0, this.length);
780                 }
781
782                 public int IndexOf (string value)
783                 {
784                         return IndexOf (value, 0, this.length);
785                 }
786
787                 public int IndexOf (char value, int startIndex)
788                 {
789                         return IndexOf (value, startIndex, this.length - startIndex);
790                 }
791
792                 public int IndexOf (string value, int startIndex)
793                 {
794                         return IndexOf (value, startIndex, this.length - startIndex);
795                 }
796
797                 public int IndexOf (char value, int startIndex, int count)
798                 {
799                         int i;
800
801                         if (startIndex < 0 || count < 0 || startIndex + count > this.length)
802                                 throw new ArgumentOutOfRangeException ();
803
804                         for (i = startIndex; i - startIndex < count; i++)
805                                 if (this.c_str[i] == value)
806                                         return i;
807
808                         return -1;
809                 }
810
811                 public int IndexOf (string value, int startIndex, int count)
812                 {
813                         if (value == null)
814                                 throw new ArgumentNullException ();
815
816                         if (startIndex < 0 || count < 0 || startIndex + count > this.length)
817                                 throw new ArgumentOutOfRangeException ();
818
819                         return BoyerMoore (this.c_str, value, startIndex, count);
820 #if XXX
821                         int i;
822                         for (i = startIndex; i - startIndex + value.Length <= count; ) {
823                                 if (this.c_str[i] == value[0]) {
824                                         bool equal = true;
825                                         int j, nexti = 0;
826
827                                         for (j = 1; equal && j < value.Length; j++) {
828                                                 equal = this.c_str[i + j] == value[j];
829                                                 if (this.c_str[i + j] == value[0] && nexti == 0)
830                                                         nexti = i + j;
831                                         }
832
833                                         if (equal)
834                                                 return i;
835
836                                         if (nexti != 0)
837                                                 i = nexti;
838                                         else
839                                                 i += j;
840                                 } else
841                                         i++;
842                         }
843
844                         return -1;
845 #endif
846                 }
847
848                 public int IndexOfAny (char[] values)
849                 {
850                         return IndexOfAny (values, 0, this.length);
851                 }
852
853                 public int IndexOfAny (char[] values, int startIndex)
854                 {
855                         return IndexOfAny (values, startIndex, this.length - startIndex);
856                 }
857
858                 public int IndexOfAny (char[] values, int startIndex, int count)
859                 {
860                         if (values == null)
861                                 throw new ArgumentNullException ();
862
863                         if (startIndex < 0 || count < 0 || startIndex + count > this.length)
864                                 throw new ArgumentOutOfRangeException ();
865
866                         for (int i = startIndex; i < startIndex + count; i++) {
867                                 for (int j = 0; j < strlen (values); j++) {
868                                         if (this.c_str[i] == values[j])
869                                                 return i;
870                                 }
871                         }
872
873                         return -1;
874                 }
875
876                 public string Insert (int startIndex, string value)
877                 {
878                         char[] str;
879                         int i, j;
880
881                         if (value == null)
882                                 throw new ArgumentNullException ();
883
884                         if (startIndex < 0 || startIndex > this.length)
885                                 throw new ArgumentOutOfRangeException ();
886
887                         String res = new String (value.length + this.length);
888
889                         str = res.c_str;
890                         for (i = 0; i < startIndex; i++)
891                                 str[i] = this.c_str[i];
892                         for (j = 0; j < value.length; j++)
893                                 str[i + j] = value.c_str[j];
894                         for ( ; i < this.length; i++)
895                                 str[i + j] = this.c_str[i];
896
897                         return res;
898                 }
899
900                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
901                 public extern static string Intern (string str);
902
903                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
904                 public extern static string IsInterned (string str);
905
906                 public static string Join (string separator, string[] value)
907                 {
908                         return Join (separator, value, 0, value.Length);
909                 }
910
911                 public static string Join (string separator, string[] value, int startIndex, int count)
912                 {
913                         // LAMESPEC: msdn doesn't specify what happens when separator is null
914                         int len, i, j, used;
915                         char[] str;
916
917                         if (separator == null || value == null)
918                                 throw new ArgumentNullException ();
919
920                         if (startIndex + count > value.Length)
921                                 throw new ArgumentOutOfRangeException ();
922
923                         len = 0;
924                         for (i = startIndex, used = 0; used < count; i++, used++) {
925                                 if (i != startIndex)
926                                         len += separator.length;
927
928                                 len += value[i].length;
929                         }
930
931                         // We have no elements to join?
932                         if (i == startIndex)
933                                 return String.Empty;
934
935                         String res = new String (len);
936
937                         str = res.c_str;
938                         for (i = 0; i < value[startIndex].length; i++)
939                                 str[i] = value[startIndex][i];
940
941                         used = 1;
942                         for (j = startIndex + 1; used < count; j++, used++) {
943                                 int k;
944
945                                 for (k = 0; k < separator.length; k++)
946                                         str[i++] = separator.c_str[k];
947                                 for (k = 0; k < value[j].length; k++)
948                                         str[i++] = value[j].c_str[k];
949                         }
950
951                         return res;
952                 }
953
954                 public int LastIndexOf (char value)
955                 {
956                         int i = this.length;
957                         if (i == 0)
958                                 return -1;
959                         --i;
960                         for (; i >= 0; i--) {
961                                 if (this.c_str[i] == value)
962                                         return i;
963                         }
964
965                         return -1;
966                 }
967
968                 public int LastIndexOf (string value)
969                 {
970                         return LastIndexOf (value, this.length, this.length);
971                 }
972
973                 public int LastIndexOf (char value, int startIndex)
974                 {
975                         if (startIndex < 0 || startIndex > this.length)
976                                 throw new ArgumentOutOfRangeException ();
977
978                         for (int i = startIndex; i >= 0; i--) {
979                                 if (this.c_str[i] == value)
980                                         return i;
981                         }
982
983                         return -1;
984                 }
985
986                 public int LastIndexOf (string value, int startIndex)
987                 {
988                         return LastIndexOf (value, startIndex, this.length);
989                 }
990
991                 public int LastIndexOf (char value, int startIndex, int count)
992                 {
993                         if (startIndex < 0 || count < 0)
994                                 throw new ArgumentOutOfRangeException ();
995
996                         if (startIndex > this.length || startIndex - count < 0)
997                                 throw new ArgumentOutOfRangeException ();
998
999                         for (int i = startIndex; i >= startIndex - count; i--) {
1000                                 if (this.c_str[i] == value)
1001                                         return i;
1002                         }
1003
1004                         return -1;
1005                 }
1006
1007                 public int LastIndexOf (string value, int startIndex, int count)
1008                 {
1009                         // LAMESPEC: currently I'm using startIndex as the 0-position in the comparison,
1010                         //           but maybe it's the end-position in MS's implementation?
1011                         //           msdn is unclear on this point. I think this is correct though.
1012                         int i, len;
1013
1014                         if (value == null)
1015                                 throw new ArgumentNullException ();
1016
1017                         if (startIndex < 0 || startIndex > this.length)
1018                                 throw new ArgumentOutOfRangeException ();
1019
1020                         if (count < 0 || startIndex - count < 0)
1021                                 throw new ArgumentOutOfRangeException ();
1022
1023                         if (value == String.Empty)
1024                                 return startIndex;
1025
1026                         if (startIndex + value.length > this.length) {
1027                                 /* just a little optimization */
1028                                 int start;
1029
1030                                 start = this.length - value.length;
1031                                 count -= startIndex - start;
1032                                 startIndex = start;
1033                         }
1034
1035                         // FIXME: use a reversed-unicode-safe-Boyer-Moore?
1036                         len = value.length - 1;
1037                         for (i = startIndex; i >= startIndex - count; i--) {
1038                                 if (this.c_str[i + len] == value.c_str[len]) {
1039                                         bool equal = true;
1040                                         int j;
1041
1042                                         for (j = len - 1; equal && j >= 0; j--)
1043                                                 equal = this.c_str[i + j] == value.c_str[j];
1044
1045                                         if (equal)
1046                                                 return i;
1047                                 }
1048                         }
1049
1050                         return -1;
1051                 }
1052
1053                 public int LastIndexOfAny (char[] values)
1054                 {
1055                         return LastIndexOfAny (values, this.length, this.length);
1056                 }
1057
1058                 public int LastIndexOfAny (char[] values, int startIndex)
1059                 {
1060                         return LastIndexOfAny (values, startIndex, startIndex);
1061                 }
1062
1063                 public int LastIndexOfAny (char[] values, int startIndex, int count)
1064                 {
1065                         int i;
1066
1067                         if (values == null)
1068                                 throw new ArgumentNullException ();
1069
1070                         if (startIndex < 0 || count < 0 || startIndex - count < 0)
1071                                 throw new ArgumentOutOfRangeException ();
1072
1073                         for (i = startIndex; i >= startIndex - count; i--) {
1074                                 for (int j = 0; j < strlen (values); j++) {
1075                                         if (this.c_str[i] == values[j])
1076                                                 return i;
1077                                 }
1078                         }
1079
1080                         return -1;
1081                 }
1082
1083                 public string PadLeft (int totalWidth)
1084                 {
1085                         return PadLeft (totalWidth, ' ');
1086                 }
1087
1088                 public string PadLeft (int totalWidth, char padChar)
1089                 {
1090                         char[] str;
1091                         int i, j;
1092
1093                         if (totalWidth < 0)
1094                                 throw new ArgumentException ();
1095
1096                         str = new char [totalWidth > this.length ? totalWidth : this.length];
1097                         for (i = 0; i < totalWidth - this.length; i++)
1098                                 str[i] = padChar;
1099
1100                         for (j = 0; j < this.length; i++, j++)
1101                                 str[i] = this.c_str[j];
1102
1103                         return new String (str);
1104                 }
1105
1106                 public string PadRight (int totalWidth)
1107                 {
1108                         return PadRight (totalWidth, ' ');
1109                 }
1110
1111                 public string PadRight (int totalWidth, char padChar)
1112                 {
1113                         char[] str;
1114                         int i;
1115
1116                         if (totalWidth < 0)
1117                                 throw new ArgumentException ();
1118
1119                         str = new char [totalWidth > this.length ? totalWidth : this.length];
1120                         for (i = 0; i < this.length; i++)
1121                                 str[i] = this.c_str[i];
1122
1123                         for ( ; i < str.Length; i++)
1124                                 str[i] = padChar;
1125
1126                         return new String (str);
1127                 }
1128
1129                 public string Remove (int startIndex, int count)
1130                 {
1131                         char[] str;
1132                         int i, j, len;
1133
1134                         if (startIndex < 0 || count < 0 || startIndex + count > this.length)
1135                                 throw new ArgumentOutOfRangeException ();
1136
1137                         len = this.length - count;
1138                         if (len == 0)
1139                                 return String.Empty;
1140                         
1141                         String res = new String (len);
1142                         str = res.c_str;
1143                         for (i = 0; i < startIndex; i++)
1144                                 str[i] = this.c_str[i];
1145                         for (j = i + count; j < this.length; j++)
1146                                 str[i++] = this.c_str[j];
1147
1148                         return res;
1149                 }
1150
1151                 public string Replace (char oldChar, char newChar)
1152                 {
1153                         char[] str;
1154                         int i;
1155
1156                         String res = new String (length);
1157                         str = res.c_str;
1158                         for (i = 0; i < this.length; i++) {
1159                                 if (this.c_str[i] == oldChar)
1160                                         str[i] = newChar;
1161                                 else
1162                                         str[i] = this.c_str[i];
1163                         }
1164
1165                         return res;
1166                 }
1167
1168                 public string Replace (string oldValue, string newValue)
1169                 {
1170                         // LAMESPEC: msdn doesn't specify what to do if either args is null
1171                         int index, len, i, j;
1172                         char[] str;
1173
1174                         if (oldValue == null || newValue == null)
1175                                 throw new ArgumentNullException ();
1176
1177                         // Use IndexOf in case I later rewrite it to use Boyer-Moore
1178                         index = IndexOf (oldValue, 0);
1179                         if (index == -1) {
1180                                 // This is the easy one ;-)
1181                                 return Substring (0, this.length);
1182                         }
1183
1184                         len = this.length - oldValue.length + newValue.length;
1185                         if (len == 0)
1186                                 return String.Empty;
1187
1188                         String res = new String (len);
1189                         str = res.c_str;
1190                         for (i = 0; i < index; i++)
1191                                 str[i] = this.c_str[i];
1192                         for (j = 0; j < newValue.length; j++)
1193                                 str[i++] = newValue[j];
1194                         for (j = index + oldValue.length; j < this.length; j++)
1195                                 str[i++] = this.c_str[j];
1196
1197                         return res;
1198                 }
1199
1200                 private int splitme (char[] separators, int startIndex)
1201                 {
1202                         /* this is basically a customized IndexOfAny() for the Split() methods */
1203                         for (int i = startIndex; i < this.length; i++) {
1204                                 if (separators != null) {
1205                                         foreach (char sep in separators) {
1206                                                 if (this.c_str[i] == sep)
1207                                                         return i - startIndex;
1208                                         }
1209                                 } else if (is_lwsp (this.c_str[i])) {
1210                                         return i - startIndex;
1211                                 }
1212                         }
1213
1214                         return -1;
1215                 }
1216
1217                 public string[] Split (params char[] separator)
1218                 {
1219                         /**
1220                          * split:
1221                          * @separator: delimiting chars or null to split on whtspc
1222                          *
1223                          * Returns: 1. An array consisting of a single
1224                          * element (@this) if none of the delimiting
1225                          * chars appear in @this. 2. An array of
1226                          * substrings which are delimited by one of
1227                          * the separator chars. 3. An array of
1228                          * substrings separated by whitespace if
1229                          * @separator is null. The Empty string should
1230                          * be returned wherever 2 delimiting chars are
1231                          * adjacent.
1232                          **/
1233                         // FIXME: would using a Queue be better?
1234                         string[] strings;
1235                         ArrayList list;
1236                         int index, len;
1237
1238                         list = new ArrayList ();
1239                         for (index = 0, len = 0; index < this.length; index += len + 1) {
1240                                 len = splitme (separator, index);
1241                                 len = len > -1 ? len : this.length - index;
1242                                 if (len == 0) {
1243                                         list.Add (String.Empty);
1244                                 } else {
1245                                         char[] str;
1246                                         int i;
1247
1248                                         str = new char [len];
1249                                         for (i = 0; i < len; i++)
1250                                                 str[i] = this.c_str[index + i];
1251
1252                                         list.Add (new String (str));
1253                                 }
1254                         }
1255
1256                         strings = new string [list.Count];
1257                         if (list.Count == 1) {
1258                                 /* special case for an array holding @this */
1259                                 strings[0] = this;
1260                         } else {
1261                                 for (index = 0; index < list.Count; index++)
1262                                         strings[index] = (string) list[index];
1263                         }
1264
1265                         return strings;
1266                 }
1267
1268                 public string[] Split (char[] separator, int maxCount)
1269                 {
1270                         // FIXME: what to do if maxCount <= 0?
1271                         // FIXME: would using Queue be better than ArrayList?
1272                         string[] strings;
1273                         ArrayList list;
1274                         int index, len, used;
1275
1276                         used = 0;
1277                         list = new ArrayList ();
1278                         for (index = 0, len = 0; index < this.length && used < maxCount; index += len + 1) {
1279                                 len = splitme (separator, index);
1280                                 len = len > -1 ? len : this.length - index;
1281                                 if (len == 0) {
1282                                         list.Add (String.Empty);
1283                                 } else {
1284                                         char[] str;
1285                                         int i;
1286
1287                                         str = new char [len];
1288                                         for (i = 0; i < len; i++)
1289                                                 str[i] = this.c_str[index + i];
1290
1291                                         list.Add (new String (str));
1292                                 }
1293                                 used++;
1294                         }
1295
1296                         /* fit the remaining chunk of the @this into it's own element */
1297                         if (index != this.length) {
1298                                 char[] str;
1299                                 int i;
1300
1301                                 str = new char [this.length - index];
1302                                 for (i = index; i < this.length; i++)
1303                                         str[i - index] = this.c_str[i];
1304
1305                                 list.Add (new String (str));
1306                         }
1307
1308                         strings = new string [list.Count];
1309                         if (list.Count == 1) {
1310                                 /* special case for an array holding @this */
1311                                 strings[0] = this;
1312                         } else {
1313                                 for (index = 0; index < list.Count; index++)
1314                                         strings[index] = (string) list[index];
1315                         }
1316
1317                         return strings;
1318                 }
1319
1320                 public bool StartsWith (string value)
1321                 {
1322                         bool startswith = true;
1323                         int i;
1324
1325                         if (value == null)
1326                                 throw new ArgumentNullException ();
1327
1328                         if (value.length > this.length)
1329                                 return false;
1330
1331                         for (i = 0; i < value.length && startswith; i++)
1332                                 startswith = startswith && value.c_str[i] == this.c_str[i];
1333
1334                         return startswith;
1335                 }
1336
1337                 public string Substring (int startIndex)
1338                 {
1339                         char[] str;
1340                         int i, len;
1341
1342                         if (startIndex < 0 || startIndex > this.length)
1343                                 throw new ArgumentOutOfRangeException ();
1344
1345                         len = this.length - startIndex;
1346                         if (len == 0)
1347                                 return String.Empty;
1348                         String res = new String (len);
1349                         str = res.c_str;
1350                         for (i = startIndex; i < this.length; i++)
1351                                 str[i - startIndex] = this.c_str[i];
1352
1353                         return res;
1354                 }
1355
1356                 public string Substring (int startIndex, int length)
1357                 {
1358                         char[] str;
1359                         int i;
1360
1361                         if (startIndex < 0 || length < 0 || startIndex + length > this.length)
1362                                 throw new ArgumentOutOfRangeException ();
1363
1364                         if (length == 0)
1365                                 return String.Empty;
1366                         
1367                         String res = new String (length);
1368                         str = res.c_str;
1369                         for (i = startIndex; i < startIndex + length; i++)
1370                                 str[i - startIndex] = this.c_str[i];
1371
1372                         return res;
1373                 }
1374
1375                 [MonoTODO]
1376                 public bool ToBoolean (IFormatProvider provider)
1377                 {
1378                         // FIXME: implement me
1379                         throw new NotImplementedException ();
1380                 }
1381                 
1382                 [MonoTODO]
1383                 public byte ToByte (IFormatProvider provider)
1384                 {
1385                         // FIXME: implement me
1386                         throw new NotImplementedException ();
1387                 }
1388                 
1389                 [MonoTODO]
1390                 public char ToChar (IFormatProvider provider)
1391                 {
1392                         // FIXME: implement me
1393                         throw new NotImplementedException ();
1394                 }
1395
1396                 public char[] ToCharArray ()
1397                 {
1398                         return ToCharArray (0, this.length);
1399                 }
1400
1401                 public char[] ToCharArray (int startIndex, int length)
1402                 {
1403                         char[] chars;
1404                         int i;
1405
1406                         if (startIndex < 0 || length < 0 || startIndex + length > this.length)
1407                                 throw new ArgumentOutOfRangeException ();
1408
1409                         chars = new char [length];
1410                         for (i = startIndex; i < length; i++)
1411                                 chars[i - startIndex] = this.c_str[i];
1412
1413                         return chars;
1414                 }
1415
1416                 [MonoTODO]
1417                 public DateTime ToDateTime (IFormatProvider provider)
1418                 {
1419                         // FIXME: implement me
1420                         // return new DateTime (0);
1421                         throw new NotImplementedException ();
1422                 }
1423
1424                 [MonoTODO]
1425                 public decimal ToDecimal (IFormatProvider provider)
1426                 {
1427                         // FIXME: implement me
1428                         throw new NotImplementedException ();
1429                 }
1430
1431                 [MonoTODO]
1432                 public double ToDouble (IFormatProvider provider)
1433                 {
1434                         // FIXME: implement me
1435                         throw new NotImplementedException ();
1436                 }
1437
1438                 [MonoTODO]
1439                 public short ToInt16 (IFormatProvider provider)
1440                 {
1441                         // FIXME: implement me
1442                         throw new NotImplementedException ();
1443                 }
1444
1445                 [MonoTODO]
1446                 public int ToInt32 (IFormatProvider provider)
1447                 {
1448                         // FIXME: implement me
1449                         throw new NotImplementedException ();
1450                 }
1451
1452                 [MonoTODO]
1453                 public long ToInt64 (IFormatProvider provider)
1454                 {
1455                         // FIXME: implement me
1456                         throw new NotImplementedException ();
1457                 }
1458
1459                 public string ToLower ()
1460                 {
1461                         char[] str;
1462                         int i;
1463
1464                         String res = new String (length);
1465                         str = res.c_str;
1466                         for (i = 0; i < this.length; i++)
1467                                 str[i] = Char.ToLower (this.c_str[i]);
1468
1469                         return res;
1470                 }
1471
1472                 [MonoTODO]
1473                 public string ToLower (CultureInfo culture)
1474                 {
1475                         // FIXME: implement me
1476                         throw new NotImplementedException ();
1477
1478                 }
1479
1480                 [CLSCompliant(false)][MonoTODO]
1481                 public sbyte ToSByte (IFormatProvider provider)
1482                 {
1483                         // FIXME: implement me
1484                         throw new NotImplementedException ();
1485                 }
1486
1487                 [MonoTODO]
1488                 public float ToSingle (IFormatProvider provider)
1489                 {
1490                         // FIXME: implement me
1491                         throw new NotImplementedException ();
1492                 }
1493
1494                 public override string ToString ()
1495                 {
1496                         return this;
1497                 }
1498
1499                 [MonoTODO]
1500                 public string ToString (IFormatProvider format)
1501                 {
1502                         // FIXME: implement me
1503                         throw new NotImplementedException ();
1504                 }
1505
1506                 [MonoTODO]
1507                 public object ToType (Type conversionType, IFormatProvider provider)
1508                 {
1509                         // FIXME: implement me
1510                         throw new NotImplementedException ();
1511                 }
1512
1513                 [CLSCompliant(false)][MonoTODO]
1514                 public ushort ToUInt16 (IFormatProvider provider)
1515                 {
1516                         // FIXME: implement me
1517                         throw new NotImplementedException ();
1518                 }
1519
1520                 [CLSCompliant(false)][MonoTODO]
1521                 public uint ToUInt32 (IFormatProvider provider)
1522                 {
1523                         // FIXME: implement me
1524                         throw new NotImplementedException ();
1525                 }
1526
1527                 [CLSCompliant(false)][MonoTODO]
1528                 public ulong ToUInt64 (IFormatProvider provider)
1529                 {
1530                         // FIXME: implement me
1531                         throw new NotImplementedException ();
1532                 }
1533
1534                 public string ToUpper ()
1535                 {
1536                         char[] str;
1537                         int i;
1538
1539                         String res = new String (length);
1540                         str = res.c_str;
1541                         for (i = 0; i < this.length; i++)
1542                                 str[i] = Char.ToUpper (this.c_str[i]);
1543
1544                         return res;
1545                 }
1546
1547                 [MonoTODO]
1548                 public string ToUpper (CultureInfo culture)
1549                 {
1550                         // FIXME: implement me
1551                         throw new NotImplementedException ();
1552                 }
1553
1554                 public string Trim ()
1555                 {
1556                         return Trim (null);
1557                 }
1558
1559                 public string Trim (params char[] trimChars)
1560                 {
1561                         int begin, end;
1562                         bool matches;
1563
1564                         matches = true;
1565                         for (begin = 0; matches && begin < this.length; begin++) {
1566                                 if (trimChars != null) {
1567                                         matches = false;
1568                                         foreach (char c in trimChars) {
1569                                                 matches = this.c_str[begin] == c;
1570                                                 if (matches)
1571                                                         break;
1572                                         }
1573                                 } else {
1574                                         matches = is_lwsp (this.c_str[begin]);
1575                                 }
1576                         }
1577
1578                         matches = true;
1579                         for (end = this.length - 1; matches && end > begin; end--) {
1580                                 if (trimChars != null) {
1581                                         matches = false;
1582                                         foreach (char c in trimChars) {
1583                                                 matches = this.c_str[end] == c;
1584                                                 if (matches)
1585                                                         break;
1586                                         }
1587                                 } else {
1588                                         matches = is_lwsp (this.c_str[end]);
1589                                 }
1590                         }
1591
1592                         if (begin >= end)
1593                                 return String.Empty;
1594
1595                         return Substring (begin, end - begin);
1596                 }
1597
1598                 public string TrimEnd (params char[] trimChars)
1599                 {
1600                         bool matches = true;
1601                         int end;
1602
1603                         for (end = this.length; end > 0; end--) {
1604                                 if (trimChars != null) {
1605                                         matches = false;
1606                                         foreach (char c in trimChars) {
1607                                                 matches = this.c_str[end] == c;
1608                                                 if (matches)
1609                                                         break;
1610                                         }
1611                                 } else {
1612                                         matches = is_lwsp (this.c_str[end]);
1613                                 }
1614                         }
1615
1616                         if (end == 0)
1617                                 return String.Empty;
1618
1619                         return Substring (0, end);
1620                 }
1621
1622                 public string TrimStart (params char[] trimChars)
1623                 {
1624                         bool matches = true;
1625                         int begin;
1626
1627                         for (begin = 0; matches && begin < this.length; begin++) {
1628                                 if (trimChars != null) {
1629                                         matches = false;
1630                                         foreach (char c in trimChars) {
1631                                                 matches = this.c_str[begin] == c;
1632                                                 if (matches)
1633                                                         break;
1634                                         }
1635                                 } else {
1636                                         matches = is_lwsp (this.c_str[begin]);
1637                                 }
1638                         }
1639
1640                         if (begin == this.length)
1641                                 return String.Empty;
1642
1643                         return Substring (begin, this.length - begin);
1644                 }
1645
1646                 // Operators
1647                 public static bool operator ==(string a, string b)
1648                 {
1649                         if (a.length != b.length)
1650                                 return false;
1651
1652                         int l = a.length;
1653                         for (int i = 0; i < l; i++)
1654                                 if (a.c_str[i] != b.c_str[i])
1655                                         return false;
1656
1657                         return true;
1658                 }
1659
1660                 public static bool operator !=(string a, string b)
1661                 {
1662                         return !(a == b);
1663                 }
1664         }
1665 }