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;
36 [System.Runtime.InteropServices.ComVisible (true)]
38 public struct Int16 : IFormattable, IConvertible, IComparable
40 , IComparable<Int16>, IEquatable <Int16>
44 public const short MaxValue = 32767;
45 public const short MinValue = -32768;
47 internal short m_value;
49 public int CompareTo (object v)
54 if (!(v is System.Int16))
55 throw new ArgumentException (Locale.GetText ("Value is not a System.Int16"));
66 public override bool Equals (object o)
68 if (!(o is System.Int16))
71 return ((short) o) == m_value;
74 public override int GetHashCode ()
80 public int CompareTo (short value)
90 public bool Equals (short value)
92 return value == m_value;
96 internal static bool Parse (string s, bool tryParse, out short result, out Exception exc)
101 bool digits_seen = false;
108 exc = new ArgumentNullException ("s");
115 for (i = 0; i < len; i++){
117 if (!Char.IsWhiteSpace (c))
123 exc = Int32.GetFormatException ();
135 for (; i < len; i++){
138 if (c >= '0' && c <= '9'){
139 val = checked ((short) (val * 10 + (c - '0') * sign));
142 if (Char.IsWhiteSpace (c)){
143 for (i++; i < len; i++){
144 if (!Char.IsWhiteSpace (s [i])) {
146 exc = Int32.GetFormatException ();
153 exc = Int32.GetFormatException ();
160 exc = Int32.GetFormatException ();
168 public static short Parse (string s, IFormatProvider fp)
170 return Parse (s, NumberStyles.Integer, fp);
173 public static short Parse (string s, NumberStyles style)
175 return Parse (s, style, null);
178 public static short Parse (string s, NumberStyles style, IFormatProvider fp)
180 int tmpResult = Int32.Parse (s, style, fp);
181 if (tmpResult > Int16.MaxValue || tmpResult < Int16.MinValue)
182 throw new OverflowException ("Value too large or too small.");
184 return (short) tmpResult;
187 public static short Parse (string s)
192 if (!Parse (s, false, out res, out exc))
199 public static bool TryParse (string s, out short result)
202 if (!Parse (s, true, out result, out exc)) {
210 public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out short result)
215 if (!Int32.TryParse (s, style, provider, out tmpResult))
218 if (tmpResult > Int16.MaxValue || tmpResult < Int16.MinValue)
221 result = (short)tmpResult;
226 public override string ToString ()
228 return NumberFormatter.FormatGeneral (new NumberFormatter.NumberStore (m_value));
231 public string ToString (IFormatProvider fp)
233 return NumberFormatter.FormatGeneral (new NumberFormatter.NumberStore (m_value), fp);
236 public string ToString (string format)
238 return ToString (format, null);
241 public string ToString (string format, IFormatProvider fp)
243 NumberFormatInfo nfi = NumberFormatInfo.GetInstance( fp );
244 return NumberFormatter.NumberToString(format, m_value, nfi);
247 // =========== IConvertible Methods =========== //
249 public TypeCode GetTypeCode ()
251 return TypeCode.Int16;
254 bool IConvertible.ToBoolean (IFormatProvider provider)
256 return System.Convert.ToBoolean (m_value);
259 byte IConvertible.ToByte (IFormatProvider provider)
261 return System.Convert.ToByte (m_value);
264 char IConvertible.ToChar (IFormatProvider provider)
266 return System.Convert.ToChar (m_value);
269 DateTime IConvertible.ToDateTime (IFormatProvider provider)
271 return System.Convert.ToDateTime (m_value);
274 decimal IConvertible.ToDecimal (IFormatProvider provider)
276 return System.Convert.ToDecimal (m_value);
279 double IConvertible.ToDouble (IFormatProvider provider)
281 return System.Convert.ToDouble (m_value);
284 short IConvertible.ToInt16 (IFormatProvider provider)
286 return System.Convert.ToInt16 (m_value);
289 int IConvertible.ToInt32 (IFormatProvider provider)
291 return System.Convert.ToInt32 (m_value);
294 long IConvertible.ToInt64 (IFormatProvider provider)
296 return System.Convert.ToInt64 (m_value);
299 sbyte IConvertible.ToSByte (IFormatProvider provider)
301 return System.Convert.ToSByte (m_value);
304 float IConvertible.ToSingle (IFormatProvider provider)
306 return System.Convert.ToSingle (m_value);
309 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
311 return System.Convert.ToType (m_value, conversionType, provider);
314 ushort IConvertible.ToUInt16 (IFormatProvider provider)
316 return System.Convert.ToUInt16 (m_value);
319 uint IConvertible.ToUInt32 (IFormatProvider provider)
321 return System.Convert.ToUInt32 (m_value);
324 ulong IConvertible.ToUInt64 (IFormatProvider provider)
326 return System.Convert.ToUInt64 (m_value);