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 : IFormattable, IConvertible,
38 IComparable, IComparable<SByte>
43 public const sbyte MinValue = -128;
44 public const sbyte MaxValue = 127;
46 internal sbyte m_value;
48 public int CompareTo (object v)
53 if (!(v is System.SByte))
54 throw new ArgumentException (Locale.GetText ("Value is not a System.SByte."));
65 public override bool Equals (object o)
67 if (!(o is System.SByte))
70 return ((sbyte) o) == m_value;
73 public override int GetHashCode ()
79 public int CompareTo (sbyte value)
89 public bool Equals (sbyte value)
91 return value == m_value;
95 internal static bool Parse (string s, bool tryParse, out sbyte result)
101 bool digits_seen = false;
109 throw new ArgumentNullException ("s");
114 for (i = 0; i < len; i++) {
116 if (!Char.IsWhiteSpace (c))
124 throw new FormatException ();
134 for (; i < len; i++) {
137 if (c >= '0' && c <= '9') {
138 ival = checked (ival * 10 - (int) (c - '0'));
141 if (Char.IsWhiteSpace (c)) {
142 for (i++; i < len; i++) {
143 if (!Char.IsWhiteSpace (s [i]))
147 throw new FormatException ();
154 throw new FormatException ();
161 throw new FormatException ();
163 ival = neg ? ival : -ival;
164 if (ival < SByte.MinValue || ival > SByte.MaxValue)
168 throw new OverflowException ();
170 result = (sbyte)ival;
174 [CLSCompliant(false)]
175 public static sbyte Parse (string s, IFormatProvider provider)
177 return Parse (s, NumberStyles.Integer, provider);
180 [CLSCompliant(false)]
181 public static sbyte Parse (string s, NumberStyles style)
183 return Parse (s, style, null);
186 [CLSCompliant(false)]
187 public static sbyte Parse (string s, NumberStyles style, IFormatProvider provider)
189 int tmpResult = Int32.Parse (s, style, provider);
190 if (tmpResult > SByte.MaxValue || tmpResult < SByte.MinValue)
191 throw new OverflowException (Locale.GetText ("Value too large or too small."));
193 return (sbyte) tmpResult;
196 [CLSCompliant(false)]
197 public static sbyte Parse (string s) {
200 Parse (s, false, out res);
206 [CLSCompliant(false)]
207 public static bool TryParse (string s, out sbyte result) {
209 return Parse (s, true, out result);
217 [CLSCompliant(false)]
218 public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out sbyte result) {
222 if (!Int32.TryParse (s, style, provider, out tmpResult))
224 if (tmpResult > SByte.MaxValue || tmpResult < SByte.MinValue)
226 result = (sbyte)tmpResult;
236 public override string ToString ()
238 return ToString (null, null);
241 public string ToString (IFormatProvider provider)
243 return ToString (null, provider);
246 public string ToString (string format)
248 return ToString (format, null);
251 public string ToString (string format, IFormatProvider provider)
253 NumberFormatInfo nfi = NumberFormatInfo.GetInstance (provider);
255 // use "G" when format is null or String.Empty
256 if ((format == null) || (format.Length == 0))
259 return IntegerFormatter.NumberToString (format, nfi, m_value);
262 // =========== ICovnertible Methods =========== //
263 public TypeCode GetTypeCode ()
265 return TypeCode.SByte;
268 bool IConvertible.ToBoolean (IFormatProvider provider)
270 return System.Convert.ToBoolean (m_value);
273 byte IConvertible.ToByte (IFormatProvider provider)
275 return System.Convert.ToByte (m_value);
278 char IConvertible.ToChar (IFormatProvider provider)
280 return System.Convert.ToChar (m_value);
283 DateTime IConvertible.ToDateTime (IFormatProvider provider)
285 return System.Convert.ToDateTime (m_value);
288 decimal IConvertible.ToDecimal (IFormatProvider provider)
290 return System.Convert.ToDecimal (m_value);
293 double IConvertible.ToDouble (IFormatProvider provider)
295 return System.Convert.ToDouble (m_value);
298 short IConvertible.ToInt16 (IFormatProvider provider)
300 return System.Convert.ToInt16 (m_value);
303 int IConvertible.ToInt32 (IFormatProvider provider)
305 return System.Convert.ToInt32 (m_value);
308 long IConvertible.ToInt64 (IFormatProvider provider)
310 return System.Convert.ToInt64 (m_value);
313 sbyte IConvertible.ToSByte (IFormatProvider provider)
318 float IConvertible.ToSingle (IFormatProvider provider)
320 return System.Convert.ToSingle (m_value);
323 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
325 return System.Convert.ToType (m_value, conversionType, provider);
328 ushort IConvertible.ToUInt16 (IFormatProvider provider)
330 return System.Convert.ToUInt16 (m_value);
333 uint IConvertible.ToUInt32 (IFormatProvider provider)
335 return System.Convert.ToUInt32 (m_value);
338 ulong IConvertible.ToUInt64 (IFormatProvider provider)
340 return System.Convert.ToUInt64 (m_value);