In Test/System.Windows.Forms:
[mono-project.git] / mcs / class / corlib / System / SByte.cs
blob21263ab5d7c95452580b285e9d46da15201c1aa1
1 //
2 // System.SByte.cs
3 //
4 // Author:
5 // Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) Ximian, Inc. http://www.ximian.com
8 // Copyright (C) 2004 Novell (http://www.novell.com)
9 //
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:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
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;
32 namespace System
34 [CLSCompliant(false)]
35 [Serializable]
36 public struct SByte : IFormattable, IConvertible, IComparable
37 #if NET_2_0
38 , IComparable<SByte>, IEquatable <SByte>
39 #endif
41 public const sbyte MinValue = -128;
42 public const sbyte MaxValue = 127;
44 internal sbyte m_value;
46 public int CompareTo (object v)
48 if (v == null)
49 return 1;
51 if (!(v is System.SByte))
52 throw new ArgumentException (Locale.GetText ("Value is not a System.SByte."));
54 sbyte xv = (sbyte) v;
55 if (m_value == xv)
56 return 0;
57 if (m_value > xv)
58 return 1;
59 else
60 return -1;
63 public override bool Equals (object o)
65 if (!(o is System.SByte))
66 return false;
68 return ((sbyte) o) == m_value;
71 public override int GetHashCode ()
73 return m_value;
76 #if NET_2_0
77 public int CompareTo (sbyte value)
79 if (m_value == value)
80 return 0;
81 if (m_value > value)
82 return 1;
83 else
84 return -1;
87 public bool Equals (sbyte value)
89 return value == m_value;
91 #endif
93 internal static bool Parse (string s, bool tryParse, out sbyte result, out Exception exc)
95 int ival = 0;
96 int len;
97 int i;
98 bool neg = false;
99 bool digits_seen = false;
101 result = 0;
102 exc = null;
104 if (s == null) {
105 if (!tryParse)
106 exc = new ArgumentNullException ("s");
107 return false;
110 len = s.Length;
112 char c;
113 for (i = 0; i < len; i++) {
114 c = s [i];
115 if (!Char.IsWhiteSpace (c))
116 break;
119 if (i == len) {
120 if (!tryParse)
121 exc = Int32.GetFormatException ();
122 return false;
125 c = s [i];
126 if (c == '+')
127 i++;
128 else if (c == '-') {
129 neg = true;
130 i++;
133 for (; i < len; i++) {
134 c = s [i];
136 if (c >= '0' && c <= '9') {
137 ival = checked (ival * 10 - (int) (c - '0'));
138 digits_seen = true;
139 } else {
140 if (Char.IsWhiteSpace (c)) {
141 for (i++; i < len; i++) {
142 if (!Char.IsWhiteSpace (s [i])) {
143 if (!tryParse)
144 exc = Int32.GetFormatException ();
145 return false;
148 break;
149 } else {
150 if (!tryParse)
151 exc = Int32.GetFormatException ();
152 return false;
156 if (!digits_seen) {
157 if (!tryParse)
158 exc = Int32.GetFormatException ();
159 return false;
162 ival = neg ? ival : -ival;
163 if (ival < SByte.MinValue || ival > SByte.MaxValue) {
164 if (!tryParse)
165 exc = new OverflowException ();
166 return false;
169 result = (sbyte)ival;
170 return true;
173 [CLSCompliant(false)]
174 public static sbyte Parse (string s, IFormatProvider provider)
176 return Parse (s, NumberStyles.Integer, provider);
179 [CLSCompliant(false)]
180 public static sbyte Parse (string s, NumberStyles style)
182 return Parse (s, style, null);
185 [CLSCompliant(false)]
186 public static sbyte Parse (string s, NumberStyles style, IFormatProvider provider)
188 int tmpResult = Int32.Parse (s, style, provider);
189 if (tmpResult > SByte.MaxValue || tmpResult < SByte.MinValue)
190 throw new OverflowException (Locale.GetText ("Value too large or too small."));
192 return (sbyte) tmpResult;
195 [CLSCompliant(false)]
196 public static sbyte Parse (string s)
198 Exception exc;
199 sbyte res;
201 if (!Parse (s, false, out res, out exc))
202 throw exc;
204 return res;
207 #if NET_2_0
208 [CLSCompliant(false)]
209 public static bool TryParse (string s, out sbyte result)
211 Exception exc;
212 if (!Parse (s, true, out result, out exc)) {
213 result = 0;
214 return false;
217 return true;
220 [CLSCompliant(false)]
221 public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out sbyte result)
223 int tmpResult;
224 result = 0;
226 if (!Int32.TryParse (s, style, provider, out tmpResult))
227 return false;
228 if (tmpResult > SByte.MaxValue || tmpResult < SByte.MinValue)
229 return false;
231 result = (sbyte)tmpResult;
232 return true;
234 #endif
236 public override string ToString ()
238 return NumberFormatter.FormatGeneral (new NumberFormatter.NumberStore (m_value));
241 public string ToString (IFormatProvider provider)
243 return NumberFormatter.FormatGeneral (new NumberFormatter.NumberStore (m_value), 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);
254 return NumberFormatter.NumberToString (format, m_value, nfi);
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)
310 return m_value;
313 float IConvertible.ToSingle (IFormatProvider provider)
315 return System.Convert.ToSingle (m_value);
318 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
320 return System.Convert.ToType (m_value, conversionType, provider);
323 ushort IConvertible.ToUInt16 (IFormatProvider provider)
325 return System.Convert.ToUInt16 (m_value);
328 uint IConvertible.ToUInt32 (IFormatProvider provider)
330 return System.Convert.ToUInt32 (m_value);
333 ulong IConvertible.ToUInt64 (IFormatProvider provider)
335 return System.Convert.ToUInt64 (m_value);