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 public struct Int16 : IFormattable, IConvertible, IComparable
37 , IComparable<Int16>, IEquatable <Int16>
41 public const short MaxValue = 32767;
42 public const short MinValue = -32768;
44 internal short m_value;
46 public int CompareTo (object v)
51 if (!(v is System.Int16))
52 throw new ArgumentException (Locale.GetText ("Value is not a System.Int16"));
63 public override bool Equals (object o)
65 if (!(o is System.Int16))
68 return ((short) o) == m_value;
71 public override int GetHashCode ()
77 public int CompareTo (short value)
87 public bool Equals (short value)
89 return value == m_value;
93 internal static bool Parse (string s, bool tryParse, out short result, out Exception exc)
98 bool digits_seen = false;
105 exc = new ArgumentNullException ("s");
112 for (i = 0; i < len; i++){
114 if (!Char.IsWhiteSpace (c))
120 exc = Int32.GetFormatException ();
132 for (; i < len; i++){
135 if (c >= '0' && c <= '9'){
136 val = checked ((short) (val * 10 + (c - '0') * sign));
139 if (Char.IsWhiteSpace (c)){
140 for (i++; i < len; i++){
141 if (!Char.IsWhiteSpace (s [i])) {
143 exc = Int32.GetFormatException ();
150 exc = Int32.GetFormatException ();
157 exc = Int32.GetFormatException ();
165 public static short Parse (string s, IFormatProvider fp)
167 return Parse (s, NumberStyles.Integer, fp);
170 public static short Parse (string s, NumberStyles style)
172 return Parse (s, style, null);
175 public static short Parse (string s, NumberStyles style, IFormatProvider fp)
177 int tmpResult = Int32.Parse (s, style, fp);
178 if (tmpResult > Int16.MaxValue || tmpResult < Int16.MinValue)
179 throw new OverflowException ("Value too large or too small.");
181 return (short) tmpResult;
184 public static short Parse (string s)
189 if (!Parse (s, false, out res, out exc))
196 public static bool TryParse (string s, out short result)
199 if (!Parse (s, true, out result, out exc)) {
207 public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out short result)
212 if (!Int32.TryParse (s, style, provider, out tmpResult))
215 if (tmpResult > Int16.MaxValue || tmpResult < Int16.MinValue)
218 result = (short)tmpResult;
223 public override string ToString ()
225 return NumberFormatter.FormatGeneral (new NumberFormatter.NumberStore (m_value));
228 public string ToString (IFormatProvider fp)
230 return NumberFormatter.FormatGeneral (new NumberFormatter.NumberStore (m_value), fp);
233 public string ToString (string format)
235 return ToString (format, null);
238 public string ToString (string format, IFormatProvider fp)
240 NumberFormatInfo nfi = NumberFormatInfo.GetInstance( fp );
241 return NumberFormatter.NumberToString(format, m_value, nfi);
244 // =========== IConvertible Methods =========== //
246 public TypeCode GetTypeCode ()
248 return TypeCode.Int16;
251 bool IConvertible.ToBoolean (IFormatProvider provider)
253 return System.Convert.ToBoolean (m_value);
256 byte IConvertible.ToByte (IFormatProvider provider)
258 return System.Convert.ToByte (m_value);
261 char IConvertible.ToChar (IFormatProvider provider)
263 return System.Convert.ToChar (m_value);
266 DateTime IConvertible.ToDateTime (IFormatProvider provider)
268 return System.Convert.ToDateTime (m_value);
271 decimal IConvertible.ToDecimal (IFormatProvider provider)
273 return System.Convert.ToDecimal (m_value);
276 double IConvertible.ToDouble (IFormatProvider provider)
278 return System.Convert.ToDouble (m_value);
281 short IConvertible.ToInt16 (IFormatProvider provider)
283 return System.Convert.ToInt16 (m_value);
286 int IConvertible.ToInt32 (IFormatProvider provider)
288 return System.Convert.ToInt32 (m_value);
291 long IConvertible.ToInt64 (IFormatProvider provider)
293 return System.Convert.ToInt64 (m_value);
296 sbyte IConvertible.ToSByte (IFormatProvider provider)
298 return System.Convert.ToSByte (m_value);
301 float IConvertible.ToSingle (IFormatProvider provider)
303 return System.Convert.ToSingle (m_value);
306 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
308 return System.Convert.ToType (m_value, conversionType, provider);
311 ushort IConvertible.ToUInt16 (IFormatProvider provider)
313 return System.Convert.ToUInt16 (m_value);
316 uint IConvertible.ToUInt32 (IFormatProvider provider)
318 return System.Convert.ToUInt32 (m_value);
321 ulong IConvertible.ToUInt64 (IFormatProvider provider)
323 return System.Convert.ToUInt64 (m_value);