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)
927 return Join (separator, value, 0, value.Length);
930 public static string Join (string separator, string[] value, int startIndex, int count)
932 // LAMESPEC: msdn doesn't specify what happens when separator is null
936 if (separator == null || value == null)
937 throw new ArgumentNullException ();
939 if (startIndex + count > value.Length)
940 throw new ArgumentOutOfRangeException ();
943 for (i = startIndex, used = 0; used < count; i++, used++) {
945 len += separator.length;
947 len += value[i].length;
950 // We have no elements to join?
954 String res = new String (len);
957 for (i = 0; i < value[startIndex].length; i++)
958 str[i] = value[startIndex][i];
961 for (j = startIndex + 1; used < count; j++, used++) {
964 for (k = 0; k < separator.length; k++)
965 str[i++] = separator.c_str[k];
966 for (k = 0; k < value[j].length; k++)
967 str[i++] = value[j].c_str[k];
973 public int LastIndexOf (char value)
979 for (; i >= 0; i--) {
980 if (this.c_str[i] == value)
987 public int LastIndexOf (string value)
990 throw new ArgumentNullException ();
991 if (value.length == 0)
993 if (this.length == 0)
996 return LastIndexOf (value, this.length - 1, this.length);
999 public int LastIndexOf (char value, int startIndex)
1001 if (startIndex < 0 || startIndex >= this.length)
1002 throw new ArgumentOutOfRangeException ();
1004 for (int i = startIndex; i >= 0; i--) {
1005 if (this.c_str[i] == value)
1012 public int LastIndexOf (string value, int startIndex)
1014 return LastIndexOf (value, startIndex, startIndex + 1);
1017 public int LastIndexOf (char value, int startIndex, int count)
1019 if (startIndex < 0 || count < 0)
1020 throw new ArgumentOutOfRangeException ();
1022 if (startIndex >= this.length || startIndex - count + 1 < 0)
1023 throw new ArgumentOutOfRangeException ();
1025 for (int i = startIndex; i > startIndex - count; i--) {
1026 if (this.c_str[i] == value)
1033 public int LastIndexOf (string value, int startIndex, int count)
1035 // LAMESPEC: currently I'm using startIndex as the 0-position in the comparison,
1036 // but maybe it's the end-position in MS's implementation?
1037 // msdn is unclear on this point. I think this is correct though.
1041 throw new ArgumentNullException ();
1043 if (startIndex < 0 || startIndex >= this.length)
1044 throw new ArgumentOutOfRangeException ();
1046 if (count < 0 || startIndex - count + 1 < 0)
1047 throw new ArgumentOutOfRangeException ();
1049 if (value == String.Empty)
1052 if (startIndex + value.length > this.length) {
1053 /* just a little optimization */
1056 start = this.length - value.length;
1057 count -= startIndex - start;
1061 // FIXME: use a reversed-unicode-safe-Boyer-Moore?
1062 len = value.length - 1;
1063 for (i = startIndex; i > startIndex - count; i--) {
1064 if (this.c_str[i + len] == value.c_str[len]) {
1068 for (j = len - 1; equal && j >= 0; j--)
1069 equal = this.c_str[i + j] == value.c_str[j];
1079 public int LastIndexOfAny (char[] values)
1081 return LastIndexOfAny (values, this.length - 1, this.length);
1084 public int LastIndexOfAny (char[] values, int startIndex)
1086 return LastIndexOfAny (values, startIndex, startIndex + 1);
1089 public int LastIndexOfAny (char[] values, int startIndex, int count)
1094 throw new ArgumentNullException ();
1096 if (startIndex < 0 || count < 0 || startIndex - count + 1 < 0)
1097 throw new ArgumentOutOfRangeException ();
1099 for (i = startIndex; i > startIndex - count; i--) {
1100 for (int j = 0; j < strlen (values); j++) {
1101 if (this.c_str[i] == values[j])
1109 public string PadLeft (int totalWidth)
1111 return PadLeft (totalWidth, ' ');
1114 public string PadLeft (int totalWidth, char padChar)
1120 throw new ArgumentException ();
1122 str = new char [totalWidth > this.length ? totalWidth : this.length];
1123 for (i = 0; i < totalWidth - this.length; i++)
1126 for (j = 0; j < this.length; i++, j++)
1127 str[i] = this.c_str[j];
1129 return new String (str);
1132 public string PadRight (int totalWidth)
1134 return PadRight (totalWidth, ' ');
1137 public string PadRight (int totalWidth, char padChar)
1143 throw new ArgumentException ();
1145 str = new char [totalWidth > this.length ? totalWidth : this.length];
1146 for (i = 0; i < this.length; i++)
1147 str[i] = this.c_str[i];
1149 for ( ; i < str.Length; i++)
1152 return new String (str);
1155 public string Remove (int startIndex, int count)
1160 if (startIndex < 0 || count < 0 || startIndex + count > this.length)
1161 throw new ArgumentOutOfRangeException ();
1163 len = this.length - count;
1165 return String.Empty;
1167 String res = new String (len);
1169 for (i = 0; i < startIndex; i++)
1170 str[i] = this.c_str[i];
1171 for (j = i + count; j < this.length; j++)
1172 str[i++] = this.c_str[j];
1177 public string Replace (char oldChar, char newChar)
1182 String res = new String (length);
1184 for (i = 0; i < this.length; i++) {
1185 if (this.c_str[i] == oldChar)
1188 str[i] = this.c_str[i];
1194 public string Replace (string oldValue, string newValue)
1196 // LAMESPEC: msdn doesn't specify what to do if either args is null
1197 int index, len, i, j;
1200 if (oldValue == null || newValue == null)
1201 throw new ArgumentNullException ();
1203 // Use IndexOf in case I later rewrite it to use Boyer-Moore
1204 index = IndexOf (oldValue, 0);
1206 // This is the easy one ;-)
1207 return Substring (0, this.length);
1210 len = this.length - oldValue.length + newValue.length;
1212 return String.Empty;
1214 String res = new String (len);
1216 for (i = 0; i < index; i++)
1217 str[i] = this.c_str[i];
1218 for (j = 0; j < newValue.length; j++)
1219 str[i++] = newValue[j];
1220 for (j = index + oldValue.length; j < this.length; j++)
1221 str[i++] = this.c_str[j];
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: what to do if maxCount <= 0?
1297 // FIXME: would using Queue be better than ArrayList?
1300 int index, len, used;
1303 list = new ArrayList ();
1304 for (index = 0, len = 0; index < this.length && used < maxCount; index += len + 1) {
1305 len = splitme (separator, index);
1306 len = len > -1 ? len : this.length - index;
1308 list.Add (String.Empty);
1313 str = new char [len];
1314 for (i = 0; i < len; i++)
1315 str[i] = this.c_str[index + i];
1317 list.Add (new String (str));
1322 /* fit the remaining chunk of the @this into it's own element */
1323 if (index < this.length - 1) {
1327 str = new char [this.length - index];
1328 for (i = index; i < this.length; i++)
1329 str[i - index] = this.c_str[i];
1331 list.Add (new String (str));
1334 strings = new string [list.Count];
1335 if (list.Count == 1) {
1336 /* special case for an array holding @this */
1339 for (index = 0; index < list.Count; index++)
1340 strings[index] = (string) list[index];
1346 public bool StartsWith (string value)
1348 bool startswith = true;
1352 throw new ArgumentNullException ();
1354 if (value.length > this.length)
1357 for (i = 0; i < value.length && startswith; i++)
1358 startswith = startswith && value.c_str[i] == this.c_str[i];
1363 public string Substring (int startIndex)
1368 if (startIndex < 0 || startIndex > this.length)
1369 throw new ArgumentOutOfRangeException ();
1371 len = this.length - startIndex;
1373 return String.Empty;
1374 String res = new String (len);
1376 for (i = startIndex; i < this.length; i++)
1377 str[i - startIndex] = this.c_str[i];
1382 public string Substring (int startIndex, int length)
1387 if (startIndex < 0 || length < 0 || startIndex + length > this.length)
1388 throw new ArgumentOutOfRangeException ();
1391 return String.Empty;
1393 String res = new String (length);
1395 for (i = startIndex; i < startIndex + length; i++)
1396 str[i - startIndex] = this.c_str[i];
1401 bool IConvertible.ToBoolean (IFormatProvider provider)
1403 return Convert.ToBoolean (this);
1406 byte IConvertible.ToByte (IFormatProvider provider)
1408 return Convert.ToByte (this);
1411 char IConvertible.ToChar (IFormatProvider provider)
1413 return Convert.ToChar (this);
1416 public char[] ToCharArray ()
1418 return ToCharArray (0, this.length);
1421 public char[] ToCharArray (int startIndex, int length)
1426 if (startIndex < 0 || length < 0 || startIndex + length > this.length)
1427 throw new ArgumentOutOfRangeException ();
1429 chars = new char [length];
1430 for (i = startIndex; i < length; i++)
1431 chars[i - startIndex] = this.c_str[i];
1436 DateTime IConvertible.ToDateTime (IFormatProvider provider)
1438 return Convert.ToDateTime (this);
1441 decimal IConvertible.ToDecimal (IFormatProvider provider)
1443 return Convert.ToDecimal (this);
1446 double IConvertible.ToDouble (IFormatProvider provider)
1448 return Convert.ToDouble (this);
1451 short IConvertible.ToInt16 (IFormatProvider provider)
1453 return Convert.ToInt16 (this);
1456 int IConvertible.ToInt32 (IFormatProvider provider)
1458 return Convert.ToInt32 (this);
1461 long IConvertible.ToInt64 (IFormatProvider provider)
1463 return Convert.ToInt64 (this);
1466 public string ToLower ()
1471 String res = new String (length);
1473 for (i = 0; i < this.length; i++)
1474 str[i] = Char.ToLower (this.c_str[i]);
1480 public string ToLower (CultureInfo culture)
1482 // FIXME: implement me
1483 throw new NotImplementedException ();
1487 [CLSCompliant(false)]
1488 sbyte IConvertible.ToSByte (IFormatProvider provider)
1490 return Convert.ToSByte (this);
1493 float IConvertible.ToSingle (IFormatProvider provider)
1495 return Convert.ToSingle (this);
1498 public override string ToString ()
1503 string IConvertible.ToString (IFormatProvider format)
1508 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
1510 return Convert.ToType (this, conversionType, provider);
1513 [CLSCompliant(false)]
1514 ushort IConvertible.ToUInt16 (IFormatProvider provider)
1516 return Convert.ToUInt16 (this);
1519 [CLSCompliant(false)]
1520 uint IConvertible.ToUInt32 (IFormatProvider provider)
1522 return Convert.ToUInt32 (this);
1525 [CLSCompliant(false)]
1526 ulong IConvertible.ToUInt64 (IFormatProvider provider)
1528 return Convert.ToUInt64 (this);
1531 public string ToUpper ()
1536 String res = new String (length);
1538 for (i = 0; i < this.length; i++)
1539 str[i] = Char.ToUpper (this.c_str[i]);
1545 public string ToUpper (CultureInfo culture)
1547 // FIXME: implement me
1548 throw new NotImplementedException ();
1551 public string Trim ()
1556 public string Trim (params char[] trimChars)
1559 bool matches = false;
1561 for (begin = 0; begin < this.length; begin++) {
1562 if (trimChars != null) {
1564 foreach (char c in trimChars) {
1565 matches = this.c_str[begin] == c;
1572 matches = is_lwsp (this.c_str[begin]);
1579 for (end = this.length - 1; end > begin; end--) {
1580 if (trimChars != null) {
1582 foreach (char c in trimChars) {
1583 matches = this.c_str[end] == c;
1590 matches = is_lwsp (this.c_str[end]);
1599 return String.Empty;
1601 return Substring (begin, end - begin);
1604 public string TrimEnd (params char[] trimChars)
1606 bool matches = true;
1609 for (end = this.length - 1; matches && end > 0; end--) {
1611 if (trimChars != null) {
1613 foreach (char c in trimChars) {
1614 matches = this.c_str[end] == c;
1619 matches = is_lwsp (this.c_str[end]);
1623 return Substring (0, end+1);
1627 return String.Empty;
1629 return Substring (0, end);
1632 public string TrimStart (params char[] trimChars)
1634 bool matches = true;
1637 for (begin = 0; matches && begin < this.length; begin++) {
1638 if (trimChars != null) {
1640 foreach (char c in trimChars) {
1641 matches = this.c_str[begin] == c;
1646 matches = is_lwsp (this.c_str[begin]);
1650 return Substring (begin, this.length - begin);
1653 return String.Empty;
1657 public static bool operator ==(string a, string b)
1659 if ((object)a == null) {
1660 if ((object)b == null)
1664 if ((object)b == null)
1667 if (a.length != b.length)
1671 for (int i = 0; i < l; i++)
1672 if (a.c_str[i] != b.c_str[i])
1678 public static bool operator !=(string a, string b)
1685 private static void ParseFormatSpecifier (string str, ref int ptr, out int n, out int width, out bool left_align, out string format) {
1686 // parses format specifier of form:
1692 // N = argument number (non-negative integer)
1694 n = ParseDecimal (str, ref ptr);
1696 throw new FormatException ("Input string was not in correct format.");
1698 // M = width (non-negative integer)
1700 if (str[ptr] == ',') {
1701 left_align = (str[++ ptr] == '-');
1705 width = ParseDecimal (str, ref ptr);
1707 throw new FormatException ("Input string was not in correct format.");
1714 // F = argument format (string)
1716 if (str[ptr] == ':') {
1718 while (str[ptr] != '}')
1721 format = str.Substring (start, ptr - start);
1726 if (str[ptr ++] != '}')
1727 throw new FormatException ("Input string was not in correct format.");
1729 catch (IndexOutOfRangeException) {
1730 throw new FormatException ("Input string was not in correct format.");
1734 private static int ParseDecimal (string str, ref int ptr) {
1739 if (c < '0' || '9' < c)
1742 n = n * 10 + c - '0';