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