Redesign System/NumberFormatter to improve primitive numeric types ToString performance.
[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 #if NET_2_0
37         [System.Runtime.InteropServices.ComVisible (true)]
38 #endif
39         public struct SByte : IFormattable, IConvertible, IComparable
40 #if NET_2_0
41                 , IComparable<SByte>, IEquatable <SByte>
42 #endif
43         {
44                 public const sbyte MinValue = -128;
45                 public const sbyte MaxValue = 127;
46
47                 internal sbyte m_value;
48
49                 public int CompareTo (object v)
50                 {
51                         if (v == null)
52                                 return 1;
53
54                         if (!(v is System.SByte))
55                                 throw new ArgumentException (Locale.GetText ("Value is not a System.SByte."));
56
57                         sbyte xv = (sbyte) v;
58                         if (m_value == xv)
59                                 return 0;
60                         if (m_value > xv)
61                                 return 1;
62                         else
63                                 return -1;
64                 }
65
66                 public override bool Equals (object o)
67                 {
68                         if (!(o is System.SByte))
69                                 return false;
70
71                         return ((sbyte) o) == m_value;
72                 }
73
74                 public override int GetHashCode ()
75                 {
76                         return m_value;
77                 }
78
79 #if NET_2_0
80                 public int CompareTo (sbyte value)
81                 {
82                         if (m_value == value)
83                                 return 0;
84                         if (m_value > value)
85                                 return 1;
86                         else
87                                 return -1;
88                 }
89
90                 public bool Equals (sbyte value)
91                 {
92                         return value == m_value;
93                 }
94 #endif
95
96                 internal static bool Parse (string s, bool tryParse, out sbyte result, out Exception exc)
97                 {
98                         int ival = 0;
99                         int len;
100                         int i;
101                         bool neg = false;
102                         bool digits_seen = false;
103
104                         result = 0;
105                         exc = null;
106
107                         if (s == null) {
108                                 if (!tryParse)
109                                         exc = new ArgumentNullException ("s");
110                                 return false;
111                         }
112
113                         len = s.Length;
114
115                         char c;
116                         for (i = 0; i < len; i++) {
117                                 c = s [i];
118                                 if (!Char.IsWhiteSpace (c))
119                                         break;
120                         }
121
122                         if (i == len) {
123                                 if (!tryParse)
124                                         exc = Int32.GetFormatException ();
125                                 return false;
126                         }
127
128                         c = s [i];
129                         if (c == '+')
130                                 i++;
131                         else if (c == '-') {
132                                 neg = true;
133                                 i++;
134                         }
135
136                         for (; i < len; i++) {
137                                 c = s [i];
138
139                                 if (c >= '0' && c <= '9') {
140                                         if (tryParse){
141                                                 int intval = ival * 10 - (int) (c - '0');
142
143                                                 if (intval < MinValue)
144                                                         return false;
145                                                 ival = (sbyte) intval;
146                                         } else
147                                                 ival = checked (ival * 10 - (int) (c - '0'));
148                                         digits_seen = true;
149                                 } else {
150                                         if (Char.IsWhiteSpace (c)) {
151                                                 for (i++; i < len; i++) {
152                                                         if (!Char.IsWhiteSpace (s [i])) {
153                                                                 if (!tryParse)
154                                                                         exc = Int32.GetFormatException ();
155                                                                 return false;
156                                                         }
157                                                 }
158                                                 break;
159                                         } else {
160                                                 if (!tryParse)
161                                                         exc = Int32.GetFormatException ();
162                                                 return false;
163                                         }
164                                 }
165                         }
166                         if (!digits_seen) {
167                                 if (!tryParse)
168                                         exc = Int32.GetFormatException ();
169                                 return false;
170                         }
171
172                         ival = neg ? ival : -ival;
173                         if (ival < SByte.MinValue || ival > SByte.MaxValue) {
174                                 if (!tryParse)
175                                         exc = new OverflowException ();
176                                 return false;
177                         }
178
179                         result = (sbyte)ival;
180                         return true;
181                 }
182
183                 [CLSCompliant(false)]
184                 public static sbyte Parse (string s, IFormatProvider provider)
185                 {
186                         return Parse (s, NumberStyles.Integer, provider);
187                 }
188
189                 [CLSCompliant(false)]
190                 public static sbyte Parse (string s, NumberStyles style)
191                 {
192                         return Parse (s, style, null);
193                 }
194
195                 [CLSCompliant(false)]
196                 public static sbyte Parse (string s, NumberStyles style, IFormatProvider provider)
197                 {
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."));
201
202                         return (sbyte) tmpResult;
203                 }
204
205                 [CLSCompliant(false)]
206                 public static sbyte Parse (string s) 
207                 {
208                         Exception exc;
209                         sbyte res;
210
211                         if (!Parse (s, false, out res, out exc))
212                                 throw exc;
213
214                         return res;
215                 }
216
217 #if NET_2_0
218                 [CLSCompliant(false)]
219                 public static bool TryParse (string s, out sbyte result) 
220                 {
221                         Exception exc;
222                         if (!Parse (s, true, out result, out exc)) {
223                                 result = 0;
224                                 return false;
225                         }
226
227                         return true;
228                 }
229
230                 [CLSCompliant(false)]
231                 public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out sbyte result) 
232                 {
233                         int tmpResult;
234                         result = 0;
235
236                         if (!Int32.TryParse (s, style, provider, out tmpResult))
237                                 return false;
238                         if (tmpResult > SByte.MaxValue || tmpResult < SByte.MinValue)
239                                 return false;
240                                 
241                         result = (sbyte)tmpResult;
242                         return true;
243                 }
244 #endif
245
246                 public override string ToString ()
247                 {
248                         return new NumberFormatter(null, m_value).FormatDecimal(-1, null);
249                 }
250
251                 public string ToString (IFormatProvider provider)
252                 {
253                         NumberFormatInfo nfi = NumberFormatInfo.GetInstance (provider);
254                         return new NumberFormatter(null, m_value).FormatDecimal(-1, nfi);
255                 }
256
257                 public string ToString (string format)
258                 {
259                         return ToString (format, null);
260                 }
261
262                 public string ToString (string format, IFormatProvider provider)
263                 {
264                         NumberFormatInfo nfi = NumberFormatInfo.GetInstance (provider);
265                         return NumberFormatter.NumberToString (format, m_value, nfi);
266                 }
267
268                 // =========== ICovnertible Methods =========== //
269                 public TypeCode GetTypeCode ()
270                 {
271                         return TypeCode.SByte;
272                 }
273
274                 bool IConvertible.ToBoolean (IFormatProvider provider)
275                 {
276                         return System.Convert.ToBoolean (m_value);
277                 }
278
279                 byte IConvertible.ToByte (IFormatProvider provider)
280                 {
281                         return System.Convert.ToByte (m_value);
282                 }
283
284                 char IConvertible.ToChar (IFormatProvider provider)
285                 {
286                         return System.Convert.ToChar (m_value);
287                 }
288
289                 DateTime IConvertible.ToDateTime (IFormatProvider provider)
290                 {
291                         return System.Convert.ToDateTime (m_value);
292                 }
293
294                 decimal IConvertible.ToDecimal (IFormatProvider provider)
295                 {
296                         return System.Convert.ToDecimal (m_value);
297                 }
298
299                 double IConvertible.ToDouble (IFormatProvider provider)
300                 {
301                         return System.Convert.ToDouble (m_value);
302                 }
303
304                 short IConvertible.ToInt16 (IFormatProvider provider)
305                 {
306                         return System.Convert.ToInt16 (m_value);
307                 }
308
309                 int IConvertible.ToInt32 (IFormatProvider provider)
310                 {
311                         return System.Convert.ToInt32 (m_value);
312                 }
313
314                 long IConvertible.ToInt64 (IFormatProvider provider)
315                 {
316                         return System.Convert.ToInt64 (m_value);
317                 }
318
319                 sbyte IConvertible.ToSByte (IFormatProvider provider)
320                 {
321                         return m_value;
322                 }
323
324                 float IConvertible.ToSingle (IFormatProvider provider)
325                 {
326                         return System.Convert.ToSingle (m_value);
327                 }
328
329                 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
330                 {
331                         return System.Convert.ToType (m_value, conversionType, provider);
332                 }
333
334                 ushort IConvertible.ToUInt16 (IFormatProvider provider)
335                 {
336                         return System.Convert.ToUInt16 (m_value);
337                 }
338
339                 uint IConvertible.ToUInt32 (IFormatProvider provider)
340                 {
341                         return System.Convert.ToUInt32 (m_value);
342                 }
343
344                 ulong IConvertible.ToUInt64 (IFormatProvider provider)
345                 {
346                         return System.Convert.ToUInt64 (m_value);
347                 }
348         }
349 }