warning cleanup
[mono.git] / mcs / class / corlib / System / UInt64.cs
1 //
2 // System.UInt64.cs
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) Ximian, Inc.  http://www.ximian.com
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Globalization;
31 using System.Threading;
32
33 namespace System
34 {
35         [Serializable]
36         [CLSCompliant (false)]
37         public struct UInt64 : IFormattable, IConvertible,
38 #if NET_2_0
39                 IComparable, IComparable<UInt64>
40 #else
41                 IComparable
42 #endif
43         {
44                 public const ulong MaxValue = 0xffffffffffffffff;
45                 public const ulong MinValue = 0;
46
47                 internal ulong m_value;
48
49                 public int CompareTo (object value)
50                 {
51                         if (value == null)
52                                 return 1;
53
54                         if (!(value is System.UInt64))
55                                 throw new ArgumentException (Locale.GetText ("Value is not a System.UInt64."));
56
57                         if (this.m_value == (ulong) value)
58                                 return 0;
59
60                         if (this.m_value < (ulong) value)
61                                 return -1;
62
63                         return 1;
64                 }
65
66                 public override bool Equals (object obj)
67                 {
68                         if (!(obj is System.UInt64))
69                                 return false;
70
71                         return ((ulong) obj) == m_value;
72                 }
73
74                 public override int GetHashCode ()
75                 {
76                         return (int)(m_value & 0xffffffff) ^ (int)(m_value >> 32);
77                 }
78
79 #if NET_2_0
80                 public int CompareTo (ulong value)
81                 {
82                         if (m_value == value)
83                                 return 0;
84                         if (m_value > value)
85                                 return 1;
86                         else
87                                 return -1;
88                 }
89
90                 public bool Equals (ulong value)
91                 {
92                         return value == m_value;
93                 }
94 #endif
95
96                 [CLSCompliant (false)]
97                 public static ulong Parse (string s)
98                 {
99                         return Parse (s, NumberStyles.Integer, null);
100                 }
101
102                 [CLSCompliant (false)]
103                 public static ulong Parse (string s, IFormatProvider provider)
104                 {
105                         return Parse (s, NumberStyles.Integer, provider);
106                 }
107
108                 [CLSCompliant (false)]
109                 public static ulong Parse (string s, NumberStyles style)
110                 {
111                         return Parse (s, style, null);
112                 }
113
114                 [CLSCompliant (false)]
115                 public static ulong Parse (string s, NumberStyles style, IFormatProvider provider)
116                 {
117                         if (s == null)
118                                 throw new ArgumentNullException ("s");
119
120                         if (s.Length == 0)
121                                 throw new FormatException (Locale.GetText ("Input string was not in the correct format."));
122
123                         NumberFormatInfo nfi;
124                         if (provider != null) {
125                                 Type typeNFI = typeof (NumberFormatInfo);
126                                 nfi = (NumberFormatInfo) provider.GetFormat (typeNFI);
127                         }
128                         else
129                                 nfi = Thread.CurrentThread.CurrentCulture.NumberFormat;
130
131                         Int32.CheckStyle (style);
132
133                         bool AllowCurrencySymbol = (style & NumberStyles.AllowCurrencySymbol) != 0;
134                         bool AllowHexSpecifier = (style & NumberStyles.AllowHexSpecifier) != 0;
135                         bool AllowThousands = (style & NumberStyles.AllowThousands) != 0;
136                         bool AllowDecimalPoint = (style & NumberStyles.AllowDecimalPoint) != 0;
137                         bool AllowParentheses = (style & NumberStyles.AllowParentheses) != 0;
138                         bool AllowTrailingSign = (style & NumberStyles.AllowTrailingSign) != 0;
139                         bool AllowLeadingSign = (style & NumberStyles.AllowLeadingSign) != 0;
140                         bool AllowTrailingWhite = (style & NumberStyles.AllowTrailingWhite) != 0;
141                         bool AllowLeadingWhite = (style & NumberStyles.AllowLeadingWhite) != 0;
142
143                         int pos = 0;
144
145                         if (AllowLeadingWhite)
146                                 pos = Int32.JumpOverWhite (pos, s, true);
147
148                         bool foundOpenParentheses = false;
149                         bool negative = false;
150                         bool foundSign = false;
151                         bool foundCurrency = false;
152
153                         // Pre-number stuff
154                         if (AllowParentheses && s [pos] == '(') {
155                                 foundOpenParentheses = true;
156                                 foundSign = true;
157                                 negative = true; // MS always make the number negative when there parentheses
158                                                  // even when NumberFormatInfo.NumberNegativePattern != 0!!!
159                                 pos++;
160                                 if (AllowLeadingWhite)
161                                         pos = Int32.JumpOverWhite (pos, s, true);
162
163                                 if (s.Substring (pos, nfi.NegativeSign.Length) == nfi.NegativeSign)
164                                         throw new FormatException (Locale.GetText ("Input string was not in the correct format."));
165                                 if (s.Substring (pos, nfi.PositiveSign.Length) == nfi.PositiveSign)
166                                         throw new FormatException (Locale.GetText ("Input string was not in the correct format."));
167                         }
168
169                         if (AllowLeadingSign && !foundSign) {
170                                 // Sign + Currency
171                                 Int32.FindSign (ref pos, s, nfi, ref foundSign, ref negative);
172                                 if (foundSign) {
173                                         if (AllowLeadingWhite)
174                                                 pos = Int32.JumpOverWhite (pos, s, true);
175                                         if (AllowCurrencySymbol) {
176                                                 Int32.FindCurrency (ref pos, s, nfi,
177                                                                     ref foundCurrency);
178                                                 if (foundCurrency && AllowLeadingWhite)
179                                                         pos = Int32.JumpOverWhite (pos, s, true);
180                                         }
181                                 }
182                         }
183
184                         if (AllowCurrencySymbol && !foundCurrency) {
185                                 // Currency + sign
186                                 Int32.FindCurrency (ref pos, s, nfi, ref foundCurrency);
187                                 if (foundCurrency) {
188                                         if (AllowLeadingWhite)
189                                                 pos = Int32.JumpOverWhite (pos, s, true);
190                                         if (foundCurrency) {
191                                                 if (!foundSign && AllowLeadingSign) {
192                                                         Int32.FindSign (ref pos, s, nfi, ref foundSign,
193                                                                         ref negative);
194                                                         if (foundSign && AllowLeadingWhite)
195                                                                 pos = Int32.JumpOverWhite (pos, s, true);
196                                                 }
197                                         }
198                                 }
199                         }
200
201                         ulong number = 0;
202                         int nDigits = 0;
203                         bool decimalPointFound = false;
204                         ulong digitValue;
205                         char hexDigit;
206
207                         // Number stuff
208                         // Just the same as Int32, but this one adds instead of substract
209                         do {
210
211                                 if (!Int32.ValidDigit (s [pos], AllowHexSpecifier)) {
212                                         if (AllowThousands && Int32.FindOther (ref pos, s, nfi.NumberGroupSeparator))
213                                                 continue;
214                                         else
215                                                 if (!decimalPointFound && AllowDecimalPoint && 
216                                                     Int32.FindOther (ref pos, s, nfi.NumberDecimalSeparator)) {
217                                                         decimalPointFound = true;
218                                                         continue;
219                                                 }
220                                         break;
221                                 }
222                                 else if (AllowHexSpecifier) {
223                                         nDigits++;
224                                         hexDigit = s [pos++];
225                                         if (Char.IsDigit (hexDigit))
226                                                 digitValue = (ulong) (hexDigit - '0');
227                                         else if (Char.IsLower (hexDigit))
228                                                 digitValue = (ulong) (hexDigit - 'a' + 10);
229                                         else
230                                                 digitValue = (ulong) (hexDigit - 'A' + 10);
231
232                                         number = checked (number * 16 + digitValue);
233                                 }
234                                 else if (decimalPointFound) {
235                                         nDigits++;
236                                         // Allows decimal point as long as it's only 
237                                         // followed by zeroes.
238                                         if (s [pos++] != '0')
239                                                 throw new OverflowException (Locale.GetText ("Value too large or too small."));
240                                 }
241                                 else {
242                                         nDigits++;
243
244                                         try {
245                                                 number = checked (number * 10 + (ulong) (s [pos++] - '0'));
246                                         }
247                                         catch (OverflowException) {
248                                                 throw new OverflowException (Locale.GetText ("Value too large or too small."));
249                                         }
250                                 }
251                         } while (pos < s.Length);
252
253                         // Post number stuff
254                         if (nDigits == 0)
255                                 throw new FormatException (Locale.GetText ("Input string was not in the correct format."));
256
257                         if (AllowTrailingSign && !foundSign) {
258                                 // Sign + Currency
259                                 Int32.FindSign (ref pos, s, nfi, ref foundSign, ref negative);
260                                 if (foundSign) {
261                                         if (AllowTrailingWhite)
262                                                 pos = Int32.JumpOverWhite (pos, s, true);
263                                         if (AllowCurrencySymbol)
264                                                 Int32.FindCurrency (ref pos, s, nfi, ref foundCurrency);
265                                 }
266                         }
267
268                         if (AllowCurrencySymbol && !foundCurrency) {
269                                 // Currency + sign
270                                 Int32.FindCurrency (ref pos, s, nfi, ref foundCurrency);
271                                 if (foundCurrency) {
272                                         if (AllowTrailingWhite)
273                                                 pos = Int32.JumpOverWhite (pos, s, true);
274                                         if (!foundSign && AllowTrailingSign)
275                                                 Int32.FindSign (ref pos, s, nfi, ref foundSign, ref negative);
276                                 }
277                         }
278
279                         if (AllowTrailingWhite && pos < s.Length)
280                                 pos = Int32.JumpOverWhite (pos, s, false);
281
282                         if (foundOpenParentheses) {
283                                 if (pos >= s.Length || s [pos++] != ')')
284                                         throw new FormatException (Locale.GetText
285                                                 ("Input string was not in the correct format."));
286                                 if (AllowTrailingWhite && pos < s.Length)
287                                         pos = Int32.JumpOverWhite (pos, s, false);
288                         }
289
290                         if (pos < s.Length && s [pos] != '\u0000')
291                                 throw new FormatException (Locale.GetText ("Input string was not in the correct format."));
292
293                         // -0 is legal but other negative values are not
294                         if (negative && (number > 0)) {
295                                 throw new OverflowException (
296                                         Locale.GetText ("Negative number"));
297                         }
298
299                         return number;
300                 }
301
302                 public override string ToString ()
303                 {
304                         return ToString (null, null);
305                 }
306
307                 public string ToString (IFormatProvider provider)
308                 {
309                         return ToString (null, provider);
310                 }
311
312                 public string ToString (string format)
313                 {
314                         return ToString (format, null);
315                 }
316
317                 public string ToString (string format, IFormatProvider provider)
318                 {
319                         NumberFormatInfo nfi = NumberFormatInfo.GetInstance (provider);
320
321                         // use "G" when format is null or String.Empty
322                         if ((format == null) || (format.Length == 0))
323                                 format = "G";
324
325                         return IntegerFormatter.NumberToString (format, nfi, m_value);
326                 }
327
328                 // =========== IConvertible Methods =========== //
329                 public TypeCode GetTypeCode ()
330                 {
331                         return TypeCode.UInt64;
332                 }
333
334                 bool IConvertible.ToBoolean (IFormatProvider provider)
335                 {
336                         return System.Convert.ToBoolean (m_value);
337                 }
338
339                 byte IConvertible.ToByte (IFormatProvider provider)
340                 {
341                         return System.Convert.ToByte (m_value);
342                 }
343
344                 char IConvertible.ToChar (IFormatProvider provider)
345                 {
346                         return System.Convert.ToChar (m_value);
347                 }
348
349                 DateTime IConvertible.ToDateTime (IFormatProvider provider)
350                 {
351                         return System.Convert.ToDateTime (m_value);
352                 }
353
354                 decimal IConvertible.ToDecimal (IFormatProvider provider)
355                 {
356                         return System.Convert.ToDecimal (m_value);
357                 }
358
359                 double IConvertible.ToDouble (IFormatProvider provider)
360                 {
361                         return System.Convert.ToDouble (m_value);
362                 }
363
364                 short IConvertible.ToInt16 (IFormatProvider provider)
365                 {
366                         return System.Convert.ToInt16 (m_value);
367                 }
368
369                 int IConvertible.ToInt32 (IFormatProvider provider)
370                 {
371                         return System.Convert.ToInt32 (m_value);
372                 }
373
374                 long IConvertible.ToInt64 (IFormatProvider provider)
375                 {
376                         return System.Convert.ToInt64 (m_value);
377                 }
378
379                 sbyte IConvertible.ToSByte(IFormatProvider provider)
380                 {
381                         return System.Convert.ToSByte (m_value);
382                 }
383
384                 float IConvertible.ToSingle (IFormatProvider provider)
385                 {
386                         return System.Convert.ToSingle (m_value);
387                 }
388
389                 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
390                 {
391                         return System.Convert.ToType (m_value, conversionType, provider);
392                 }
393
394                 ushort IConvertible.ToUInt16 (IFormatProvider provider)
395                 {
396                         return System.Convert.ToUInt16 (m_value);
397                 }
398
399                 uint IConvertible.ToUInt32 (IFormatProvider provider)
400                 {
401                         return System.Convert.ToUInt32 (m_value);
402                 }
403
404                 ulong IConvertible.ToUInt64 (IFormatProvider provider)
405                 {
406                         return m_value;
407                 }
408         }
409 }