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 sbyte xv
= (sbyte) obj
;
61 public override bool Equals (object obj
)
63 if (!(obj
is System
.SByte
))
66 return ((sbyte) obj
) == m_value
;
69 public override int GetHashCode ()
74 public int CompareTo (sbyte value)
84 public bool Equals (sbyte obj
)
86 return obj
== m_value
;
89 internal static bool Parse (string s
, bool tryParse
, out sbyte result
, out Exception exc
)
95 bool digits_seen
= false;
102 exc
= new ArgumentNullException ("s");
109 for (i
= 0; i
< len
; i
++) {
111 if (!Char
.IsWhiteSpace (c
))
117 exc
= Int32
.GetFormatException ();
129 for (; i
< len
; i
++) {
132 if (c
>= '0' && c
<= '9') {
134 int intval
= ival
* 10 - (int) (c
- '0');
136 if (intval
< MinValue
)
138 ival
= (sbyte) intval
;
140 ival
= checked (ival
* 10 - (int) (c
- '0'));
143 if (Char
.IsWhiteSpace (c
)) {
144 for (i
++; i
< len
; i
++) {
145 if (!Char
.IsWhiteSpace (s
[i
])) {
147 exc
= Int32
.GetFormatException ();
154 exc
= Int32
.GetFormatException ();
161 exc
= Int32
.GetFormatException ();
165 ival
= neg
? ival
: -ival
;
166 if (ival
< SByte
.MinValue
|| ival
> SByte
.MaxValue
) {
168 exc
= new OverflowException ();
172 result
= (sbyte)ival
;
176 [CLSCompliant(false)]
177 public static sbyte Parse (string s
, IFormatProvider provider
)
179 return Parse (s
, NumberStyles
.Integer
, provider
);
182 [CLSCompliant(false)]
183 public static sbyte Parse (string s
, NumberStyles style
)
185 return Parse (s
, style
, null);
188 [CLSCompliant(false)]
189 public static sbyte Parse (string s
, NumberStyles style
, IFormatProvider provider
)
191 int tmpResult
= Int32
.Parse (s
, style
, provider
);
192 if (tmpResult
> SByte
.MaxValue
|| tmpResult
< SByte
.MinValue
)
193 throw new OverflowException (Locale
.GetText ("Value too large or too small."));
195 return (sbyte) tmpResult
;
198 [CLSCompliant(false)]
199 public static sbyte Parse (string s
)
204 if (!Parse (s
, false, out res
, out exc
))
210 [CLSCompliant(false)]
211 public static bool TryParse (string s
, out sbyte result
)
214 if (!Parse (s
, true, out result
, out exc
)) {
222 [CLSCompliant(false)]
223 public static bool TryParse (string s
, NumberStyles style
, IFormatProvider provider
, out sbyte result
)
228 if (!Int32
.TryParse (s
, style
, provider
, out tmpResult
))
230 if (tmpResult
> SByte
.MaxValue
|| tmpResult
< SByte
.MinValue
)
233 result
= (sbyte)tmpResult
;
237 public override string ToString ()
239 return NumberFormatter
.NumberToString (m_value
, null);
242 public string ToString (IFormatProvider provider
)
244 return NumberFormatter
.NumberToString (m_value
, provider
);
247 public string ToString (string format
)
249 return ToString (format
, null);
252 public string ToString (string format
, IFormatProvider provider
)
254 return NumberFormatter
.NumberToString (format
, m_value
, provider
);
257 // =========== ICovnertible Methods =========== //
258 public TypeCode
GetTypeCode ()
260 return TypeCode
.SByte
;
263 bool IConvertible
.ToBoolean (IFormatProvider provider
)
265 return System
.Convert
.ToBoolean (m_value
);
268 byte IConvertible
.ToByte (IFormatProvider provider
)
270 return System
.Convert
.ToByte (m_value
);
273 char IConvertible
.ToChar (IFormatProvider provider
)
275 return System
.Convert
.ToChar (m_value
);
278 DateTime IConvertible
.ToDateTime (IFormatProvider provider
)
280 return System
.Convert
.ToDateTime (m_value
);
283 decimal IConvertible
.ToDecimal (IFormatProvider provider
)
285 return System
.Convert
.ToDecimal (m_value
);
288 double IConvertible
.ToDouble (IFormatProvider provider
)
290 return System
.Convert
.ToDouble (m_value
);
293 short IConvertible
.ToInt16 (IFormatProvider provider
)
295 return System
.Convert
.ToInt16 (m_value
);
298 int IConvertible
.ToInt32 (IFormatProvider provider
)
300 return System
.Convert
.ToInt32 (m_value
);
303 long IConvertible
.ToInt64 (IFormatProvider provider
)
305 return System
.Convert
.ToInt64 (m_value
);
308 sbyte IConvertible
.ToSByte (IFormatProvider provider
)
313 float IConvertible
.ToSingle (IFormatProvider provider
)
315 return System
.Convert
.ToSingle (m_value
);
318 object IConvertible
.ToType (Type targetType
, IFormatProvider provider
)
320 if (targetType
== null)
321 throw new ArgumentNullException ("targetType");
322 return System
.Convert
.ToType (m_value
, targetType
, provider
, false);
325 ushort IConvertible
.ToUInt16 (IFormatProvider provider
)
327 return System
.Convert
.ToUInt16 (m_value
);
330 uint IConvertible
.ToUInt32 (IFormatProvider provider
)
332 return System
.Convert
.ToUInt32 (m_value
);
335 ulong IConvertible
.ToUInt64 (IFormatProvider provider
)
337 return System
.Convert
.ToUInt64 (m_value
);