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 public struct SByte : IComparable, IFormattable, IConvertible
38 public const sbyte MinValue = -128;
39 public const sbyte MaxValue = 127;
41 internal sbyte m_value;
43 public int CompareTo (object v)
48 if (!(v is System.SByte))
49 throw new ArgumentException (Locale.GetText ("Value is not a System.SByte."));
60 public override bool Equals (object o)
62 if (!(o is System.SByte))
65 return ((sbyte) o) == m_value;
68 public override int GetHashCode ()
74 public static sbyte Parse (string s)
80 bool digits_seen = false;
83 throw new ArgumentNullException ("s");
88 for (i = 0; i < len; i++) {
90 if (!Char.IsWhiteSpace (c))
95 throw new FormatException ();
105 for (; i < len; i++) {
108 if (c >= '0' && c <= '9') {
109 ival = checked (ival * 10 - (int) (c - '0'));
112 if (Char.IsWhiteSpace (c)) {
113 for (i++; i < len; i++) {
114 if (!Char.IsWhiteSpace (s [i]))
115 throw new FormatException ();
119 throw new FormatException ();
123 throw new FormatException ();
125 ival = neg ? ival : -ival;
126 if (ival < SByte.MinValue || ival > SByte.MaxValue)
127 throw new OverflowException ();
132 [CLSCompliant(false)]
133 public static sbyte Parse (string s, IFormatProvider provider)
135 return Parse (s, NumberStyles.Integer, provider);
138 [CLSCompliant(false)]
139 public static sbyte Parse (string s, NumberStyles style)
141 return Parse (s, style, null);
144 [CLSCompliant(false)]
145 public static sbyte Parse (string s, NumberStyles style, IFormatProvider provider)
147 int tmpResult = Int32.Parse (s, style, provider);
148 if (tmpResult > SByte.MaxValue || tmpResult < SByte.MinValue)
149 throw new OverflowException (Locale.GetText ("Value too large or too small."));
151 return (sbyte) tmpResult;
154 public override string ToString ()
156 return ToString (null, null);
159 public string ToString (IFormatProvider provider)
161 return ToString (null, provider);
164 public string ToString (string format)
166 return ToString (format, null);
169 public string ToString (string format, IFormatProvider provider)
171 NumberFormatInfo nfi = NumberFormatInfo.GetInstance (provider);
173 // use "G" when format is null or String.Empty
174 if ((format == null) || (format.Length == 0))
177 return IntegerFormatter.NumberToString (format, nfi, m_value);
180 // =========== ICovnertible Methods =========== //
181 public TypeCode GetTypeCode ()
183 return TypeCode.SByte;
186 bool IConvertible.ToBoolean (IFormatProvider provider)
188 return System.Convert.ToBoolean (m_value);
191 byte IConvertible.ToByte (IFormatProvider provider)
193 return System.Convert.ToByte (m_value);
196 char IConvertible.ToChar (IFormatProvider provider)
198 return System.Convert.ToChar (m_value);
201 DateTime IConvertible.ToDateTime (IFormatProvider provider)
203 return System.Convert.ToDateTime (m_value);
206 decimal IConvertible.ToDecimal (IFormatProvider provider)
208 return System.Convert.ToDecimal (m_value);
211 double IConvertible.ToDouble (IFormatProvider provider)
213 return System.Convert.ToDouble (m_value);
216 short IConvertible.ToInt16 (IFormatProvider provider)
218 return System.Convert.ToInt16 (m_value);
221 int IConvertible.ToInt32 (IFormatProvider provider)
223 return System.Convert.ToInt32 (m_value);
226 long IConvertible.ToInt64 (IFormatProvider provider)
228 return System.Convert.ToInt64 (m_value);
231 sbyte IConvertible.ToSByte (IFormatProvider provider)
236 float IConvertible.ToSingle (IFormatProvider provider)
238 return System.Convert.ToSingle (m_value);
241 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
243 return System.Convert.ToType (m_value, conversionType, provider);
246 ushort IConvertible.ToUInt16 (IFormatProvider provider)
248 return System.Convert.ToUInt16 (m_value);
251 uint IConvertible.ToUInt32 (IFormatProvider provider)
253 return System.Convert.ToUInt32 (m_value);
256 ulong IConvertible.ToUInt64 (IFormatProvider provider)
258 return System.Convert.ToUInt64 (m_value);