2002-01-05 Nick Drochak <ndrochak@gol.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                 [MonoTODO]
115                 public static uint Parse (string s, NumberStyles style, IFormatProvider fp)
116                 {
117                         // TODO: Implement me
118                         throw new NotImplementedException ();
119                 }
120
121                 public override string ToString ()
122                 {
123                         return ToString ("G", null);
124                 }
125
126                 public string ToString (IFormatProvider fp)
127                 {
128                         return ToString ("G", fp);
129                 }
130
131                 public string ToString (string format)
132                 {
133                         return ToString (format, null);
134                 }
135
136                 public string ToString (string format, IFormatProvider fp)
137                 {
138                         string fmt;
139                         NumberFormatInfo nfi;
140                         
141                         fmt = (format == null) ? "G" : format;
142                         
143                         if (fp == null)
144                                 nfi = NumberFormatInfo.CurrentInfo;
145                         else {
146                                 nfi = (NumberFormatInfo) fp.GetFormat (Type);
147                                 
148                                 if (nfi == null)
149                                         nfi = NumberFormatInfo.CurrentInfo;
150                         }
151
152                         return IntegerFormatter.NumberToString (fmt, nfi, value);
153                 }
154
155                 // =========== IConvertible Methods =========== //
156
157                 public TypeCode GetTypeCode ()
158                 {
159                         return TypeCode.UInt32;
160                 }                               
161         }
162 }