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