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++){
137 if (c >= '0' && c <= '9'){
138 byte d = (byte) (c - '0');
140 if (val > (MaxValue/10))
143 if (val == (MaxValue/10)){
144 if ((d > (MaxValue % 10)) && (sign == 1 || (d > ((MaxValue % 10) + 1))))
147 val = (short) ((val * sign * 10) - d);
149 val = (short) ((val * 10) + d);
151 if (Int32.ProcessTrailingWhitespace (tryParse, s, i + 1, ref exc)){
157 val = (short) (val * 10 + d);
161 } else if (!Int32.ProcessTrailingWhitespace (tryParse, s, i, ref exc))
167 exc = Int32.GetFormatException ();
172 result = (short) (val * sign);
180 exc = new OverflowException ("Value is too large");
184 public static short Parse (string s, IFormatProvider fp)
186 return Parse (s, NumberStyles.Integer, fp);
189 public static short Parse (string s, NumberStyles style)
191 return Parse (s, style, null);
194 public static short Parse (string s, NumberStyles style, IFormatProvider fp)
196 int tmpResult = Int32.Parse (s, style, fp);
197 if (tmpResult > Int16.MaxValue || tmpResult < Int16.MinValue)
198 throw new OverflowException ("Value too large or too small.");
200 return (short) tmpResult;
203 public static short Parse (string s)
208 if (!Parse (s, false, out res, out exc))
215 public static bool TryParse (string s, out short result)
218 if (!Parse (s, true, out result, out exc)) {
226 public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out short result)
231 if (!Int32.TryParse (s, style, provider, out tmpResult))
234 if (tmpResult > Int16.MaxValue || tmpResult < Int16.MinValue)
237 result = (short)tmpResult;
242 public override string ToString ()
244 return new NumberFormatter(null, m_value).FormatDecimal(-1, null);
247 public string ToString (IFormatProvider provider)
249 NumberFormatInfo nfi = NumberFormatInfo.GetInstance (provider);
250 return new NumberFormatter(null, m_value).FormatDecimal(-1, nfi);
253 public string ToString (string format)
255 return ToString (format, null);
258 public string ToString (string format, IFormatProvider fp)
260 NumberFormatInfo nfi = NumberFormatInfo.GetInstance( fp );
261 return NumberFormatter.NumberToString(format, m_value, nfi);
264 // =========== IConvertible Methods =========== //
266 public TypeCode GetTypeCode ()
268 return TypeCode.Int16;
271 bool IConvertible.ToBoolean (IFormatProvider provider)
273 return System.Convert.ToBoolean (m_value);
276 byte IConvertible.ToByte (IFormatProvider provider)
278 return System.Convert.ToByte (m_value);
281 char IConvertible.ToChar (IFormatProvider provider)
283 return System.Convert.ToChar (m_value);
286 DateTime IConvertible.ToDateTime (IFormatProvider provider)
288 return System.Convert.ToDateTime (m_value);
291 decimal IConvertible.ToDecimal (IFormatProvider provider)
293 return System.Convert.ToDecimal (m_value);
296 double IConvertible.ToDouble (IFormatProvider provider)
298 return System.Convert.ToDouble (m_value);
301 short IConvertible.ToInt16 (IFormatProvider provider)
303 return System.Convert.ToInt16 (m_value);
306 int IConvertible.ToInt32 (IFormatProvider provider)
308 return System.Convert.ToInt32 (m_value);
311 long IConvertible.ToInt64 (IFormatProvider provider)
313 return System.Convert.ToInt64 (m_value);
316 sbyte IConvertible.ToSByte (IFormatProvider provider)
318 return System.Convert.ToSByte (m_value);
321 float IConvertible.ToSingle (IFormatProvider provider)
323 return System.Convert.ToSingle (m_value);
326 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
328 return System.Convert.ToType (m_value, conversionType, provider);
331 ushort IConvertible.ToUInt16 (IFormatProvider provider)
333 return System.Convert.ToUInt16 (m_value);
336 uint IConvertible.ToUInt32 (IFormatProvider provider)
338 return System.Convert.ToUInt32 (m_value);
341 ulong IConvertible.ToUInt64 (IFormatProvider provider)
343 return System.Convert.ToUInt64 (m_value);