1 // -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
6 // Jeffrey Stedfast (fejj@ximian.com)
7 // Dan Lewis (dihlewis@yahoo.co.uk)
9 // (C) 2001 Ximian, Inc. http://www.ximian.com
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.
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 :-)
20 // FIXME: The ToLower(), ToUpper(), and Compare(..., bool ignoreCase) methods
21 // need to be made unicode aware.
23 // FIXME: when you have a char carr[], does carr.Length include the terminating null char?
27 using System.Collections;
28 using System.Globalization;
29 using System.Runtime.CompilerServices;
33 //[DefaultMemberName("Chars")]
35 public sealed class String : IComparable, ICloneable, IConvertible, IEnumerable {
36 public static readonly string Empty = "";
42 internal String (int storage)
45 throw new ArgumentOutOfRangeException ();
47 c_str = new char [storage];
51 unsafe public String (char *value)
55 // FIXME: can I do value.Length here?
59 for (i = 0; *(value + i) != '\0'; i++);
63 this.c_str = new char [this.length + 1];
64 for (i = 0; i < this.length; i++)
65 this.c_str[i] = *(value + i);
68 public String (char[] value)
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];
80 unsafe public String (sbyte *value)
82 // FIXME: consider unicode?
85 // FIXME: can I do value.Length here? */
89 for (i = 0; *(value + i) != '\0'; i++);
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);
98 public String (char c, int count)
103 this.c_str = new char [count + 1];
104 for (i = 0; i < count; i++)
108 [CLSCompliant(false)]
109 unsafe public String (char *value, int startIndex, int length)
113 if (value == null && startIndex != 0 && length != 0)
114 throw new ArgumentNullException ();
116 if (startIndex < 0 || length < 0)
117 throw new ArgumentOutOfRangeException ();
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);
125 public String (char[] value, int startIndex, int length)
129 if (value == null && startIndex != 0 && length != 0)
130 throw new ArgumentNullException ();
132 if (startIndex < 0 || length < 0)
133 throw new ArgumentOutOfRangeException ();
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];
141 [CLSCompliant(false)]
142 unsafe public String (sbyte *value, int startIndex, int length)
144 // FIXME: consider unicode?
147 if (value == null && startIndex != 0 && length != 0)
148 throw new ArgumentNullException ();
150 if (startIndex < 0 || length < 0)
151 throw new ArgumentOutOfRangeException ();
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);
159 [CLSCompliant(false)][MonoTODO]
160 unsafe public String (sbyte *value, int startIndex, int length, Encoding enc)
162 // FIXME: implement me
165 // FIXME: is there anything we need to do here?
180 [IndexerName("Chars")]
181 public char this [int index] {
183 if (index >= this.length)
184 throw new ArgumentOutOfRangeException ();
186 return this.c_str[index];
190 // Private helper methods
191 private static int strlen (char[] str)
193 // FIXME: if str.Length includes terminating null char, then return (str.Length - 1)
198 private static char tolowerordinal (char c)
200 // FIXME: implement me
204 private static bool is_lwsp (char c)
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')
214 private static int BoyerMoore (char[] haystack, string needle, int startIndex, int count)
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;
220 if (haystack == null || needle == null)
221 throw new ArgumentNullException ();
223 /* if the search buffer is shorter than the pattern buffer, we can't match */
224 if (count < needle.length)
227 /* return an instant match if the pattern is 0-length */
228 if (needle.length == 0)
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 */
235 /* init the skip table with the pattern length */
237 for (i = 0; i < 65536; i++)
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;
247 while (count >= needle.length) {
248 hc = h + needle.length - 1; /* set the haystack compare pointer */
249 nc = ne; /* set the needle compare pointer */
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])
256 if (needle[nc] != haystack[hc]) {
257 n = skiptable[(int) haystack[hc]] - i;
269 public object Clone ()
271 // FIXME: implement me
275 const int StringCompareModeDirect = 0;
276 const int StringCompareModeCaseInsensitive = 1;
277 const int StringCompareModeOrdinal = 2;
279 internal static int _CompareGetLength (string strA, string strB)
281 if ((strA == null) || (strB == null))
284 return Math.Max (strA.Length, strB.Length);
287 internal static int _CompareChar (char chrA, char chrB, CultureInfo culture,
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
299 if (Char.IsUpper (chrA) && Char.IsLower (chrB))
301 else if (Char.IsLower (chrA) && Char.IsUpper (chrB))
303 result = (int) (chrA - chrB);
305 case StringCompareModeCaseInsensitive:
306 result = (int) (Char.ToLower (chrA) - Char.ToLower (chrB));
308 case StringCompareModeOrdinal:
309 result = (int) (tolowerordinal (chrA) - tolowerordinal (chrB));
322 internal static int _Compare (string strA, int indexA, string strB, int indexB,
323 int length, CultureInfo culture,
329 /* When will the hurting stop!?!? */
335 } else if (strB == null)
338 if (length < 0 || indexA < 0 || indexB < 0)
339 throw new ArgumentOutOfRangeException ();
341 if (indexA > strA.Length || indexB > strB.Length)
342 throw new ArgumentOutOfRangeException ();
344 // FIXME: Implement culture
346 throw new NotImplementedException ();
348 for (i = 0; i < length - 1; i++) {
349 if ((indexA+i >= strA.Length) || (indexB+i >= strB.Length))
352 if (_CompareChar (strA[indexA+i], strB[indexB+i], culture, mode) != 0)
356 if (indexA+i >= strA.Length) {
357 if (indexB+i >= strB.Length)
361 } else if (indexB+i >= strB.Length)
364 return _CompareChar (strA[indexA+i], strB[indexB+i], culture, mode);
368 public static int Compare (string strA, string strB)
370 return Compare (strA, strB, false);
373 public static int Compare (string strA, string strB, bool ignoreCase)
375 return Compare (strA, strB, ignoreCase, null);
378 public static int Compare (string strA, string strB, bool ignoreCase, CultureInfo culture)
380 return Compare (strA, 0, strB, 0,
381 _CompareGetLength (strA, strB),
382 ignoreCase, culture);
385 public static int Compare (string strA, int indexA, string strB, int indexB, int length)
387 return Compare (strA, indexA, strB, indexB, length, false);
390 public static int Compare (string strA, int indexA, string strB, int indexB,
391 int length, bool ignoreCase)
393 return Compare (strA, indexA, strB, indexB, length, ignoreCase, null);
396 public static int Compare (string strA, int indexA, string strB, int indexB,
397 int length, bool ignoreCase, CultureInfo culture)
401 mode = ignoreCase ? StringCompareModeCaseInsensitive :
402 StringCompareModeDirect;
404 return _Compare (strA, indexA, strB, indexB, length, culture, mode);
407 public static int CompareOrdinal (string strA, string strB)
409 return CompareOrdinal (strA, 0, strB, 0, _CompareGetLength (strA, strB));
412 public static int CompareOrdinal (string strA, int indexA, string strB, int indexB,
415 return _Compare (strA, indexA, strB, indexB, length, null,
416 StringCompareModeOrdinal);
419 public int CompareTo (object obj)
421 return Compare (this, obj == null ? null : obj.ToString ());
424 public int CompareTo (string str)
426 return Compare (this, str);
429 public static string Concat (object arg)
431 return arg != null ? arg.ToString () : String.Empty;
434 public static string Concat (params object[] args)
441 throw new ArgumentNullException ();
443 strings = new string [args.Length];
446 foreach (object arg in args) {
447 /* use Empty for each null argument */
449 strings[i] = String.Empty;
451 strings[i] = arg.ToString ();
452 len += strings[i].length;
459 String res = new String (len);
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];
469 public static string Concat (params string[] values)
475 throw new ArgumentNullException ();
478 foreach (string value in values)
479 len += value != null ? value.Length : 0;
484 String res = new String (len);
487 foreach (string value in values) {
491 for (int j = 0; j < value.length; j++)
492 str[i++] = value.c_str[j];
498 public static string Concat (object arg0, object arg1)
500 string str0 = arg0 != null ? arg0.ToString () : String.Empty;
501 string str1 = arg1 != null ? arg1.ToString () : String.Empty;
503 return Concat (str0, str1);
506 public static string Concat (string str0, string str1)
516 len = str0.length + str1.length;
520 String res = new String (len);
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];
531 public static string Concat (object arg0, object arg1, object arg2)
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;
537 return Concat (str0, str1, str2);
540 public static string Concat (string str0, string str1, string str2)
552 len = str0.length + str1.length + str2.length;
556 String res = new String (len);
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];
569 public static string Concat (string str0, string str1, string str2, string str3)
583 len = str0.length + str1.length + str2.length + str3.length;
586 String res = new String (len);
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];
601 public static string Copy (string str)
603 // FIXME: how do I *copy* a string if I can only have 1 of each?
605 throw new ArgumentNullException ();
610 public void CopyTo (int sourceIndex, char[] destination, int destinationIndex, int count)
612 // LAMESPEC: should I null-terminate?
615 if (destination == null)
616 throw new ArgumentNullException ();
618 if (sourceIndex < 0 || destinationIndex < 0 || count < 0)
619 throw new ArgumentOutOfRangeException ();
621 if (sourceIndex + count > this.length)
622 throw new ArgumentOutOfRangeException ();
624 if (destinationIndex + count > destination.Length)
625 throw new ArgumentOutOfRangeException ();
627 for (i = 0; i < count; i++)
628 destination[destinationIndex + i] = this.c_str[sourceIndex + i];
631 public bool EndsWith (string value)
633 bool endswith = true;
637 throw new ArgumentNullException ();
639 start = this.length - value.length;
643 for (i = start; i < this.length && endswith; i++)
644 endswith = this.c_str[i] == value.c_str[i - start];
649 public override bool Equals (object obj)
651 if (!(obj is String))
654 return this == (String) obj;
657 public bool Equals (string value)
659 return this == value;
662 public static bool Equals (string a, string b)
667 public static string Format (string format, object arg0) {
668 return Format (null, format, new object[] { arg0 });
671 public static string Format (string format, object arg0, object arg1) {
672 return Format (null, format, new object[] { arg0, arg1 });
675 public static string Format (string format, object arg0, object arg1, object arg2) {
676 return Format (null, format, new object[] { arg0, arg1, arg2 });
679 public static string Format (string format, params object[] args) {
680 return Format (null, format, args);
683 public static string Format (IFormatProvider provider, string format, params object[] args) {
684 if (format == null || args == null)
685 throw new ArgumentNullException ();
687 StringBuilder result = new StringBuilder ();
691 while (ptr < format.Length) {
692 char c = format[ptr ++];
695 result.Append (format, start, ptr - start - 1);
697 // check for escaped open bracket
699 if (format[ptr] == '{') {
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.");
716 object arg = args[n];
721 else if (arg is IFormattable)
722 str = ((IFormattable)arg).ToString (arg_format, provider);
724 str = arg.ToString ();
726 // pad formatted string and append to result
728 if (width > str.Length) {
729 string pad = new String (' ', width - str.Length);
745 else if (c == '}' && format[ptr] == '}') {
746 result.Append (format, start, ptr - start - 1);
751 if (start < format.Length)
752 result.Append (format.Substring (start));
754 return result.ToString ();
757 public CharEnumerator GetEnumerator ()
759 return new CharEnumerator (this);
762 IEnumerator IEnumerable.GetEnumerator ()
764 return new CharEnumerator (this);
767 [MethodImplAttribute(MethodImplOptions.InternalCall)]
768 public extern override int GetHashCode ();
770 public TypeCode GetTypeCode ()
772 return TypeCode.String;
775 public int IndexOf (char value)
777 return IndexOf (value, 0, this.length);
780 public int IndexOf (string value)
782 return IndexOf (value, 0, this.length);
785 public int IndexOf (char value, int startIndex)
787 return IndexOf (value, startIndex, this.length - startIndex);
790 public int IndexOf (string value, int startIndex)
792 return IndexOf (value, startIndex, this.length - startIndex);
795 public int IndexOf (char value, int startIndex, int count)
799 if (startIndex < 0 || count < 0 || startIndex + count > this.length)
800 throw new ArgumentOutOfRangeException ();
802 for (i = startIndex; i - startIndex < count; i++)
803 if (this.c_str[i] == value)
809 public int IndexOf (string value, int startIndex, int count)
812 throw new ArgumentNullException ();
814 if (startIndex < 0 || count < 0 || startIndex + count > this.length)
815 throw new ArgumentOutOfRangeException ();
818 return BoyerMoore (this.c_str, value, startIndex, count);
821 for (i = startIndex; i - startIndex + value.length <= count; ) {
822 if (this.c_str[i] == value.c_str [0]) {
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)
846 public int IndexOfAny (char[] values)
848 return IndexOfAny (values, 0, this.length);
851 public int IndexOfAny (char[] values, int startIndex)
853 return IndexOfAny (values, startIndex, this.length - startIndex);
856 [MethodImplAttribute(MethodImplOptions.InternalCall)]
857 internal extern int InternalIndexOfAny (char[] values, int startIndex, int count);
859 public int IndexOfAny (char[] values, int startIndex, int count)
862 throw new ArgumentNullException ();
864 if (startIndex < 0 || count < 0 || startIndex + count > this.length)
865 throw new ArgumentOutOfRangeException ();
867 return InternalIndexOfAny (values, startIndex, count);
870 public string Insert (int startIndex, string value)
876 throw new ArgumentNullException ();
878 if (startIndex < 0 || startIndex > this.length)
879 throw new ArgumentOutOfRangeException ();
881 String res = new String (value.length + this.length);
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];
894 [MethodImplAttribute(MethodImplOptions.InternalCall)]
895 internal extern static string _Intern (string str);
897 public static string Intern (string str)
900 throw new ArgumentNullException ();
902 return _Intern (str);
905 [MethodImplAttribute(MethodImplOptions.InternalCall)]
906 internal extern static string _IsInterned (string str);
908 public static string IsInterned (string str)
911 throw new ArgumentNullException ();
913 return _IsInterned (str);
916 public static string Join (string separator, string[] value)
919 throw new ArgumentNullException ();
921 return Join (separator, value, 0, value.Length);
924 public static string Join (string separator, string[] value, int startIndex, int count)
926 // LAMESPEC: msdn doesn't specify what happens when separator is null
930 if (separator == null || value == null)
931 throw new ArgumentNullException ();
933 if (startIndex + count > value.Length)
934 throw new ArgumentOutOfRangeException ();
937 for (i = startIndex, used = 0; used < count; i++, used++) {
939 len += separator.length;
941 len += value[i].length;
944 // We have no elements to join?
948 String res = new String (len);
951 for (i = 0; i < value[startIndex].length; i++)
952 str[i] = value[startIndex][i];
955 for (j = startIndex + 1; used < count; j++, used++) {
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];
967 public int LastIndexOf (char value)
973 for (; i >= 0; i--) {
974 if (this.c_str[i] == value)
981 public int LastIndexOf (string value)
984 throw new ArgumentNullException ();
985 if (value.length == 0)
987 if (this.length == 0)
990 return LastIndexOf (value, this.length - 1, this.length);
993 public int LastIndexOf (char value, int startIndex)
995 if (startIndex < 0 || startIndex >= this.length)
996 throw new ArgumentOutOfRangeException ();
998 for (int i = startIndex; i >= 0; i--) {
999 if (this.c_str[i] == value)
1006 public int LastIndexOf (string value, int startIndex)
1008 return LastIndexOf (value, startIndex, startIndex + 1);
1011 public int LastIndexOf (char value, int startIndex, int count)
1013 if (startIndex < 0 || count < 0)
1014 throw new ArgumentOutOfRangeException ();
1016 if (startIndex >= this.length || startIndex - count + 1 < 0)
1017 throw new ArgumentOutOfRangeException ();
1019 for (int i = startIndex; i > startIndex - count; i--) {
1020 if (this.c_str[i] == value)
1027 public int LastIndexOf (string value, int startIndex, int count)
1029 // startIndex points to the end of value, ie. we're searching backwards.
1033 throw new ArgumentNullException ();
1035 if (startIndex < 0 || startIndex > this.length)
1036 throw new ArgumentOutOfRangeException ();
1038 if (count < 0 || startIndex - count + 1 < 0)
1039 throw new ArgumentOutOfRangeException ();
1041 if (value.length > startIndex)
1044 if (value == String.Empty)
1047 if (startIndex == this.length)
1050 // FIXME: use a reversed-unicode-safe-Boyer-Moore?
1051 len = value.length - 1;
1052 for (i = startIndex; i > startIndex - count; i--) {
1054 if (this.c_str[i] == value.c_str[len]) {
1058 for (j = 0; equal && j < len; j++)
1059 equal = this.c_str[i - j] == value.c_str[len - j];
1069 public int LastIndexOfAny (char[] values)
1071 return LastIndexOfAny (values, this.length - 1, this.length);
1074 public int LastIndexOfAny (char[] values, int startIndex)
1076 return LastIndexOfAny (values, startIndex, startIndex + 1);
1079 public int LastIndexOfAny (char[] values, int startIndex, int count)
1084 throw new ArgumentNullException ();
1086 if (startIndex < 0 || count < 0 || startIndex - count + 1 < 0)
1087 throw new ArgumentOutOfRangeException ();
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])
1099 public string PadLeft (int totalWidth)
1101 return PadLeft (totalWidth, ' ');
1104 public string PadLeft (int totalWidth, char padChar)
1110 throw new ArgumentException ();
1112 str = new char [totalWidth > this.length ? totalWidth : this.length];
1113 for (i = 0; i < totalWidth - this.length; i++)
1116 for (j = 0; j < this.length; i++, j++)
1117 str[i] = this.c_str[j];
1119 return new String (str);
1122 public string PadRight (int totalWidth)
1124 return PadRight (totalWidth, ' ');
1127 public string PadRight (int totalWidth, char padChar)
1133 throw new ArgumentException ();
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];
1139 for ( ; i < str.Length; i++)
1142 return new String (str);
1145 public string Remove (int startIndex, int count)
1150 if (startIndex < 0 || count < 0 || startIndex + count > this.length)
1151 throw new ArgumentOutOfRangeException ();
1153 len = this.length - count;
1155 return String.Empty;
1157 String res = new String (len);
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];
1167 public string Replace (char oldChar, char newChar)
1172 String res = new String (length);
1174 for (i = 0; i < this.length; i++) {
1175 if (this.c_str[i] == oldChar)
1178 str[i] = this.c_str[i];
1184 public string Replace (string oldValue, string newValue)
1186 // If newValue is null, oldValue is removed.
1187 int index, len, i, j, newlen;
1191 if (oldValue == null)
1192 throw new ArgumentNullException ();
1194 thestring = Substring (0, this.length);
1197 // Runs until all occurences of oldValue have been replaced.
1199 // Use IndexOf in case I later rewrite it to use Boyer-Moore
1200 index = thestring.IndexOf (oldValue, index);
1205 newlen = (newValue == null) ? 0 : newValue.length;
1206 len = thestring.length - oldValue.length + newlen;
1209 return String.Empty;
1211 String res = new String (len);
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];
1220 // Increment index, we're already done replacing until this index.
1226 private int splitme (char[] separators, int startIndex)
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;
1235 } else if (is_lwsp (this.c_str[i])) {
1236 return i - startIndex;
1243 public string[] Split (params char[] separator)
1247 * @separator: delimiting chars or null to split on whtspc
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
1259 // FIXME: would using a Queue be better?
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;
1269 list.Add (String.Empty);
1274 str = new char [len];
1275 for (i = 0; i < len; i++)
1276 str[i] = this.c_str[index + i];
1278 list.Add (new String (str));
1282 strings = new string [list.Count];
1283 if (list.Count == 1) {
1284 /* special case for an array holding @this */
1287 for (index = 0; index < list.Count; index++)
1288 strings[index] = (string) list[index];
1294 public string[] Split (char[] separator, int maxCount)
1296 // FIXME: would using Queue be better than ArrayList?
1299 int index, len, used;
1302 return new string[0];
1303 else if (maxCount < 0)
1304 throw new ArgumentOutOfRangeException ();
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;
1312 list.Add (String.Empty);
1317 str = new char [len];
1318 for (i = 0; i < len; i++)
1319 str[i] = this.c_str[index + i];
1321 list.Add (new String (str));
1326 /* fit the remaining chunk of the @this into it's own element */
1327 if (index <= this.length)
1332 str = new char [this.length - index];
1333 for (i = index; i < this.length; i++)
1334 str[i - index] = this.c_str[i];
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);
1341 list.Add (new String (str));
1344 strings = new string [list.Count];
1345 if (list.Count == 1) {
1346 /* special case for an array holding @this */
1349 for (index = 0; index < list.Count; index++)
1350 strings[index] = (string) list[index];
1356 public bool StartsWith (string value)
1358 bool startswith = true;
1362 throw new ArgumentNullException ();
1364 if (value.length > this.length)
1367 for (i = 0; i < value.length && startswith; i++)
1368 startswith = startswith && value.c_str[i] == this.c_str[i];
1373 public string Substring (int startIndex)
1378 if (startIndex < 0 || startIndex > this.length)
1379 throw new ArgumentOutOfRangeException ();
1381 len = this.length - startIndex;
1383 return String.Empty;
1384 String res = new String (len);
1386 for (i = startIndex; i < this.length; i++)
1387 str[i - startIndex] = this.c_str[i];
1392 public string Substring (int startIndex, int length)
1397 if (startIndex < 0 || length < 0 || startIndex + length > this.length)
1398 throw new ArgumentOutOfRangeException ();
1401 return String.Empty;
1403 String res = new String (length);
1406 for (i = startIndex; i < startIndex + length; i++)
1407 str[i - startIndex] = this.c_str[i];
1412 bool IConvertible.ToBoolean (IFormatProvider provider)
1414 return Convert.ToBoolean (this);
1417 byte IConvertible.ToByte (IFormatProvider provider)
1419 return Convert.ToByte (this);
1422 char IConvertible.ToChar (IFormatProvider provider)
1424 return Convert.ToChar (this);
1427 public char[] ToCharArray ()
1429 return ToCharArray (0, this.length);
1432 public char[] ToCharArray (int startIndex, int length)
1437 if (startIndex < 0 || length < 0 || startIndex + length > this.length)
1438 throw new ArgumentOutOfRangeException ();
1440 chars = new char [length];
1441 for (i = startIndex; i < length; i++)
1442 chars[i - startIndex] = this.c_str[i];
1447 DateTime IConvertible.ToDateTime (IFormatProvider provider)
1449 return Convert.ToDateTime (this);
1452 decimal IConvertible.ToDecimal (IFormatProvider provider)
1454 return Convert.ToDecimal (this);
1457 double IConvertible.ToDouble (IFormatProvider provider)
1459 return Convert.ToDouble (this);
1462 short IConvertible.ToInt16 (IFormatProvider provider)
1464 return Convert.ToInt16 (this);
1467 int IConvertible.ToInt32 (IFormatProvider provider)
1469 return Convert.ToInt32 (this);
1472 long IConvertible.ToInt64 (IFormatProvider provider)
1474 return Convert.ToInt64 (this);
1477 public string ToLower ()
1482 String res = new String (length);
1484 for (i = 0; i < this.length; i++)
1485 str[i] = Char.ToLower (this.c_str[i]);
1491 public string ToLower (CultureInfo culture)
1493 // FIXME: implement me
1494 throw new NotImplementedException ();
1498 [CLSCompliant(false)]
1499 sbyte IConvertible.ToSByte (IFormatProvider provider)
1501 return Convert.ToSByte (this);
1504 float IConvertible.ToSingle (IFormatProvider provider)
1506 return Convert.ToSingle (this);
1509 public override string ToString ()
1514 string IConvertible.ToString (IFormatProvider format)
1519 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
1521 return Convert.ToType (this, conversionType, provider);
1524 [CLSCompliant(false)]
1525 ushort IConvertible.ToUInt16 (IFormatProvider provider)
1527 return Convert.ToUInt16 (this);
1530 [CLSCompliant(false)]
1531 uint IConvertible.ToUInt32 (IFormatProvider provider)
1533 return Convert.ToUInt32 (this);
1536 [CLSCompliant(false)]
1537 ulong IConvertible.ToUInt64 (IFormatProvider provider)
1539 return Convert.ToUInt64 (this);
1542 public string ToUpper ()
1547 String res = new String (length);
1549 for (i = 0; i < this.length; i++)
1550 str[i] = Char.ToUpper (this.c_str[i]);
1556 public string ToUpper (CultureInfo culture)
1558 // FIXME: implement me
1559 throw new NotImplementedException ();
1562 public string Trim ()
1567 public string Trim (params char[] trimChars)
1570 bool matches = false;
1572 for (begin = 0; begin < this.length; begin++) {
1573 if (trimChars != null) {
1575 foreach (char c in trimChars) {
1576 matches = this.c_str[begin] == c;
1583 matches = is_lwsp (this.c_str[begin]);
1590 for (end = this.length - 1; end > begin; end--) {
1591 if (trimChars != null) {
1593 foreach (char c in trimChars) {
1594 matches = this.c_str[end] == c;
1601 matches = is_lwsp (this.c_str[end]);
1610 return String.Empty;
1612 return Substring (begin, end - begin);
1615 public string TrimEnd (params char[] trimChars)
1617 bool matches = true;
1620 for (end = this.length - 1; matches && end > 0; end--) {
1622 if (trimChars != null) {
1624 foreach (char c in trimChars) {
1625 matches = this.c_str[end] == c;
1630 matches = is_lwsp (this.c_str[end]);
1634 return Substring (0, end+1);
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 return Substring (begin, this.length - begin);
1664 return String.Empty;
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)
1696 private static void ParseFormatSpecifier (string str, ref int ptr, out int n, out int width, out bool left_align, out string format) {
1697 // parses format specifier of form:
1703 // N = argument number (non-negative integer)
1705 n = ParseDecimal (str, ref ptr);
1707 throw new FormatException ("Input string was not in correct format.");
1709 // M = width (non-negative integer)
1711 if (str[ptr] == ',') {
1712 left_align = (str[++ ptr] == '-');
1716 width = ParseDecimal (str, ref ptr);
1718 throw new FormatException ("Input string was not in correct format.");
1725 // F = argument format (string)
1727 if (str[ptr] == ':') {
1729 while (str[ptr] != '}')
1732 format = str.Substring (start, ptr - start);
1737 if (str[ptr ++] != '}')
1738 throw new FormatException ("Input string was not in correct format.");
1740 catch (IndexOutOfRangeException) {
1741 throw new FormatException ("Input string was not in correct format.");
1745 private static int ParseDecimal (string str, ref int ptr) {
1750 if (c < '0' || '9' < c)
1753 n = n * 10 + c - '0';