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