5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) Ximian, Inc. http://www.ximian.com
10 // Note about the ToString()'s. ECMA says there's only a ToString() method,
11 // BUT it is just a wrapper for ToString(null). However there is no other ToString
12 // in the docs. Turning to the NET framework sdk reveals that there is a
13 // ToString(formatprovider) method, as well as a 'static ToString (char c)' method,
14 // which appears to just be a Convert.ToString(char c) type method. ECMA also
15 // doesn't list this class as implementing IFormattable.
17 using System.Globalization;
18 using System.Runtime.CompilerServices;
23 public struct Char : IComparable, IConvertible { //, IFormattable {
24 public const char MaxValue = (char) 0xffff;
25 public const char MinValue = (char) 0;
27 // VES needs to know about value. public is workaround
28 // so source will compile
31 public int CompareTo (object v)
36 if (!(v is System.Char))
37 throw new ArgumentException (Locale.GetText ("Value is not a System.Char"));
49 public override bool Equals (object o)
51 if (!(o is System.Char))
54 return ((Char) o) == value;
57 public override int GetHashCode ()
62 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
63 public static extern double GetNumericValue (char c);
65 public static double GetNumericValue (string str, int index)
68 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
70 if (index < 0 || index >= str.Length)
71 throw new ArgumentOutOfRangeException (Locale.GetText (
72 "The value of index is less than zero, or greater than or equal to the length of str"));
75 return GetNumericValue (str[index]);
78 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
79 public static extern UnicodeCategory GetUnicodeCategory (char c);
81 public static UnicodeCategory GetUnicodeCategory (string str, int index) {
83 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
85 if (index < 0 || index >= str.Length)
86 throw new ArgumentOutOfRangeException (Locale.GetText ("The value of index is less "+
87 "than zero, or greater than or equal to the length of str"));
89 return GetUnicodeCategory (str[index]);
92 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
93 public static extern bool IsControl (char c);
95 public static bool IsControl (string str, int index)
98 throw new ArgumentNullException (Locale.GetText ("Str is a null reference"));
100 if (index < 0 || index >= str.Length)
101 throw new ArgumentOutOfRangeException (Locale.GetText (
102 "The value of index is less than zero, or greater than or equal to the length of str"));
104 return IsControl (str[index]);
107 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
108 public static extern bool IsDigit (char c);
110 public static bool IsDigit (string str, int index)
113 throw new ArgumentNullException (Locale.GetText ("Str is a null reference"));
115 if (index < 0 || index >= str.Length)
116 throw new ArgumentOutOfRangeException (Locale.GetText (
117 "The value of index is less than zero, or greater than or equal to the length of str"));
119 return IsDigit (str[index]);
122 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
123 public static extern bool IsLetter (char c);
125 public static bool IsLetter (string str, int index)
128 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
130 if (index < 0 || index >= str.Length)
131 throw new ArgumentOutOfRangeException (Locale.GetText (
132 "The value of index is less than zero, or greater than or equal to the length of str"));
134 return IsLetter (str[index]);
137 public static bool IsLetterOrDigit (char c)
139 if (IsLetter (c) == false && IsDigit (c) == false)
145 public static bool IsLetterOrDigit (string str, int index)
148 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
150 if (index < 0 || index >= str.Length)
151 throw new ArgumentOutOfRangeException (Locale.GetText (
152 "The value of index is less than zero, or greater than or equal to the length of str"));
154 return IsLetterOrDigit (str[index]);
157 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
158 public static extern bool IsLower (char c);
160 public static bool IsLower (string str, int index)
163 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
165 if (index < 0 || index >= str.Length)
166 throw new ArgumentOutOfRangeException (Locale.GetText (
167 "The value of index is less than zero, or greater than or equal to the length of str"));
169 return IsLower (str[index]);
172 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
173 public static extern bool IsNumber (char c);
175 public static bool IsNumber (string str, int index)
178 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
180 if (index < 0 || index >= str.Length)
181 throw new ArgumentOutOfRangeException (Locale.GetText (
182 "The value of index is less than zero, or greater than or equal to the length of str"));
184 return IsNumber (str[index]);
187 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
188 public static extern bool IsPunctuation (char c);
190 public static bool IsPunctuation (string str, int index)
193 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
195 if (index < 0 || index >= str.Length)
196 throw new ArgumentOutOfRangeException (Locale.GetText (
197 "The value of index is less than zero, or greater than or equal to the length of str"));
199 return IsPunctuation (str[index]);
202 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
203 public static extern bool IsSeparator (char c);
205 public static bool IsSeparator (string str, int index)
208 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
210 if (index < 0 || index >= str.Length)
211 throw new ArgumentOutOfRangeException (Locale.GetText (
212 "The value of index is less than zero, or greater than or equal to the length of str"));
214 return IsSeparator (str[index]);
217 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
218 public static extern bool IsSurrogate (char c);
220 public static bool IsSurrogate (string str, int index)
223 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
225 if (index < 0 || index >= str.Length)
226 throw new ArgumentOutOfRangeException (Locale.GetText (
227 "The value of index is less than zero, or greater than or equal to the length of str"));
229 return IsSurrogate (str[index]);
232 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
233 public static extern bool IsSymbol (char c);
235 public static bool IsSymbol (string str, int index)
238 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
240 if (index < 0 || index >= str.Length)
241 throw new ArgumentOutOfRangeException (Locale.GetText (
242 "The value of index is less than zero, or greater than or equal to the length of str"));
244 return IsSymbol (str[index]);
247 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
248 public static extern bool IsUpper (char c);
250 public static bool IsUpper (string str, int index)
253 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
255 if (index < 0 || index >= str.Length)
256 throw new ArgumentOutOfRangeException (Locale.GetText (
257 "The value of index is less than zero, or greater than or equal to the length of str"));
259 return IsUpper (str[index]);
262 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
263 public static extern bool IsWhiteSpace (char c);
265 public static bool IsWhiteSpace (string str, int index)
268 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
270 if (index < 0 || index >= str.Length)
271 throw new ArgumentOutOfRangeException (Locale.GetText (
272 "The value of index is less than zero, or greater than or equal to the length of str"));
274 return IsWhiteSpace (str[index]);
277 public static char Parse (string str)
280 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
283 throw new FormatException ("string contains more than one character.");
288 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
289 public static extern char ToLower (char c);
291 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
292 public static extern char ToUpper (char c);
294 public override string ToString ()
296 // LAMESPEC: ECMA draft lists this as returning ToString (null),
297 // However it doesn't list another ToString() method.
298 return new String (value, 1);
301 public string ToString (IFormatProvider fp)
303 // LAMESPEC: ECMA draft doesn't say Char implements IFormattable
304 return new String (value, 1);
307 // =========== IConvertible Methods =========== //
309 public TypeCode GetTypeCode ()
311 return TypeCode.Char;
314 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
316 return System.Convert.ToType(value, conversionType, provider);
319 bool IConvertible.ToBoolean (IFormatProvider provider)
321 throw new InvalidCastException();
324 byte IConvertible.ToByte (IFormatProvider provider)
326 return System.Convert.ToByte(value);
329 char IConvertible.ToChar (IFormatProvider provider)
334 [CLSCompliant(false)]
335 DateTime IConvertible.ToDateTime (IFormatProvider provider)
337 throw new InvalidCastException();
340 decimal IConvertible.ToDecimal (IFormatProvider provider)
342 throw new InvalidCastException();
345 double IConvertible.ToDouble (IFormatProvider provider)
347 throw new InvalidCastException();
350 short IConvertible.ToInt16 (IFormatProvider provider)
352 return System.Convert.ToInt16(value);
355 int IConvertible.ToInt32 (IFormatProvider provider)
357 return System.Convert.ToInt32(value);
360 long IConvertible.ToInt64 (IFormatProvider provider)
362 return System.Convert.ToInt64(value);
365 [CLSCompliant(false)]
366 sbyte IConvertible.ToSByte (IFormatProvider provider)
368 return System.Convert.ToSByte(value);
371 float IConvertible.ToSingle (IFormatProvider provider)
373 throw new InvalidCastException();
376 string IConvertible.ToString (IFormatProvider provider)
378 return ToString(provider);
381 [CLSCompliant(false)]
382 ushort IConvertible.ToUInt16 (IFormatProvider provider)
384 return System.Convert.ToUInt16(value);
387 [CLSCompliant(false)]
388 uint IConvertible.ToUInt32 (IFormatProvider provider)
390 return System.Convert.ToUInt32(value);
393 [CLSCompliant(false)]
394 ulong IConvertible.ToUInt64 (IFormatProvider provider)
396 return System.Convert.ToUInt64(value);