2002-06-27 Martin Baulig <martin@gnome.org>
[mono.git] / mcs / class / corlib / System / UInt16.cs
1 //
2 // System.UInt16.cs
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) Ximian, Inc.  http://www.ximian.com
8 //
9
10 using System.Globalization;
11
12 namespace System {
13
14         [CLSCompliant(false)]
15         [Serializable]
16         public struct UInt16 : IComparable, IFormattable, IConvertible {
17
18                 public const ushort MaxValue = 0xffff;
19                 public const ushort MinValue = 0;
20                 
21                 public ushort value;
22
23                 public int CompareTo (object v)
24                 {
25                         if (v == null)
26                                 return 1;
27                         
28                         if(!(v is System.UInt16))
29                                 throw new ArgumentException (Locale.GetText ("Value is not a System.UInt16"));
30
31                         return value - ((ushort) v);
32                 }
33
34                 public override bool Equals (object o)
35                 {
36                         if (!(o is System.UInt16))
37                                 return false;
38
39                         return ((ushort) o) == value;
40                 }
41
42                 public override int GetHashCode ()
43                 {
44                         return value;
45                 }
46
47                 [CLSCompliant(false)]
48                 public static ushort Parse (string s)
49                 {
50                         ushort val = 0;
51                         int len;
52                         int i;
53                         bool digits_seen = false;
54                         bool has_negative_sign = false;
55                         
56                         if (s == null)
57                                 throw new ArgumentNullException (Locale.GetText ("s is null"));
58
59                         len = s.Length;
60
61                         char c;
62                         for (i = 0; i < len; i++){
63                                 c = s [i];
64                                 if (!Char.IsWhiteSpace (c))
65                                         break;
66                         }
67                         
68                         if (i == len)
69                                 throw new FormatException ();
70
71                         if (s [i] == '+')
72                                 i++;
73                         else
74                                 if (s[i] == '-'){
75                                         i++;
76                                         has_negative_sign = true;
77                                 }
78
79                         for (; i < len; i++){
80                                 c = s [i];
81
82                                 if (c >= '0' && c <= '9'){
83                                         ushort d = (ushort) (c - '0');
84                                         
85                                         val = checked ((ushort) (val * 10 + d));
86                                         digits_seen = true;
87                                 } else {
88                                         if (Char.IsWhiteSpace (c)){
89                                                 for (i++; i < len; i++){
90                                                         if (!Char.IsWhiteSpace (s [i]))
91                                                                 throw new FormatException ();
92                                                 }
93                                                 break;
94                                         } else
95                                                 throw new FormatException ();
96                                 }
97                         }
98                         if (!digits_seen)
99                                 throw new FormatException ();
100                         
101                         if (has_negative_sign)
102                                 throw new OverflowException ();
103
104                         return val;
105
106                 }
107
108                 [CLSCompliant(false)]
109                 public static ushort Parse (string s, IFormatProvider fp)
110                 {
111                         return Parse (s, NumberStyles.Integer, fp);
112                 }
113
114                 [CLSCompliant(false)]
115                 public static ushort Parse (string s, NumberStyles style)
116                 {
117                         return Parse (s, style, null);
118                 }
119
120                 [CLSCompliant(false)]
121                 public static ushort Parse (string s, NumberStyles style, IFormatProvider fp)
122                 {
123                         uint tmpResult = UInt32.Parse (s, style, fp);
124                         if (tmpResult > UInt16.MaxValue || tmpResult < UInt16.MinValue)
125                                 throw new OverflowException ("Value too large or too small.");
126
127                         return (ushort) tmpResult;
128                 }
129
130                 public override string ToString ()
131                 {
132                         return ToString (null, null);
133                 }
134
135                 public string ToString (IFormatProvider fp)
136                 {
137                         return ToString (null, fp);
138                 }
139
140                 public string ToString (string format)
141                 {
142                         return ToString (format, null);
143                 }
144
145                 public string ToString (string format, IFormatProvider fp)
146                 {
147                         NumberFormatInfo nfi = NumberFormatInfo.GetInstance( fp );
148                         
149                         if ( format == null )
150                                 format = "G";
151                         
152                         return IntegerFormatter.NumberToString(format, nfi, value);
153                 }
154
155                 // =========== IConvertible Methods =========== //
156
157                 public TypeCode GetTypeCode ()
158                 {
159                         return TypeCode.UInt16;
160                 }
161
162                 bool IConvertible.ToBoolean (IFormatProvider provider)
163                 {
164                         return System.Convert.ToBoolean (value);
165                 }
166
167                 byte IConvertible.ToByte (IFormatProvider provider)
168                 {
169                         return System.Convert.ToByte (value);
170                 }
171
172                 char IConvertible.ToChar (IFormatProvider provider)
173                 {
174                         return System.Convert.ToChar (value);
175                 }
176
177                 DateTime IConvertible.ToDateTime (IFormatProvider provider)
178                 {
179                         return System.Convert.ToDateTime (value);
180                 }
181
182                 decimal IConvertible.ToDecimal (IFormatProvider provider)
183                 {
184                         return System.Convert.ToDecimal (value);
185                 }
186
187                 double IConvertible.ToDouble (IFormatProvider provider)
188                 {
189                         return System.Convert.ToDouble (value);
190                 }
191
192                 short IConvertible.ToInt16 (IFormatProvider provider)
193                 {
194                         return System.Convert.ToInt16 (value);
195                 }
196
197                 int IConvertible.ToInt32 (IFormatProvider provider)
198                 {
199                         return System.Convert.ToInt32 (value);
200                 }
201
202                 long IConvertible.ToInt64 (IFormatProvider provider)
203                 {
204                         return System.Convert.ToInt64 (value);
205                 }
206
207                 [CLSCompliant (false)]
208                 sbyte IConvertible.ToSByte (IFormatProvider provider)
209                 {
210                         return System.Convert.ToSByte (value);
211                 }
212                 
213                 float IConvertible.ToSingle (IFormatProvider provider)
214                 {
215                         return System.Convert.ToSingle (value);
216                 }
217
218                 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
219                 {
220                         return System.Convert.ToType (value, conversionType, provider);
221                 }
222
223                 [CLSCompliant (false)]
224                 ushort IConvertible.ToUInt16 (IFormatProvider provider)
225                 {
226                         return System.Convert.ToUInt16 (value);
227                 }
228
229                 [CLSCompliant (false)]
230                 uint IConvertible.ToUInt32 (IFormatProvider provider)
231                 {
232                         return System.Convert.ToUInt32 (value);
233                 }
234
235                 [CLSCompliant (false)]
236                 ulong IConvertible.ToUInt64 (IFormatProvider provider)
237                 {
238                         return System.Convert.ToUInt64 (value);
239                 }
240         }
241 }