2002-01-14 Miguel de Icaza <miguel@ximian.com>
[mono.git] / mcs / class / corlib / System / Int32.cs
1 //
2 // System.Int32.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 Int32 : IComparable, IFormattable { //, IConvertible {
15                 private static Type Type = typeof (int);
16                 
17                 public const int MaxValue = 0x7fffffff;
18                 public const int MinValue = -2147483648;
19                 
20                 public int value;
21
22                 public int CompareTo (object v)
23                 {
24                         if (v == null)
25                                 return 1;
26                         
27                         if (!(v is System.Int32))
28                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Int32"));
29
30                         int xv = (int) v;
31                         if (value == xv)
32                                 return 0;
33                         if (value > xv)
34                                 return 1;
35                         else
36                                 return -1;
37                 }
38
39                 public override bool Equals (object o)
40                 {
41                         if (!(o is System.Int32))
42                                 return false;
43
44                         return ((int) o) == value;
45                 }
46
47                 public override int GetHashCode ()
48                 {
49                         return value;
50                 }
51
52                 public static int Parse (string s)
53                 {
54                         int val = 0;
55                         int len;
56                         int i;
57                         bool neg = false;
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                         c = s [i];
76                         if (c == '+')
77                                 i++;
78                         else if (c == '-'){
79                                 neg = true;
80                                 i++;
81                         }
82                         
83                         for (; i < len; i++){
84                                 c = s [i];
85
86                                 if (c >= '0' && c <= '9'){
87                                         val = checked (val * 10 + (c - '0'));
88                                         digits_seen = true;
89                                 } else {
90                                         if (Char.IsWhiteSpace (c)){
91                                                 for (i++; i < len; i++){
92                                                         if (!Char.IsWhiteSpace (s [i]))
93                                                                 throw new FormatException ();
94                                                 }
95                                                 break;
96                                         } else
97                                                 throw new FormatException ();
98                                 }
99                         }
100                         if (!digits_seen)
101                                 throw new FormatException ();
102                         
103                         if (neg)
104                                 val = -val;
105
106                         return val;
107                 }
108
109                 public static int Parse (string s, IFormatProvider fp)
110                 {
111                         return Parse (s, NumberStyles.Integer, fp);
112                 }
113
114                 public static int Parse (string s, NumberStyles style)
115                 {
116                         return Parse (s, style, null);
117                 }
118
119                 [MonoTODO]
120                 public static int Parse (string s, NumberStyles style, IFormatProvider fp)
121                 {
122                         // FIXME: Better than nothing ;-)
123                         return Parse (s);
124                         //throw new NotImplementedException ();
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.Int32;
166                 }
167         }
168 }