2001-12-21 Miguel de Icaza <miguel@ximian.com>
[mono.git] / mcs / class / corlib / System / UInt32.cs
1 //
2 // System.UInt32.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 UInt32 : IComparable, IFormattable { //, IConvertible {
16                 public static Type Type = typeof (uint);
17
18                 public const uint MaxValue = 0xffffffff;
19                 public const uint MinValue = 0;
20                 
21                 public uint value;
22
23                 public int CompareTo (object v)
24                 {
25                         if (v == null)
26                                 return 1;
27                         
28                         if (!(v is System.UInt32))
29                                 throw new ArgumentException (Locale.GetText ("Value is not a System.UInt32"));
30
31                         if (value == (uint) v)
32                                 return 0;
33
34                         if (value < (uint) v)
35                                 return -1;
36
37                         return 1;
38                 }
39
40                 public override bool Equals (object o)
41                 {
42                         if (!(o is System.UInt32))
43                                 return false;
44
45                         return ((uint) o) == value;
46                 }
47
48                 public override int GetHashCode ()
49                 {
50                         return (int) value;
51                 }
52
53                 public static uint Parse (string s)
54                 {
55                         uint 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 uint Parse (string s, IFormatProvider fp)
105                 {
106                         return Parse (s, NumberStyles.Integer, fp);
107                 }
108
109                 public static uint Parse (string s, NumberStyles style)
110                 {
111                         return Parse (s, style, null);
112                 }
113                 
114                 public static uint Parse (string s, NumberStyles style, IFormatProvider fp)
115                 {
116                         // TODO: Implement me
117                         throw new NotImplementedException ();
118                 }
119
120                 public override string ToString ()
121                 {
122                         return ToString ("G", null);
123                 }
124
125                 public string ToString (IFormatProvider fp)
126                 {
127                         return ToString ("G", fp);
128                 }
129
130                 public string ToString (string format)
131                 {
132                         return ToString (format, null);
133                 }
134
135                 public string ToString (string format, IFormatProvider fp)
136                 {
137                         string fmt;
138                         NumberFormatInfo nfi;
139                         
140                         fmt = (format == null) ? "G" : format;
141                         
142                         if (fp == null)
143                                 nfi = NumberFormatInfo.CurrentInfo;
144                         else {
145                                 nfi = (NumberFormatInfo) fp.GetFormat (Type);
146                                 
147                                 if (nfi == null)
148                                         nfi = NumberFormatInfo.CurrentInfo;
149                         }
150
151                         return IntegerFormatter.NumberToString (fmt, nfi, value);
152                 }
153
154                 // =========== IConvertible Methods =========== //
155
156                 public TypeCode GetTypeCode ()
157                 {
158                         return TypeCode.UInt32;
159                 }                               
160         }
161 }