5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) Ximian, Inc. http://www.ximian.com
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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.
33 using System.Globalization;
38 public struct Byte : IFormattable, IConvertible,
40 IComparable, IComparable<Byte>
45 public const byte MinValue = 0;
46 public const byte MaxValue = 255;
48 internal byte m_value;
50 public int CompareTo (object value)
55 if (!(value is System.Byte))
56 throw new ArgumentException (Locale.GetText ("Value is not a System.Byte."));
58 byte xv = (byte) value;
68 public override bool Equals (object obj)
70 if (!(obj is System.Byte))
73 return ((byte) obj) == m_value;
76 public override int GetHashCode ()
82 public int CompareTo (byte value)
92 public bool Equals (byte value)
94 return value == m_value;
98 public static byte Parse (string s)
103 bool digits_seen = false;
104 bool negative = false;
107 throw new ArgumentNullException ("s");
111 // look for the first non-whitespace character
113 for (i = 0; i < len; i++){
115 if (!Char.IsWhiteSpace (c))
119 // if it's all whitespace, then throw exception
121 throw new FormatException ();
123 // look for the optional '+' sign
126 else if (s [i] == '-') {
131 // we should just have numerals followed by whitespace now
132 for (; i < len; i++){
135 if (c >= '0' && c <= '9'){
136 // shift left and accumulate every time we find a numeral
137 byte d = (byte) (c - '0');
139 val = checked ((byte) (val * 10 + d));
142 // after the last numeral, only whitespace is allowed
143 if (Char.IsWhiteSpace (c)){
144 for (i++; i < len; i++){
145 if (!Char.IsWhiteSpace (s [i]))
146 throw new FormatException ();
150 throw new FormatException ();
154 // -0 is legal but other negative values are not
155 if (negative && (val > 0)) {
156 throw new OverflowException (
157 Locale.GetText ("Negative number"));
160 // if all we had was a '+' sign, then throw exception
162 throw new FormatException ();
167 public static byte Parse (string s, IFormatProvider provider)
169 return Parse (s, NumberStyles.Integer, provider);
172 public static byte Parse (string s, NumberStyles style)
174 return Parse (s, style, null);
177 public static byte Parse (string s, NumberStyles style, IFormatProvider provider)
179 uint tmpResult = UInt32.Parse (s, style, provider);
180 if (tmpResult > Byte.MaxValue || tmpResult < Byte.MinValue)
181 throw new OverflowException (Locale.GetText ("Value too large or too small."));
183 return (byte) tmpResult;
186 public override string ToString ()
188 return ToString (null, null);
191 public string ToString (string format)
193 return ToString (format, null);
196 public string ToString (IFormatProvider provider)
198 return ToString (null, provider);
201 public string ToString (string format, IFormatProvider provider)
203 NumberFormatInfo nfi = NumberFormatInfo.GetInstance (provider);
205 // null or empty ("")
206 if ((format == null) || (format.Length == 0))
209 return IntegerFormatter.NumberToString (format, nfi, m_value);
212 // =========== IConvertible Methods =========== //
213 public TypeCode GetTypeCode ()
215 return TypeCode.Byte;
218 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
220 return System.Convert.ToType (m_value, conversionType, provider);
223 bool IConvertible.ToBoolean (IFormatProvider provider)
225 return System.Convert.ToBoolean (m_value);
228 byte IConvertible.ToByte (IFormatProvider provider)
233 char IConvertible.ToChar (IFormatProvider provider)
235 return System.Convert.ToChar (m_value);
238 DateTime IConvertible.ToDateTime (IFormatProvider provider)
240 throw new InvalidCastException ();
243 decimal IConvertible.ToDecimal (IFormatProvider provider)
245 return System.Convert.ToDecimal (m_value);
248 double IConvertible.ToDouble (IFormatProvider provider)
250 return System.Convert.ToDouble (m_value);
253 short IConvertible.ToInt16 (IFormatProvider provider)
255 return System.Convert.ToInt16 (m_value);
258 int IConvertible.ToInt32 (IFormatProvider provider)
260 return System.Convert.ToInt32 (m_value);
263 long IConvertible.ToInt64 (IFormatProvider provider)
265 return System.Convert.ToInt64 (m_value);
268 sbyte IConvertible.ToSByte (IFormatProvider provider)
270 return System.Convert.ToSByte (m_value);
273 float IConvertible.ToSingle (IFormatProvider provider)
275 return System.Convert.ToSingle (m_value);
279 string IConvertible.ToString (IFormatProvider provider)
281 return ToString("G", provider);
285 ushort IConvertible.ToUInt16 (IFormatProvider provider)
287 return System.Convert.ToUInt16 (m_value);
290 uint IConvertible.ToUInt32 (IFormatProvider provider)
292 return System.Convert.ToUInt32 (m_value);
295 ulong IConvertible.ToUInt64 (IFormatProvider provider)
297 return System.Convert.ToUInt64 (m_value);