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)]
36 public struct UInt16 : IFormattable, IConvertible, IComparable
38 , IComparable<UInt16>, IEquatable <UInt16>
41 public const ushort MaxValue = 0xffff;
42 public const ushort MinValue = 0;
44 internal ushort m_value;
46 public int CompareTo (object value)
51 if(!(value is System.UInt16))
52 throw new ArgumentException (Locale.GetText ("Value is not a System.UInt16."));
54 return this.m_value - ((ushort) value);
57 public override bool Equals (object obj)
59 if (!(obj is System.UInt16))
62 return ((ushort) obj) == m_value;
65 public override int GetHashCode ()
71 public int CompareTo (ushort value)
81 public bool Equals (ushort value)
83 return value == m_value;
87 internal static bool Parse (string s, bool tryParse, out ushort result)
92 bool digits_seen = false;
93 bool has_negative_sign = false;
101 throw new ArgumentNullException ("s");
106 for (i = 0; i < len; i++) {
108 if (!Char.IsWhiteSpace (c))
116 throw new FormatException ();
123 has_negative_sign = true;
126 for (; i < len; i++) {
129 if (c >= '0' && c <= '9') {
130 ushort d = (ushort) (c - '0');
132 val = checked ((ushort) (val * 10 + d));
136 if (Char.IsWhiteSpace (c)) {
137 for (i++; i < len; i++) {
138 if (!Char.IsWhiteSpace (s [i]))
142 throw new FormatException ();
150 throw new FormatException ();
157 throw new FormatException ();
159 // -0 is legal but other negative values are not
160 if (has_negative_sign && (val > 0)) {
164 throw new OverflowException (
165 Locale.GetText ("Negative number"));
172 [CLSCompliant (false)]
173 public static ushort Parse (string s, IFormatProvider provider)
175 return Parse (s, NumberStyles.Integer, provider);
178 [CLSCompliant (false)]
179 public static ushort Parse (string s, NumberStyles style)
181 return Parse (s, style, null);
184 [CLSCompliant (false)]
185 public static ushort Parse (string s, NumberStyles style, IFormatProvider provider)
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."));
191 return (ushort) tmpResult;
194 [CLSCompliant(false)]
195 public static ushort Parse (string s) {
198 Parse (s, false, out res);
204 [CLSCompliant(false)]
205 public static bool TryParse (string s, out ushort result) {
207 return Parse (s, true, out result);
215 [CLSCompliant(false)]
216 public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out ushort result) {
221 if (!UInt32.TryParse (s, style, provider, out tmpResult))
223 if (tmpResult > UInt16.MaxValue || tmpResult < UInt16.MinValue)
225 result = (ushort)tmpResult;
235 public override string ToString ()
237 return NumberFormatter.FormatGeneral (new NumberFormatter.NumberStore (m_value));
240 public string ToString (IFormatProvider provider)
242 return NumberFormatter.FormatGeneral (new NumberFormatter.NumberStore (m_value), provider);
245 public string ToString (string format)
247 return ToString (format, null);
250 public string ToString (string format, IFormatProvider provider)
252 NumberFormatInfo nfi = NumberFormatInfo.GetInstance (provider);
253 return NumberFormatter.NumberToString (format, m_value, nfi);
256 // =========== IConvertible Methods =========== //
257 public TypeCode GetTypeCode ()
259 return TypeCode.UInt16;
262 bool IConvertible.ToBoolean (IFormatProvider provider)
264 return System.Convert.ToBoolean (m_value);
267 byte IConvertible.ToByte (IFormatProvider provider)
269 return System.Convert.ToByte (m_value);
272 char IConvertible.ToChar (IFormatProvider provider)
274 return System.Convert.ToChar (m_value);
277 DateTime IConvertible.ToDateTime (IFormatProvider provider)
279 return System.Convert.ToDateTime (m_value);
282 decimal IConvertible.ToDecimal (IFormatProvider provider)
284 return System.Convert.ToDecimal (m_value);
287 double IConvertible.ToDouble (IFormatProvider provider)
289 return System.Convert.ToDouble (m_value);
292 short IConvertible.ToInt16 (IFormatProvider provider)
294 return System.Convert.ToInt16 (m_value);
297 int IConvertible.ToInt32 (IFormatProvider provider)
299 return System.Convert.ToInt32 (m_value);
302 long IConvertible.ToInt64 (IFormatProvider provider)
304 return System.Convert.ToInt64 (m_value);
307 sbyte IConvertible.ToSByte (IFormatProvider provider)
309 return System.Convert.ToSByte (m_value);
312 float IConvertible.ToSingle (IFormatProvider provider)
314 return System.Convert.ToSingle (m_value);
317 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
319 return System.Convert.ToType (m_value, conversionType, provider);
322 ushort IConvertible.ToUInt16 (IFormatProvider provider)
327 uint IConvertible.ToUInt32 (IFormatProvider provider)
329 return System.Convert.ToUInt32 (m_value);
332 ulong IConvertible.ToUInt64 (IFormatProvider provider)
334 return System.Convert.ToUInt64 (m_value);