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 [System.Runtime.InteropServices.ComVisible (true)]
36 public struct Int16 : IFormattable, IConvertible, IComparable, IComparable<Int16>, IEquatable <Int16>
39 public const short MaxValue = 32767;
40 public const short MinValue = -32768;
42 internal short m_value;
44 public int CompareTo (object value)
49 if (!(value is System.Int16))
50 throw new ArgumentException (Locale.GetText ("Value is not a System.Int16"));
52 return CompareTo ((short) value);
55 public override bool Equals (object obj)
57 if (!(obj is System.Int16))
60 return ((short) obj) == m_value;
63 public override int GetHashCode ()
68 public int CompareTo (short value)
70 return m_value - value;
73 public bool Equals (short obj)
75 return obj == m_value;
78 internal static bool Parse (string s, bool tryParse, out short result, out Exception exc)
83 bool digits_seen = false;
90 exc = new ArgumentNullException ("s");
97 for (i = 0; i < len; i++){
99 if (!Char.IsWhiteSpace (c))
105 exc = Int32.GetFormatException ();
117 for (; i < len; i++){
119 if (c >= '0' && c <= '9'){
120 byte d = (byte) (c - '0');
122 if (val > (MaxValue/10))
125 if (val == (MaxValue/10)){
126 if ((d > (MaxValue % 10)) && (sign == 1 || (d > ((MaxValue % 10) + 1))))
129 val = (short) ((val * sign * 10) - d);
131 val = (short) ((val * 10) + d);
133 if (Int32.ProcessTrailingWhitespace (tryParse, s, i + 1, ref exc)){
139 val = (short) (val * 10 + d);
143 } else if (!Int32.ProcessTrailingWhitespace (tryParse, s, i, ref exc))
149 exc = Int32.GetFormatException ();
154 result = (short) (val * sign);
162 exc = new OverflowException ("Value is too large");
166 public static short Parse (string s, IFormatProvider provider)
168 return Parse (s, NumberStyles.Integer, provider);
171 public static short Parse (string s, NumberStyles style)
173 return Parse (s, style, null);
176 public static short Parse (string s, NumberStyles style, IFormatProvider provider)
178 int tmpResult = Int32.Parse (s, style, provider);
179 if ((style & NumberStyles.AllowHexSpecifier) != 0) {
180 if (tmpResult >= 0 && tmpResult <= ushort.MaxValue)
181 return (short) tmpResult;
182 } else if (tmpResult <= MaxValue && tmpResult >= MinValue) {
183 return (short) tmpResult;
186 throw new OverflowException ("Value too large or too small.");
189 public static short Parse (string s)
194 if (!Parse (s, false, out res, out exc))
200 public static bool TryParse (string s, out short result)
203 if (!Parse (s, true, out result, out exc)) {
211 public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out short result)
216 if (!Int32.TryParse (s, style, provider, out tmpResult))
219 if (tmpResult > Int16.MaxValue || tmpResult < Int16.MinValue)
222 result = (short)tmpResult;
226 public override string ToString ()
228 return NumberFormatter.NumberToString (m_value, null);
231 public string ToString (IFormatProvider provider)
233 return NumberFormatter.NumberToString (m_value, provider);
236 public string ToString (string format)
238 return ToString (format, null);
241 public string ToString (string format, IFormatProvider provider)
243 return NumberFormatter.NumberToString(format, m_value, provider);
246 // =========== IConvertible Methods =========== //
248 public TypeCode GetTypeCode ()
250 return TypeCode.Int16;
253 bool IConvertible.ToBoolean (IFormatProvider provider)
255 return System.Convert.ToBoolean (m_value);
258 byte IConvertible.ToByte (IFormatProvider provider)
260 return System.Convert.ToByte (m_value);
263 char IConvertible.ToChar (IFormatProvider provider)
265 return System.Convert.ToChar (m_value);
268 DateTime IConvertible.ToDateTime (IFormatProvider provider)
270 return System.Convert.ToDateTime (m_value);
273 decimal IConvertible.ToDecimal (IFormatProvider provider)
275 return System.Convert.ToDecimal (m_value);
278 double IConvertible.ToDouble (IFormatProvider provider)
280 return System.Convert.ToDouble (m_value);
283 short IConvertible.ToInt16 (IFormatProvider provider)
285 return System.Convert.ToInt16 (m_value);
288 int IConvertible.ToInt32 (IFormatProvider provider)
290 return System.Convert.ToInt32 (m_value);
293 long IConvertible.ToInt64 (IFormatProvider provider)
295 return System.Convert.ToInt64 (m_value);
298 sbyte IConvertible.ToSByte (IFormatProvider provider)
300 return System.Convert.ToSByte (m_value);
303 float IConvertible.ToSingle (IFormatProvider provider)
305 return System.Convert.ToSingle (m_value);
308 object IConvertible.ToType (Type targetType, IFormatProvider provider)
310 if (targetType == null)
311 throw new ArgumentNullException ("targetType");
312 return System.Convert.ToType (m_value, targetType, provider, false);
315 ushort IConvertible.ToUInt16 (IFormatProvider provider)
317 return System.Convert.ToUInt16 (m_value);
320 uint IConvertible.ToUInt32 (IFormatProvider provider)
322 return System.Convert.ToUInt32 (m_value);
325 ulong IConvertible.ToUInt64 (IFormatProvider provider)
327 return System.Convert.ToUInt64 (m_value);