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
;
37 [System
.Runtime
.InteropServices
.ComVisible (true)]
39 public struct SByte
: IFormattable
, IConvertible
, IComparable
41 , IComparable
<SByte
>, IEquatable
<SByte
>
44 public const sbyte MinValue
= -128;
45 public const sbyte MaxValue
= 127;
47 internal sbyte m_value
;
49 public int CompareTo (object obj
)
54 if (!(obj
is System
.SByte
))
55 throw new ArgumentException (Locale
.GetText ("Value is not a System.SByte."));
57 sbyte xv
= (sbyte) obj
;
66 public override bool Equals (object obj
)
68 if (!(obj
is System
.SByte
))
71 return ((sbyte) obj
) == m_value
;
74 public override int GetHashCode ()
80 public int CompareTo (sbyte value)
90 public bool Equals (sbyte obj
)
92 return obj
== m_value
;
96 internal static bool Parse (string s
, bool tryParse
, out sbyte result
, out Exception exc
)
102 bool digits_seen
= false;
109 exc
= new ArgumentNullException ("s");
116 for (i
= 0; i
< len
; i
++) {
118 if (!Char
.IsWhiteSpace (c
))
124 exc
= Int32
.GetFormatException ();
136 for (; i
< len
; i
++) {
139 if (c
>= '0' && c
<= '9') {
141 int intval
= ival
* 10 - (int) (c
- '0');
143 if (intval
< MinValue
)
145 ival
= (sbyte) intval
;
147 ival
= checked (ival
* 10 - (int) (c
- '0'));
150 if (Char
.IsWhiteSpace (c
)) {
151 for (i
++; i
< len
; i
++) {
152 if (!Char
.IsWhiteSpace (s
[i
])) {
154 exc
= Int32
.GetFormatException ();
161 exc
= Int32
.GetFormatException ();
168 exc
= Int32
.GetFormatException ();
172 ival
= neg
? ival
: -ival
;
173 if (ival
< SByte
.MinValue
|| ival
> SByte
.MaxValue
) {
175 exc
= new OverflowException ();
179 result
= (sbyte)ival
;
183 [CLSCompliant(false)]
184 public static sbyte Parse (string s
, IFormatProvider provider
)
186 return Parse (s
, NumberStyles
.Integer
, provider
);
189 [CLSCompliant(false)]
190 public static sbyte Parse (string s
, NumberStyles style
)
192 return Parse (s
, style
, null);
195 [CLSCompliant(false)]
196 public static sbyte Parse (string s
, NumberStyles style
, IFormatProvider provider
)
198 int tmpResult
= Int32
.Parse (s
, style
, provider
);
199 if (tmpResult
> SByte
.MaxValue
|| tmpResult
< SByte
.MinValue
)
200 throw new OverflowException (Locale
.GetText ("Value too large or too small."));
202 return (sbyte) tmpResult
;
205 [CLSCompliant(false)]
206 public static sbyte Parse (string s
)
211 if (!Parse (s
, false, out res
, out exc
))
218 [CLSCompliant(false)]
219 public static bool TryParse (string s
, out sbyte result
)
222 if (!Parse (s
, true, out result
, out exc
)) {
230 [CLSCompliant(false)]
231 public static bool TryParse (string s
, NumberStyles style
, IFormatProvider provider
, out sbyte result
)
236 if (!Int32
.TryParse (s
, style
, provider
, out tmpResult
))
238 if (tmpResult
> SByte
.MaxValue
|| tmpResult
< SByte
.MinValue
)
241 result
= (sbyte)tmpResult
;
246 public override string ToString ()
248 return NumberFormatter
.NumberToString (m_value
, null);
251 public string ToString (IFormatProvider provider
)
253 return NumberFormatter
.NumberToString (m_value
, provider
);
256 public string ToString (string format
)
258 return ToString (format
, null);
261 public string ToString (string format
, IFormatProvider provider
)
263 return NumberFormatter
.NumberToString (format
, m_value
, provider
);
266 // =========== ICovnertible Methods =========== //
267 public TypeCode
GetTypeCode ()
269 return TypeCode
.SByte
;
272 bool IConvertible
.ToBoolean (IFormatProvider provider
)
274 return System
.Convert
.ToBoolean (m_value
);
277 byte IConvertible
.ToByte (IFormatProvider provider
)
279 return System
.Convert
.ToByte (m_value
);
282 char IConvertible
.ToChar (IFormatProvider provider
)
284 return System
.Convert
.ToChar (m_value
);
287 DateTime IConvertible
.ToDateTime (IFormatProvider provider
)
289 return System
.Convert
.ToDateTime (m_value
);
292 decimal IConvertible
.ToDecimal (IFormatProvider provider
)
294 return System
.Convert
.ToDecimal (m_value
);
297 double IConvertible
.ToDouble (IFormatProvider provider
)
299 return System
.Convert
.ToDouble (m_value
);
302 short IConvertible
.ToInt16 (IFormatProvider provider
)
304 return System
.Convert
.ToInt16 (m_value
);
307 int IConvertible
.ToInt32 (IFormatProvider provider
)
309 return System
.Convert
.ToInt32 (m_value
);
312 long IConvertible
.ToInt64 (IFormatProvider provider
)
314 return System
.Convert
.ToInt64 (m_value
);
317 sbyte IConvertible
.ToSByte (IFormatProvider provider
)
322 float IConvertible
.ToSingle (IFormatProvider provider
)
324 return System
.Convert
.ToSingle (m_value
);
327 object IConvertible
.ToType (Type targetType
, IFormatProvider provider
)
329 if (targetType
== null)
330 throw new ArgumentNullException ("targetType");
331 return System
.Convert
.ToType (m_value
, targetType
, provider
, false);
334 ushort IConvertible
.ToUInt16 (IFormatProvider provider
)
336 return System
.Convert
.ToUInt16 (m_value
);
339 uint IConvertible
.ToUInt32 (IFormatProvider provider
)
341 return System
.Convert
.ToUInt32 (m_value
);
344 ulong IConvertible
.ToUInt64 (IFormatProvider provider
)
346 return System
.Convert
.ToUInt64 (m_value
);