2002-03-14 Miguel de Icaza <miguel@ximian.com>
[mono.git] / mcs / class / corlib / System / UInt64.cs
1 //
2 // System.UInt64.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         [CLSCompliant(false)]
15         public struct UInt64 : IComparable, IFormattable, IConvertible {
16                 public const ulong MaxValue = 0xffffffffffffffff;
17                 public const ulong MinValue = 0;
18                 
19                 public ulong value;
20
21                 public int CompareTo (object v)
22                 {
23                         if (v == null)
24                                 return 1;
25
26                         if (!(v is System.UInt64))
27                                 throw new ArgumentException (Locale.GetText ("Value is not a System.UInt64"));
28
29                         if (value == (ulong) v)
30                                 return 0;
31
32                         if (value < (ulong) v)
33                                 return -1;
34
35                         return 1;
36                 }
37
38                 public override bool Equals (object o)
39                 {
40                         if (!(o is System.UInt64))
41                                 return false;
42
43                         return ((ulong) o) == value;
44                 }
45
46                 public override int GetHashCode ()
47                 {
48                         return (int)(value & 0xffffffff) ^ (int)(value >> 32);
49                 }
50
51                 [CLSCompliant(false)]
52                 public static ulong Parse (string s)
53                 {
54                         return Parse (s, NumberStyles.Integer, null);
55                 }
56
57                 [CLSCompliant(false)]
58                 public static ulong Parse (string s, IFormatProvider fp)
59                 {
60                         return Parse (s, NumberStyles.Integer, fp);
61                 }
62
63                 [CLSCompliant(false)]
64                 public static ulong Parse (string s, NumberStyles style)
65                 {
66                         return Parse (s, style, null);
67                 }
68
69                 [CLSCompliant(false)]
70                 public static ulong Parse (string s, NumberStyles style, IFormatProvider fp)
71                 {
72                         ulong val = 0;
73                         int len;
74                         int i;
75                         bool digits_seen = false;
76                         
77                         if (s == null)
78                                 throw new ArgumentNullException (Locale.GetText ("s is null"));
79
80                         len = s.Length;
81
82                         char c;
83                         i = 0;
84                         if ((style & NumberStyles.AllowLeadingWhite) != 0)
85                                 for (i = 0; i < len; i++){
86                                         c = s [i];
87                                         if (!Char.IsWhiteSpace (c))
88                                                 break;
89                                 }
90                         
91                         if (i == len)
92                                 throw new FormatException ();
93
94                         if ((style & NumberStyles.AllowLeadingSign) != 0 && (s [i] == '+'))
95                                 i++;
96
97                         for (; i < len; i++){
98                                 c = s [i];
99
100                                 if ((style & NumberStyles.AllowHexSpecifier) != 0) {
101                                         if (c >= '0' && c <= '9') {
102                                                 uint d = (uint) (c - '0');
103                                                 val = checked (val * 16 + d);
104                                                 digits_seen = true;
105                                         } else if (c >= 'a' && c <= 'f') {
106                                                 uint d = (uint) (c - 'a');
107                                                 val = checked (val * 16 + 10 + d);
108                                                 digits_seen = true;
109                                         } else if (c >= 'A' && c <= 'F') {
110                                                 uint d = (uint) (c - 'A');
111                                                 val = checked (val * 16 + 10 + d);
112                                                 digits_seen = true;
113                                         } else
114                                                 break;
115                                 } else if (c >= '0' && c <= '9'){
116                                         uint d = (uint) (c - '0');
117                                         
118                                         val = checked (val * 10 + d);
119                                         digits_seen = true;
120                                 } else {
121                                         break;
122                                 }
123                         }
124                         if (!digits_seen)
125                                 throw new FormatException ();
126                         if (i < len) {
127                                 if ((style & NumberStyles.AllowTrailingWhite) != 0 && Char.IsWhiteSpace (s [i])){
128                                         for (i++; i < len; i++){
129                                                 if (!Char.IsWhiteSpace (s [i]))
130                                                         throw new FormatException ();
131                                         }
132                                 } else
133                                         throw new FormatException ();
134                         }
135         
136                         return val;
137
138                 }
139
140                 public override string ToString ()
141                 {
142                         return ToString (null, null);
143                 }
144
145                 public string ToString (IFormatProvider fp)
146                 {
147                         return ToString (null, fp);
148                 }
149
150                 public string ToString (string format)
151                 {
152                         return ToString (format, null);
153                 }
154
155                 public string ToString (string format, IFormatProvider fp)
156                 {
157                         NumberFormatInfo nfi = NumberFormatInfo.GetInstance( fp );
158                         
159                         if ( format == null )
160                                 format = "G";
161                         
162                         return IntegerFormatter.NumberToString(format, nfi, value);
163                 }
164
165                 // =========== IConvertible Methods =========== //
166
167                 public TypeCode GetTypeCode ()
168                 {
169                         return TypeCode.UInt64;
170                 }
171                 public bool     ToBoolean  (IFormatProvider provider)
172                 {
173                         return System.Convert.ToBoolean (value);
174                 }
175                 public byte     ToByte     (IFormatProvider provider)
176                 {
177                         return System.Convert.ToByte (value);
178                 }
179                 public char     ToChar     (IFormatProvider provider)
180                 {
181                         return System.Convert.ToChar (value);
182                 }
183                 public DateTime ToDateTime (IFormatProvider provider)
184                 {
185                         throw new NotImplementedException ();
186                 }
187                 public decimal  ToDecimal  (IFormatProvider provider)
188                 {
189                         return System.Convert.ToDecimal (value);
190                 }
191                 public double   ToDouble   (IFormatProvider provider)
192                 {
193                         return System.Convert.ToDouble (value);
194                 }
195                 public short    ToInt16    (IFormatProvider provider)
196                 {
197                         return System.Convert.ToInt16 (value);
198                 }
199                 public int      ToInt32    (IFormatProvider provider)
200                 {
201                         return System.Convert.ToInt32 (value);
202                 }
203                 public long     ToInt64    (IFormatProvider provider)
204                 {
205                         return System.Convert.ToInt64 (value);
206                 }
207                 [CLSCompliant(false)]
208                 public sbyte    ToSByte    (IFormatProvider provider)
209                 {
210                         return System.Convert.ToSByte (value);
211                 }
212                 public float    ToSingle   (IFormatProvider provider)
213                 {
214                         return System.Convert.ToSingle (value);
215                 }
216                 public object   ToType     (Type conversionType, IFormatProvider provider)
217                 {
218                         throw new NotImplementedException ();
219                 }
220                 [CLSCompliant(false)]
221                 public ushort   ToUInt16   (IFormatProvider provider)
222                 {
223                         return System.Convert.ToUInt16 (value);
224                 }
225                 [CLSCompliant(false)]
226                 public uint     ToUInt32   (IFormatProvider provider)
227                 {
228                         return System.Convert.ToUInt32 (value);
229                 }
230                 [CLSCompliant(false)]
231                 public ulong    ToUInt64   (IFormatProvider provider)
232                 {
233                         return value;
234                 }
235         }
236 }