This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / corlib / System / SByte.cs
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.
28 //
29
30 using System.Globalization;
31
32 namespace System
33 {
34         [CLSCompliant(false)]
35         [Serializable]
36         public struct SByte : IComparable, IFormattable, IConvertible
37         {
38                 public const sbyte MinValue = -128;
39                 public const sbyte MaxValue = 127;
40
41                 internal sbyte m_value;
42
43                 public int CompareTo (object v)
44                 {
45                         if (v == null)
46                                 return 1;
47
48                         if (!(v is System.SByte))
49                                 throw new ArgumentException (Locale.GetText ("Value is not a System.SByte."));
50
51                         sbyte xv = (sbyte) v;
52                         if (m_value == xv)
53                                 return 0;
54                         if (m_value > xv)
55                                 return 1;
56                         else
57                                 return -1;
58                 }
59
60                 public override bool Equals (object o)
61                 {
62                         if (!(o is System.SByte))
63                                 return false;
64
65                         return ((sbyte) o) == m_value;
66                 }
67
68                 public override int GetHashCode ()
69                 {
70                         return m_value;
71                 }
72
73                 [CLSCompliant(false)]
74                 public static sbyte Parse (string s)
75                 {
76                         int ival = 0;
77                         int len;
78                         int i;
79                         bool neg = false;
80                         bool digits_seen = false;
81
82                         if (s == null)
83                                 throw new ArgumentNullException ("s");
84
85                         len = s.Length;
86
87                         char c;
88                         for (i = 0; i < len; i++) {
89                                 c = s [i];
90                                 if (!Char.IsWhiteSpace (c))
91                                         break;
92                         }
93
94                         if (i == len)
95                                 throw new FormatException ();
96
97                         c = s [i];
98                         if (c == '+')
99                                 i++;
100                         else if (c == '-') {
101                                 neg = true;
102                                 i++;
103                         }
104
105                         for (; i < len; i++) {
106                                 c = s [i];
107
108                                 if (c >= '0' && c <= '9') {
109                                         ival = checked (ival * 10 - (int) (c - '0'));
110                                         digits_seen = true;
111                                 } else {
112                                         if (Char.IsWhiteSpace (c)) {
113                                                 for (i++; i < len; i++) {
114                                                         if (!Char.IsWhiteSpace (s [i]))
115                                                                 throw new FormatException ();
116                                                 }
117                                                 break;
118                                         } else
119                                                 throw new FormatException ();
120                                 }
121                         }
122                         if (!digits_seen)
123                                 throw new FormatException ();
124
125                         ival = neg ? ival : -ival;
126                         if (ival < SByte.MinValue || ival > SByte.MaxValue)
127                                 throw new OverflowException ();
128
129                         return (sbyte) ival;
130                 }
131
132                 [CLSCompliant(false)]
133                 public static sbyte Parse (string s, IFormatProvider provider)
134                 {
135                         return Parse (s, NumberStyles.Integer, provider);
136                 }
137
138                 [CLSCompliant(false)]
139                 public static sbyte Parse (string s, NumberStyles style)
140                 {
141                         return Parse (s, style, null);
142                 }
143
144                 [CLSCompliant(false)]
145                 public static sbyte Parse (string s, NumberStyles style, IFormatProvider provider)
146                 {
147                         int tmpResult = Int32.Parse (s, style, provider);
148                         if (tmpResult > SByte.MaxValue || tmpResult < SByte.MinValue)
149                                 throw new OverflowException (Locale.GetText ("Value too large or too small."));
150
151                         return (sbyte) tmpResult;
152                 }
153
154                 public override string ToString ()
155                 {
156                         return ToString (null, null);
157                 }
158
159                 public string ToString (IFormatProvider provider)
160                 {
161                         return ToString (null, provider);
162                 }
163
164                 public string ToString (string format)
165                 {
166                         return ToString (format, null);
167                 }
168
169                 public string ToString (string format, IFormatProvider provider)
170                 {
171                         NumberFormatInfo nfi = NumberFormatInfo.GetInstance (provider);
172
173                         // use "G" when format is null or String.Empty
174                         if ((format == null) || (format.Length == 0))
175                                 format = "G";
176
177                         return IntegerFormatter.NumberToString (format, nfi, m_value);
178                 }
179
180                 // =========== ICovnertible Methods =========== //
181                 public TypeCode GetTypeCode ()
182                 {
183                         return TypeCode.SByte;
184                 }
185
186                 bool IConvertible.ToBoolean (IFormatProvider provider)
187                 {
188                         return System.Convert.ToBoolean (m_value);
189                 }
190
191                 byte IConvertible.ToByte (IFormatProvider provider)
192                 {
193                         return System.Convert.ToByte (m_value);
194                 }
195
196                 char IConvertible.ToChar (IFormatProvider provider)
197                 {
198                         return System.Convert.ToChar (m_value);
199                 }
200
201                 DateTime IConvertible.ToDateTime (IFormatProvider provider)
202                 {
203                         return System.Convert.ToDateTime (m_value);
204                 }
205
206                 decimal IConvertible.ToDecimal (IFormatProvider provider)
207                 {
208                         return System.Convert.ToDecimal (m_value);
209                 }
210
211                 double IConvertible.ToDouble (IFormatProvider provider)
212                 {
213                         return System.Convert.ToDouble (m_value);
214                 }
215
216                 short IConvertible.ToInt16 (IFormatProvider provider)
217                 {
218                         return System.Convert.ToInt16 (m_value);
219                 }
220
221                 int IConvertible.ToInt32 (IFormatProvider provider)
222                 {
223                         return System.Convert.ToInt32 (m_value);
224                 }
225
226                 long IConvertible.ToInt64 (IFormatProvider provider)
227                 {
228                         return System.Convert.ToInt64 (m_value);
229                 }
230
231                 sbyte IConvertible.ToSByte (IFormatProvider provider)
232                 {
233                         return m_value;
234                 }
235
236                 float IConvertible.ToSingle (IFormatProvider provider)
237                 {
238                         return System.Convert.ToSingle (m_value);
239                 }
240
241                 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
242                 {
243                         return System.Convert.ToType (m_value, conversionType, provider);
244                 }
245
246                 ushort IConvertible.ToUInt16 (IFormatProvider provider)
247                 {
248                         return System.Convert.ToUInt16 (m_value);
249                 }
250
251                 uint IConvertible.ToUInt32 (IFormatProvider provider)
252                 {
253                         return System.Convert.ToUInt32 (m_value);
254                 }
255
256                 ulong IConvertible.ToUInt64 (IFormatProvider provider)
257                 {
258                         return System.Convert.ToUInt64 (m_value);
259                 }
260         }
261 }