In .:
[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, IComparable
39 #if NET_2_0
40                 , IComparable<Byte>, IEquatable <Byte>
41 #endif
42         {
43                 public const byte MinValue = 0;
44                 public const byte MaxValue = 255;
45
46                 internal byte m_value;
47
48                 public int CompareTo (object value)
49                 {
50                         if (value == null)
51                                 return 1;
52
53                         if (!(value is System.Byte))
54                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Byte."));
55
56                         byte xv = (byte) value;
57
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 obj)
67                 {
68                         if (!(obj is System.Byte))
69                                 return false;
70
71                         return ((byte) obj) == 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 (byte 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 (byte value)
91                 {
92                         return value == m_value;
93                 }
94 #endif
95
96                 internal static bool Parse (string s, bool tryParse, out byte result, out Exception exc)
97                 {
98                         byte val = 0;
99                         int len;
100                         int i;
101                         bool digits_seen = false;
102                         bool negative = 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                         // look for the first non-whitespace character
116                         char c;
117                         for (i = 0; i < len; i++){
118                                 c = s [i];
119                                 if (!Char.IsWhiteSpace (c))
120                                         break;
121                         }
122
123                         // if it's all whitespace, then throw exception
124                         if (i == len) {
125                                 if (!tryParse)
126                                         exc = Int32.GetFormatException ();
127                                 return false;
128                         }
129
130                         // look for the optional '+' sign
131                         if (s [i] == '+')
132                                 i++;
133                         else if (s [i] == '-') {
134                                 negative = true;
135                                 i++;
136                         }
137
138                         // we should just have numerals followed by whitespace now
139                         for (; i < len; i++){
140                                 c = s [i];
141
142                                 if (c >= '0' && c <= '9'){
143                                         // shift left and accumulate every time we find a numeral
144                                         byte d = (byte) (c - '0');
145
146                                         val = checked ((byte) (val * 10 + d));
147                                         digits_seen = true;
148                                 } else {
149                                         // after the last numeral, only whitespace is allowed
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
167                         // -0 is legal but other negative values are not
168                         if (negative && (val > 0)) {
169                                 if (!tryParse)
170                                         exc = new OverflowException (
171                                             Locale.GetText ("Negative number"));
172                                 return false;
173                         }
174
175                         // if all we had was a '+' sign, then throw exception
176                         if (!digits_seen) {
177                                 if (!tryParse)
178                                         exc = Int32.GetFormatException ();
179                                 return false;
180                         }
181
182                         result = val;
183                         return true;
184                 }
185
186                 public static byte Parse (string s, IFormatProvider provider)
187                 {
188                         return Parse (s, NumberStyles.Integer, provider);
189                 }
190
191                 public static byte Parse (string s, NumberStyles style)
192                 {
193                         return Parse (s, style, null);
194                 }
195
196                 public static byte Parse (string s, NumberStyles style, IFormatProvider provider)
197                 {
198                         uint tmpResult = UInt32.Parse (s, style, provider);
199                         if (tmpResult > Byte.MaxValue || tmpResult < Byte.MinValue)
200                                 throw new OverflowException (Locale.GetText ("Value too large or too small."));
201
202                         return (byte) tmpResult;
203                 }
204
205                 public static byte Parse (string s) 
206                 {
207                         Exception exc;
208                         byte res;
209
210                         if (!Parse (s, false, out res, out exc))
211                                 throw exc;
212
213                         return res;
214                 }
215
216 #if NET_2_0
217                 public static bool TryParse (string s, out byte result) 
218                 {
219                         Exception exc;
220                         if (!Parse (s, true, out result, out exc)) {
221                                 result = 0;
222                                 return false;
223                         }
224
225                         return true;
226                 }
227
228                 public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out byte result) 
229                 {
230                         uint tmpResult;
231                         result = 0;
232                         
233                         if (!UInt32.TryParse (s, style, provider, out tmpResult))
234                                 return false;
235                                 
236                         if (tmpResult > Byte.MaxValue || tmpResult < Byte.MinValue)
237                                 return false;
238                                 
239                         result = (byte)tmpResult;
240                         return true;
241                 }
242 #endif
243
244                 public override string ToString ()
245                 {
246                         return NumberFormatter.FormatGeneral (new NumberFormatter.NumberStore (m_value));
247                 }
248
249                 public string ToString (string format)
250                 {
251                         return ToString (format, null);
252                 }
253
254                 public string ToString (IFormatProvider provider)
255                 {
256                         return NumberFormatter.FormatGeneral (new NumberFormatter.NumberStore (m_value), provider);
257                 }
258
259                 public string ToString (string format, IFormatProvider provider)
260                 {
261                         NumberFormatInfo nfi = NumberFormatInfo.GetInstance (provider);
262                         return NumberFormatter.NumberToString (format, m_value, nfi);
263                 }
264
265                 // =========== IConvertible Methods =========== //
266                 public TypeCode GetTypeCode ()
267                 {
268                         return TypeCode.Byte;
269                 }
270
271                 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
272                 {
273                         return System.Convert.ToType (m_value, conversionType, provider);
274                 }
275
276                 bool IConvertible.ToBoolean (IFormatProvider provider)
277                 {
278                         return System.Convert.ToBoolean (m_value);
279                 }
280
281                 byte IConvertible.ToByte (IFormatProvider provider)
282                 {
283                         return m_value;
284                 }
285
286                 char IConvertible.ToChar (IFormatProvider provider)
287                 {
288                         return System.Convert.ToChar (m_value);
289                 }
290
291                 DateTime IConvertible.ToDateTime (IFormatProvider provider)
292                 {
293                         throw new InvalidCastException ();
294                 }
295
296                 decimal IConvertible.ToDecimal (IFormatProvider provider)
297                 {
298                         return System.Convert.ToDecimal (m_value);
299                 }
300
301                 double IConvertible.ToDouble (IFormatProvider provider)
302                 {
303                         return System.Convert.ToDouble (m_value);
304                 }
305
306                 short IConvertible.ToInt16 (IFormatProvider provider)
307                 {
308                         return System.Convert.ToInt16 (m_value);
309                 }
310
311                 int IConvertible.ToInt32 (IFormatProvider provider)
312                 {
313                         return System.Convert.ToInt32 (m_value);
314                 }
315
316                 long IConvertible.ToInt64 (IFormatProvider provider)
317                 {
318                         return System.Convert.ToInt64 (m_value);
319                 }
320
321                 sbyte IConvertible.ToSByte (IFormatProvider provider)
322                 {
323                         return System.Convert.ToSByte (m_value);
324                 }
325
326                 float IConvertible.ToSingle (IFormatProvider provider)
327                 {
328                         return System.Convert.ToSingle (m_value);
329                 }
330
331 /*
332                 string IConvertible.ToString (IFormatProvider provider)
333                 {
334                         return ToString("G", provider);
335                 }
336 */
337
338                 ushort IConvertible.ToUInt16 (IFormatProvider provider)
339                 {
340                         return System.Convert.ToUInt16 (m_value);
341                 }
342
343                 uint IConvertible.ToUInt32 (IFormatProvider provider)
344                 {
345                         return System.Convert.ToUInt32 (m_value);
346                 }
347
348                 ulong IConvertible.ToUInt64 (IFormatProvider provider)
349                 {
350                         return System.Convert.ToUInt64 (m_value);
351                 }
352         }
353 }