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")]
34 public sealed class String : IComparable, ICloneable, IConvertible, IEnumerable {
35 public static readonly string Empty = "";
41 internal String (int storage)
44 throw new ArgumentOutOfRangeException ();
46 c_str = new char [storage];
50 unsafe public String (char *value)
54 // FIXME: can I do value.Length here?
58 for (i = 0; *(value + i) != '\0'; i++);
62 this.c_str = new char [this.length + 1];
63 for (i = 0; i < this.length; i++)
64 this.c_str[i] = *(value + i);
67 public String (char[] value)
71 // FIXME: value.Length includes the terminating null char?
72 this.length = value != null ? strlen (value): 0;
73 this.c_str = new char [this.length + 1];
74 for (i = 0; i < this.length; i++)
75 this.c_str[i] = value[i];
79 unsafe public String (sbyte *value)
81 // FIXME: consider unicode?
84 // FIXME: can I do value.Length here? */
88 for (i = 0; *(value + i) != '\0'; i++);
92 this.c_str = new char [this.length + 1];
93 for (i = 0; i < this.length; i++)
94 this.c_str[i] = (char) *(value + i);
97 public String (char c, int count)
102 this.c_str = new char [count + 1];
103 for (i = 0; i < count; i++)
107 [CLSCompliant(false)]
108 unsafe public String (char *value, int startIndex, int length)
112 if (value == null && startIndex != 0 && length != 0)
113 throw new ArgumentNullException ();
115 if (startIndex < 0 || length < 0)
116 throw new ArgumentOutOfRangeException ();
118 this.length = length;
119 this.c_str = new char [length + 1];
120 for (i = 0; i < length; i++)
121 this.c_str[i] = *(value + startIndex + i);
124 public String (char[] value, int startIndex, int length)
128 if (value == null && startIndex != 0 && length != 0)
129 throw new ArgumentNullException ();
131 if (startIndex < 0 || length < 0)
132 throw new ArgumentOutOfRangeException ();
134 this.length = length;
135 this.c_str = new char [length + 1];
136 for (i = 0; i < length; i++)
137 this.c_str[i] = value[startIndex + i];
140 [CLSCompliant(false)]
141 unsafe public String (sbyte *value, int startIndex, int length)
143 // FIXME: consider unicode?
146 if (value == null && startIndex != 0 && length != 0)
147 throw new ArgumentNullException ();
149 if (startIndex < 0 || length < 0)
150 throw new ArgumentOutOfRangeException ();
152 this.length = length;
153 this.c_str = new char [length + 1];
154 for (i = 0; i < length; i++)
155 this.c_str[i] = (char) *(value + startIndex + i);
158 [CLSCompliant(false)][MonoTODO]
159 unsafe public String (sbyte *value, int startIndex, int length, Encoding enc)
161 // FIXME: implement me
166 // FIXME: is there anything we need to do here?
167 /*base.Finalize ();*/
177 [IndexerName("Chars")]
178 public char this [int index] {
180 if (index >= this.length)
181 throw new ArgumentOutOfRangeException ();
183 return this.c_str[index];
187 // Private helper methods
188 private static int strlen (char[] str)
190 // FIXME: if str.Length includes terminating null char, then return (str.Length - 1)
195 private static char tolowerordinal (char c)
197 // FIXME: implement me
201 private static bool is_lwsp (char c)
203 /* this comes from the msdn docs for String.Trim() */
204 if ((c >= '\x9' && c <= '\xD') || c == '\x20' || c == '\xA0' ||
205 (c >= '\x2000' && c <= '\x200B') || c == '\x3000' || c == '\xFEFF')
211 private static int BoyerMoore (char[] haystack, string needle, int startIndex, int count)
213 /* (hopefully) Unicode-safe Boyer-Moore implementation */
214 int[] skiptable = new int[65536]; /* our unicode-safe skip-table */
215 int h, n, he, ne, hc, nc, i;
217 if (haystack == null || needle == null)
218 throw new ArgumentNullException ();
220 /* if the search buffer is shorter than the pattern buffer, we can't match */
221 if (count < needle.length)
224 /* return an instant match if the pattern is 0-length */
225 if (needle.length == 0)
228 /* set a pointer at the end of each string */
229 ne = needle.length - 1; /* position of char before '\0' */
230 he = startIndex + count; /* position of last valid char */
232 /* init the skip table with the pattern length */
234 for (i = 0; i < 65536; i++)
237 /* set the skip value for the chars that *do* appear in the
238 * pattern buffer (needle) to the distance from the index to
239 * the end of the pattern buffer. */
240 for (nc = 0; nc < ne; nc++)
241 skiptable[(int) needle[nc]] = ne - nc;
244 while (count >= needle.length) {
245 hc = h + needle.length - 1; /* set the haystack compare pointer */
246 nc = ne; /* set the needle compare pointer */
248 /* work our way backwards until they don't match */
249 for (i = 0; nc > 0; nc--, hc--, i++)
250 if (needle[nc] != haystack[hc])
253 if (needle[nc] != haystack[hc]) {
254 n = skiptable[(int) haystack[hc]] - i;
266 public object Clone ()
268 // FIXME: implement me
272 internal enum _StringCompareMode {
274 CompareCaseInsensitive,
278 internal static int _CompareGetLength (string strA, string strB)
280 if ((strA == null) || (strB == null))
283 return Math.Max (strA.Length, strB.Length);
286 internal static int _CompareChar (char chrA, char chrB, CultureInfo culture,
287 _StringCompareMode mode)
292 case _StringCompareMode.CompareDirect:
293 // FIXME: We should do a culture based comparision here,
294 // but for the moment let's do it by hand.
295 // In the microsoft runtime, uppercase letters
296 // sort after lowercase letters in the default
298 if (Char.IsUpper (chrA) && Char.IsLower (chrB))
300 else if (Char.IsLower (chrA) && Char.IsUpper (chrB))
302 result = (int) (chrA - chrB);
304 case _StringCompareMode.CompareCaseInsensitive:
305 result = (int) (Char.ToLower (chrA) - Char.ToLower (chrB));
307 case _StringCompareMode.CompareOrdinal:
308 result = (int) (tolowerordinal (chrA) - tolowerordinal (chrB));
321 internal static int _Compare (string strA, int indexA, string strB, int indexB,
322 int length, CultureInfo culture,
323 _StringCompareMode mode)
328 /* When will the hurting stop!?!? */
334 } else if (strB == null)
337 if (length < 0 || indexA < 0 || indexB < 0)
338 throw new ArgumentOutOfRangeException ();
340 if (indexA > strA.Length || indexB > strB.Length)
341 throw new ArgumentOutOfRangeException ();
343 // FIXME: Implement culture
345 throw new NotImplementedException ();
347 for (i = 0; i < length - 1; i++) {
348 if ((indexA+i >= strA.Length) || (indexB+i >= strB.Length))
351 if (_CompareChar (strA[indexA+i], strB[indexB+i], culture, mode) != 0)
355 if (indexA+i >= strA.Length) {
356 if (indexB+i >= strB.Length)
360 } else if (indexB+i >= strB.Length)
363 return _CompareChar (strA[indexA+i], strB[indexB+i], culture, mode);
367 public static int Compare (string strA, string strB)
369 return Compare (strA, strB, false);
372 public static int Compare (string strA, string strB, bool ignoreCase)
374 return Compare (strA, strB, ignoreCase, null);
377 public static int Compare (string strA, string strB, bool ignoreCase, CultureInfo culture)
379 return Compare (strA, 0, strB, 0,
380 _CompareGetLength (strA, strB),
381 ignoreCase, culture);
384 public static int Compare (string strA, int indexA, string strB, int indexB, int length)
386 return Compare (strA, indexA, strB, indexB, length, false);
389 public static int Compare (string strA, int indexA, string strB, int indexB,
390 int length, bool ignoreCase)
392 return Compare (strA, indexA, strB, indexB, length, ignoreCase, null);
395 public static int Compare (string strA, int indexA, string strB, int indexB,
396 int length, bool ignoreCase, CultureInfo culture)
398 _StringCompareMode mode;
400 mode = ignoreCase ? _StringCompareMode.CompareCaseInsensitive :
401 _StringCompareMode.CompareDirect;
403 return _Compare (strA, indexA, strB, indexB, length, culture, mode);
406 public static int CompareOrdinal (string strA, string strB)
408 return CompareOrdinal (strA, 0, strB, 0, _CompareGetLength (strA, strB));
411 public static int CompareOrdinal (string strA, int indexA, string strB, int indexB,
414 return _Compare (strA, indexA, strB, indexB, length, null,
415 _StringCompareMode.CompareOrdinal);
418 public int CompareTo (object obj)
420 return Compare (this, obj == null ? null : obj.ToString ());
423 public int CompareTo (string str)
425 return Compare (this, str);
428 public static string Concat (object arg)
430 return arg != null ? arg.ToString () : String.Empty;
433 public static string Concat (params object[] args)
440 throw new ArgumentNullException ();
442 strings = new string [args.Length];
445 foreach (object arg in args) {
446 /* use Empty for each null argument */
448 strings[i] = String.Empty;
450 strings[i] = arg.ToString ();
451 len += strings[i].length;
458 String res = new String (len);
461 for (int j = 0; j < strings.Length; j++)
462 for (int k = 0; k < strings[j].length; k++)
463 str[i++] = strings[j].c_str[k];
468 public static string Concat (params string[] values)
474 throw new ArgumentNullException ();
477 foreach (string value in values)
478 len += value != null ? value.Length : 0;
483 String res = new String (len);
486 foreach (string value in values) {
490 for (int j = 0; j < value.length; j++)
491 str[i++] = value.c_str[j];
497 public static string Concat (object arg0, object arg1)
499 string str0 = arg0 != null ? arg0.ToString () : String.Empty;
500 string str1 = arg1 != null ? arg1.ToString () : String.Empty;
502 return Concat (str0, str1);
505 public static string Concat (string str0, string str1)
515 len = str0.length + str1.length;
519 String res = new String (len);
522 for (i = 0; i < str0.length; i++)
523 concat[i] = str0.c_str[i];
524 for (j = 0 ; j < str1.length; j++)
525 concat[i + j] = str1.c_str[j];
530 public static string Concat (object arg0, object arg1, object arg2)
532 string str0 = arg0 != null ? arg0.ToString () : String.Empty;
533 string str1 = arg1 != null ? arg1.ToString () : String.Empty;
534 string str2 = arg2 != null ? arg2.ToString () : String.Empty;
536 return Concat (str0, str1, str2);
539 public static string Concat (string str0, string str1, string str2)
551 len = str0.length + str1.length + str2.length;
555 String res = new String (len);
558 for (i = 0; i < str0.length; i++)
559 concat[i] = str0.c_str[i];
560 for (j = 0; j < str1.length; j++)
561 concat[i + j] = str1.c_str[j];
562 for (k = 0; k < str2.length; k++)
563 concat[i + j + k] = str2.c_str[k];
568 public static string Concat (string str0, string str1, string str2, string str3)
582 len = str0.length + str1.length + str2.length + str3.length;
585 String res = new String (len);
588 for (i = 0; i < str0.length; i++)
589 concat[i] = str0.c_str[i];
590 for (j = 0; j < str1.length; j++)
591 concat[i + j] = str1.c_str[j];
592 for (k = 0; k < str2.length; k++)
593 concat[i + j + k] = str2.c_str[k];
594 for (l = 0; l < str3.length; l++)
595 concat[i + j + k + l] = str3.c_str[l];
600 public static string Copy (string str)
602 // FIXME: how do I *copy* a string if I can only have 1 of each?
604 throw new ArgumentNullException ();
609 public void CopyTo (int sourceIndex, char[] destination, int destinationIndex, int count)
611 // LAMESPEC: should I null-terminate?
614 if (destination == null)
615 throw new ArgumentNullException ();
617 if (sourceIndex < 0 || destinationIndex < 0 || count < 0)
618 throw new ArgumentOutOfRangeException ();
620 if (sourceIndex + count > this.length)
621 throw new ArgumentOutOfRangeException ();
623 if (destinationIndex + count > destination.Length)
624 throw new ArgumentOutOfRangeException ();
626 for (i = 0; i < count; i++)
627 destination[destinationIndex + i] = this.c_str[sourceIndex + i];
630 public bool EndsWith (string value)
632 bool endswith = true;
636 throw new ArgumentNullException ();
638 start = this.length - value.length;
642 for (i = start; i < this.length && endswith; i++)
643 endswith = this.c_str[i] == value.c_str[i - start];
648 public override bool Equals (object obj)
650 if (!(obj is String))
653 return this == (String) obj;
656 public bool Equals (string value)
658 return this == value;
661 public static bool Equals (string a, string b)
666 public static string Format (string format, object arg0) {
667 return Format (null, format, new object[] { arg0 });
670 public static string Format (string format, object arg0, object arg1) {
671 return Format (null, format, new object[] { arg0, arg1 });
674 public static string Format (string format, object arg0, object arg1, object arg2) {
675 return Format (null, format, new object[] { arg0, arg1, arg2 });
678 public static string Format (string format, params object[] args) {
679 return Format (null, format, args);
682 public static string Format (IFormatProvider provider, string format, params object[] args) {
683 if (format == null || args == null)
684 throw new ArgumentNullException ();
686 StringBuilder result = new StringBuilder ();
690 while (ptr < format.Length) {
691 char c = format[ptr ++];
694 result.Append (format, start, ptr - start - 1);
696 // check for escaped open bracket
698 if (format[ptr] == '{') {
709 ParseFormatSpecifier (format, ref ptr, out n, out width, out left_align, out arg_format);
710 if (n >= args.Length)
711 throw new FormatException ("Index (zero based) must be greater than or equal to zero and less than the size of the argument list.");
715 object arg = args[n];
720 else if (arg is IFormattable)
721 str = ((IFormattable)arg).ToString (arg_format, provider);
723 str = arg.ToString ();
725 // pad formatted string and append to result
727 if (width > str.Length) {
728 string pad = new String (' ', width - str.Length);
744 else if (c == '}' && format[ptr] == '}') {
745 result.Append (format, start, ptr - start - 1);
750 if (start < format.Length)
751 result.Append (format.Substring (start));
753 return result.ToString ();
756 public CharEnumerator GetEnumerator ()
758 return new CharEnumerator (this);
761 IEnumerator IEnumerable.GetEnumerator ()
763 return new CharEnumerator (this);
766 public override int GetHashCode ()
770 for (i = 0; i < length; ++i)
771 h = (h << 5) - h + c_str [i];
775 public TypeCode GetTypeCode ()
777 return TypeCode.String;
780 public int IndexOf (char value)
782 return IndexOf (value, 0, this.length);
785 public int IndexOf (string value)
787 return IndexOf (value, 0, this.length);
790 public int IndexOf (char value, int startIndex)
792 return IndexOf (value, startIndex, this.length - startIndex);
795 public int IndexOf (string value, int startIndex)
797 return IndexOf (value, startIndex, this.length - startIndex);
800 public int IndexOf (char value, int startIndex, int count)
804 if (startIndex < 0 || count < 0 || startIndex + count > this.length)
805 throw new ArgumentOutOfRangeException ();
807 for (i = startIndex; i - startIndex < count; i++)
808 if (this.c_str[i] == value)
814 public int IndexOf (string value, int startIndex, int count)
817 throw new ArgumentNullException ();
819 if (startIndex < 0 || count < 0 || startIndex + count > this.length)
820 throw new ArgumentOutOfRangeException ();
822 return BoyerMoore (this.c_str, value, startIndex, count);
825 for (i = startIndex; i - startIndex + value.Length <= count; ) {
826 if (this.c_str[i] == value[0]) {
830 for (j = 1; equal && j < value.Length; j++) {
831 equal = this.c_str[i + j] == value[j];
832 if (this.c_str[i + j] == value[0] && nexti == 0)
851 public int IndexOfAny (char[] values)
853 return IndexOfAny (values, 0, this.length);
856 public int IndexOfAny (char[] values, int startIndex)
858 return IndexOfAny (values, startIndex, this.length - startIndex);
861 public int IndexOfAny (char[] values, int startIndex, int count)
864 throw new ArgumentNullException ();
866 if (startIndex < 0 || count < 0 || startIndex + count > this.length)
867 throw new ArgumentOutOfRangeException ();
869 for (int i = startIndex; i < startIndex + count; i++) {
870 for (int j = 0; j < strlen (values); j++) {
871 if (this.c_str[i] == values[j])
879 public string Insert (int startIndex, string value)
885 throw new ArgumentNullException ();
887 if (startIndex < 0 || startIndex > this.length)
888 throw new ArgumentOutOfRangeException ();
890 String res = new String (value.length + this.length);
893 for (i = 0; i < startIndex; i++)
894 str[i] = this.c_str[i];
895 for (j = 0; j < value.length; j++)
896 str[i + j] = value.c_str[j];
897 for ( ; i < this.length; i++)
898 str[i + j] = this.c_str[i];
903 [MethodImplAttribute(MethodImplOptions.InternalCall)]
904 internal extern static string _Intern (string str);
906 public static string Intern (string str)
909 throw new ArgumentNullException ();
911 return _Intern (str);
914 [MethodImplAttribute(MethodImplOptions.InternalCall)]
915 internal extern static string _IsInterned (string str);
917 public static string IsInterned (string str)
920 throw new ArgumentNullException ();
922 return _IsInterned (str);
925 public static string Join (string separator, string[] value)
928 throw new ArgumentNullException ();
930 return Join (separator, value, 0, value.Length);
933 public static string Join (string separator, string[] value, int startIndex, int count)
935 // LAMESPEC: msdn doesn't specify what happens when separator is null
939 if (separator == null || value == null)
940 throw new ArgumentNullException ();
942 if (startIndex + count > value.Length)
943 throw new ArgumentOutOfRangeException ();
946 for (i = startIndex, used = 0; used < count; i++, used++) {
948 len += separator.length;
950 len += value[i].length;
953 // We have no elements to join?
957 String res = new String (len);
960 for (i = 0; i < value[startIndex].length; i++)
961 str[i] = value[startIndex][i];
964 for (j = startIndex + 1; used < count; j++, used++) {
967 for (k = 0; k < separator.length; k++)
968 str[i++] = separator.c_str[k];
969 for (k = 0; k < value[j].length; k++)
970 str[i++] = value[j].c_str[k];
976 public int LastIndexOf (char value)
982 for (; i >= 0; i--) {
983 if (this.c_str[i] == value)
990 public int LastIndexOf (string value)
993 throw new ArgumentNullException ();
994 if (value.length == 0)
996 if (this.length == 0)
999 return LastIndexOf (value, this.length - 1, this.length);
1002 public int LastIndexOf (char value, int startIndex)
1004 if (startIndex < 0 || startIndex >= this.length)
1005 throw new ArgumentOutOfRangeException ();
1007 for (int i = startIndex; i >= 0; i--) {
1008 if (this.c_str[i] == value)
1015 public int LastIndexOf (string value, int startIndex)
1017 return LastIndexOf (value, startIndex, startIndex + 1);
1020 public int LastIndexOf (char value, int startIndex, int count)
1022 if (startIndex < 0 || count < 0)
1023 throw new ArgumentOutOfRangeException ();
1025 if (startIndex >= this.length || startIndex - count + 1 < 0)
1026 throw new ArgumentOutOfRangeException ();
1028 for (int i = startIndex; i > startIndex - count; i--) {
1029 if (this.c_str[i] == value)
1036 public int LastIndexOf (string value, int startIndex, int count)
1038 // startIndex points to the end of value, ie. we're searching backwards.
1042 throw new ArgumentNullException ();
1044 if (startIndex < 0 || startIndex > this.length)
1045 throw new ArgumentOutOfRangeException ();
1047 if (count < 0 || startIndex - count + 1 < 0)
1048 throw new ArgumentOutOfRangeException ();
1050 if (value.length > startIndex)
1053 if (value == String.Empty)
1056 if (startIndex == this.length)
1059 // FIXME: use a reversed-unicode-safe-Boyer-Moore?
1060 len = value.length - 1;
1061 for (i = startIndex; i > startIndex - count; i--) {
1063 if (this.c_str[i] == value.c_str[len]) {
1067 for (j = 0; equal && j < len; j++)
1068 equal = this.c_str[i - j] == value.c_str[len - j];
1078 public int LastIndexOfAny (char[] values)
1080 return LastIndexOfAny (values, this.length - 1, this.length);
1083 public int LastIndexOfAny (char[] values, int startIndex)
1085 return LastIndexOfAny (values, startIndex, startIndex + 1);
1088 public int LastIndexOfAny (char[] values, int startIndex, int count)
1093 throw new ArgumentNullException ();
1095 if (startIndex < 0 || count < 0 || startIndex - count + 1 < 0)
1096 throw new ArgumentOutOfRangeException ();
1098 for (i = startIndex; i > startIndex - count; i--) {
1099 for (int j = 0; j < strlen (values); j++) {
1100 if (this.c_str[i] == values[j])
1108 public string PadLeft (int totalWidth)
1110 return PadLeft (totalWidth, ' ');
1113 public string PadLeft (int totalWidth, char padChar)
1119 throw new ArgumentException ();
1121 str = new char [totalWidth > this.length ? totalWidth : this.length];
1122 for (i = 0; i < totalWidth - this.length; i++)
1125 for (j = 0; j < this.length; i++, j++)
1126 str[i] = this.c_str[j];
1128 return new String (str);
1131 public string PadRight (int totalWidth)
1133 return PadRight (totalWidth, ' ');
1136 public string PadRight (int totalWidth, char padChar)
1142 throw new ArgumentException ();
1144 str = new char [totalWidth > this.length ? totalWidth : this.length];
1145 for (i = 0; i < this.length; i++)
1146 str[i] = this.c_str[i];
1148 for ( ; i < str.Length; i++)
1151 return new String (str);
1154 public string Remove (int startIndex, int count)
1159 if (startIndex < 0 || count < 0 || startIndex + count > this.length)
1160 throw new ArgumentOutOfRangeException ();
1162 len = this.length - count;
1164 return String.Empty;
1166 String res = new String (len);
1168 for (i = 0; i < startIndex; i++)
1169 str[i] = this.c_str[i];
1170 for (j = i + count; j < this.length; j++)
1171 str[i++] = this.c_str[j];
1176 public string Replace (char oldChar, char newChar)
1181 String res = new String (length);
1183 for (i = 0; i < this.length; i++) {
1184 if (this.c_str[i] == oldChar)
1187 str[i] = this.c_str[i];
1193 public string Replace (string oldValue, string newValue)
1195 // If newValue is null, oldValue is removed.
1196 int index, len, i, j, newlen;
1200 if (oldValue == null)
1201 throw new ArgumentNullException ();
1203 thestring = Substring (0, this.length);
1206 // Runs until all occurences of oldValue have been replaced.
1208 // Use IndexOf in case I later rewrite it to use Boyer-Moore
1209 index = thestring.IndexOf (oldValue, index);
1214 newlen = (newValue == null) ? 0 : newValue.length;
1215 len = thestring.length - oldValue.length + newlen;
1218 return String.Empty;
1220 String res = new String (len);
1222 for (i = 0; i < index; i++)
1223 str[i] = thestring.c_str[i];
1224 for (j = 0; j < newlen; j++)
1225 str[i++] = newValue[j];
1226 for (j = index + oldValue.length; j < thestring.length; j++)
1227 str[i++] = thestring.c_str[j];
1229 // Increment index, we're already done replacing until this index.
1235 private int splitme (char[] separators, int startIndex)
1237 /* this is basically a customized IndexOfAny() for the Split() methods */
1238 for (int i = startIndex; i < this.length; i++) {
1239 if (separators != null) {
1240 foreach (char sep in separators) {
1241 if (this.c_str[i] == sep)
1242 return i - startIndex;
1244 } else if (is_lwsp (this.c_str[i])) {
1245 return i - startIndex;
1252 public string[] Split (params char[] separator)
1256 * @separator: delimiting chars or null to split on whtspc
1258 * Returns: 1. An array consisting of a single
1259 * element (@this) if none of the delimiting
1260 * chars appear in @this. 2. An array of
1261 * substrings which are delimited by one of
1262 * the separator chars. 3. An array of
1263 * substrings separated by whitespace if
1264 * @separator is null. The Empty string should
1265 * be returned wherever 2 delimiting chars are
1268 // FIXME: would using a Queue be better?
1273 list = new ArrayList ();
1274 for (index = 0, len = 0; index < this.length; index += len + 1) {
1275 len = splitme (separator, index);
1276 len = len > -1 ? len : this.length - index;
1278 list.Add (String.Empty);
1283 str = new char [len];
1284 for (i = 0; i < len; i++)
1285 str[i] = this.c_str[index + i];
1287 list.Add (new String (str));
1291 strings = new string [list.Count];
1292 if (list.Count == 1) {
1293 /* special case for an array holding @this */
1296 for (index = 0; index < list.Count; index++)
1297 strings[index] = (string) list[index];
1303 public string[] Split (char[] separator, int maxCount)
1305 // FIXME: would using Queue be better than ArrayList?
1308 int index, len, used;
1311 return new string[0];
1312 else if (maxCount < 0)
1313 throw new ArgumentOutOfRangeException ();
1316 list = new ArrayList ();
1317 for (index = 0, len = 0; index < this.length && used < maxCount; index += len + 1) {
1318 len = splitme (separator, index);
1319 len = len > -1 ? len : this.length - index;
1321 list.Add (String.Empty);
1326 str = new char [len];
1327 for (i = 0; i < len; i++)
1328 str[i] = this.c_str[index + i];
1330 list.Add (new String (str));
1335 /* fit the remaining chunk of the @this into it's own element */
1336 if (index < this.length - 1) {
1340 str = new char [this.length - index];
1341 for (i = index; i < this.length; i++)
1342 str[i - index] = this.c_str[i];
1344 list.Add (new String (str));
1347 strings = new string [list.Count];
1348 if (list.Count == 1) {
1349 /* special case for an array holding @this */
1352 for (index = 0; index < list.Count; index++)
1353 strings[index] = (string) list[index];
1359 public bool StartsWith (string value)
1361 bool startswith = true;
1365 throw new ArgumentNullException ();
1367 if (value.length > this.length)
1370 for (i = 0; i < value.length && startswith; i++)
1371 startswith = startswith && value.c_str[i] == this.c_str[i];
1376 public string Substring (int startIndex)
1381 if (startIndex < 0 || startIndex > this.length)
1382 throw new ArgumentOutOfRangeException ();
1384 len = this.length - startIndex;
1386 return String.Empty;
1387 String res = new String (len);
1389 for (i = startIndex; i < this.length; i++)
1390 str[i - startIndex] = this.c_str[i];
1395 public string Substring (int startIndex, int length)
1400 if (startIndex < 0 || length < 0 || startIndex + length > this.length)
1401 throw new ArgumentOutOfRangeException ();
1404 return String.Empty;
1406 String res = new String (length);
1408 for (i = startIndex; i < startIndex + length; i++)
1409 str[i - startIndex] = this.c_str[i];
1414 bool IConvertible.ToBoolean (IFormatProvider provider)
1416 return Convert.ToBoolean (this);
1419 byte IConvertible.ToByte (IFormatProvider provider)
1421 return Convert.ToByte (this);
1424 char IConvertible.ToChar (IFormatProvider provider)
1426 return Convert.ToChar (this);
1429 public char[] ToCharArray ()
1431 return ToCharArray (0, this.length);
1434 public char[] ToCharArray (int startIndex, int length)
1439 if (startIndex < 0 || length < 0 || startIndex + length > this.length)
1440 throw new ArgumentOutOfRangeException ();
1442 chars = new char [length];
1443 for (i = startIndex; i < length; i++)
1444 chars[i - startIndex] = this.c_str[i];
1449 DateTime IConvertible.ToDateTime (IFormatProvider provider)
1451 return Convert.ToDateTime (this);
1454 decimal IConvertible.ToDecimal (IFormatProvider provider)
1456 return Convert.ToDecimal (this);
1459 double IConvertible.ToDouble (IFormatProvider provider)
1461 return Convert.ToDouble (this);
1464 short IConvertible.ToInt16 (IFormatProvider provider)
1466 return Convert.ToInt16 (this);
1469 int IConvertible.ToInt32 (IFormatProvider provider)
1471 return Convert.ToInt32 (this);
1474 long IConvertible.ToInt64 (IFormatProvider provider)
1476 return Convert.ToInt64 (this);
1479 public string ToLower ()
1484 String res = new String (length);
1486 for (i = 0; i < this.length; i++)
1487 str[i] = Char.ToLower (this.c_str[i]);
1493 public string ToLower (CultureInfo culture)
1495 // FIXME: implement me
1496 throw new NotImplementedException ();
1500 [CLSCompliant(false)]
1501 sbyte IConvertible.ToSByte (IFormatProvider provider)
1503 return Convert.ToSByte (this);
1506 float IConvertible.ToSingle (IFormatProvider provider)
1508 return Convert.ToSingle (this);
1511 public override string ToString ()
1516 string IConvertible.ToString (IFormatProvider format)
1521 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
1523 return Convert.ToType (this, conversionType, provider);
1526 [CLSCompliant(false)]
1527 ushort IConvertible.ToUInt16 (IFormatProvider provider)
1529 return Convert.ToUInt16 (this);
1532 [CLSCompliant(false)]
1533 uint IConvertible.ToUInt32 (IFormatProvider provider)
1535 return Convert.ToUInt32 (this);
1538 [CLSCompliant(false)]
1539 ulong IConvertible.ToUInt64 (IFormatProvider provider)
1541 return Convert.ToUInt64 (this);
1544 public string ToUpper ()
1549 String res = new String (length);
1551 for (i = 0; i < this.length; i++)
1552 str[i] = Char.ToUpper (this.c_str[i]);
1558 public string ToUpper (CultureInfo culture)
1560 // FIXME: implement me
1561 throw new NotImplementedException ();
1564 public string Trim ()
1569 public string Trim (params char[] trimChars)
1572 bool matches = false;
1574 for (begin = 0; begin < this.length; begin++) {
1575 if (trimChars != null) {
1577 foreach (char c in trimChars) {
1578 matches = this.c_str[begin] == c;
1585 matches = is_lwsp (this.c_str[begin]);
1592 for (end = this.length - 1; end > begin; end--) {
1593 if (trimChars != null) {
1595 foreach (char c in trimChars) {
1596 matches = this.c_str[end] == c;
1603 matches = is_lwsp (this.c_str[end]);
1612 return String.Empty;
1614 return Substring (begin, end - begin);
1617 public string TrimEnd (params char[] trimChars)
1619 bool matches = true;
1622 for (end = this.length - 1; matches && end > 0; end--) {
1624 if (trimChars != null) {
1626 foreach (char c in trimChars) {
1627 matches = this.c_str[end] == c;
1632 matches = is_lwsp (this.c_str[end]);
1636 return Substring (0, end+1);
1640 return String.Empty;
1642 return Substring (0, end);
1645 public string TrimStart (params char[] trimChars)
1647 bool matches = true;
1650 for (begin = 0; matches && begin < this.length; begin++) {
1651 if (trimChars != null) {
1653 foreach (char c in trimChars) {
1654 matches = this.c_str[begin] == c;
1659 matches = is_lwsp (this.c_str[begin]);
1663 return Substring (begin, this.length - begin);
1666 return String.Empty;
1670 public static bool operator ==(string a, string b)
1672 if ((object)a == null) {
1673 if ((object)b == null)
1677 if ((object)b == null)
1680 if (a.length != b.length)
1684 for (int i = 0; i < l; i++)
1685 if (a.c_str[i] != b.c_str[i])
1691 public static bool operator !=(string a, string b)
1698 private static void ParseFormatSpecifier (string str, ref int ptr, out int n, out int width, out bool left_align, out string format) {
1699 // parses format specifier of form:
1705 // N = argument number (non-negative integer)
1707 n = ParseDecimal (str, ref ptr);
1709 throw new FormatException ("Input string was not in correct format.");
1711 // M = width (non-negative integer)
1713 if (str[ptr] == ',') {
1714 left_align = (str[++ ptr] == '-');
1718 width = ParseDecimal (str, ref ptr);
1720 throw new FormatException ("Input string was not in correct format.");
1727 // F = argument format (string)
1729 if (str[ptr] == ':') {
1731 while (str[ptr] != '}')
1734 format = str.Substring (start, ptr - start);
1739 if (str[ptr ++] != '}')
1740 throw new FormatException ("Input string was not in correct format.");
1742 catch (IndexOutOfRangeException) {
1743 throw new FormatException ("Input string was not in correct format.");
1747 private static int ParseDecimal (string str, ref int ptr) {
1752 if (c < '0' || '9' < c)
1755 n = n * 10 + c - '0';