- CultureInfo implements IFormatProvider
[mono.git] / mcs / class / corlib / System / Int64.cs
1 //
2 // System.Int64.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 Int64 : IComparable, IFormattable { //, IConvertible {
15
16                 public const long MaxValue = 0x7fffffffffffffff;
17                 public const long MinValue = -9223372036854775808;
18                 
19                 public long value;
20
21                 public int CompareTo (object v)
22                 {
23                         if (v == null)
24                                 return 1;
25                         
26                         if (!(v is System.Int64))
27                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Int64"));
28
29                         if (value == (long) v)
30                                 return 0;
31
32                         if (value < (long) v)
33                                 return -1;
34
35                         return 1;
36                 }
37
38                 public override bool Equals (object o)
39                 {
40                         if (!(o is System.Int64))
41                                 return false;
42
43                         return ((long) o) == value;
44                 }
45
46                 public override int GetHashCode ()
47                 {
48                         return (int)(value & 0xffffffff) ^ (int)(value >> 32);
49                 }
50
51                 public static long Parse (string s)
52                 {
53                         long val = 0;
54                         int len;
55                         int i;
56                         bool neg = false;
57                         bool digits_seen = false;
58                         
59                         if (s == null)
60                                 throw new ArgumentNullException (Locale.GetText ("s is null"));
61
62                         len = s.Length;
63
64                         char c;
65                         for (i = 0; i < len; i++){
66                                 c = s [i];
67                                 if (!Char.IsWhiteSpace (c))
68                                         break;
69                         }
70                         
71                         if (i == len)
72                                 throw new FormatException ();
73
74                         c = s [i];
75                         if (c == '+')
76                                 i++;
77                         else if (c == '-'){
78                                 neg = true;
79                                 i++;
80                         }
81                         
82                         for (; i < len; i++){
83                                 c = s [i];
84
85                                 if (c >= '0' && c <= '9'){
86                                         val = checked (val * 10 + (c - '0'));
87                                         digits_seen = true;
88                                 } else {
89                                         if (Char.IsWhiteSpace (c)){
90                                                 for (i++; i < len; i++){
91                                                         if (!Char.IsWhiteSpace (s [i]))
92                                                                 throw new FormatException ();
93                                                 }
94                                                 break;
95                                         } else
96                                                 throw new FormatException ();
97                                 }
98                         }
99                         if (!digits_seen)
100                                 throw new FormatException ();
101                         
102                         if (neg)
103                                 val = -val;
104
105                         return val;
106                 }
107
108                 public static long Parse (string s, IFormatProvider fp)
109                 {
110                         return Parse (s, NumberStyles.Integer, fp);
111                 }
112
113                 public static long Parse (string s, NumberStyles style)
114                 {
115                         return Parse (s, style, null);
116                 }
117
118                 [MonoTODO]
119                 public static long Parse (string s, NumberStyles style, IFormatProvider fp)
120                 {
121                         // TODO: Implement me
122                         // throw new NotImplementedException ();
123                         // good enough for now
124                         return Parse (s);
125                 }
126
127                 public override string ToString ()
128                 {
129                         return ToString (null, null);
130                 }
131
132                 public string ToString (IFormatProvider fp)
133                 {
134                         return ToString (null, 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                         NumberFormatInfo nfi = NumberFormatInfo.GetInstance( fp );
145                         
146                         if ( format == null )
147                                 format = "G";
148                         
149                         return IntegerFormatter.NumberToString (format, nfi, value);
150                 }
151
152                 // =========== IConvertible Methods =========== //
153
154                 public TypeCode GetTypeCode ()
155                 {
156                         return TypeCode.Int64;
157                 }
158         }
159 }