5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) Ximian, Inc. http://www.ximian.com
8 // Copyright (C) 2004 Novell (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
30 using System.Globalization;
35 [CLSCompliant (false)]
37 [System.Runtime.InteropServices.ComVisible (true)]
39 public struct UInt16 : IFormattable, IConvertible, IComparable
41 , IComparable<UInt16>, IEquatable <UInt16>
44 public const ushort MaxValue = 0xffff;
45 public const ushort MinValue = 0;
47 internal ushort m_value;
49 public int CompareTo (object value)
54 if(!(value is System.UInt16))
55 throw new ArgumentException (Locale.GetText ("Value is not a System.UInt16."));
57 return this.m_value - ((ushort) value);
60 public override bool Equals (object obj)
62 if (!(obj is System.UInt16))
65 return ((ushort) obj) == m_value;
68 public override int GetHashCode ()
74 public int CompareTo (ushort value)
84 public bool Equals (ushort value)
86 return value == m_value;
90 internal static bool Parse (string s, bool tryParse, out ushort result, out Exception exc)
95 bool digits_seen = false;
96 bool has_negative_sign = false;
103 exc = new ArgumentNullException ("s");
110 for (i = 0; i < len; i++) {
112 if (!Char.IsWhiteSpace (c))
118 exc = Int32.GetFormatException ();
127 has_negative_sign = true;
130 for (; i < len; i++) {
133 if (c >= '0' && c <= '9') {
134 ushort d = (ushort) (c - '0');
136 val = checked ((ushort) (val * 10 + d));
140 if (Char.IsWhiteSpace (c)) {
141 for (i++; i < len; i++) {
142 if (!Char.IsWhiteSpace (s [i])) {
144 exc = Int32.GetFormatException ();
152 exc = Int32.GetFormatException ();
159 exc = Int32.GetFormatException ();
163 // -0 is legal but other negative values are not
164 if (has_negative_sign && (val > 0)) {
166 exc = new OverflowException (
167 Locale.GetText ("Negative number"));
175 [CLSCompliant (false)]
176 public static ushort Parse (string s, IFormatProvider provider)
178 return Parse (s, NumberStyles.Integer, provider);
181 [CLSCompliant (false)]
182 public static ushort Parse (string s, NumberStyles style)
184 return Parse (s, style, null);
187 [CLSCompliant (false)]
188 public static ushort Parse (string s, NumberStyles style, IFormatProvider provider)
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."));
194 return (ushort) tmpResult;
197 [CLSCompliant(false)]
198 public static ushort Parse (string s)
203 if (!Parse (s, false, out res, out exc))
210 [CLSCompliant(false)]
211 public static bool TryParse (string s, out ushort result)
214 if (!Parse (s, true, out result, out exc)) {
222 [CLSCompliant(false)]
223 public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out ushort result)
228 if (!UInt32.TryParse (s, style, provider, out tmpResult))
231 if (tmpResult > UInt16.MaxValue || tmpResult < UInt16.MinValue)
234 result = (ushort)tmpResult;
239 public override string ToString ()
241 return NumberFormatter.FormatGeneral (new NumberFormatter.NumberStore (m_value));
244 public string ToString (IFormatProvider provider)
246 return NumberFormatter.FormatGeneral (new NumberFormatter.NumberStore (m_value), provider);
249 public string ToString (string format)
251 return ToString (format, null);
254 public string ToString (string format, IFormatProvider provider)
256 NumberFormatInfo nfi = NumberFormatInfo.GetInstance (provider);
257 return NumberFormatter.NumberToString (format, m_value, nfi);
260 // =========== IConvertible Methods =========== //
261 public TypeCode GetTypeCode ()
263 return TypeCode.UInt16;
266 bool IConvertible.ToBoolean (IFormatProvider provider)
268 return System.Convert.ToBoolean (m_value);
271 byte IConvertible.ToByte (IFormatProvider provider)
273 return System.Convert.ToByte (m_value);
276 char IConvertible.ToChar (IFormatProvider provider)
278 return System.Convert.ToChar (m_value);
281 DateTime IConvertible.ToDateTime (IFormatProvider provider)
283 return System.Convert.ToDateTime (m_value);
286 decimal IConvertible.ToDecimal (IFormatProvider provider)
288 return System.Convert.ToDecimal (m_value);
291 double IConvertible.ToDouble (IFormatProvider provider)
293 return System.Convert.ToDouble (m_value);
296 short IConvertible.ToInt16 (IFormatProvider provider)
298 return System.Convert.ToInt16 (m_value);
301 int IConvertible.ToInt32 (IFormatProvider provider)
303 return System.Convert.ToInt32 (m_value);
306 long IConvertible.ToInt64 (IFormatProvider provider)
308 return System.Convert.ToInt64 (m_value);
311 sbyte IConvertible.ToSByte (IFormatProvider provider)
313 return System.Convert.ToSByte (m_value);
316 float IConvertible.ToSingle (IFormatProvider provider)
318 return System.Convert.ToSingle (m_value);
321 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
323 return System.Convert.ToType (m_value, conversionType, provider);
326 ushort IConvertible.ToUInt16 (IFormatProvider provider)
331 uint IConvertible.ToUInt32 (IFormatProvider provider)
333 return System.Convert.ToUInt32 (m_value);
336 ulong IConvertible.ToUInt64 (IFormatProvider provider)
338 return System.Convert.ToUInt64 (m_value);