1 // -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
6 // Jeffrey Stedfast (fejj@ximian.com)
8 // (C) 2001 Ximian, Inc. http://www.ximian.com
11 // FIXME: from what I gather from msdn, when a function is to return an empty string
12 // we should be returning this.Empty - some methods do this and others don't.
14 // FIXME: I didn't realise until later that `string' has a .Length method and so
15 // I am missing some proper bounds-checking in some methods. Find these
16 // instances and throw the ArgumentOutOfBoundsException at the programmer.
17 // I like pelting programmers with ArgumentOutOfBoundsException's :-)
19 // FIXME: The ToLower(), ToUpper(), and Compare(..., bool ignoreCase) methods
20 // need to be made unicode aware.
22 // FIXME: when you have a char carr[], does carr.Length include the terminating null char?
26 using System.Collections;
27 using System.Globalization;
28 using System.Runtime.CompilerServices;
32 //[DefaultMemberName("Chars")]
33 public sealed class String : IComparable, ICloneable, IConvertible, IEnumerable {
34 public static readonly string Empty = "";
40 internal String (int storage)
43 c_str = new char [storage];
47 unsafe public String (char *value)
51 // FIXME: can I do value.Length here?
55 for (i = 0; *(value + i) != '\0'; i++);
59 this.c_str = new char [this.length + 1];
60 for (i = 0; i < this.length; i++)
61 this.c_str[i] = *(value + i);
64 public String (char[] value)
68 // FIXME: value.Length includes the terminating null char?
69 this.length = value != null ? strlen (value): 0;
70 this.c_str = new char [this.length + 1];
71 for (i = 0; i < this.length; i++)
72 this.c_str[i] = value[i];
76 unsafe public String (sbyte *value)
78 // FIXME: consider unicode?
81 // FIXME: can I do value.Length here? */
85 for (i = 0; *(value + i) != '\0'; i++);
89 this.c_str = new char [this.length + 1];
90 for (i = 0; i < this.length; i++)
91 this.c_str[i] = (char) *(value + i);
94 public String (char c, int count)
99 this.c_str = new char [count + 1];
100 for (i = 0; i < count; i++)
104 [CLSCompliant(false)]
105 unsafe public String (char *value, int startIndex, int length)
109 if (value == null && startIndex != 0 && length != 0)
110 throw new ArgumentNullException ();
112 if (startIndex < 0 || length < 0)
113 throw new ArgumentOutOfRangeException ();
115 this.length = length;
116 this.c_str = new char [length + 1];
117 for (i = 0; i < length; i++)
118 this.c_str[i] = *(value + startIndex + i);
121 public String (char[] value, int startIndex, int length)
125 if (value == null && startIndex != 0 && length != 0)
126 throw new ArgumentNullException ();
128 if (startIndex < 0 || length < 0)
129 throw new ArgumentOutOfRangeException ();
131 this.length = length;
132 this.c_str = new char [length + 1];
133 for (i = 0; i < length; i++)
134 this.c_str[i] = value[startIndex + i];
137 [CLSCompliant(false)]
138 unsafe public String (sbyte *value, int startIndex, int length)
140 // FIXME: consider unicode?
143 if (value == null && startIndex != 0 && length != 0)
144 throw new ArgumentNullException ();
146 if (startIndex < 0 || length < 0)
147 throw new ArgumentOutOfRangeException ();
149 this.length = length;
150 this.c_str = new char [length + 1];
151 for (i = 0; i < length; i++)
152 this.c_str[i] = (char) *(value + startIndex + i);
155 [CLSCompliant(false)][MonoTODO]
156 unsafe public String (sbyte *value, int startIndex, int length, Encoding enc)
158 // FIXME: implement me
163 // FIXME: is there anything we need to do here?
164 /*base.Finalize ();*/
174 [IndexerName("Chars")]
175 public char this [int index] {
177 if (index >= this.length)
178 throw new ArgumentOutOfRangeException ();
180 return this.c_str[index];
184 // Private helper methods
185 private static int strlen (char[] str)
187 // FIXME: if str.Length includes terminating null char, then return (str.Length - 1)
192 private static char tolowerordinal (char c)
194 // FIXME: implement me
198 private static bool is_lwsp (char c)
200 /* this comes from the msdn docs for String.Trim() */
201 if ((c >= '\x9' && c <= '\xD') || c == '\x20' || c == '\xA0' ||
202 (c >= '\x2000' && c <= '\x200B') || c == '\x3000' || c == '\xFEFF')
208 private static int BoyerMoore (char[] haystack, string needle, int startIndex, int count)
210 /* (hopefully) Unicode-safe Boyer-Moore implementation */
211 int[] skiptable = new int[65536]; /* our unicode-safe skip-table */
212 int h, n, he, ne, hc, nc, i;
214 if (haystack == null || needle == null)
215 throw new ArgumentNullException ();
217 /* if the search buffer is shorter than the pattern buffer, we can't match */
218 if (count < needle.length)
221 /* return an instant match if the pattern is 0-length */
222 if (needle.length == 0)
225 /* set a pointer at the end of each string */
226 ne = needle.length - 1; /* position of char before '\0' */
227 he = startIndex + count; /* position of last valid char */
229 /* init the skip table with the pattern length */
231 for (i = 0; i < 65536; i++)
234 /* set the skip value for the chars that *do* appear in the
235 * pattern buffer (needle) to the distance from the index to
236 * the end of the pattern buffer. */
237 for (nc = 0; nc < ne; nc++)
238 skiptable[(int) needle[nc]] = ne - nc;
241 while (count >= needle.length) {
242 hc = h + needle.length - 1; /* set the haystack compare pointer */
243 nc = ne; /* set the needle compare pointer */
245 /* work our way backwards until they don't match */
246 for (i = 0; nc > 0; nc--, hc--, i++)
247 if (needle[nc] != haystack[hc])
250 if (needle[nc] != haystack[hc]) {
251 n = skiptable[(int) haystack[hc]] - i;
263 public object Clone ()
265 // FIXME: implement me
269 public static int Compare (string strA, string strB)
278 } else if (strB == null)
281 min = strA.length < strB.length ? strA.length : strB.length;
283 for (i = 0; i < min; i++) {
284 if (strA.c_str[i] != strB.c_str[i]) {
285 return (int)strA.c_str[i] - (int)strB.c_str[i];
288 if (strA.length == strB.length) {
291 if (strA.length > min) {
298 public static int Compare (string strA, string strB, bool ignoreCase)
303 return Compare (strA, strB);
306 * And here I thought Eazel developers were on crack...
307 * if a string is null it should pelt the programmer with
308 * ArgumentNullExceptions, damnit!
315 } else if (strB == null)
318 min = strA.length < strB.length ? strA.length : strB.length;
320 for (i = 0; i < min; i++) {
321 if (Char.ToLower (strA.c_str[i]) != Char.ToLower (strB.c_str[i])) {
322 return (int)strA.c_str[i] - (int)strB.c_str[i];
325 if (strA.length == strB.length) {
328 if (strA.length > min) {
336 public static int Compare (string strA, string strB, bool ignoreCase, CultureInfo culture)
338 // FIXME: implement me
342 public static int Compare (string strA, int indexA, string strB, int indexB, int length)
346 if (length < 0 || indexA < 0 || indexB < 0)
347 throw new ArgumentOutOfRangeException ();
349 if (indexA > strA.Length || indexB > strB.Length)
350 throw new ArgumentOutOfRangeException ();
352 /* And again with the ("" > null) logic... lord have mercy! */
358 } else if (strB == null)
361 for (i = 0; i < length - 1; i++) {
362 if (strA[indexA + i] != strB[indexB + i])
366 return ((int) (strA[indexA + i] - strB[indexB + i]));
369 public static int Compare (string strA, int indexA, string strB, int indexB,
370 int length, bool ignoreCase)
375 return Compare (strA, indexA, strB, indexB, length);
377 if (length < 0 || indexA < 0 || indexB < 0)
378 throw new ArgumentOutOfRangeException ();
380 if (indexA > strA.Length || indexB > strB.Length)
381 throw new ArgumentOutOfRangeException ();
383 /* When will the hurting stop!?!? */
389 } else if (strB == null)
392 for (i = 0; i < length - 1; i++) {
393 if (Char.ToLower (strA[indexA + i]) != Char.ToLower (strB[indexB + i]))
397 return ((int) (strA[indexA + i] - strB[indexB + i]));
401 public static int Compare (string strA, int indexA, string strB, int indexB,
402 int length, bool ignoreCase, CultureInfo culture)
405 throw new ArgumentNullException ();
407 if (length < 0 || indexA < 0 || indexB < 0)
408 throw new ArgumentOutOfRangeException ();
410 if (indexA > strA.Length || indexB > strB.Length)
411 throw new ArgumentOutOfRangeException ();
413 /* I can't take it anymore! */
419 } else if (strB == null)
422 // FIXME: implement me
426 public static int CompareOrdinal (string strA, string strB)
430 /* Please God, make it stop! */
436 } else if (strB == null)
439 for (i = 0; i < strA.Length && i < strB.Length; i++) {
442 cA = tolowerordinal (strA[i]);
443 cB = tolowerordinal (strB[i]);
449 return ((int) (strA[i] - strB[i]));
452 public static int CompareOrdinal (string strA, int indexA, string strB, int indexB,
457 if (length < 0 || indexA < 0 || indexB < 0)
458 throw new ArgumentOutOfRangeException ();
465 } else if (strB == null)
468 for (i = 0; i < length; i++) {
471 cA = tolowerordinal (strA[indexA + i]);
472 cB = tolowerordinal (strB[indexB + i]);
478 return ((int) (strA[indexA + i] - strB[indexB + i]));
481 public int CompareTo (object obj)
483 return Compare (this, obj == null ? null : obj.ToString ());
486 public int CompareTo (string str)
488 return Compare (this, str);
491 public static string Concat (object arg)
493 return arg != null ? arg.ToString () : String.Empty;
496 public static string Concat (params object[] args)
503 throw new ArgumentNullException ();
505 strings = new string [args.Length];
508 foreach (object arg in args) {
509 /* use Empty for each null argument */
511 strings[i] = String.Empty;
513 strings[i] = arg.ToString ();
514 len += strings[i].length;
521 String res = new String (len);
524 for (int j = 0; j < strings.Length; j++)
525 for (int k = 0; k < strings[j].length; k++)
526 str[i++] = strings[j].c_str[k];
531 public static string Concat (params string[] values)
537 throw new ArgumentNullException ();
540 foreach (string value in values)
541 len += value != null ? value.Length : 0;
546 String res = new String (len);
549 foreach (string value in values) {
553 for (int j = 0; j < value.length; j++)
554 str[i++] = value.c_str[j];
560 public static string Concat (object arg0, object arg1)
562 string str0 = arg0 != null ? arg0.ToString () : String.Empty;
563 string str1 = arg1 != null ? arg1.ToString () : String.Empty;
565 return Concat (str0, str1);
568 public static string Concat (string str0, string str1)
578 len = str0.length + str1.length;
582 String res = new String (len);
585 for (i = 0; i < str0.length; i++)
586 concat[i] = str0.c_str[i];
587 for (j = 0 ; j < str1.length; j++)
588 concat[i + j] = str1.c_str[j];
593 public static string Concat (object arg0, object arg1, object arg2)
595 string str0 = arg0 != null ? arg0.ToString () : String.Empty;
596 string str1 = arg1 != null ? arg1.ToString () : String.Empty;
597 string str2 = arg2 != null ? arg2.ToString () : String.Empty;
599 return Concat (str0, str1, str2);
602 public static string Concat (string str0, string str1, string str2)
614 len = str0.length + str1.length + str2.length;
618 String res = new String (len);
621 for (i = 0; i < str0.length; i++)
622 concat[i] = str0.c_str[i];
623 for (j = 0; j < str1.length; j++)
624 concat[i + j] = str1.c_str[j];
625 for (k = 0; k < str2.length; k++)
626 concat[i + j + k] = str2.c_str[k];
631 public static string Concat (string str0, string str1, string str2, string str3)
645 len = str0.length + str1.length + str2.length + str3.length;
648 String res = new String (len);
651 for (i = 0; i < str0.length; i++)
652 concat[i] = str0.c_str[i];
653 for (j = 0; j < str1.length; j++)
654 concat[i + j] = str1.c_str[j];
655 for (k = 0; k < str2.length; k++)
656 concat[i + j + k] = str2.c_str[k];
657 for (l = 0; l < str3.length; l++)
658 concat[i + j + k + l] = str3.c_str[l];
663 public static string Copy (string str)
665 // FIXME: how do I *copy* a string if I can only have 1 of each?
667 throw new ArgumentNullException ();
672 public void CopyTo (int sourceIndex, char[] destination, int destinationIndex, int count)
674 // LAMESPEC: should I null-terminate?
677 if (destination == null)
678 throw new ArgumentNullException ();
680 if (sourceIndex < 0 || destinationIndex < 0 || count < 0)
681 throw new ArgumentOutOfRangeException ();
683 if (sourceIndex + count > this.length)
684 throw new ArgumentOutOfRangeException ();
686 if (destinationIndex + count > destination.Length)
687 throw new ArgumentOutOfRangeException ();
689 for (i = 0; i < count; i++)
690 destination[destinationIndex + i] = this.c_str[sourceIndex + i];
693 public bool EndsWith (string value)
695 bool endswith = true;
699 throw new ArgumentNullException ();
701 start = this.length - value.length;
705 for (i = start; i < this.length && endswith; i++)
706 endswith = this.c_str[i] == value.c_str[i - start];
711 public override bool Equals (object obj)
713 if (!(obj is String))
716 return this == (String) obj;
719 public bool Equals (string value)
721 return this == value;
724 public static bool Equals (string a, string b)
730 public static string Format (string format, object arg0)
732 // FIXME: implement me
733 return format+arg0.ToString();
737 public static string Format (string format, params object[] args)
739 // FIXME: implement me
740 Console.WriteLine (args[0].ToString());
741 if (args.Length == 1)
742 return format+args[0].ToString();
743 if (args.Length == 2)
744 return format+args[0].ToString()+args[1].ToString();
745 if (args.Length == 3)
746 return format+args[0].ToString()+args[1].ToString()+args[2].ToString();
747 if (args.Length == 4)
748 return format+args[0].ToString()+args[1].ToString()+args[2].ToString()+args[3].ToString();
749 Console.WriteLine ("String.Format with args: "+args.Length.ToString());
754 public static string Format (IFormatProvider provider, string format, params object[] args)
756 // FIXME: implement me
761 public static string Format (string format, object arg0, object arg1)
763 // FIXME: implement me
764 return format+arg0.ToString()+arg1.ToString();
768 public static string Format (string format, object arg0, object arg1, object arg2)
770 // FIXME: implement me
771 return format+arg0.ToString()+arg1.ToString()+arg2.ToString();
774 public CharEnumerator GetEnumerator ()
776 return new CharEnumerator (this);
779 IEnumerator IEnumerable.GetEnumerator ()
781 return new CharEnumerator (this);
784 public override int GetHashCode ()
788 for (i = 0; i < length; ++i)
789 h = (h << 5) - h + c_str [i];
793 public TypeCode GetTypeCode ()
795 return TypeCode.String;
798 public int IndexOf (char value)
800 return IndexOf (value, 0, this.length);
803 public int IndexOf (string value)
805 return IndexOf (value, 0, this.length);
808 public int IndexOf (char value, int startIndex)
810 return IndexOf (value, startIndex, this.length - startIndex);
813 public int IndexOf (string value, int startIndex)
815 return IndexOf (value, startIndex, this.length - startIndex);
818 public int IndexOf (char value, int startIndex, int count)
822 if (startIndex < 0 || count < 0 || startIndex + count > this.length)
823 throw new ArgumentOutOfRangeException ();
825 for (i = startIndex; i - startIndex < count; i++)
826 if (this.c_str[i] == value)
832 public int IndexOf (string value, int startIndex, int count)
835 throw new ArgumentNullException ();
837 if (startIndex < 0 || count < 0 || startIndex + count > this.length)
838 throw new ArgumentOutOfRangeException ();
840 return BoyerMoore (this.c_str, value, startIndex, count);
843 for (i = startIndex; i - startIndex + value.Length <= count; ) {
844 if (this.c_str[i] == value[0]) {
848 for (j = 1; equal && j < value.Length; j++) {
849 equal = this.c_str[i + j] == value[j];
850 if (this.c_str[i + j] == value[0] && nexti == 0)
869 public int IndexOfAny (char[] values)
871 return IndexOfAny (values, 0, this.length);
874 public int IndexOfAny (char[] values, int startIndex)
876 return IndexOfAny (values, startIndex, this.length - startIndex);
879 public int IndexOfAny (char[] values, int startIndex, int count)
882 throw new ArgumentNullException ();
884 if (startIndex < 0 || count < 0 || startIndex + count > this.length)
885 throw new ArgumentOutOfRangeException ();
887 for (int i = startIndex; i < startIndex + count; i++) {
888 for (int j = 0; j < strlen (values); j++) {
889 if (this.c_str[i] == values[j])
897 public string Insert (int startIndex, string value)
903 throw new ArgumentNullException ();
905 if (startIndex < 0 || startIndex > this.length)
906 throw new ArgumentOutOfRangeException ();
908 String res = new String (value.length + this.length);
911 for (i = 0; i < startIndex; i++)
912 str[i] = this.c_str[i];
913 for (j = 0; j < value.length; j++)
914 str[i + j] = value.c_str[j];
915 for ( ; i < this.length; i++)
916 str[i + j] = this.c_str[i];
921 [MethodImplAttribute(MethodImplOptions.InternalCall)]
922 public extern static string Intern (string str);
924 [MethodImplAttribute(MethodImplOptions.InternalCall)]
925 public extern static string IsInterned (string str);
927 public static string Join (string separator, string[] value)
929 return Join (separator, value, 0, value.Length);
932 public static string Join (string separator, string[] value, int startIndex, int count)
934 // LAMESPEC: msdn doesn't specify what happens when separator is null
938 if (separator == null || value == null)
939 throw new ArgumentNullException ();
941 if (startIndex + count > value.Length)
942 throw new ArgumentOutOfRangeException ();
945 for (i = startIndex, used = 0; used < count; i++, used++) {
947 len += separator.length;
949 len += value[i].length;
952 // We have no elements to join?
956 String res = new String (len);
959 for (i = 0; i < value[startIndex].length; i++)
960 str[i] = value[startIndex][i];
963 for (j = startIndex + 1; used < count; j++, used++) {
966 for (k = 0; k < separator.length; k++)
967 str[i++] = separator.c_str[k];
968 for (k = 0; k < value[j].length; k++)
969 str[i++] = value[j].c_str[k];
975 public int LastIndexOf (char value)
981 for (; i >= 0; i--) {
982 if (this.c_str[i] == value)
989 public int LastIndexOf (string value)
991 return LastIndexOf (value, this.length - 1, this.length);
994 public int LastIndexOf (char value, int startIndex)
996 if (startIndex < 0 || startIndex >= this.length)
997 throw new ArgumentOutOfRangeException ();
999 for (int i = startIndex; i >= 0; i--) {
1000 if (this.c_str[i] == value)
1007 public int LastIndexOf (string value, int startIndex)
1009 return LastIndexOf (value, startIndex, startIndex + 1);
1012 public int LastIndexOf (char value, int startIndex, int count)
1014 if (startIndex < 0 || count < 0)
1015 throw new ArgumentOutOfRangeException ();
1017 if (startIndex >= this.length || startIndex - count + 1 < 0)
1018 throw new ArgumentOutOfRangeException ();
1020 for (int i = startIndex; i > startIndex - count; i--) {
1021 if (this.c_str[i] == value)
1028 public int LastIndexOf (string value, int startIndex, int count)
1030 // LAMESPEC: currently I'm using startIndex as the 0-position in the comparison,
1031 // but maybe it's the end-position in MS's implementation?
1032 // msdn is unclear on this point. I think this is correct though.
1036 throw new ArgumentNullException ();
1038 if (startIndex < 0 || startIndex >= this.length)
1039 throw new ArgumentOutOfRangeException ();
1041 if (count < 0 || startIndex - count + 1 < 0)
1042 throw new ArgumentOutOfRangeException ();
1044 if (value == String.Empty)
1047 if (startIndex + value.length > this.length) {
1048 /* just a little optimization */
1051 start = this.length - value.length;
1052 count -= startIndex - start;
1056 // FIXME: use a reversed-unicode-safe-Boyer-Moore?
1057 len = value.length - 1;
1058 for (i = startIndex; i > startIndex - count; i--) {
1059 if (this.c_str[i + len] == value.c_str[len]) {
1063 for (j = len - 1; equal && j >= 0; j--)
1064 equal = this.c_str[i + j] == value.c_str[j];
1074 public int LastIndexOfAny (char[] values)
1076 return LastIndexOfAny (values, this.length - 1, this.length);
1079 public int LastIndexOfAny (char[] values, int startIndex)
1081 return LastIndexOfAny (values, startIndex, startIndex + 1);
1084 public int LastIndexOfAny (char[] values, int startIndex, int count)
1089 throw new ArgumentNullException ();
1091 if (startIndex < 0 || count < 0 || startIndex - count + 1 < 0)
1092 throw new ArgumentOutOfRangeException ();
1094 for (i = startIndex; i > startIndex - count; i--) {
1095 for (int j = 0; j < strlen (values); j++) {
1096 if (this.c_str[i] == values[j])
1104 public string PadLeft (int totalWidth)
1106 return PadLeft (totalWidth, ' ');
1109 public string PadLeft (int totalWidth, char padChar)
1115 throw new ArgumentException ();
1117 str = new char [totalWidth > this.length ? totalWidth : this.length];
1118 for (i = 0; i < totalWidth - this.length; i++)
1121 for (j = 0; j < this.length; i++, j++)
1122 str[i] = this.c_str[j];
1124 return new String (str);
1127 public string PadRight (int totalWidth)
1129 return PadRight (totalWidth, ' ');
1132 public string PadRight (int totalWidth, char padChar)
1138 throw new ArgumentException ();
1140 str = new char [totalWidth > this.length ? totalWidth : this.length];
1141 for (i = 0; i < this.length; i++)
1142 str[i] = this.c_str[i];
1144 for ( ; i < str.Length; i++)
1147 return new String (str);
1150 public string Remove (int startIndex, int count)
1155 if (startIndex < 0 || count < 0 || startIndex + count > this.length)
1156 throw new ArgumentOutOfRangeException ();
1158 len = this.length - count;
1160 return String.Empty;
1162 String res = new String (len);
1164 for (i = 0; i < startIndex; i++)
1165 str[i] = this.c_str[i];
1166 for (j = i + count; j < this.length; j++)
1167 str[i++] = this.c_str[j];
1172 public string Replace (char oldChar, char newChar)
1177 String res = new String (length);
1179 for (i = 0; i < this.length; i++) {
1180 if (this.c_str[i] == oldChar)
1183 str[i] = this.c_str[i];
1189 public string Replace (string oldValue, string newValue)
1191 // LAMESPEC: msdn doesn't specify what to do if either args is null
1192 int index, len, i, j;
1195 if (oldValue == null || newValue == null)
1196 throw new ArgumentNullException ();
1198 // Use IndexOf in case I later rewrite it to use Boyer-Moore
1199 index = IndexOf (oldValue, 0);
1201 // This is the easy one ;-)
1202 return Substring (0, this.length);
1205 len = this.length - oldValue.length + newValue.length;
1207 return String.Empty;
1209 String res = new String (len);
1211 for (i = 0; i < index; i++)
1212 str[i] = this.c_str[i];
1213 for (j = 0; j < newValue.length; j++)
1214 str[i++] = newValue[j];
1215 for (j = index + oldValue.length; j < this.length; j++)
1216 str[i++] = this.c_str[j];
1221 private int splitme (char[] separators, int startIndex)
1223 /* this is basically a customized IndexOfAny() for the Split() methods */
1224 for (int i = startIndex; i < this.length; i++) {
1225 if (separators != null) {
1226 foreach (char sep in separators) {
1227 if (this.c_str[i] == sep)
1228 return i - startIndex;
1230 } else if (is_lwsp (this.c_str[i])) {
1231 return i - startIndex;
1238 public string[] Split (params char[] separator)
1242 * @separator: delimiting chars or null to split on whtspc
1244 * Returns: 1. An array consisting of a single
1245 * element (@this) if none of the delimiting
1246 * chars appear in @this. 2. An array of
1247 * substrings which are delimited by one of
1248 * the separator chars. 3. An array of
1249 * substrings separated by whitespace if
1250 * @separator is null. The Empty string should
1251 * be returned wherever 2 delimiting chars are
1254 // FIXME: would using a Queue be better?
1259 list = new ArrayList ();
1260 for (index = 0, len = 0; index < this.length; index += len + 1) {
1261 len = splitme (separator, index);
1262 len = len > -1 ? len : this.length - index;
1264 list.Add (String.Empty);
1269 str = new char [len];
1270 for (i = 0; i < len; i++)
1271 str[i] = this.c_str[index + i];
1273 list.Add (new String (str));
1277 strings = new string [list.Count];
1278 if (list.Count == 1) {
1279 /* special case for an array holding @this */
1282 for (index = 0; index < list.Count; index++)
1283 strings[index] = (string) list[index];
1289 public string[] Split (char[] separator, int maxCount)
1291 // FIXME: what to do if maxCount <= 0?
1292 // FIXME: would using Queue be better than ArrayList?
1295 int index, len, used;
1298 list = new ArrayList ();
1299 for (index = 0, len = 0; index < this.length && used < maxCount; index += len + 1) {
1300 len = splitme (separator, index);
1301 len = len > -1 ? len : this.length - index;
1303 list.Add (String.Empty);
1308 str = new char [len];
1309 for (i = 0; i < len; i++)
1310 str[i] = this.c_str[index + i];
1312 list.Add (new String (str));
1317 /* fit the remaining chunk of the @this into it's own element */
1318 if (index != this.length) {
1322 str = new char [this.length - index];
1323 for (i = index; i < this.length; i++)
1324 str[i - index] = this.c_str[i];
1326 list.Add (new String (str));
1329 strings = new string [list.Count];
1330 if (list.Count == 1) {
1331 /* special case for an array holding @this */
1334 for (index = 0; index < list.Count; index++)
1335 strings[index] = (string) list[index];
1341 public bool StartsWith (string value)
1343 bool startswith = true;
1347 throw new ArgumentNullException ();
1349 if (value.length > this.length)
1352 for (i = 0; i < value.length && startswith; i++)
1353 startswith = startswith && value.c_str[i] == this.c_str[i];
1358 public string Substring (int startIndex)
1363 if (startIndex < 0 || startIndex > this.length)
1364 throw new ArgumentOutOfRangeException ();
1366 len = this.length - startIndex;
1368 return String.Empty;
1369 String res = new String (len);
1371 for (i = startIndex; i < this.length; i++)
1372 str[i - startIndex] = this.c_str[i];
1377 public string Substring (int startIndex, int length)
1382 if (startIndex < 0 || length < 0 || startIndex + length > this.length)
1383 throw new ArgumentOutOfRangeException ();
1386 return String.Empty;
1388 String res = new String (length);
1390 for (i = startIndex; i < startIndex + length; i++)
1391 str[i - startIndex] = this.c_str[i];
1397 public bool ToBoolean (IFormatProvider provider)
1399 // FIXME: implement me
1400 throw new NotImplementedException ();
1404 public byte ToByte (IFormatProvider provider)
1406 // FIXME: implement me
1407 throw new NotImplementedException ();
1411 public char ToChar (IFormatProvider provider)
1413 // FIXME: implement me
1414 throw new NotImplementedException ();
1417 public char[] ToCharArray ()
1419 return ToCharArray (0, this.length);
1422 public char[] ToCharArray (int startIndex, int length)
1427 if (startIndex < 0 || length < 0 || startIndex + length > this.length)
1428 throw new ArgumentOutOfRangeException ();
1430 chars = new char [length];
1431 for (i = startIndex; i < length; i++)
1432 chars[i - startIndex] = this.c_str[i];
1438 public DateTime ToDateTime (IFormatProvider provider)
1440 // FIXME: implement me
1441 // return new DateTime (0);
1442 throw new NotImplementedException ();
1446 public decimal ToDecimal (IFormatProvider provider)
1448 // FIXME: implement me
1449 throw new NotImplementedException ();
1453 public double ToDouble (IFormatProvider provider)
1455 // FIXME: implement me
1456 throw new NotImplementedException ();
1460 public short ToInt16 (IFormatProvider provider)
1462 // FIXME: implement me
1463 throw new NotImplementedException ();
1467 public int ToInt32 (IFormatProvider provider)
1469 // FIXME: implement me
1470 throw new NotImplementedException ();
1474 public long ToInt64 (IFormatProvider provider)
1476 // FIXME: implement me
1477 throw new NotImplementedException ();
1480 public string ToLower ()
1485 String res = new String (length);
1487 for (i = 0; i < this.length; i++)
1488 str[i] = Char.ToLower (this.c_str[i]);
1494 public string ToLower (CultureInfo culture)
1496 // FIXME: implement me
1497 throw new NotImplementedException ();
1501 [CLSCompliant(false)][MonoTODO]
1502 public sbyte ToSByte (IFormatProvider provider)
1504 // FIXME: implement me
1505 throw new NotImplementedException ();
1509 public float ToSingle (IFormatProvider provider)
1511 // FIXME: implement me
1512 throw new NotImplementedException ();
1515 public override string ToString ()
1521 public string ToString (IFormatProvider format)
1523 // FIXME: implement me
1524 throw new NotImplementedException ();
1528 public object ToType (Type conversionType, IFormatProvider provider)
1530 // FIXME: implement me
1531 throw new NotImplementedException ();
1534 [CLSCompliant(false)][MonoTODO]
1535 public ushort ToUInt16 (IFormatProvider provider)
1537 // FIXME: implement me
1538 throw new NotImplementedException ();
1541 [CLSCompliant(false)][MonoTODO]
1542 public uint ToUInt32 (IFormatProvider provider)
1544 // FIXME: implement me
1545 throw new NotImplementedException ();
1548 [CLSCompliant(false)][MonoTODO]
1549 public ulong ToUInt64 (IFormatProvider provider)
1551 // FIXME: implement me
1552 throw new NotImplementedException ();
1555 public string ToUpper ()
1560 String res = new String (length);
1562 for (i = 0; i < this.length; i++)
1563 str[i] = Char.ToUpper (this.c_str[i]);
1569 public string ToUpper (CultureInfo culture)
1571 // FIXME: implement me
1572 throw new NotImplementedException ();
1575 public string Trim ()
1580 public string Trim (params char[] trimChars)
1586 for (begin = 0; matches && begin < this.length; begin++) {
1587 if (trimChars != null) {
1589 foreach (char c in trimChars) {
1590 matches = this.c_str[begin] == c;
1595 matches = is_lwsp (this.c_str[begin]);
1600 for (end = this.length - 1; matches && end > begin; end--) {
1601 if (trimChars != null) {
1603 foreach (char c in trimChars) {
1604 matches = this.c_str[end] == c;
1609 matches = is_lwsp (this.c_str[end]);
1614 return String.Empty;
1616 return Substring (begin, end - begin);
1619 public string TrimEnd (params char[] trimChars)
1621 bool matches = true;
1624 for (end = this.length; end > 0; end--) {
1625 if (trimChars != null) {
1627 foreach (char c in trimChars) {
1628 matches = this.c_str[end] == c;
1633 matches = is_lwsp (this.c_str[end]);
1638 return String.Empty;
1640 return Substring (0, end);
1643 public string TrimStart (params char[] trimChars)
1645 bool matches = true;
1648 for (begin = 0; matches && begin < this.length; begin++) {
1649 if (trimChars != null) {
1651 foreach (char c in trimChars) {
1652 matches = this.c_str[begin] == c;
1657 matches = is_lwsp (this.c_str[begin]);
1661 if (begin == this.length)
1662 return String.Empty;
1664 return Substring (begin, this.length - begin);
1668 public static bool operator ==(string a, string b)
1670 if ((object)a == null) {
1671 if ((object)b == null)
1675 if ((object)b == null)
1678 if (a.length != b.length)
1682 for (int i = 0; i < l; i++)
1683 if (a.c_str[i] != b.c_str[i])
1689 public static bool operator !=(string a, string b)