2001-12-21 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                 private static Type Type = typeof (ulong);
17
18                 public const ulong MaxValue = 0xffffffffffffffff;
19                 public const ulong MinValue = 0;
20                 
21                 public ulong value;
22
23                 public int CompareTo (object v)
24                 {
25                         if (v == null)
26                                 return 1;
27
28                         if (!(v is System.UInt64))
29                                 throw new ArgumentException (Locale.GetText ("Value is not a System.UInt64"));
30
31                         if (value == (ulong) v)
32                                 return 0;
33
34                         if (value < (ulong) v)
35                                 return -1;
36
37                         return 1;
38                 }
39
40                 public override bool Equals (object o)
41                 {
42                         if (!(o is System.UInt64))
43                                 return false;
44
45                         return ((ulong) o) == value;
46                 }
47
48                 public override int GetHashCode ()
49                 {
50                         return (int)(value & 0xffffffff) ^ (int)(value >> 32);
51                 }
52
53                 public static ulong Parse (string s)
54                 {
55                         ulong val = 0;
56                         int len;
57                         int i;
58                         bool digits_seen = false;
59                         
60                         if (s == null)
61                                 throw new ArgumentNullException (Locale.GetText ("s is null"));
62
63                         len = s.Length;
64
65                         char c;
66                         for (i = 0; i < len; i++){
67                                 c = s [i];
68                                 if (!Char.IsWhiteSpace (c))
69                                         break;
70                         }
71                         
72                         if (i == len)
73                                 throw new FormatException ();
74
75                         if (s [i] == '+')
76                                 i++;
77
78                         for (; i < len; i++){
79                                 c = s [i];
80
81                                 if (c >= '0' && c <= '9'){
82                                         uint d = (uint) (c - '0');
83                                         
84                                         val = checked (val * 10 + d);
85                                         digits_seen = true;
86                                 } else {
87                                         if (Char.IsWhiteSpace (c)){
88                                                 for (i++; i < len; i++){
89                                                         if (!Char.IsWhiteSpace (s [i]))
90                                                                 throw new FormatException ();
91                                                 }
92                                                 break;
93                                         } else
94                                                 throw new FormatException ();
95                                 }
96                         }
97                         if (!digits_seen)
98                                 throw new FormatException ();
99                         
100                         return val;
101
102                 }
103
104                 public static ulong Parse (string s, IFormatProvider fp)
105                 {
106                         return Parse (s, NumberStyles.Integer, fp);
107                 }
108
109                 public static ulong Parse (string s, NumberStyles style)
110                 {
111                         return Parse (s, style, null);
112                 }
113
114                 public static ulong Parse (string s, NumberStyles style, IFormatProvider fp)
115                 {
116                         ulong val = 0;
117                         int j;
118                         for (j = 0; j < s.Length; ++j) {
119                                 if (s [j] >= '0' && s [j] <= '9')
120                                         val = val * 10 + s [j] - '0';
121                                 else
122                                         break;
123                         }
124                         return val;
125                 }
126
127                 public override string ToString ()
128                 {
129                         return ToString ("G", null);
130                 }
131
132                 public string ToString (IFormatProvider fp)
133                 {
134                         return ToString ("G", fp);
135                 }
136
137                 public string ToString (string format)
138                 {
139                         return ToString (format, null);
140                 }
141
142                 public string ToString (string format, IFormatProvider fp)
143                 {
144                         string fmt;
145                         NumberFormatInfo nfi;
146                         
147                         fmt = (format == null) ? "G" : format;
148                         
149                         if (fp == null)
150                                 nfi = NumberFormatInfo.CurrentInfo;
151                         else {
152                                 nfi = (NumberFormatInfo) fp.GetFormat (Type);
153                                 
154                                 if (nfi == null)
155                                         nfi = NumberFormatInfo.CurrentInfo;
156                         }
157
158                         return IntegerFormatter.NumberToString (fmt, nfi, value);
159                 }
160
161                 // =========== IConvertible Methods =========== //
162
163                 public TypeCode GetTypeCode ()
164                 {
165                         return TypeCode.UInt64;
166                 }
167         }
168 }