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, IComparable
40 , IComparable<Byte>, IEquatable <Byte>
43 public const byte MinValue = 0;
44 public const byte MaxValue = 255;
46 internal byte m_value;
48 public int CompareTo (object value)
53 if (!(value is System.Byte))
54 throw new ArgumentException (Locale.GetText ("Value is not a System.Byte."));
56 byte xv = (byte) value;
66 public override bool Equals (object obj)
68 if (!(obj is System.Byte))
71 return ((byte) obj) == m_value;
74 public override int GetHashCode ()
80 public int CompareTo (byte value)
90 public bool Equals (byte value)
92 return value == m_value;
96 internal static bool Parse (string s, bool tryParse, out byte result, out Exception exc)
101 bool digits_seen = false;
102 bool negative = false;
109 exc = new ArgumentNullException ("s");
115 // look for the first non-whitespace character
117 for (i = 0; i < len; i++){
119 if (!Char.IsWhiteSpace (c))
123 // if it's all whitespace, then throw exception
126 exc = Int32.GetFormatException ();
130 // look for the optional '+' sign
133 else if (s [i] == '-') {
138 // we should just have numerals followed by whitespace now
139 for (; i < len; i++){
142 if (c >= '0' && c <= '9'){
143 // shift left and accumulate every time we find a numeral
144 byte d = (byte) (c - '0');
146 val = checked ((byte) (val * 10 + d));
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])) {
154 exc = Int32.GetFormatException ();
161 exc = Int32.GetFormatException ();
167 // -0 is legal but other negative values are not
168 if (negative && (val > 0)) {
170 exc = new OverflowException (
171 Locale.GetText ("Negative number"));
175 // if all we had was a '+' sign, then throw exception
178 exc = Int32.GetFormatException ();
186 public static byte Parse (string s, IFormatProvider provider)
188 return Parse (s, NumberStyles.Integer, provider);
191 public static byte Parse (string s, NumberStyles style)
193 return Parse (s, style, null);
196 public static byte Parse (string s, NumberStyles style, IFormatProvider provider)
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."));
202 return (byte) tmpResult;
205 public static byte Parse (string s)
210 if (!Parse (s, false, out res, out exc))
217 public static bool TryParse (string s, out byte result)
220 if (!Parse (s, true, out result, out exc)) {
228 public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out byte result)
233 if (!UInt32.TryParse (s, style, provider, out tmpResult))
236 if (tmpResult > Byte.MaxValue || tmpResult < Byte.MinValue)
239 result = (byte)tmpResult;
244 public override string ToString ()
246 return NumberFormatter.FormatGeneral (new NumberFormatter.NumberStore (m_value));
249 public string ToString (string format)
251 return ToString (format, null);
254 public string ToString (IFormatProvider provider)
256 return NumberFormatter.FormatGeneral (new NumberFormatter.NumberStore (m_value), provider);
259 public string ToString (string format, IFormatProvider provider)
261 NumberFormatInfo nfi = NumberFormatInfo.GetInstance (provider);
262 return NumberFormatter.NumberToString (format, m_value, nfi);
265 // =========== IConvertible Methods =========== //
266 public TypeCode GetTypeCode ()
268 return TypeCode.Byte;
271 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
273 return System.Convert.ToType (m_value, conversionType, provider);
276 bool IConvertible.ToBoolean (IFormatProvider provider)
278 return System.Convert.ToBoolean (m_value);
281 byte IConvertible.ToByte (IFormatProvider provider)
286 char IConvertible.ToChar (IFormatProvider provider)
288 return System.Convert.ToChar (m_value);
291 DateTime IConvertible.ToDateTime (IFormatProvider provider)
293 throw new InvalidCastException ();
296 decimal IConvertible.ToDecimal (IFormatProvider provider)
298 return System.Convert.ToDecimal (m_value);
301 double IConvertible.ToDouble (IFormatProvider provider)
303 return System.Convert.ToDouble (m_value);
306 short IConvertible.ToInt16 (IFormatProvider provider)
308 return System.Convert.ToInt16 (m_value);
311 int IConvertible.ToInt32 (IFormatProvider provider)
313 return System.Convert.ToInt32 (m_value);
316 long IConvertible.ToInt64 (IFormatProvider provider)
318 return System.Convert.ToInt64 (m_value);
321 sbyte IConvertible.ToSByte (IFormatProvider provider)
323 return System.Convert.ToSByte (m_value);
326 float IConvertible.ToSingle (IFormatProvider provider)
328 return System.Convert.ToSingle (m_value);
332 string IConvertible.ToString (IFormatProvider provider)
334 return ToString("G", provider);
338 ushort IConvertible.ToUInt16 (IFormatProvider provider)
340 return System.Convert.ToUInt16 (m_value);
343 uint IConvertible.ToUInt32 (IFormatProvider provider)
345 return System.Convert.ToUInt32 (m_value);
348 ulong IConvertible.ToUInt64 (IFormatProvider provider)
350 return System.Convert.ToUInt64 (m_value);