This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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 : IComparable, IFormattable, IConvertible
39         {
40                 public const byte MinValue = 0;
41                 public const byte MaxValue = 255;
42
43                 internal byte m_value;
44
45                 public int CompareTo (object value)
46                 {
47                         if (value == null)
48                                 return 1;
49
50                         if (!(value is System.Byte))
51                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Byte."));
52
53                         byte xv = (byte) value;
54
55                         if (m_value == xv)
56                                 return 0;
57                         if (m_value > xv)
58                                 return 1;
59                         else
60                                 return -1;
61                 }
62
63                 public override bool Equals (object obj)
64                 {
65                         if (!(obj is System.Byte))
66                                 return false;
67
68                         return ((byte) obj) == m_value;
69                 }
70
71                 public override int GetHashCode ()
72                 {
73                         return m_value;
74                 }
75
76                 public static byte Parse (string s)
77                 {
78                         byte val = 0;
79                         int len;
80                         int i;
81                         bool digits_seen = false;
82                         bool negative = false;
83
84                         if (s == null)
85                                 throw new ArgumentNullException ("s");
86
87                         len = s.Length;
88
89                         // look for the first non-whitespace character
90                         char c;
91                         for (i = 0; i < len; i++){
92                                 c = s [i];
93                                 if (!Char.IsWhiteSpace (c))
94                                         break;
95                         }
96
97                         // if it's all whitespace, then throw exception
98                         if (i == len)
99                                 throw new FormatException ();
100
101                         // look for the optional '+' sign
102                         if (s [i] == '+')
103                                 i++;
104                         else if (s [i] == '-') {
105                                 negative = true;
106                                 i++;
107                         }
108
109                         // we should just have numerals followed by whitespace now
110                         for (; i < len; i++){
111                                 c = s [i];
112
113                                 if (c >= '0' && c <= '9'){
114                                         // shift left and accumulate every time we find a numeral
115                                         byte d = (byte) (c - '0');
116
117                                         val = checked ((byte) (val * 10 + d));
118                                         digits_seen = true;
119                                 } else {
120                                         // after the last numeral, only whitespace is allowed
121                                         if (Char.IsWhiteSpace (c)){
122                                                 for (i++; i < len; i++){
123                                                         if (!Char.IsWhiteSpace (s [i]))
124                                                                 throw new FormatException ();
125                                                 }
126                                                 break;
127                                         } else
128                                                 throw new FormatException ();
129                                 }
130                         }
131
132                         // -0 is legal but other negative values are not
133                         if (negative && (val > 0)) {
134                                 throw new OverflowException (
135                                         Locale.GetText ("Negative number"));
136                         }
137
138                         // if all we had was a '+' sign, then throw exception
139                         if (!digits_seen)
140                                 throw new FormatException ();
141
142                         return val;
143                 }
144
145                 public static byte Parse (string s, IFormatProvider provider)
146                 {
147                         return Parse (s, NumberStyles.Integer, provider);
148                 }
149
150                 public static byte Parse (string s, NumberStyles style)
151                 {
152                         return Parse (s, style, null);
153                 }
154
155                 public static byte Parse (string s, NumberStyles style, IFormatProvider provider)
156                 {
157                         uint tmpResult = UInt32.Parse (s, style, provider);
158                         if (tmpResult > Byte.MaxValue || tmpResult < Byte.MinValue)
159                                 throw new OverflowException (Locale.GetText ("Value too large or too small."));
160
161                         return (byte) tmpResult;
162                 }
163
164                 public override string ToString ()
165                 {
166                         return ToString (null, null);
167                 }
168
169                 public string ToString (string format)
170                 {
171                         return ToString (format, null);
172                 }
173
174                 public string ToString (IFormatProvider provider)
175                 {
176                         return ToString (null, provider);
177                 }
178
179                 public string ToString (string format, IFormatProvider provider)
180                 {
181                         NumberFormatInfo nfi = NumberFormatInfo.GetInstance (provider);
182
183                         // null or empty ("")
184                         if ((format == null) || (format.Length == 0))
185                                 format = "G";
186
187                         return IntegerFormatter.NumberToString (format, nfi, m_value);
188                 }
189
190                 // =========== IConvertible Methods =========== //
191                 public TypeCode GetTypeCode ()
192                 {
193                         return TypeCode.Byte;
194                 }
195
196                 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
197                 {
198                         return System.Convert.ToType (m_value, conversionType, provider);
199                 }
200
201                 bool IConvertible.ToBoolean (IFormatProvider provider)
202                 {
203                         return System.Convert.ToBoolean (m_value);
204                 }
205
206                 byte IConvertible.ToByte (IFormatProvider provider)
207                 {
208                         return m_value;
209                 }
210
211                 char IConvertible.ToChar (IFormatProvider provider)
212                 {
213                         return System.Convert.ToChar (m_value);
214                 }
215
216                 DateTime IConvertible.ToDateTime (IFormatProvider provider)
217                 {
218                         throw new InvalidCastException ();
219                 }
220
221                 decimal IConvertible.ToDecimal (IFormatProvider provider)
222                 {
223                         return System.Convert.ToDecimal (m_value);
224                 }
225
226                 double IConvertible.ToDouble (IFormatProvider provider)
227                 {
228                         return System.Convert.ToDouble (m_value);
229                 }
230
231                 short IConvertible.ToInt16 (IFormatProvider provider)
232                 {
233                         return System.Convert.ToInt16 (m_value);
234                 }
235
236                 int IConvertible.ToInt32 (IFormatProvider provider)
237                 {
238                         return System.Convert.ToInt32 (m_value);
239                 }
240
241                 long IConvertible.ToInt64 (IFormatProvider provider)
242                 {
243                         return System.Convert.ToInt64 (m_value);
244                 }
245
246                 sbyte IConvertible.ToSByte (IFormatProvider provider)
247                 {
248                         return System.Convert.ToSByte (m_value);
249                 }
250
251                 float IConvertible.ToSingle (IFormatProvider provider)
252                 {
253                         return System.Convert.ToSingle (m_value);
254                 }
255
256 /*
257                 string IConvertible.ToString (IFormatProvider provider)
258                 {
259                         return ToString("G", provider);
260                 }
261 */
262
263                 ushort IConvertible.ToUInt16 (IFormatProvider provider)
264                 {
265                         return System.Convert.ToUInt16 (m_value);
266                 }
267
268                 uint IConvertible.ToUInt32 (IFormatProvider provider)
269                 {
270                         return System.Convert.ToUInt32 (m_value);
271                 }
272
273                 ulong IConvertible.ToUInt64 (IFormatProvider provider)
274                 {
275                         return System.Convert.ToUInt64 (m_value);
276                 }
277         }
278 }