78eab694f58eeeba63ee1fa8acda7ac3aa11acb2
[mono.git] / mcs / class / corlib / System / Byte.cs
1 //
2 // System.Byte.cs
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) Ximian, Inc.  http://www.ximian.com
8 //
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System.Globalization;
34
35 namespace System
36 {
37         [Serializable]
38         public struct Byte : IFormattable, IConvertible,
39 #if NET_2_0
40                 IComparable, IComparable<Byte>
41 #else
42                 IComparable
43 #endif
44         {
45                 public const byte MinValue = 0;
46                 public const byte MaxValue = 255;
47
48                 internal byte m_value;
49
50                 public int CompareTo (object value)
51                 {
52                         if (value == null)
53                                 return 1;
54
55                         if (!(value is System.Byte))
56                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Byte."));
57
58                         byte xv = (byte) value;
59
60                         if (m_value == xv)
61                                 return 0;
62                         if (m_value > xv)
63                                 return 1;
64                         else
65                                 return -1;
66                 }
67
68                 public override bool Equals (object obj)
69                 {
70                         if (!(obj is System.Byte))
71                                 return false;
72
73                         return ((byte) obj) == m_value;
74                 }
75
76                 public override int GetHashCode ()
77                 {
78                         return m_value;
79                 }
80
81 #if NET_2_0
82                 public int CompareTo (byte value)
83                 {
84                         if (m_value == value)
85                                 return 0;
86                         if (m_value > value)
87                                 return 1;
88                         else
89                                 return -1;
90                 }
91
92                 public bool Equals (byte value)
93                 {
94                         return value == m_value;
95                 }
96 #endif
97
98                 internal static bool Parse (string s, bool tryParse, out byte result)
99                 {
100                         byte val = 0;
101                         int len;
102                         int i;
103                         bool digits_seen = false;
104                         bool negative = false;
105
106                         result = 0;
107
108                         if (s == null)
109                                 if (tryParse)
110                                         return false;
111                                 else
112                                         throw new ArgumentNullException ("s");
113
114                         len = s.Length;
115
116                         // look for the first non-whitespace character
117                         char c;
118                         for (i = 0; i < len; i++){
119                                 c = s [i];
120                                 if (!Char.IsWhiteSpace (c))
121                                         break;
122                         }
123
124                         // if it's all whitespace, then throw exception
125                         if (i == len)
126                                 if (tryParse)
127                                         return false;
128                                 else
129                                         throw new FormatException ();
130
131                         // look for the optional '+' sign
132                         if (s [i] == '+')
133                                 i++;
134                         else if (s [i] == '-') {
135                                 negative = true;
136                                 i++;
137                         }
138
139                         // we should just have numerals followed by whitespace now
140                         for (; i < len; i++){
141                                 c = s [i];
142
143                                 if (c >= '0' && c <= '9'){
144                                         // shift left and accumulate every time we find a numeral
145                                         byte d = (byte) (c - '0');
146
147                                         val = checked ((byte) (val * 10 + d));
148                                         digits_seen = true;
149                                 } else {
150                                         // after the last numeral, only whitespace is allowed
151                                         if (Char.IsWhiteSpace (c)){
152                                                 for (i++; i < len; i++){
153                                                         if (!Char.IsWhiteSpace (s [i]))
154                                                                 if (tryParse)
155                                                                         return false;
156                                                                 else
157                                                                         throw new FormatException ();
158                                                 }
159                                                 break;
160                                         } else
161                                                 if (tryParse)
162                                                         return false;
163                                                 else
164                                                         throw new FormatException ();
165                                 }
166                         }
167
168                         // -0 is legal but other negative values are not
169                         if (negative && (val > 0)) {
170                                 if (tryParse)
171                                         return false;
172                                 else
173                                         throw new OverflowException (
174                                             Locale.GetText ("Negative number"));
175                         }
176
177                         // if all we had was a '+' sign, then throw exception
178                         if (!digits_seen)
179                                 if (tryParse)
180                                         return false;
181                                 else
182                                         throw new FormatException ();
183
184                     result = val;
185                         return true;
186                 }
187
188                 public static byte Parse (string s, IFormatProvider provider)
189                 {
190                         return Parse (s, NumberStyles.Integer, provider);
191                 }
192
193                 public static byte Parse (string s, NumberStyles style)
194                 {
195                         return Parse (s, style, null);
196                 }
197
198                 public static byte Parse (string s, NumberStyles style, IFormatProvider provider)
199                 {
200                         uint tmpResult = UInt32.Parse (s, style, provider);
201                         if (tmpResult > Byte.MaxValue || tmpResult < Byte.MinValue)
202                                 throw new OverflowException (Locale.GetText ("Value too large or too small."));
203
204                         return (byte) tmpResult;
205                 }
206
207                 public static byte Parse (string s) {
208                         byte res;
209
210                         Parse (s, false, out res);
211
212                         return res;
213                 }
214
215 #if NET_2_0
216                 public static bool TryParse (string s, out byte result) {
217                         try {
218                                 return Parse (s, true, out result);
219                         }
220                         catch (Exception) {
221                                 result = 0;
222                                 return false;
223                         }
224                 }
225
226                 public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out byte result) {
227                         try {
228                                 uint tmpResult;
229
230                                 result = 0;
231                                 if (!UInt32.TryParse (s, style, provider, out tmpResult))
232                                         return false;
233                                 if (tmpResult > Byte.MaxValue || tmpResult < Byte.MinValue)
234                                         return false;
235                                 result = (byte)tmpResult;
236                                 return true;
237                         }
238                         catch (Exception) {
239                                 result = 0;
240                                 return false;
241                         }
242                 }
243 #endif
244
245                 public override string ToString ()
246                 {
247                         return ToString (null, null);
248                 }
249
250                 public string ToString (string format)
251                 {
252                         return ToString (format, null);
253                 }
254
255                 public string ToString (IFormatProvider provider)
256                 {
257                         return ToString (null, provider);
258                 }
259
260                 public string ToString (string format, IFormatProvider provider)
261                 {
262                         NumberFormatInfo nfi = NumberFormatInfo.GetInstance (provider);
263
264                         // null or empty ("")
265                         if ((format == null) || (format.Length == 0))
266                                 format = "G";
267
268                         return IntegerFormatter.NumberToString (format, nfi, m_value);
269                 }
270
271                 // =========== IConvertible Methods =========== //
272                 public TypeCode GetTypeCode ()
273                 {
274                         return TypeCode.Byte;
275                 }
276
277                 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
278                 {
279                         return System.Convert.ToType (m_value, conversionType, provider);
280                 }
281
282                 bool IConvertible.ToBoolean (IFormatProvider provider)
283                 {
284                         return System.Convert.ToBoolean (m_value);
285                 }
286
287                 byte IConvertible.ToByte (IFormatProvider provider)
288                 {
289                         return m_value;
290                 }
291
292                 char IConvertible.ToChar (IFormatProvider provider)
293                 {
294                         return System.Convert.ToChar (m_value);
295                 }
296
297                 DateTime IConvertible.ToDateTime (IFormatProvider provider)
298                 {
299                         throw new InvalidCastException ();
300                 }
301
302                 decimal IConvertible.ToDecimal (IFormatProvider provider)
303                 {
304                         return System.Convert.ToDecimal (m_value);
305                 }
306
307                 double IConvertible.ToDouble (IFormatProvider provider)
308                 {
309                         return System.Convert.ToDouble (m_value);
310                 }
311
312                 short IConvertible.ToInt16 (IFormatProvider provider)
313                 {
314                         return System.Convert.ToInt16 (m_value);
315                 }
316
317                 int IConvertible.ToInt32 (IFormatProvider provider)
318                 {
319                         return System.Convert.ToInt32 (m_value);
320                 }
321
322                 long IConvertible.ToInt64 (IFormatProvider provider)
323                 {
324                         return System.Convert.ToInt64 (m_value);
325                 }
326
327                 sbyte IConvertible.ToSByte (IFormatProvider provider)
328                 {
329                         return System.Convert.ToSByte (m_value);
330                 }
331
332                 float IConvertible.ToSingle (IFormatProvider provider)
333                 {
334                         return System.Convert.ToSingle (m_value);
335                 }
336
337 /*
338                 string IConvertible.ToString (IFormatProvider provider)
339                 {
340                         return ToString("G", provider);
341                 }
342 */
343
344                 ushort IConvertible.ToUInt16 (IFormatProvider provider)
345                 {
346                         return System.Convert.ToUInt16 (m_value);
347                 }
348
349                 uint IConvertible.ToUInt32 (IFormatProvider provider)
350                 {
351                         return System.Convert.ToUInt32 (m_value);
352                 }
353
354                 ulong IConvertible.ToUInt64 (IFormatProvider provider)
355                 {
356                         return System.Convert.ToUInt64 (m_value);
357                 }
358         }
359 }