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)]
37 public struct SByte : IFormattable, IConvertible, IComparable, IComparable<SByte>, IEquatable <SByte>
39 public const sbyte MinValue = -128;
40 public const sbyte MaxValue = 127;
42 internal sbyte m_value;
44 public int CompareTo (object obj)
49 if (!(obj is System.SByte))
50 throw new ArgumentException (Locale.GetText ("Value is not a System.SByte."));
52 return CompareTo ((sbyte) obj);
55 public override bool Equals (object obj)
57 if (!(obj is System.SByte))
60 return ((sbyte) obj) == m_value;
63 public override int GetHashCode ()
68 public int CompareTo (sbyte value)
70 return m_value - value;
73 public bool Equals (sbyte obj)
75 return obj == m_value;
78 internal static bool Parse (string s, bool tryParse, out sbyte result, out Exception exc)
84 bool digits_seen = false;
91 exc = new ArgumentNullException ("s");
98 for (i = 0; i < len; i++) {
100 if (!Char.IsWhiteSpace (c))
106 exc = Int32.GetFormatException ();
118 for (; i < len; i++) {
121 if (c >= '0' && c <= '9') {
123 int intval = ival * 10 - (int) (c - '0');
125 if (intval < MinValue)
127 ival = (sbyte) intval;
129 ival = checked (ival * 10 - (int) (c - '0'));
132 if (Char.IsWhiteSpace (c)) {
133 for (i++; i < len; i++) {
134 if (!Char.IsWhiteSpace (s [i])) {
136 exc = Int32.GetFormatException ();
143 exc = Int32.GetFormatException ();
150 exc = Int32.GetFormatException ();
154 ival = neg ? ival : -ival;
155 if (ival < SByte.MinValue || ival > SByte.MaxValue) {
157 exc = new OverflowException ();
161 result = (sbyte)ival;
165 [CLSCompliant(false)]
166 public static sbyte Parse (string s, IFormatProvider provider)
168 return Parse (s, NumberStyles.Integer, provider);
171 [CLSCompliant(false)]
172 public static sbyte Parse (string s, NumberStyles style)
174 return Parse (s, style, null);
177 [CLSCompliant(false)]
178 public static sbyte Parse (string s, NumberStyles style, IFormatProvider provider)
180 int tmpResult = Int32.Parse (s, style, provider);
181 if ((style & NumberStyles.AllowHexSpecifier) != 0) {
182 if (tmpResult >= 0 && tmpResult <= byte.MaxValue)
183 return (sbyte) tmpResult;
184 } else if (tmpResult <= MaxValue && tmpResult >= MinValue) {
185 return (sbyte) tmpResult;
188 throw new OverflowException (Locale.GetText ("Value too large or too small."));
191 [CLSCompliant(false)]
192 public static sbyte Parse (string s)
197 if (!Parse (s, false, out res, out exc))
203 [CLSCompliant(false)]
204 public static bool TryParse (string s, out sbyte result)
207 if (!Parse (s, true, out result, out exc)) {
215 [CLSCompliant(false)]
216 public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out sbyte result)
221 if (!Int32.TryParse (s, style, provider, out tmpResult))
223 if (tmpResult > SByte.MaxValue || tmpResult < SByte.MinValue)
226 result = (sbyte)tmpResult;
230 public override string ToString ()
232 return NumberFormatter.NumberToString (m_value, null);
235 public string ToString (IFormatProvider provider)
237 return NumberFormatter.NumberToString (m_value, provider);
240 public string ToString (string format)
242 return ToString (format, null);
245 public string ToString (string format, IFormatProvider provider)
247 return NumberFormatter.NumberToString (format, m_value, provider);
250 // =========== ICovnertible Methods =========== //
251 public TypeCode GetTypeCode ()
253 return TypeCode.SByte;
256 bool IConvertible.ToBoolean (IFormatProvider provider)
258 return System.Convert.ToBoolean (m_value);
261 byte IConvertible.ToByte (IFormatProvider provider)
263 return System.Convert.ToByte (m_value);
266 char IConvertible.ToChar (IFormatProvider provider)
268 return System.Convert.ToChar (m_value);
271 DateTime IConvertible.ToDateTime (IFormatProvider provider)
273 return System.Convert.ToDateTime (m_value);
276 decimal IConvertible.ToDecimal (IFormatProvider provider)
278 return System.Convert.ToDecimal (m_value);
281 double IConvertible.ToDouble (IFormatProvider provider)
283 return System.Convert.ToDouble (m_value);
286 short IConvertible.ToInt16 (IFormatProvider provider)
288 return System.Convert.ToInt16 (m_value);
291 int IConvertible.ToInt32 (IFormatProvider provider)
293 return System.Convert.ToInt32 (m_value);
296 long IConvertible.ToInt64 (IFormatProvider provider)
298 return System.Convert.ToInt64 (m_value);
301 sbyte IConvertible.ToSByte (IFormatProvider provider)
306 float IConvertible.ToSingle (IFormatProvider provider)
308 return System.Convert.ToSingle (m_value);
311 object IConvertible.ToType (Type targetType, IFormatProvider provider)
313 if (targetType == null)
314 throw new ArgumentNullException ("targetType");
315 return System.Convert.ToType (m_value, targetType, provider, false);
318 ushort IConvertible.ToUInt16 (IFormatProvider provider)
320 return System.Convert.ToUInt16 (m_value);
323 uint IConvertible.ToUInt32 (IFormatProvider provider)
325 return System.Convert.ToUInt32 (m_value);
328 ulong IConvertible.ToUInt64 (IFormatProvider provider)
330 return System.Convert.ToUInt64 (m_value);