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