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]))
325 return ((int) (strA.c_str[i] - strB.c_str[i]));
329 public static int Compare (string strA, string strB, bool ignoreCase, CultureInfo culture)
331 // FIXME: implement me
335 public static int Compare (string strA, int indexA, string strB, int indexB, int length)
339 if (length < 0 || indexA < 0 || indexB < 0)
340 throw new ArgumentOutOfRangeException ();
342 if (indexA > strA.Length || indexB > strB.Length)
343 throw new ArgumentOutOfRangeException ();
345 /* And again with the ("" > null) logic... lord have mercy! */
351 } else if (strB == null)
354 for (i = 0; i < length - 1; i++) {
355 if (strA[indexA + i] != strB[indexB + i])
359 return ((int) (strA[indexA + i] - strB[indexB + i]));
362 public static int Compare (string strA, int indexA, string strB, int indexB,
363 int length, bool ignoreCase)
368 return Compare (strA, indexA, strB, indexB, length);
370 if (length < 0 || indexA < 0 || indexB < 0)
371 throw new ArgumentOutOfRangeException ();
373 if (indexA > strA.Length || indexB > strB.Length)
374 throw new ArgumentOutOfRangeException ();
376 /* When will the hurting stop!?!? */
382 } else if (strB == null)
385 for (i = 0; i < length - 1; i++) {
386 if (Char.ToLower (strA[indexA + i]) != Char.ToLower (strB[indexB + i]))
390 return ((int) (strA[indexA + i] - strB[indexB + i]));
394 public static int Compare (string strA, int indexA, string strB, int indexB,
395 int length, bool ignoreCase, CultureInfo culture)
398 throw new ArgumentNullException ();
400 if (length < 0 || indexA < 0 || indexB < 0)
401 throw new ArgumentOutOfRangeException ();
403 if (indexA > strA.Length || indexB > strB.Length)
404 throw new ArgumentOutOfRangeException ();
406 /* I can't take it anymore! */
412 } else if (strB == null)
415 // FIXME: implement me
419 public static int CompareOrdinal (string strA, string strB)
423 /* Please God, make it stop! */
429 } else if (strB == null)
432 for (i = 0; i < strA.Length && i < strB.Length; i++) {
435 cA = tolowerordinal (strA[i]);
436 cB = tolowerordinal (strB[i]);
442 return ((int) (strA[i] - strB[i]));
445 public static int CompareOrdinal (string strA, int indexA, string strB, int indexB,
450 if (length < 0 || indexA < 0 || indexB < 0)
451 throw new ArgumentOutOfRangeException ();
458 } else if (strB == null)
461 for (i = 0; i < length; i++) {
464 cA = tolowerordinal (strA[indexA + i]);
465 cB = tolowerordinal (strB[indexB + i]);
471 return ((int) (strA[indexA + i] - strB[indexB + i]));
474 public int CompareTo (object obj)
476 return Compare (this, obj == null ? null : obj.ToString ());
479 public int CompareTo (string str)
481 return Compare (this, str);
484 public static string Concat (object arg)
486 return arg != null ? arg.ToString () : String.Empty;
489 public static string Concat (params object[] args)
496 throw new ArgumentNullException ();
498 strings = new string [args.Length];
501 foreach (object arg in args) {
502 /* use Empty for each null argument */
504 strings[i] = String.Empty;
506 strings[i] = arg.ToString ();
507 len += strings[i].length;
514 String res = new String (len);
517 for (int j = 0; j < strings.Length; j++)
518 for (int k = 0; k < strings[j].length; k++)
519 str[i++] = strings[j].c_str[k];
524 public static string Concat (params string[] values)
530 throw new ArgumentNullException ();
533 foreach (string value in values)
534 len += value != null ? value.Length : 0;
539 String res = new String (len);
542 foreach (string value in values) {
546 for (int j = 0; j < value.length; j++)
547 str[i++] = value.c_str[j];
553 public static string Concat (object arg0, object arg1)
555 string str0 = arg0 != null ? arg0.ToString () : String.Empty;
556 string str1 = arg1 != null ? arg1.ToString () : String.Empty;
558 return Concat (str0, str1);
561 public static string Concat (string str0, string str1)
571 len = str0.length + str1.length;
575 String res = new String (len);
578 for (i = 0; i < str0.length; i++)
579 concat[i] = str0.c_str[i];
580 for (j = 0 ; j < str1.length; j++)
581 concat[i + j] = str1.c_str[j];
586 public static string Concat (object arg0, object arg1, object arg2)
588 string str0 = arg0 != null ? arg0.ToString () : String.Empty;
589 string str1 = arg1 != null ? arg1.ToString () : String.Empty;
590 string str2 = arg2 != null ? arg2.ToString () : String.Empty;
592 return Concat (str0, str1, str2);
595 public static string Concat (string str0, string str1, string str2)
607 len = str0.length + str1.length + str2.length;
611 String res = new String (len);
614 for (i = 0; i < str0.length; i++)
615 concat[i] = str0.c_str[i];
616 for (j = 0; j < str1.length; j++)
617 concat[i + j] = str1.c_str[j];
618 for (k = 0; k < str2.length; k++)
619 concat[i + j + k] = str2.c_str[k];
624 public static string Concat (string str0, string str1, string str2, string str3)
638 len = str0.length + str1.length + str2.length + str3.length;
641 String res = new String (len);
644 for (i = 0; i < str0.length; i++)
645 concat[i] = str0.c_str[i];
646 for (j = 0; j < str1.length; j++)
647 concat[i + j] = str1.c_str[j];
648 for (k = 0; k < str2.length; k++)
649 concat[i + j + k] = str2.c_str[k];
650 for (l = 0; l < str3.length; l++)
651 concat[i + j + k + l] = str3.c_str[l];
656 public static string Copy (string str)
658 // FIXME: how do I *copy* a string if I can only have 1 of each?
660 throw new ArgumentNullException ();
665 public void CopyTo (int sourceIndex, char[] destination, int destinationIndex, int count)
667 // LAMESPEC: should I null-terminate?
670 if (destination == null)
671 throw new ArgumentNullException ();
673 if (sourceIndex < 0 || destinationIndex < 0 || count < 0)
674 throw new ArgumentOutOfRangeException ();
676 if (sourceIndex + count > this.length)
677 throw new ArgumentOutOfRangeException ();
679 if (destinationIndex + count > destination.Length)
680 throw new ArgumentOutOfRangeException ();
682 for (i = 0; i < count; i++)
683 destination[destinationIndex + i] = this.c_str[sourceIndex + i];
686 public bool EndsWith (string value)
688 bool endswith = true;
692 throw new ArgumentNullException ();
694 start = this.length - value.length;
698 for (i = start; i < this.length && endswith; i++)
699 endswith = this.c_str[i] == value.c_str[i - start];
704 public override bool Equals (object obj)
706 if (!(obj is String))
709 return this == (String) obj;
712 public bool Equals (string value)
714 return this == value;
717 public static bool Equals (string a, string b)
723 public static string Format (string format, object arg0)
725 // FIXME: implement me
726 return format+arg0.ToString();
730 public static string Format (string format, params object[] args)
732 // FIXME: implement me
733 Console.WriteLine (args[0].ToString());
734 if (args.Length == 1)
735 return format+args[0].ToString();
736 if (args.Length == 2)
737 return format+args[0].ToString()+args[1].ToString();
738 if (args.Length == 3)
739 return format+args[0].ToString()+args[1].ToString()+args[2].ToString();
740 if (args.Length == 4)
741 return format+args[0].ToString()+args[1].ToString()+args[2].ToString()+args[3].ToString();
742 Console.WriteLine ("String.Format with args: "+args.Length.ToString());
747 public static string Format (IFormatProvider provider, string format, params object[] args)
749 // FIXME: implement me
754 public static string Format (string format, object arg0, object arg1)
756 // FIXME: implement me
757 return format+arg0.ToString()+arg1.ToString();
761 public static string Format (string format, object arg0, object arg1, object arg2)
763 // FIXME: implement me
764 return format+arg0.ToString()+arg1.ToString()+arg2.ToString();
767 public CharEnumerator GetEnumerator ()
769 return new CharEnumerator (this);
772 IEnumerator IEnumerable.GetEnumerator ()
774 return new CharEnumerator (this);
777 public override int GetHashCode ()
781 for (i = 0; i < length; ++i)
782 h = (h << 5) - h + c_str [i];
786 public TypeCode GetTypeCode ()
788 return TypeCode.String;
791 public int IndexOf (char value)
793 return IndexOf (value, 0, this.length);
796 public int IndexOf (string value)
798 return IndexOf (value, 0, this.length);
801 public int IndexOf (char value, int startIndex)
803 return IndexOf (value, startIndex, this.length - startIndex);
806 public int IndexOf (string value, int startIndex)
808 return IndexOf (value, startIndex, this.length - startIndex);
811 public int IndexOf (char value, int startIndex, int count)
815 if (startIndex < 0 || count < 0 || startIndex + count > this.length)
816 throw new ArgumentOutOfRangeException ();
818 for (i = startIndex; i - startIndex < count; i++)
819 if (this.c_str[i] == value)
825 public int IndexOf (string value, int startIndex, int count)
828 throw new ArgumentNullException ();
830 if (startIndex < 0 || count < 0 || startIndex + count > this.length)
831 throw new ArgumentOutOfRangeException ();
833 return BoyerMoore (this.c_str, value, startIndex, count);
836 for (i = startIndex; i - startIndex + value.Length <= count; ) {
837 if (this.c_str[i] == value[0]) {
841 for (j = 1; equal && j < value.Length; j++) {
842 equal = this.c_str[i + j] == value[j];
843 if (this.c_str[i + j] == value[0] && nexti == 0)
862 public int IndexOfAny (char[] values)
864 return IndexOfAny (values, 0, this.length);
867 public int IndexOfAny (char[] values, int startIndex)
869 return IndexOfAny (values, startIndex, this.length - startIndex);
872 public int IndexOfAny (char[] values, int startIndex, int count)
875 throw new ArgumentNullException ();
877 if (startIndex < 0 || count < 0 || startIndex + count > this.length)
878 throw new ArgumentOutOfRangeException ();
880 for (int i = startIndex; i < startIndex + count; i++) {
881 for (int j = 0; j < strlen (values); j++) {
882 if (this.c_str[i] == values[j])
890 public string Insert (int startIndex, string value)
896 throw new ArgumentNullException ();
898 if (startIndex < 0 || startIndex > this.length)
899 throw new ArgumentOutOfRangeException ();
901 String res = new String (value.length + this.length);
904 for (i = 0; i < startIndex; i++)
905 str[i] = this.c_str[i];
906 for (j = 0; j < value.length; j++)
907 str[i + j] = value.c_str[j];
908 for ( ; i < this.length; i++)
909 str[i + j] = this.c_str[i];
914 [MethodImplAttribute(MethodImplOptions.InternalCall)]
915 public extern static string Intern (string str);
917 [MethodImplAttribute(MethodImplOptions.InternalCall)]
918 public extern static string IsInterned (string str);
920 public static string Join (string separator, string[] value)
922 return Join (separator, value, 0, value.Length);
925 public static string Join (string separator, string[] value, int startIndex, int count)
927 // LAMESPEC: msdn doesn't specify what happens when separator is null
931 if (separator == null || value == null)
932 throw new ArgumentNullException ();
934 if (startIndex + count > value.Length)
935 throw new ArgumentOutOfRangeException ();
938 for (i = startIndex, used = 0; used < count; i++, used++) {
940 len += separator.length;
942 len += value[i].length;
945 // We have no elements to join?
949 String res = new String (len);
952 for (i = 0; i < value[startIndex].length; i++)
953 str[i] = value[startIndex][i];
956 for (j = startIndex + 1; used < count; j++, used++) {
959 for (k = 0; k < separator.length; k++)
960 str[i++] = separator.c_str[k];
961 for (k = 0; k < value[j].length; k++)
962 str[i++] = value[j].c_str[k];
968 public int LastIndexOf (char value)
974 for (; i >= 0; i--) {
975 if (this.c_str[i] == value)
982 public int LastIndexOf (string value)
984 return LastIndexOf (value, this.length - 1, this.length);
987 public int LastIndexOf (char value, int startIndex)
989 if (startIndex < 0 || startIndex >= this.length)
990 throw new ArgumentOutOfRangeException ();
992 for (int i = startIndex; i >= 0; i--) {
993 if (this.c_str[i] == value)
1000 public int LastIndexOf (string value, int startIndex)
1002 return LastIndexOf (value, startIndex, startIndex + 1);
1005 public int LastIndexOf (char value, int startIndex, int count)
1007 if (startIndex < 0 || count < 0)
1008 throw new ArgumentOutOfRangeException ();
1010 if (startIndex >= this.length || startIndex - count + 1 < 0)
1011 throw new ArgumentOutOfRangeException ();
1013 for (int i = startIndex; i > startIndex - count; i--) {
1014 if (this.c_str[i] == value)
1021 public int LastIndexOf (string value, int startIndex, int count)
1023 // LAMESPEC: currently I'm using startIndex as the 0-position in the comparison,
1024 // but maybe it's the end-position in MS's implementation?
1025 // msdn is unclear on this point. I think this is correct though.
1029 throw new ArgumentNullException ();
1031 if (startIndex < 0 || startIndex >= this.length)
1032 throw new ArgumentOutOfRangeException ();
1034 if (count < 0 || startIndex - count + 1 < 0)
1035 throw new ArgumentOutOfRangeException ();
1037 if (value == String.Empty)
1040 if (startIndex + value.length > this.length) {
1041 /* just a little optimization */
1044 start = this.length - value.length;
1045 count -= startIndex - start;
1049 // FIXME: use a reversed-unicode-safe-Boyer-Moore?
1050 len = value.length - 1;
1051 for (i = startIndex; i > startIndex - count; i--) {
1052 if (this.c_str[i + len] == value.c_str[len]) {
1056 for (j = len - 1; equal && j >= 0; j--)
1057 equal = this.c_str[i + j] == value.c_str[j];
1067 public int LastIndexOfAny (char[] values)
1069 return LastIndexOfAny (values, this.length - 1, this.length);
1072 public int LastIndexOfAny (char[] values, int startIndex)
1074 return LastIndexOfAny (values, startIndex, startIndex + 1);
1077 public int LastIndexOfAny (char[] values, int startIndex, int count)
1082 throw new ArgumentNullException ();
1084 if (startIndex < 0 || count < 0 || startIndex - count + 1 < 0)
1085 throw new ArgumentOutOfRangeException ();
1087 for (i = startIndex; i > startIndex - count; i--) {
1088 for (int j = 0; j < strlen (values); j++) {
1089 if (this.c_str[i] == values[j])
1097 public string PadLeft (int totalWidth)
1099 return PadLeft (totalWidth, ' ');
1102 public string PadLeft (int totalWidth, char padChar)
1108 throw new ArgumentException ();
1110 str = new char [totalWidth > this.length ? totalWidth : this.length];
1111 for (i = 0; i < totalWidth - this.length; i++)
1114 for (j = 0; j < this.length; i++, j++)
1115 str[i] = this.c_str[j];
1117 return new String (str);
1120 public string PadRight (int totalWidth)
1122 return PadRight (totalWidth, ' ');
1125 public string PadRight (int totalWidth, char padChar)
1131 throw new ArgumentException ();
1133 str = new char [totalWidth > this.length ? totalWidth : this.length];
1134 for (i = 0; i < this.length; i++)
1135 str[i] = this.c_str[i];
1137 for ( ; i < str.Length; i++)
1140 return new String (str);
1143 public string Remove (int startIndex, int count)
1148 if (startIndex < 0 || count < 0 || startIndex + count > this.length)
1149 throw new ArgumentOutOfRangeException ();
1151 len = this.length - count;
1153 return String.Empty;
1155 String res = new String (len);
1157 for (i = 0; i < startIndex; i++)
1158 str[i] = this.c_str[i];
1159 for (j = i + count; j < this.length; j++)
1160 str[i++] = this.c_str[j];
1165 public string Replace (char oldChar, char newChar)
1170 String res = new String (length);
1172 for (i = 0; i < this.length; i++) {
1173 if (this.c_str[i] == oldChar)
1176 str[i] = this.c_str[i];
1182 public string Replace (string oldValue, string newValue)
1184 // LAMESPEC: msdn doesn't specify what to do if either args is null
1185 int index, len, i, j;
1188 if (oldValue == null || newValue == null)
1189 throw new ArgumentNullException ();
1191 // Use IndexOf in case I later rewrite it to use Boyer-Moore
1192 index = IndexOf (oldValue, 0);
1194 // This is the easy one ;-)
1195 return Substring (0, this.length);
1198 len = this.length - oldValue.length + newValue.length;
1200 return String.Empty;
1202 String res = new String (len);
1204 for (i = 0; i < index; i++)
1205 str[i] = this.c_str[i];
1206 for (j = 0; j < newValue.length; j++)
1207 str[i++] = newValue[j];
1208 for (j = index + oldValue.length; j < this.length; j++)
1209 str[i++] = this.c_str[j];
1214 private int splitme (char[] separators, int startIndex)
1216 /* this is basically a customized IndexOfAny() for the Split() methods */
1217 for (int i = startIndex; i < this.length; i++) {
1218 if (separators != null) {
1219 foreach (char sep in separators) {
1220 if (this.c_str[i] == sep)
1221 return i - startIndex;
1223 } else if (is_lwsp (this.c_str[i])) {
1224 return i - startIndex;
1231 public string[] Split (params char[] separator)
1235 * @separator: delimiting chars or null to split on whtspc
1237 * Returns: 1. An array consisting of a single
1238 * element (@this) if none of the delimiting
1239 * chars appear in @this. 2. An array of
1240 * substrings which are delimited by one of
1241 * the separator chars. 3. An array of
1242 * substrings separated by whitespace if
1243 * @separator is null. The Empty string should
1244 * be returned wherever 2 delimiting chars are
1247 // FIXME: would using a Queue be better?
1252 list = new ArrayList ();
1253 for (index = 0, len = 0; index < this.length; index += len + 1) {
1254 len = splitme (separator, index);
1255 len = len > -1 ? len : this.length - index;
1257 list.Add (String.Empty);
1262 str = new char [len];
1263 for (i = 0; i < len; i++)
1264 str[i] = this.c_str[index + i];
1266 list.Add (new String (str));
1270 strings = new string [list.Count];
1271 if (list.Count == 1) {
1272 /* special case for an array holding @this */
1275 for (index = 0; index < list.Count; index++)
1276 strings[index] = (string) list[index];
1282 public string[] Split (char[] separator, int maxCount)
1284 // FIXME: what to do if maxCount <= 0?
1285 // FIXME: would using Queue be better than ArrayList?
1288 int index, len, used;
1291 list = new ArrayList ();
1292 for (index = 0, len = 0; index < this.length && used < maxCount; index += len + 1) {
1293 len = splitme (separator, index);
1294 len = len > -1 ? len : this.length - index;
1296 list.Add (String.Empty);
1301 str = new char [len];
1302 for (i = 0; i < len; i++)
1303 str[i] = this.c_str[index + i];
1305 list.Add (new String (str));
1310 /* fit the remaining chunk of the @this into it's own element */
1311 if (index != this.length) {
1315 str = new char [this.length - index];
1316 for (i = index; i < this.length; i++)
1317 str[i - index] = this.c_str[i];
1319 list.Add (new String (str));
1322 strings = new string [list.Count];
1323 if (list.Count == 1) {
1324 /* special case for an array holding @this */
1327 for (index = 0; index < list.Count; index++)
1328 strings[index] = (string) list[index];
1334 public bool StartsWith (string value)
1336 bool startswith = true;
1340 throw new ArgumentNullException ();
1342 if (value.length > this.length)
1345 for (i = 0; i < value.length && startswith; i++)
1346 startswith = startswith && value.c_str[i] == this.c_str[i];
1351 public string Substring (int startIndex)
1356 if (startIndex < 0 || startIndex > this.length)
1357 throw new ArgumentOutOfRangeException ();
1359 len = this.length - startIndex;
1361 return String.Empty;
1362 String res = new String (len);
1364 for (i = startIndex; i < this.length; i++)
1365 str[i - startIndex] = this.c_str[i];
1370 public string Substring (int startIndex, int length)
1375 if (startIndex < 0 || length < 0 || startIndex + length > this.length)
1376 throw new ArgumentOutOfRangeException ();
1379 return String.Empty;
1381 String res = new String (length);
1383 for (i = startIndex; i < startIndex + length; i++)
1384 str[i - startIndex] = this.c_str[i];
1390 public bool ToBoolean (IFormatProvider provider)
1392 // FIXME: implement me
1393 throw new NotImplementedException ();
1397 public byte ToByte (IFormatProvider provider)
1399 // FIXME: implement me
1400 throw new NotImplementedException ();
1404 public char ToChar (IFormatProvider provider)
1406 // FIXME: implement me
1407 throw new NotImplementedException ();
1410 public char[] ToCharArray ()
1412 return ToCharArray (0, this.length);
1415 public char[] ToCharArray (int startIndex, int length)
1420 if (startIndex < 0 || length < 0 || startIndex + length > this.length)
1421 throw new ArgumentOutOfRangeException ();
1423 chars = new char [length];
1424 for (i = startIndex; i < length; i++)
1425 chars[i - startIndex] = this.c_str[i];
1431 public DateTime ToDateTime (IFormatProvider provider)
1433 // FIXME: implement me
1434 // return new DateTime (0);
1435 throw new NotImplementedException ();
1439 public decimal ToDecimal (IFormatProvider provider)
1441 // FIXME: implement me
1442 throw new NotImplementedException ();
1446 public double ToDouble (IFormatProvider provider)
1448 // FIXME: implement me
1449 throw new NotImplementedException ();
1453 public short ToInt16 (IFormatProvider provider)
1455 // FIXME: implement me
1456 throw new NotImplementedException ();
1460 public int ToInt32 (IFormatProvider provider)
1462 // FIXME: implement me
1463 throw new NotImplementedException ();
1467 public long ToInt64 (IFormatProvider provider)
1469 // FIXME: implement me
1470 throw new NotImplementedException ();
1473 public string ToLower ()
1478 String res = new String (length);
1480 for (i = 0; i < this.length; i++)
1481 str[i] = Char.ToLower (this.c_str[i]);
1487 public string ToLower (CultureInfo culture)
1489 // FIXME: implement me
1490 throw new NotImplementedException ();
1494 [CLSCompliant(false)][MonoTODO]
1495 public sbyte ToSByte (IFormatProvider provider)
1497 // FIXME: implement me
1498 throw new NotImplementedException ();
1502 public float ToSingle (IFormatProvider provider)
1504 // FIXME: implement me
1505 throw new NotImplementedException ();
1508 public override string ToString ()
1514 public string ToString (IFormatProvider format)
1516 // FIXME: implement me
1517 throw new NotImplementedException ();
1521 public object ToType (Type conversionType, IFormatProvider provider)
1523 // FIXME: implement me
1524 throw new NotImplementedException ();
1527 [CLSCompliant(false)][MonoTODO]
1528 public ushort ToUInt16 (IFormatProvider provider)
1530 // FIXME: implement me
1531 throw new NotImplementedException ();
1534 [CLSCompliant(false)][MonoTODO]
1535 public uint ToUInt32 (IFormatProvider provider)
1537 // FIXME: implement me
1538 throw new NotImplementedException ();
1541 [CLSCompliant(false)][MonoTODO]
1542 public ulong ToUInt64 (IFormatProvider provider)
1544 // FIXME: implement me
1545 throw new NotImplementedException ();
1548 public string ToUpper ()
1553 String res = new String (length);
1555 for (i = 0; i < this.length; i++)
1556 str[i] = Char.ToUpper (this.c_str[i]);
1562 public string ToUpper (CultureInfo culture)
1564 // FIXME: implement me
1565 throw new NotImplementedException ();
1568 public string Trim ()
1573 public string Trim (params char[] trimChars)
1579 for (begin = 0; matches && begin < this.length; begin++) {
1580 if (trimChars != null) {
1582 foreach (char c in trimChars) {
1583 matches = this.c_str[begin] == c;
1588 matches = is_lwsp (this.c_str[begin]);
1593 for (end = this.length - 1; matches && end > begin; end--) {
1594 if (trimChars != null) {
1596 foreach (char c in trimChars) {
1597 matches = this.c_str[end] == c;
1602 matches = is_lwsp (this.c_str[end]);
1607 return String.Empty;
1609 return Substring (begin, end - begin);
1612 public string TrimEnd (params char[] trimChars)
1614 bool matches = true;
1617 for (end = this.length; end > 0; end--) {
1618 if (trimChars != null) {
1620 foreach (char c in trimChars) {
1621 matches = this.c_str[end] == c;
1626 matches = is_lwsp (this.c_str[end]);
1631 return String.Empty;
1633 return Substring (0, end);
1636 public string TrimStart (params char[] trimChars)
1638 bool matches = true;
1641 for (begin = 0; matches && begin < this.length; begin++) {
1642 if (trimChars != null) {
1644 foreach (char c in trimChars) {
1645 matches = this.c_str[begin] == c;
1650 matches = is_lwsp (this.c_str[begin]);
1654 if (begin == this.length)
1655 return String.Empty;
1657 return Substring (begin, this.length - begin);
1661 public static bool operator ==(string a, string b)
1663 if ((object)a == null) {
1664 if ((object)b == null)
1668 if ((object)b == null)
1671 if (a.length != b.length)
1675 for (int i = 0; i < l; i++)
1676 if (a.c_str[i] != b.c_str[i])
1682 public static bool operator !=(string a, string b)