2002-01-14 Miguel de Icaza <miguel@ximian.com>
[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 using System.Globalization;
11
12 namespace System {
13         
14         public struct Byte : IComparable, IFormattable { //, IConvertible {
15                 private static Type Type = typeof (byte);
16                 
17                 public const byte MinValue = 0;
18                 public const byte MaxValue = 255;
19                 
20                 // VES needs to know about value.  public is workaround
21                 // so source will compile
22                 public byte value;
23
24                 public int CompareTo (object v)
25                 {
26                         if (v == null)
27                                 return 1;
28
29                         if (!(v is System.Byte))
30                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Byte"));
31
32                         byte xv = (byte) v;
33
34                         if (value == xv)
35                                 return 0;
36                         if (value > xv)
37                                 return 1;
38                         else
39                                 return -1;
40                 }
41
42                 public override bool Equals (object o)
43                 {
44                         if (!(o is System.Byte))
45                                 return false;
46
47                         return ((byte) o) == value;
48                 }
49
50                 public override int GetHashCode ()
51                 {
52                         return value;
53                 }
54
55                 public static byte Parse (string s)
56                 {
57                         byte val = 0;
58                         int len;
59                         int i;
60                         bool digits_seen = false;
61                         
62                         if (s == null)
63                                 throw new ArgumentNullException (Locale.GetText ("s is null"));
64
65                         len = s.Length;
66
67                         // look for the first non-whitespace character
68                         char c;
69                         for (i = 0; i < len; i++){
70                                 c = s [i];
71                                 if (!Char.IsWhiteSpace (c))
72                                         break;
73                         }
74                         
75                         // if it's all whitespace, then throw exception
76                         if (i == len)
77                                 throw new FormatException ();
78
79                         // look for the optional '+' sign
80                         if (s [i] == '+')
81                                 i++;
82
83                         // we should just have numerals followed by whitespace now
84                         for (; i < len; i++){
85                                 c = s [i];
86
87                                 if (c >= '0' && c <= '9'){
88                                         // shift left and accumulate every time we find a numeral
89                                         byte d = (byte) (c - '0');
90                                         
91                                         val = checked ((byte) (val * 10 + d));
92                                         digits_seen = true;
93                                 } else {
94                                         // after the last numeral, only whitespace is allowed
95                                         if (Char.IsWhiteSpace (c)){
96                                                 for (i++; i < len; i++){
97                                                         if (!Char.IsWhiteSpace (s [i]))
98                                                                 throw new FormatException ();
99                                                 }
100                                                 break;
101                                         } else
102                                                 throw new FormatException ();
103                                 }
104                         }
105
106                         // if all we had was a '+' sign, then throw exception
107                         if (!digits_seen)
108                                 throw new FormatException ();
109                         
110                         return val;
111                 }
112
113                 public static byte Parse (string s, IFormatProvider fp)
114                 {
115                         return Parse (s, NumberStyles.Integer, fp);
116                 }
117
118                 public static byte Parse (string s, NumberStyles style)
119                 {
120                         return Parse (s, style, null);
121                 }
122
123                 [MonoTODO]
124                 public static byte Parse (string s, NumberStyles style, IFormatProvider fp)
125                 {
126                         if (null == s){
127                                 throw new ArgumentNullException();
128                         }
129
130                         // TODO: Handle other styles and FormatProvider properties
131                         throw new NotImplementedException();
132                 }
133
134                 public override string ToString ()
135                 {
136                         return ToString ("G", null);
137                 }
138
139                 public string ToString (IFormatProvider fp)
140                 {
141                         return ToString ("G", fp);
142                 }
143
144                 public string ToString (string format)
145                 {
146                         return ToString (format, null);
147                 }
148
149                 public string ToString (string format, IFormatProvider fp)
150                 {
151                         string fmt;
152                         NumberFormatInfo nfi;
153                         
154                         fmt = (format == null) ? "G" : format;
155                         
156                         if (fp == null)
157                                 nfi = NumberFormatInfo.CurrentInfo;
158                         else {
159                                 nfi = (NumberFormatInfo) fp.GetFormat (Type);
160                                 
161                                 if (nfi == null)
162                                         nfi = NumberFormatInfo.CurrentInfo;
163                         }
164
165                         return IntegerFormatter.NumberToString (fmt, nfi, value);
166                 }
167
168                 // =========== IConvertible Methods =========== //
169                 
170                 public TypeCode GetTypeCode ()
171                 {
172                         return TypeCode.Byte;
173                 }
174         }
175 }