2002-06-27 Martin Baulig <martin@gnome.org>
[mono.git] / mcs / class / corlib / System / Char.cs
1 //
2 // System.Char.cs
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) Ximian, Inc.  http://www.ximian.com
8 //
9
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.
16
17 using System.Globalization;
18 using System.Runtime.CompilerServices;
19
20 namespace System {
21         
22         [Serializable]
23         public struct Char : IComparable, IConvertible { //, IFormattable {
24                 public const char MaxValue = (char) 0xffff;
25                 public const char MinValue = (char) 0;
26                 
27                 // VES needs to know about value.  public is workaround
28                 // so source will compile
29                 public char value;
30                 
31                 public int CompareTo (object v)
32                 {
33                         if (v == null)
34                                 return 1;
35                         
36                         if (!(v is System.Char))
37                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Char"));
38
39                         char xv = (char) v;
40                         if (value == xv)
41                                 return 0;
42
43                         if (value > xv)
44                                 return 1;
45                         else
46                                 return -1;
47                 }
48
49                 public override bool Equals (object o)
50                 {
51                         if (!(o is System.Char))
52                                 return false;
53
54                         return ((Char) o) == value;
55                 }
56
57                 public override int GetHashCode ()
58                 {
59                         return value;
60                 }
61
62                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
63                 public static extern double GetNumericValue (char c);
64
65                 public static double GetNumericValue (string str, int index)
66                 {
67                         if (str == null) 
68                                 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
69                         
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"));
73                                         
74                         
75                         return GetNumericValue (str[index]);
76                 }
77
78                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
79                 public static extern UnicodeCategory GetUnicodeCategory (char c); 
80
81                 public static UnicodeCategory GetUnicodeCategory (string str, int index) {
82                         if (str == null) 
83                                 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
84                         
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"));
88                         
89                         return GetUnicodeCategory (str[index]);
90                 }
91
92                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
93                 public static extern bool IsControl (char c);
94
95                 public static bool IsControl (string str, int index)
96                 {
97                         if (str == null) 
98                                 throw new ArgumentNullException (Locale.GetText ("Str is a null reference"));
99                         
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"));
103                         
104                         return IsControl (str[index]);
105                 }
106                 
107                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
108                 public static extern bool IsDigit (char c);
109
110                 public static bool IsDigit (string str, int index)
111                 {
112                         if (str == null) 
113                                 throw new ArgumentNullException (Locale.GetText ("Str is a null reference"));
114                         
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"));
118                         
119                         return IsDigit (str[index]);
120                 }
121
122                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
123                 public static extern bool IsLetter (char c);
124
125                 public static bool IsLetter (string str, int index)
126                 {
127                         if (str == null) 
128                                 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
129                         
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"));
133                         
134                         return IsLetter (str[index]);
135                 }
136
137                 public static bool IsLetterOrDigit (char c)
138                 {
139                         if (IsLetter (c) == false && IsDigit (c) == false)
140                                 return false;
141                         else
142                                 return true;
143                 }
144
145                 public static bool IsLetterOrDigit (string str, int index)
146                 {
147                         if (str == null) 
148                                 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
149                         
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"));
153                         
154                         return IsLetterOrDigit (str[index]);
155                 }
156
157                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
158                 public static extern bool IsLower (char c);
159                 
160                 public static bool IsLower (string str, int index)
161                 {
162                         if (str == null) 
163                                 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
164                         
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"));
168                         
169                         return IsLower (str[index]);
170                 }
171
172                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
173                 public static extern bool IsNumber (char c);
174                 
175                 public static bool IsNumber (string str, int index)
176                 {
177                         if (str == null) 
178                                 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
179                         
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"));
183                         
184                         return IsNumber (str[index]);
185                 }
186
187                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
188                 public static extern bool IsPunctuation (char c);
189                 
190                 public static bool IsPunctuation (string str, int index)
191                 {
192                         if (str == null) 
193                                 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
194                         
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"));
198                         
199                         return IsPunctuation (str[index]);
200                 }
201
202                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
203                 public static extern bool IsSeparator (char c);
204                 
205                 public static bool IsSeparator (string str, int index)
206                 {
207                         if (str == null) 
208                                 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
209                         
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"));
213                         
214                         return IsSeparator (str[index]);
215                 }
216
217                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
218                 public static extern bool IsSurrogate (char c);
219                 
220                 public static bool IsSurrogate (string str, int index)
221                 {
222                         if (str == null) 
223                                 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
224                         
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"));
228                         
229                         return IsSurrogate (str[index]);
230                 }
231
232                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
233                 public static extern bool IsSymbol (char c);
234                 
235                 public static bool IsSymbol (string str, int index)
236                 {
237                         if (str == null) 
238                                 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
239                         
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"));
243                         
244                         return IsSymbol (str[index]);
245                 }
246
247                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
248                 public static extern bool IsUpper (char c);
249                 
250                 public static bool IsUpper (string str, int index)
251                 {
252                         if (str == null) 
253                                 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
254                         
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"));
258                         
259                         return IsUpper (str[index]);
260                 }
261
262                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
263                 public static extern bool IsWhiteSpace (char c);
264                 
265                 public static bool IsWhiteSpace (string str, int index)
266                 {
267                         if (str == null) 
268                                 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
269                         
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"));
273                         
274                         return IsWhiteSpace (str[index]);
275                 }
276
277                 public static char Parse (string str)
278                 {
279                         if (str == null)
280                                 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
281
282                         if (str.Length != 1)
283                                 throw new FormatException ("string contains more than one character.");
284                         
285                         return str [0];
286                 }
287
288                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
289                 public static extern char ToLower (char c);
290
291                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
292                 public static extern char ToUpper (char c);
293
294                 public override string ToString ()
295                 {
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);
299                 }
300
301                 public string ToString (IFormatProvider fp)
302                 {
303                         // LAMESPEC: ECMA draft doesn't say Char implements IFormattable
304                         return new String (value, 1);
305                 }
306
307                 // =========== IConvertible Methods =========== //
308                 
309                 public TypeCode GetTypeCode ()
310                 {
311                         return TypeCode.Char;
312                 }         
313
314                 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
315                 {
316                         return System.Convert.ToType(value, conversionType, provider);
317                 }
318                 
319                 bool IConvertible.ToBoolean (IFormatProvider provider)
320                 {
321                         throw new InvalidCastException();
322                 }
323                 
324                 byte IConvertible.ToByte (IFormatProvider provider)
325                 {
326                         return System.Convert.ToByte(value);
327                 }
328                 
329                 char IConvertible.ToChar (IFormatProvider provider)
330                 {
331                         return value;
332                 }
333                 
334                 [CLSCompliant(false)]
335                 DateTime IConvertible.ToDateTime (IFormatProvider provider)
336                 {
337                         throw new InvalidCastException();
338                 }
339                 
340                 decimal IConvertible.ToDecimal (IFormatProvider provider)
341                 {
342                         throw new InvalidCastException();
343                 }
344                 
345                 double IConvertible.ToDouble (IFormatProvider provider)
346                 {
347                         throw new InvalidCastException();
348                 }
349                 
350                 short IConvertible.ToInt16 (IFormatProvider provider)
351                 {
352                         return System.Convert.ToInt16(value);
353                 }
354                 
355                 int IConvertible.ToInt32 (IFormatProvider provider)
356                 {
357                         return System.Convert.ToInt32(value);
358                 }
359                 
360                 long IConvertible.ToInt64 (IFormatProvider provider)
361                 {
362                         return System.Convert.ToInt64(value);
363                 }
364                 
365                 [CLSCompliant(false)] 
366                 sbyte IConvertible.ToSByte (IFormatProvider provider)
367                 {
368                         return System.Convert.ToSByte(value);
369                 }
370                 
371                 float IConvertible.ToSingle (IFormatProvider provider)
372                 {
373                         throw new InvalidCastException();
374                 }
375                 
376                 string IConvertible.ToString (IFormatProvider provider)
377                 {
378                         return ToString(provider);
379                 }
380
381                 [CLSCompliant(false)]
382                 ushort IConvertible.ToUInt16 (IFormatProvider provider)
383                 {
384                         return System.Convert.ToUInt16(value);
385                 }
386                 
387                 [CLSCompliant(false)]
388                 uint IConvertible.ToUInt32 (IFormatProvider provider)
389                 {
390                         return System.Convert.ToUInt32(value);
391                 }
392                 
393                 [CLSCompliant(false)]
394                 ulong IConvertible.ToUInt64 (IFormatProvider provider)
395                 {
396                         return System.Convert.ToUInt64(value);
397                 }
398         }
399 }