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