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