2005-06-04 Ben Maurer <bmaurer@ximian.com>
[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 // Copyright (C) 2004 Novell (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Globalization;
31
32 namespace System
33 {
34         [Serializable]
35         [CLSCompliant (false)]
36         public struct UInt16 : IFormattable, IConvertible, IComparable
37 #if NET_2_0
38                 , IComparable<UInt16>, IEquatable <UInt16>
39 #endif
40         {
41                 public const ushort MaxValue = 0xffff;
42                 public const ushort MinValue = 0;
43
44                 internal ushort m_value;
45
46                 public int CompareTo (object value)
47                 {
48                         if (value == null)
49                                 return 1;
50
51                         if(!(value is System.UInt16))
52                                 throw new ArgumentException (Locale.GetText ("Value is not a System.UInt16."));
53
54                         return this.m_value - ((ushort) value);
55                 }
56
57                 public override bool Equals (object obj)
58                 {
59                         if (!(obj is System.UInt16))
60                                 return false;
61
62                         return ((ushort) obj) == m_value;
63                 }
64
65                 public override int GetHashCode ()
66                 {
67                         return m_value;
68                 }
69
70 #if NET_2_0
71                 public int CompareTo (ushort value)
72                 {
73                         if (m_value == value)
74                                 return 0;
75                         if (m_value > value)
76                                 return 1;
77                         else
78                                 return -1;
79                 }
80
81                 public bool Equals (ushort value)
82                 {
83                         return value == m_value;
84                 }
85 #endif
86
87                 internal static bool Parse (string s, bool tryParse, out ushort result)
88                 {
89                         ushort val = 0;
90                         int len;
91                         int i;
92                         bool digits_seen = false;
93                         bool has_negative_sign = false;
94
95                         result = 0;
96
97                         if (s == null)
98                                 if (tryParse)
99                                         return false;
100                                 else
101                                         throw new ArgumentNullException ("s");
102
103                         len = s.Length;
104
105                         char c;
106                         for (i = 0; i < len; i++) {
107                                 c = s [i];
108                                 if (!Char.IsWhiteSpace (c))
109                                         break;
110                         }
111
112                         if (i == len)
113                                 if (tryParse)
114                                         return false;
115                                 else
116                                         throw new FormatException ();
117
118                         if (s [i] == '+')
119                                 i++;
120                         else
121                                 if (s[i] == '-') {
122                                         i++;
123                                         has_negative_sign = true;
124                                 }
125
126                         for (; i < len; i++) {
127                                 c = s [i];
128
129                                 if (c >= '0' && c <= '9') {
130                                         ushort d = (ushort) (c - '0');
131
132                                         val = checked ((ushort) (val * 10 + d));
133                                         digits_seen = true;
134                                 }
135                                 else {
136                                         if (Char.IsWhiteSpace (c)) {
137                                                 for (i++; i < len; i++) {
138                                                         if (!Char.IsWhiteSpace (s [i]))
139                                                                 if (tryParse)
140                                                                         return false;
141                                                                 else
142                                                                         throw new FormatException ();
143                                                 }
144                                                 break;
145                                         }
146                                         else
147                                                 if (tryParse)
148                                                         return false;
149                                                 else
150                                                         throw new FormatException ();
151                                 }
152                         }
153                         if (!digits_seen)
154                                 if (tryParse)
155                                         return false;
156                                 else
157                                         throw new FormatException ();
158
159                         // -0 is legal but other negative values are not
160                         if (has_negative_sign && (val > 0)) {
161                                 if (tryParse)
162                                         return false;
163                                 else
164                                         throw new OverflowException (
165                                             Locale.GetText ("Negative number"));
166                         }
167
168                         result = val;
169                         return true;
170                 }
171
172                 [CLSCompliant (false)]
173                 public static ushort Parse (string s, IFormatProvider provider)
174                 {
175                         return Parse (s, NumberStyles.Integer, provider);
176                 }
177
178                 [CLSCompliant (false)]
179                 public static ushort Parse (string s, NumberStyles style)
180                 {
181                         return Parse (s, style, null);
182                 }
183
184                 [CLSCompliant (false)]
185                 public static ushort Parse (string s, NumberStyles style, IFormatProvider provider)
186                 {
187                         uint tmpResult = UInt32.Parse (s, style, provider);
188                         if (tmpResult > UInt16.MaxValue || tmpResult < UInt16.MinValue)
189                                 throw new OverflowException (Locale.GetText ("Value too large or too small."));
190
191                         return (ushort) tmpResult;
192                 }
193
194                 [CLSCompliant(false)]
195                 public static ushort Parse (string s) {
196                         ushort res;
197
198                         Parse (s, false, out res);
199
200                         return res;
201                 }
202
203 #if NET_2_0
204                 [CLSCompliant(false)]
205                 public static bool TryParse (string s, out ushort result) {
206                         try {
207                                 return Parse (s, true, out result);
208                         }
209                         catch (Exception) {
210                                 result = 0;
211                                 return false;
212                         }
213                 }
214
215                 [CLSCompliant(false)]
216                 public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out ushort result) {
217                         try {
218                                 uint tmpResult;
219
220                                 result = 0;
221                                 if (!UInt32.TryParse (s, style, provider, out tmpResult))
222                                         return false;
223                                 if (tmpResult > UInt16.MaxValue || tmpResult < UInt16.MinValue)
224                                         return false;
225                                 result = (ushort)tmpResult;
226                                 return true;
227                         }
228                         catch (Exception) {
229                                 result = 0;
230                                 return false;
231                         }
232                 }
233 #endif
234
235                 public override string ToString ()
236                 {
237                         return NumberFormatter.FormatGeneral (new NumberFormatter.NumberStore (m_value));
238                 }
239
240                 public string ToString (IFormatProvider provider)
241                 {
242                         return NumberFormatter.FormatGeneral (new NumberFormatter.NumberStore (m_value), provider);
243                 }
244
245                 public string ToString (string format)
246                 {
247                         return ToString (format, null);
248                 }
249
250                 public string ToString (string format, IFormatProvider provider)
251                 {
252                         NumberFormatInfo nfi = NumberFormatInfo.GetInstance (provider);
253                         return NumberFormatter.NumberToString (format, m_value, nfi);
254                 }
255
256                 // =========== IConvertible Methods =========== //
257                 public TypeCode GetTypeCode ()
258                 {
259                         return TypeCode.UInt16;
260                 }
261
262                 bool IConvertible.ToBoolean (IFormatProvider provider)
263                 {
264                         return System.Convert.ToBoolean (m_value);
265                 }
266
267                 byte IConvertible.ToByte (IFormatProvider provider)
268                 {
269                         return System.Convert.ToByte (m_value);
270                 }
271
272                 char IConvertible.ToChar (IFormatProvider provider)
273                 {
274                         return System.Convert.ToChar (m_value);
275                 }
276
277                 DateTime IConvertible.ToDateTime (IFormatProvider provider)
278                 {
279                         return System.Convert.ToDateTime (m_value);
280                 }
281
282                 decimal IConvertible.ToDecimal (IFormatProvider provider)
283                 {
284                         return System.Convert.ToDecimal (m_value);
285                 }
286
287                 double IConvertible.ToDouble (IFormatProvider provider)
288                 {
289                         return System.Convert.ToDouble (m_value);
290                 }
291
292                 short IConvertible.ToInt16 (IFormatProvider provider)
293                 {
294                         return System.Convert.ToInt16 (m_value);
295                 }
296
297                 int IConvertible.ToInt32 (IFormatProvider provider)
298                 {
299                         return System.Convert.ToInt32 (m_value);
300                 }
301
302                 long IConvertible.ToInt64 (IFormatProvider provider)
303                 {
304                         return System.Convert.ToInt64 (m_value);
305                 }
306
307                 sbyte IConvertible.ToSByte (IFormatProvider provider)
308                 {
309                         return System.Convert.ToSByte (m_value);
310                 }
311
312                 float IConvertible.ToSingle (IFormatProvider provider)
313                 {
314                         return System.Convert.ToSingle (m_value);
315                 }
316
317                 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
318                 {
319                         return System.Convert.ToType (m_value, conversionType, provider);
320                 }
321
322                 ushort IConvertible.ToUInt16 (IFormatProvider provider)
323                 {
324                         return m_value;
325                 }
326
327                 uint IConvertible.ToUInt32 (IFormatProvider provider)
328                 {
329                         return System.Convert.ToUInt32 (m_value);
330                 }
331
332                 ulong IConvertible.ToUInt64 (IFormatProvider provider)
333                 {
334                         return System.Convert.ToUInt64 (m_value);
335                 }
336         }
337 }