2002-01-14 Miguel de Icaza <miguel@ximian.com>
[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         public struct Char : IComparable { //, IFormattable, IConvertible {
23                 public const char MaxValue = (char) 0xffff;
24                 public const char MinValue = (char) 0;
25                 
26                 // VES needs to know about value.  public is workaround
27                 // so source will compile
28                 public char value;
29                 
30                 public int CompareTo (object v)
31                 {
32                         if (v == null)
33                                 return 1;
34                         
35                         if (!(v is System.Char))
36                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Char"));
37
38                         char xv = (char) v;
39                         if (value == xv)
40                                 return 0;
41
42                         if (value > xv)
43                                 return 1;
44                         else
45                                 return -1;
46                 }
47
48                 public override bool Equals (object o)
49                 {
50                         if (!(o is System.Char))
51                                 return false;
52
53                         return ((Char) o) == value;
54                 }
55
56                 public override int GetHashCode ()
57                 {
58                         return value;
59                 }
60
61                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
62                 public static extern double GetNumericValue (char c);
63
64                 public static double GetNumericValue (string str, int index)
65                 {
66                         if (str == null) 
67                                 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
68                         
69                         if (index < 0 || index >= str.Length)
70                                 throw new ArgumentOutOfRangeException (Locale.GetText (
71                                         "The value of index is less than zero, or greater than or equal to the length of str"));
72                                         
73                         
74                         return GetNumericValue (str[index]);
75                 }
76
77                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
78                 public static extern UnicodeCategory GetUnicodeCategory (char c); 
79
80                 public static UnicodeCategory GetUnicodeCategory (string str, int index) {
81                         if (str == null) 
82                                 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
83                         
84                         if (index < 0 || index >= str.Length)
85                                 throw new ArgumentOutOfRangeException (Locale.GetText ("The value of index is less "+
86                                                           "than zero, or greater than or equal to the length of str"));
87                         
88                         return GetUnicodeCategory (str[index]);
89                 }
90
91                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
92                 public static extern bool IsControl (char c);
93
94                 public static bool IsControl (string str, int index)
95                 {
96                         if (str == null) 
97                                 throw new ArgumentNullException (Locale.GetText ("Str is a null reference"));
98                         
99                         if (index < 0 || index >= str.Length)
100                                 throw new ArgumentOutOfRangeException (Locale.GetText (
101                                         "The value of index is less than zero, or greater than or equal to the length of str"));
102                         
103                         return IsControl (str[index]);
104                 }
105                 
106                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
107                 public static extern bool IsDigit (char c);
108
109                 public static bool IsDigit (string str, int index)
110                 {
111                         if (str == null) 
112                                 throw new ArgumentNullException (Locale.GetText ("Str is a null reference"));
113                         
114                         if (index < 0 || index >= str.Length)
115                                 throw new ArgumentOutOfRangeException (Locale.GetText (
116                                  "The value of index is less than zero, or greater than or equal to the length of str"));
117                         
118                         return IsDigit (str[index]);
119                 }
120
121                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
122                 public static extern bool IsLetter (char c);
123
124                 public static bool IsLetter (string str, int index)
125                 {
126                         if (str == null) 
127                                 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
128                         
129                         if (index < 0 || index >= str.Length)
130                                 throw new ArgumentOutOfRangeException (Locale.GetText (
131                                  "The value of index is less than zero, or greater than or equal to the length of str"));
132                         
133                         return IsLetter (str[index]);
134                 }
135
136                 public static bool IsLetterOrDigit (char c)
137                 {
138                         if (IsLetter (c) == false && IsDigit (c) == false)
139                                 return false;
140                         else
141                                 return true;
142                 }
143
144                 public static bool IsLetterOrDigit (string str, int index)
145                 {
146                         if (str == null) 
147                                 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
148                         
149                         if (index < 0 || index >= str.Length)
150                                 throw new ArgumentOutOfRangeException (Locale.GetText (
151                                  "The value of index is less than zero, or greater than or equal to the length of str"));
152                         
153                         return IsLetterOrDigit (str[index]);
154                 }
155
156                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
157                 public static extern bool IsLower (char c);
158                 
159                 public static bool IsLower (string str, int index)
160                 {
161                         if (str == null) 
162                                 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
163                         
164                         if (index < 0 || index >= str.Length)
165                                 throw new ArgumentOutOfRangeException (Locale.GetText (
166                                  "The value of index is less than zero, or greater than or equal to the length of str"));
167                         
168                         return IsLower (str[index]);
169                 }
170
171                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
172                 public static extern bool IsNumber (char c);
173                 
174                 public static bool IsNumber (string str, int index)
175                 {
176                         if (str == null) 
177                                 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
178                         
179                         if (index < 0 || index >= str.Length)
180                                 throw new ArgumentOutOfRangeException (Locale.GetText (
181                                 "The value of index is less than zero, or greater than or equal to the length of str"));
182                         
183                         return IsNumber (str[index]);
184                 }
185
186                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
187                 public static extern bool IsPunctuation (char c);
188                 
189                 public static bool IsPunctuation (string str, int index)
190                 {
191                         if (str == null) 
192                                 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
193                         
194                         if (index < 0 || index >= str.Length)
195                                 throw new ArgumentOutOfRangeException (Locale.GetText (
196                                  "The value of index is less than zero, or greater than or equal to the length of str"));
197                         
198                         return IsPunctuation (str[index]);
199                 }
200
201                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
202                 public static extern bool IsSeparator (char c);
203                 
204                 public static bool IsSeparator (string str, int index)
205                 {
206                         if (str == null) 
207                                 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
208                         
209                         if (index < 0 || index >= str.Length)
210                                 throw new ArgumentOutOfRangeException (Locale.GetText (
211                                  "The value of index is less than zero, or greater than or equal to the length of str"));
212                         
213                         return IsSeparator (str[index]);
214                 }
215
216                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
217                 public static extern bool IsSurrogate (char c);
218                 
219                 public static bool IsSurrogate (string str, int index)
220                 {
221                         if (str == null) 
222                                 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
223                         
224                         if (index < 0 || index >= str.Length)
225                                 throw new ArgumentOutOfRangeException (Locale.GetText (
226                                  "The value of index is less than zero, or greater than or equal to the length of str"));
227                         
228                         return IsSurrogate (str[index]);
229                 }
230
231                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
232                 public static extern bool IsSymbol (char c);
233                 
234                 public static bool IsSymbol (string str, int index)
235                 {
236                         if (str == null) 
237                                 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
238                         
239                         if (index < 0 || index >= str.Length)
240                                 throw new ArgumentOutOfRangeException (Locale.GetText (
241                                         "The value of index is less than zero, or greater than or equal to the length of str"));
242                         
243                         return IsSymbol (str[index]);
244                 }
245
246                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
247                 public static extern bool IsUpper (char c);
248                 
249                 public static bool IsUpper (string str, int index)
250                 {
251                         if (str == null) 
252                                 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
253                         
254                         if (index < 0 || index >= str.Length)
255                                 throw new ArgumentOutOfRangeException (Locale.GetText (
256                                  "The value of index is less than zero, or greater than or equal to the length of str"));
257                         
258                         return IsUpper (str[index]);
259                 }
260
261                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
262                 public static extern bool IsWhiteSpace (char c);
263                 
264                 public static bool IsWhiteSpace (string str, int index)
265                 {
266                         if (str == null) 
267                                 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
268                         
269                         if (index < 0 || index >= str.Length)
270                                 throw new ArgumentOutOfRangeException (Locale.GetText (
271                                         "The value of index is less than zero, or greater than or equal to the length of str"));
272                         
273                         return IsWhiteSpace (str[index]);
274                 }
275
276                 public static char Parse (string str)
277                 {
278                         if (str == null)
279                                 throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
280
281                         int len = str.Length;
282                         if (len != 1){
283                                 if (len < 1)
284                                         throw new ArgumentNullException ();
285                                 else
286                                         throw new FormatException ();
287                         }
288                         return str [0];
289                 }
290
291                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
292                 public static extern char ToLower (char c);
293
294                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
295                 public static extern char ToUpper (char c);
296
297                 public override string ToString ()
298                 {
299                         // LAMESPEC: ECMA draft lists this as returning ToString (null), 
300                         // However it doesn't list another ToString() method.
301                         return new String (value, 1);
302                 }
303
304                 public string ToString (IFormatProvider fp)
305                 {
306                         // LAMESPEC: ECMA draft doesn't say Char implements IFormattable
307                         return new String (value, 1);
308                 }
309
310                 // =========== IConvertible Methods =========== //
311                 
312                 public TypeCode GetTypeCode ()
313                 {
314                         return TypeCode.Char;
315                 }         
316         }
317 }