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 : IComparable, IFormattable, IConvertible {
37 public const short MaxValue = 32767;
38 public const short MinValue = -32768;
40 internal short m_value;
42 public int CompareTo (object v)
47 if (!(v is System.Int16))
48 throw new ArgumentException (Locale.GetText ("Value is not a System.Int16"));
59 public override bool Equals (object o)
61 if (!(o is System.Int16))
64 return ((short) o) == m_value;
67 public override int GetHashCode ()
72 public static short Parse (string s)
77 bool digits_seen = false;
80 throw new ArgumentNullException ("s");
85 for (i = 0; i < len; i++){
87 if (!Char.IsWhiteSpace (c))
92 throw new FormatException ();
102 for (; i < len; i++){
105 if (c >= '0' && c <= '9'){
106 val = checked ((short) (val * 10 + (c - '0') * sign));
109 if (Char.IsWhiteSpace (c)){
110 for (i++; i < len; i++){
111 if (!Char.IsWhiteSpace (s [i]))
112 throw new FormatException ();
116 throw new FormatException ();
120 throw new FormatException ();
125 public static short Parse (string s, IFormatProvider fp)
127 return Parse (s, NumberStyles.Integer, fp);
130 public static short Parse (string s, NumberStyles style)
132 return Parse (s, style, null);
135 public static short Parse (string s, NumberStyles style, IFormatProvider fp)
137 int tmpResult = Int32.Parse (s, style, fp);
138 if (tmpResult > Int16.MaxValue || tmpResult < Int16.MinValue)
139 throw new OverflowException ("Value too large or too small.");
141 return (short) tmpResult;
144 public override string ToString ()
146 return ToString (null, null);
149 public string ToString (IFormatProvider fp)
151 return ToString (null, fp);
154 public string ToString (string format)
156 return ToString (format, null);
159 public string ToString (string format, IFormatProvider fp)
161 NumberFormatInfo nfi = NumberFormatInfo.GetInstance( fp );
163 // use "G" when format is null or String.Empty
164 if ((format == null) || (format.Length == 0))
167 return IntegerFormatter.NumberToString(format, nfi, m_value);
170 // =========== IConvertible Methods =========== //
172 public TypeCode GetTypeCode ()
174 return TypeCode.Int16;
177 bool IConvertible.ToBoolean (IFormatProvider provider)
179 return System.Convert.ToBoolean (m_value);
182 byte IConvertible.ToByte (IFormatProvider provider)
184 return System.Convert.ToByte (m_value);
187 char IConvertible.ToChar (IFormatProvider provider)
189 return System.Convert.ToChar (m_value);
192 DateTime IConvertible.ToDateTime (IFormatProvider provider)
194 return System.Convert.ToDateTime (m_value);
197 decimal IConvertible.ToDecimal (IFormatProvider provider)
199 return System.Convert.ToDecimal (m_value);
202 double IConvertible.ToDouble (IFormatProvider provider)
204 return System.Convert.ToDouble (m_value);
207 short IConvertible.ToInt16 (IFormatProvider provider)
209 return System.Convert.ToInt16 (m_value);
212 int IConvertible.ToInt32 (IFormatProvider provider)
214 return System.Convert.ToInt32 (m_value);
217 long IConvertible.ToInt64 (IFormatProvider provider)
219 return System.Convert.ToInt64 (m_value);
222 sbyte IConvertible.ToSByte (IFormatProvider provider)
224 return System.Convert.ToSByte (m_value);
227 float IConvertible.ToSingle (IFormatProvider provider)
229 return System.Convert.ToSingle (m_value);
232 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
234 return System.Convert.ToType (m_value, conversionType, provider);
237 ushort IConvertible.ToUInt16 (IFormatProvider provider)
239 return System.Convert.ToUInt16 (m_value);
242 uint IConvertible.ToUInt32 (IFormatProvider provider)
244 return System.Convert.ToUInt32 (m_value);
247 ulong IConvertible.ToUInt64 (IFormatProvider provider)
249 return System.Convert.ToUInt64 (m_value);