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 internal static bool Parse (string s, bool tryParse, out byte result)
103 bool digits_seen = false;
104 bool negative = false;
112 throw new ArgumentNullException ("s");
116 // look for the first non-whitespace character
118 for (i = 0; i < len; i++){
120 if (!Char.IsWhiteSpace (c))
124 // if it's all whitespace, then throw exception
129 throw new FormatException ();
131 // look for the optional '+' sign
134 else if (s [i] == '-') {
139 // we should just have numerals followed by whitespace now
140 for (; i < len; i++){
143 if (c >= '0' && c <= '9'){
144 // shift left and accumulate every time we find a numeral
145 byte d = (byte) (c - '0');
147 val = checked ((byte) (val * 10 + d));
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]))
157 throw new FormatException ();
164 throw new FormatException ();
168 // -0 is legal but other negative values are not
169 if (negative && (val > 0)) {
173 throw new OverflowException (
174 Locale.GetText ("Negative number"));
177 // if all we had was a '+' sign, then throw exception
182 throw new FormatException ();
188 public static byte Parse (string s, IFormatProvider provider)
190 return Parse (s, NumberStyles.Integer, provider);
193 public static byte Parse (string s, NumberStyles style)
195 return Parse (s, style, null);
198 public static byte Parse (string s, NumberStyles style, IFormatProvider provider)
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."));
204 return (byte) tmpResult;
207 public static byte Parse (string s) {
210 Parse (s, false, out res);
216 public static bool TryParse (string s, out byte result) {
218 return Parse (s, true, out result);
226 public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out byte result) {
231 if (!UInt32.TryParse (s, style, provider, out tmpResult))
233 if (tmpResult > Byte.MaxValue || tmpResult < Byte.MinValue)
235 result = (byte)tmpResult;
245 public override string ToString ()
247 return ToString (null, null);
250 public string ToString (string format)
252 return ToString (format, null);
255 public string ToString (IFormatProvider provider)
257 return ToString (null, provider);
260 public string ToString (string format, IFormatProvider provider)
262 NumberFormatInfo nfi = NumberFormatInfo.GetInstance (provider);
264 // null or empty ("")
265 if ((format == null) || (format.Length == 0))
268 return IntegerFormatter.NumberToString (format, nfi, m_value);
271 // =========== IConvertible Methods =========== //
272 public TypeCode GetTypeCode ()
274 return TypeCode.Byte;
277 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
279 return System.Convert.ToType (m_value, conversionType, provider);
282 bool IConvertible.ToBoolean (IFormatProvider provider)
284 return System.Convert.ToBoolean (m_value);
287 byte IConvertible.ToByte (IFormatProvider provider)
292 char IConvertible.ToChar (IFormatProvider provider)
294 return System.Convert.ToChar (m_value);
297 DateTime IConvertible.ToDateTime (IFormatProvider provider)
299 throw new InvalidCastException ();
302 decimal IConvertible.ToDecimal (IFormatProvider provider)
304 return System.Convert.ToDecimal (m_value);
307 double IConvertible.ToDouble (IFormatProvider provider)
309 return System.Convert.ToDouble (m_value);
312 short IConvertible.ToInt16 (IFormatProvider provider)
314 return System.Convert.ToInt16 (m_value);
317 int IConvertible.ToInt32 (IFormatProvider provider)
319 return System.Convert.ToInt32 (m_value);
322 long IConvertible.ToInt64 (IFormatProvider provider)
324 return System.Convert.ToInt64 (m_value);
327 sbyte IConvertible.ToSByte (IFormatProvider provider)
329 return System.Convert.ToSByte (m_value);
332 float IConvertible.ToSingle (IFormatProvider provider)
334 return System.Convert.ToSingle (m_value);
338 string IConvertible.ToString (IFormatProvider provider)
340 return ToString("G", provider);
344 ushort IConvertible.ToUInt16 (IFormatProvider provider)
346 return System.Convert.ToUInt16 (m_value);
349 uint IConvertible.ToUInt32 (IFormatProvider provider)
351 return System.Convert.ToUInt32 (m_value);
354 ulong IConvertible.ToUInt64 (IFormatProvider provider)
356 return System.Convert.ToUInt64 (m_value);