(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / corlib / System / UInt64.cs
blob70f6bc97f130075c9576c0495d550434fbed4e65
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.
30 using System.Globalization;
31 using System.Threading;
33 namespace System
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
44 public const ulong MaxValue = 0xffffffffffffffff;
45 public const ulong MinValue = 0;
47 internal ulong m_value;
49 public int CompareTo (object value)
51 if (value == null)
52 return 1;
54 if (!(value is System.UInt64))
55 throw new ArgumentException (Locale.GetText ("Value is not a System.UInt64."));
57 if (this.m_value == (ulong) value)
58 return 0;
60 if (this.m_value < (ulong) value)
61 return -1;
63 return 1;
66 public override bool Equals (object obj)
68 if (!(obj is System.UInt64))
69 return false;
71 return ((ulong) obj) == m_value;
74 public override int GetHashCode ()
76 return (int)(m_value & 0xffffffff) ^ (int)(m_value >> 32);
79 #if NET_2_0
80 public int CompareTo (ulong value)
82 if (m_value == value)
83 return 0;
84 if (m_value > value)
85 return 1;
86 else
87 return -1;
90 public bool Equals (ulong value)
92 return value == m_value;
94 #endif
96 [CLSCompliant (false)]
97 public static ulong Parse (string s)
99 return Parse (s, NumberStyles.Integer, null);
102 [CLSCompliant (false)]
103 public static ulong Parse (string s, IFormatProvider provider)
105 return Parse (s, NumberStyles.Integer, provider);
108 [CLSCompliant (false)]
109 public static ulong Parse (string s, NumberStyles style)
111 return Parse (s, style, null);
114 internal static bool Parse (string s, NumberStyles style, IFormatProvider provider, bool tryParse, out ulong result)
116 result = 0;
118 if (s == null)
119 if (tryParse)
120 return false;
121 else
122 throw new ArgumentNullException ("s");
124 if (s.Length == 0)
125 if (tryParse)
126 return false;
127 else
128 throw new FormatException (Locale.GetText ("Input string was not in the correct format."));
130 NumberFormatInfo nfi;
131 if (provider != null) {
132 Type typeNFI = typeof (NumberFormatInfo);
133 nfi = (NumberFormatInfo) provider.GetFormat (typeNFI);
135 else
136 nfi = Thread.CurrentThread.CurrentCulture.NumberFormat;
138 Int32.CheckStyle (style);
140 bool AllowCurrencySymbol = (style & NumberStyles.AllowCurrencySymbol) != 0;
141 bool AllowHexSpecifier = (style & NumberStyles.AllowHexSpecifier) != 0;
142 bool AllowThousands = (style & NumberStyles.AllowThousands) != 0;
143 bool AllowDecimalPoint = (style & NumberStyles.AllowDecimalPoint) != 0;
144 bool AllowParentheses = (style & NumberStyles.AllowParentheses) != 0;
145 bool AllowTrailingSign = (style & NumberStyles.AllowTrailingSign) != 0;
146 bool AllowLeadingSign = (style & NumberStyles.AllowLeadingSign) != 0;
147 bool AllowTrailingWhite = (style & NumberStyles.AllowTrailingWhite) != 0;
148 bool AllowLeadingWhite = (style & NumberStyles.AllowLeadingWhite) != 0;
150 int pos = 0;
152 if (AllowLeadingWhite)
153 pos = Int32.JumpOverWhite (pos, s, true);
155 bool foundOpenParentheses = false;
156 bool negative = false;
157 bool foundSign = false;
158 bool foundCurrency = false;
160 // Pre-number stuff
161 if (AllowParentheses && s [pos] == '(') {
162 foundOpenParentheses = true;
163 foundSign = true;
164 negative = true; // MS always make the number negative when there parentheses
165 // even when NumberFormatInfo.NumberNegativePattern != 0!!!
166 pos++;
167 if (AllowLeadingWhite)
168 pos = Int32.JumpOverWhite (pos, s, true);
170 if (s.Substring (pos, nfi.NegativeSign.Length) == nfi.NegativeSign)
171 if (tryParse)
172 return false;
173 else
174 throw new FormatException (Locale.GetText ("Input string was not in the correct format."));
175 if (s.Substring (pos, nfi.PositiveSign.Length) == nfi.PositiveSign)
176 if (tryParse)
177 return false;
178 else
179 throw new FormatException (Locale.GetText ("Input string was not in the correct format."));
182 if (AllowLeadingSign && !foundSign) {
183 // Sign + Currency
184 Int32.FindSign (ref pos, s, nfi, ref foundSign, ref negative);
185 if (foundSign) {
186 if (AllowLeadingWhite)
187 pos = Int32.JumpOverWhite (pos, s, true);
188 if (AllowCurrencySymbol) {
189 Int32.FindCurrency (ref pos, s, nfi,
190 ref foundCurrency);
191 if (foundCurrency && AllowLeadingWhite)
192 pos = Int32.JumpOverWhite (pos, s, true);
197 if (AllowCurrencySymbol && !foundCurrency) {
198 // Currency + sign
199 Int32.FindCurrency (ref pos, s, nfi, ref foundCurrency);
200 if (foundCurrency) {
201 if (AllowLeadingWhite)
202 pos = Int32.JumpOverWhite (pos, s, true);
203 if (foundCurrency) {
204 if (!foundSign && AllowLeadingSign) {
205 Int32.FindSign (ref pos, s, nfi, ref foundSign,
206 ref negative);
207 if (foundSign && AllowLeadingWhite)
208 pos = Int32.JumpOverWhite (pos, s, true);
214 ulong number = 0;
215 int nDigits = 0;
216 bool decimalPointFound = false;
217 ulong digitValue;
218 char hexDigit;
220 // Number stuff
221 // Just the same as Int32, but this one adds instead of substract
222 do {
224 if (!Int32.ValidDigit (s [pos], AllowHexSpecifier)) {
225 if (AllowThousands && Int32.FindOther (ref pos, s, nfi.NumberGroupSeparator))
226 continue;
227 else
228 if (!decimalPointFound && AllowDecimalPoint &&
229 Int32.FindOther (ref pos, s, nfi.NumberDecimalSeparator)) {
230 decimalPointFound = true;
231 continue;
233 break;
235 else if (AllowHexSpecifier) {
236 nDigits++;
237 hexDigit = s [pos++];
238 if (Char.IsDigit (hexDigit))
239 digitValue = (ulong) (hexDigit - '0');
240 else if (Char.IsLower (hexDigit))
241 digitValue = (ulong) (hexDigit - 'a' + 10);
242 else
243 digitValue = (ulong) (hexDigit - 'A' + 10);
245 number = checked (number * 16 + digitValue);
247 else if (decimalPointFound) {
248 nDigits++;
249 // Allows decimal point as long as it's only
250 // followed by zeroes.
251 if (s [pos++] != '0')
252 if (tryParse)
253 return false;
254 else
255 throw new OverflowException (Locale.GetText ("Value too large or too small."));
257 else {
258 nDigits++;
260 try {
261 number = checked (number * 10 + (ulong) (s [pos++] - '0'));
263 catch (OverflowException) {
264 if (tryParse)
265 return false;
266 else
267 throw new OverflowException (Locale.GetText ("Value too large or too small."));
270 } while (pos < s.Length);
272 // Post number stuff
273 if (nDigits == 0)
274 if (tryParse)
275 return false;
276 else
277 throw new FormatException (Locale.GetText ("Input string was not in the correct format."));
279 if (AllowTrailingSign && !foundSign) {
280 // Sign + Currency
281 Int32.FindSign (ref pos, s, nfi, ref foundSign, ref negative);
282 if (foundSign) {
283 if (AllowTrailingWhite)
284 pos = Int32.JumpOverWhite (pos, s, true);
285 if (AllowCurrencySymbol)
286 Int32.FindCurrency (ref pos, s, nfi, ref foundCurrency);
290 if (AllowCurrencySymbol && !foundCurrency) {
291 // Currency + sign
292 Int32.FindCurrency (ref pos, s, nfi, ref foundCurrency);
293 if (foundCurrency) {
294 if (AllowTrailingWhite)
295 pos = Int32.JumpOverWhite (pos, s, true);
296 if (!foundSign && AllowTrailingSign)
297 Int32.FindSign (ref pos, s, nfi, ref foundSign, ref negative);
301 if (AllowTrailingWhite && pos < s.Length)
302 pos = Int32.JumpOverWhite (pos, s, false);
304 if (foundOpenParentheses) {
305 if (pos >= s.Length || s [pos++] != ')')
306 if (tryParse)
307 return false;
308 else
309 throw new FormatException (Locale.GetText
310 ("Input string was not in the correct format."));
311 if (AllowTrailingWhite && pos < s.Length)
312 pos = Int32.JumpOverWhite (pos, s, false);
315 if (pos < s.Length && s [pos] != '\u0000')
316 if (tryParse)
317 return false;
318 else
319 throw new FormatException (Locale.GetText ("Input string was not in the correct format."));
321 // -0 is legal but other negative values are not
322 if (negative && (number > 0)) {
323 if (tryParse)
324 return false;
325 else
326 throw new OverflowException (
327 Locale.GetText ("Negative number"));
330 result = number;
331 return true;
334 [CLSCompliant (false)]
335 public static ulong Parse (string s, NumberStyles style, IFormatProvider fp) {
336 ulong res;
338 Parse (s, style, fp, false, out res);
340 return res;
344 #if NET_2_0
345 [CLSCompliant (false)]
346 public static bool TryParse (string s, out ulong result) {
347 try {
348 return Parse (s, NumberStyles.Integer, null, true, out result);
350 catch (Exception) {
351 result = 0;
352 return false;
356 [CLSCompliant (false)]
357 public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out ulong result) {
358 try {
359 return Parse (s, style, provider, true, out result);
361 catch (Exception) {
362 result = 0;
363 return false;
366 #endif
368 public override string ToString ()
370 return ToString (null, null);
373 public string ToString (IFormatProvider provider)
375 return ToString (null, provider);
378 public string ToString (string format)
380 return ToString (format, null);
383 public string ToString (string format, IFormatProvider provider)
385 NumberFormatInfo nfi = NumberFormatInfo.GetInstance (provider);
387 // use "G" when format is null or String.Empty
388 if ((format == null) || (format.Length == 0))
389 format = "G";
391 return IntegerFormatter.NumberToString (format, nfi, m_value);
394 // =========== IConvertible Methods =========== //
395 public TypeCode GetTypeCode ()
397 return TypeCode.UInt64;
400 bool IConvertible.ToBoolean (IFormatProvider provider)
402 return System.Convert.ToBoolean (m_value);
405 byte IConvertible.ToByte (IFormatProvider provider)
407 return System.Convert.ToByte (m_value);
410 char IConvertible.ToChar (IFormatProvider provider)
412 return System.Convert.ToChar (m_value);
415 DateTime IConvertible.ToDateTime (IFormatProvider provider)
417 return System.Convert.ToDateTime (m_value);
420 decimal IConvertible.ToDecimal (IFormatProvider provider)
422 return System.Convert.ToDecimal (m_value);
425 double IConvertible.ToDouble (IFormatProvider provider)
427 return System.Convert.ToDouble (m_value);
430 short IConvertible.ToInt16 (IFormatProvider provider)
432 return System.Convert.ToInt16 (m_value);
435 int IConvertible.ToInt32 (IFormatProvider provider)
437 return System.Convert.ToInt32 (m_value);
440 long IConvertible.ToInt64 (IFormatProvider provider)
442 return System.Convert.ToInt64 (m_value);
445 sbyte IConvertible.ToSByte(IFormatProvider provider)
447 return System.Convert.ToSByte (m_value);
450 float IConvertible.ToSingle (IFormatProvider provider)
452 return System.Convert.ToSingle (m_value);
455 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
457 return System.Convert.ToType (m_value, conversionType, provider);
460 ushort IConvertible.ToUInt16 (IFormatProvider provider)
462 return System.Convert.ToUInt16 (m_value);
465 uint IConvertible.ToUInt32 (IFormatProvider provider)
467 return System.Convert.ToUInt32 (m_value);
470 ulong IConvertible.ToUInt64 (IFormatProvider provider)
472 return m_value;