2007-05-23 Atsushi Enomoto <atsushi@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 #if NET_2_0
37         [System.Runtime.InteropServices.ComVisible (true)]
38 #endif
39         public struct UInt16 : IFormattable, IConvertible, IComparable
40 #if NET_2_0
41                 , IComparable<UInt16>, IEquatable <UInt16>
42 #endif
43         {
44                 public const ushort MaxValue = 0xffff;
45                 public const ushort MinValue = 0;
46
47                 internal ushort m_value;
48
49                 public int CompareTo (object value)
50                 {
51                         if (value == null)
52                                 return 1;
53
54                         if(!(value is System.UInt16))
55                                 throw new ArgumentException (Locale.GetText ("Value is not a System.UInt16."));
56
57                         return this.m_value - ((ushort) value);
58                 }
59
60                 public override bool Equals (object obj)
61                 {
62                         if (!(obj is System.UInt16))
63                                 return false;
64
65                         return ((ushort) obj) == m_value;
66                 }
67
68                 public override int GetHashCode ()
69                 {
70                         return m_value;
71                 }
72
73 #if NET_2_0
74                 public int CompareTo (ushort value)
75                 {
76                         if (m_value == value)
77                                 return 0;
78                         if (m_value > value)
79                                 return 1;
80                         else
81                                 return -1;
82                 }
83
84                 public bool Equals (ushort value)
85                 {
86                         return value == m_value;
87                 }
88 #endif
89
90                 internal static bool Parse (string s, bool tryParse, out ushort result, out Exception exc)
91                 {
92                         ushort val = 0;
93                         int len;
94                         int i;
95                         bool digits_seen = false;
96                         bool has_negative_sign = false;
97
98                         result = 0;
99                         exc = null;
100
101                         if (s == null) {
102                                 if (!tryParse)
103                                         exc = new ArgumentNullException ("s");
104                                 return false;
105                         }
106
107                         len = s.Length;
108
109                         char c;
110                         for (i = 0; i < len; i++) {
111                                 c = s [i];
112                                 if (!Char.IsWhiteSpace (c))
113                                         break;
114                         }
115
116                         if (i == len) {
117                                 if (!tryParse)
118                                         exc = Int32.GetFormatException ();
119                                 return false;
120                         }
121
122                         if (s [i] == '+')
123                                 i++;
124                         else
125                                 if (s[i] == '-') {
126                                         i++;
127                                         has_negative_sign = true;
128                                 }
129
130                         for (; i < len; i++) {
131                                 c = s [i];
132
133                                 if (c >= '0' && c <= '9') {
134                                         ushort d = (ushort) (c - '0');
135
136                                         val = checked ((ushort) (val * 10 + d));
137                                         digits_seen = true;
138                                 }
139                                 else {
140                                         if (Char.IsWhiteSpace (c)) {
141                                                 for (i++; i < len; i++) {
142                                                         if (!Char.IsWhiteSpace (s [i])) {
143                                                                 if (!tryParse)
144                                                                         exc = Int32.GetFormatException ();
145                                                                 return false;
146                                                         }
147                                                 }
148                                                 break;
149                                         }
150                                         else {
151                                                 if (!tryParse)
152                                                         exc = Int32.GetFormatException ();
153                                                 return false;
154                                         }
155                                 }
156                         }
157                         if (!digits_seen) {
158                                 if (!tryParse)
159                                         exc = Int32.GetFormatException ();
160                                 return false;
161                         }
162
163                         // -0 is legal but other negative values are not
164                         if (has_negative_sign && (val > 0)) {
165                                 if (!tryParse)
166                                         exc = new OverflowException (
167                                             Locale.GetText ("Negative number"));
168                                 return false;
169                         }
170
171                         result = val;
172                         return true;
173                 }
174
175                 [CLSCompliant (false)]
176                 public static ushort Parse (string s, IFormatProvider provider)
177                 {
178                         return Parse (s, NumberStyles.Integer, provider);
179                 }
180
181                 [CLSCompliant (false)]
182                 public static ushort Parse (string s, NumberStyles style)
183                 {
184                         return Parse (s, style, null);
185                 }
186
187                 [CLSCompliant (false)]
188                 public static ushort Parse (string s, NumberStyles style, IFormatProvider provider)
189                 {
190                         uint tmpResult = UInt32.Parse (s, style, provider);
191                         if (tmpResult > UInt16.MaxValue || tmpResult < UInt16.MinValue)
192                                 throw new OverflowException (Locale.GetText ("Value too large or too small."));
193
194                         return (ushort) tmpResult;
195                 }
196
197                 [CLSCompliant(false)]
198                 public static ushort Parse (string s) 
199                 {
200                         Exception exc;
201                         ushort res;
202
203                         if (!Parse (s, false, out res, out exc))
204                                 throw exc;
205
206                         return res;
207                 }
208
209 #if NET_2_0
210                 [CLSCompliant(false)]
211                 public static bool TryParse (string s, out ushort result) 
212                 {
213                         Exception exc;
214                         if (!Parse (s, true, out result, out exc)) {
215                                 result = 0;
216                                 return false;
217                         }
218
219                         return true;
220                 }
221
222                 [CLSCompliant(false)]
223                 public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out ushort result) 
224                 {
225                         uint tmpResult;
226                         result = 0;
227                                 
228                         if (!UInt32.TryParse (s, style, provider, out tmpResult))
229                                 return false;
230                                 
231                         if (tmpResult > UInt16.MaxValue || tmpResult < UInt16.MinValue)
232                                 return false;
233                                 
234                         result = (ushort)tmpResult;
235                         return true;
236                 }
237 #endif
238
239                 public override string ToString ()
240                 {
241                         return NumberFormatter.FormatGeneral (new NumberFormatter.NumberStore (m_value));
242                 }
243
244                 public string ToString (IFormatProvider provider)
245                 {
246                         return NumberFormatter.FormatGeneral (new NumberFormatter.NumberStore (m_value), provider);
247                 }
248
249                 public string ToString (string format)
250                 {
251                         return ToString (format, null);
252                 }
253
254                 public string ToString (string format, IFormatProvider provider)
255                 {
256                         NumberFormatInfo nfi = NumberFormatInfo.GetInstance (provider);
257                         return NumberFormatter.NumberToString (format, m_value, nfi);
258                 }
259
260                 // =========== IConvertible Methods =========== //
261                 public TypeCode GetTypeCode ()
262                 {
263                         return TypeCode.UInt16;
264                 }
265
266                 bool IConvertible.ToBoolean (IFormatProvider provider)
267                 {
268                         return System.Convert.ToBoolean (m_value);
269                 }
270
271                 byte IConvertible.ToByte (IFormatProvider provider)
272                 {
273                         return System.Convert.ToByte (m_value);
274                 }
275
276                 char IConvertible.ToChar (IFormatProvider provider)
277                 {
278                         return System.Convert.ToChar (m_value);
279                 }
280
281                 DateTime IConvertible.ToDateTime (IFormatProvider provider)
282                 {
283                         return System.Convert.ToDateTime (m_value);
284                 }
285
286                 decimal IConvertible.ToDecimal (IFormatProvider provider)
287                 {
288                         return System.Convert.ToDecimal (m_value);
289                 }
290
291                 double IConvertible.ToDouble (IFormatProvider provider)
292                 {
293                         return System.Convert.ToDouble (m_value);
294                 }
295
296                 short IConvertible.ToInt16 (IFormatProvider provider)
297                 {
298                         return System.Convert.ToInt16 (m_value);
299                 }
300
301                 int IConvertible.ToInt32 (IFormatProvider provider)
302                 {
303                         return System.Convert.ToInt32 (m_value);
304                 }
305
306                 long IConvertible.ToInt64 (IFormatProvider provider)
307                 {
308                         return System.Convert.ToInt64 (m_value);
309                 }
310
311                 sbyte IConvertible.ToSByte (IFormatProvider provider)
312                 {
313                         return System.Convert.ToSByte (m_value);
314                 }
315
316                 float IConvertible.ToSingle (IFormatProvider provider)
317                 {
318                         return System.Convert.ToSingle (m_value);
319                 }
320
321                 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
322                 {
323                         return System.Convert.ToType (m_value, conversionType, provider);
324                 }
325
326                 ushort IConvertible.ToUInt16 (IFormatProvider provider)
327                 {
328                         return m_value;
329                 }
330
331                 uint IConvertible.ToUInt32 (IFormatProvider provider)
332                 {
333                         return System.Convert.ToUInt32 (m_value);
334                 }
335
336                 ulong IConvertible.ToUInt64 (IFormatProvider provider)
337                 {
338                         return System.Convert.ToUInt64 (m_value);
339                 }
340         }
341 }