5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) Ximian, Inc. http://www.ximian.com
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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;
36 [CLSCompliant (false)]
37 public struct UInt64 : IComparable, IFormattable, IConvertible
39 public const ulong MaxValue = 0xffffffffffffffff;
40 public const ulong MinValue = 0;
42 internal ulong m_value;
44 public int CompareTo (object value)
49 if (!(value is System.UInt64))
50 throw new ArgumentException (Locale.GetText ("Value is not a System.UInt64."));
52 if (this.m_value == (ulong) value)
55 if (this.m_value < (ulong) value)
61 public override bool Equals (object obj)
63 if (!(obj is System.UInt64))
66 return ((ulong) obj) == m_value;
69 public override int GetHashCode ()
71 return (int)(m_value & 0xffffffff) ^ (int)(m_value >> 32);
74 [CLSCompliant (false)]
75 public static ulong Parse (string s)
77 return Parse (s, NumberStyles.Integer, null);
80 [CLSCompliant (false)]
81 public static ulong Parse (string s, IFormatProvider provider)
83 return Parse (s, NumberStyles.Integer, provider);
86 [CLSCompliant (false)]
87 public static ulong Parse (string s, NumberStyles style)
89 return Parse (s, style, null);
92 [CLSCompliant (false)]
93 public static ulong Parse (string s, NumberStyles style, IFormatProvider provider)
96 throw new ArgumentNullException ("s");
99 throw new FormatException (Locale.GetText ("Input string was not in the correct format."));
101 NumberFormatInfo nfi;
102 if (provider != null) {
103 Type typeNFI = typeof (NumberFormatInfo);
104 nfi = (NumberFormatInfo) provider.GetFormat (typeNFI);
107 nfi = Thread.CurrentThread.CurrentCulture.NumberFormat;
109 Int32.CheckStyle (style);
111 bool AllowCurrencySymbol = (style & NumberStyles.AllowCurrencySymbol) != 0;
112 bool AllowExponent = (style & NumberStyles.AllowExponent) != 0;
113 bool AllowHexSpecifier = (style & NumberStyles.AllowHexSpecifier) != 0;
114 bool AllowThousands = (style & NumberStyles.AllowThousands) != 0;
115 bool AllowDecimalPoint = (style & NumberStyles.AllowDecimalPoint) != 0;
116 bool AllowParentheses = (style & NumberStyles.AllowParentheses) != 0;
117 bool AllowTrailingSign = (style & NumberStyles.AllowTrailingSign) != 0;
118 bool AllowLeadingSign = (style & NumberStyles.AllowLeadingSign) != 0;
119 bool AllowTrailingWhite = (style & NumberStyles.AllowTrailingWhite) != 0;
120 bool AllowLeadingWhite = (style & NumberStyles.AllowLeadingWhite) != 0;
124 if (AllowLeadingWhite)
125 pos = Int32.JumpOverWhite (pos, s, true);
127 bool foundOpenParentheses = false;
128 bool negative = false;
129 bool foundSign = false;
130 bool foundCurrency = false;
133 if (AllowParentheses && s [pos] == '(') {
134 foundOpenParentheses = true;
136 negative = true; // MS always make the number negative when there parentheses
137 // even when NumberFormatInfo.NumberNegativePattern != 0!!!
139 if (AllowLeadingWhite)
140 pos = Int32.JumpOverWhite (pos, s, true);
142 if (s.Substring (pos, nfi.NegativeSign.Length) == nfi.NegativeSign)
143 throw new FormatException (Locale.GetText ("Input string was not in the correct format."));
144 if (s.Substring (pos, nfi.PositiveSign.Length) == nfi.PositiveSign)
145 throw new FormatException (Locale.GetText ("Input string was not in the correct format."));
148 if (AllowLeadingSign && !foundSign) {
150 Int32.FindSign (ref pos, s, nfi, ref foundSign, ref negative);
152 if (AllowLeadingWhite)
153 pos = Int32.JumpOverWhite (pos, s, true);
154 if (AllowCurrencySymbol) {
155 Int32.FindCurrency (ref pos, s, nfi,
157 if (foundCurrency && AllowLeadingWhite)
158 pos = Int32.JumpOverWhite (pos, s, true);
163 if (AllowCurrencySymbol && !foundCurrency) {
165 Int32.FindCurrency (ref pos, s, nfi, ref foundCurrency);
167 if (AllowLeadingWhite)
168 pos = Int32.JumpOverWhite (pos, s, true);
170 if (!foundSign && AllowLeadingSign) {
171 Int32.FindSign (ref pos, s, nfi, ref foundSign,
173 if (foundSign && AllowLeadingWhite)
174 pos = Int32.JumpOverWhite (pos, s, true);
182 bool decimalPointFound = false;
187 // Just the same as Int32, but this one adds instead of substract
190 if (!Int32.ValidDigit (s [pos], AllowHexSpecifier)) {
191 if (AllowThousands && Int32.FindOther (ref pos, s, nfi.NumberGroupSeparator))
194 if (!decimalPointFound && AllowDecimalPoint &&
195 Int32.FindOther (ref pos, s, nfi.NumberDecimalSeparator)) {
196 decimalPointFound = true;
201 else if (AllowHexSpecifier) {
203 hexDigit = s [pos++];
204 if (Char.IsDigit (hexDigit))
205 digitValue = (ulong) (hexDigit - '0');
206 else if (Char.IsLower (hexDigit))
207 digitValue = (ulong) (hexDigit - 'a' + 10);
209 digitValue = (ulong) (hexDigit - 'A' + 10);
211 number = checked (number * 16 + digitValue);
213 else if (decimalPointFound) {
215 // Allows decimal point as long as it's only
216 // followed by zeroes.
217 if (s [pos++] != '0')
218 throw new OverflowException (Locale.GetText ("Value too large or too small."));
224 number = checked (number * 10 + (ulong) (s [pos++] - '0'));
226 catch (OverflowException) {
227 throw new OverflowException (Locale.GetText ("Value too large or too small."));
230 } while (pos < s.Length);
234 throw new FormatException (Locale.GetText ("Input string was not in the correct format."));
236 if (AllowTrailingSign && !foundSign) {
238 Int32.FindSign (ref pos, s, nfi, ref foundSign, ref negative);
240 if (AllowTrailingWhite)
241 pos = Int32.JumpOverWhite (pos, s, true);
242 if (AllowCurrencySymbol)
243 Int32.FindCurrency (ref pos, s, nfi, ref foundCurrency);
247 if (AllowCurrencySymbol && !foundCurrency) {
249 Int32.FindCurrency (ref pos, s, nfi, ref foundCurrency);
251 if (AllowTrailingWhite)
252 pos = Int32.JumpOverWhite (pos, s, true);
253 if (!foundSign && AllowTrailingSign)
254 Int32.FindSign (ref pos, s, nfi, ref foundSign, ref negative);
258 if (AllowTrailingWhite && pos < s.Length)
259 pos = Int32.JumpOverWhite (pos, s, false);
261 if (foundOpenParentheses) {
262 if (pos >= s.Length || s [pos++] != ')')
263 throw new FormatException (Locale.GetText
264 ("Input string was not in the correct format."));
265 if (AllowTrailingWhite && pos < s.Length)
266 pos = Int32.JumpOverWhite (pos, s, false);
269 if (pos < s.Length && s [pos] != '\u0000')
270 throw new FormatException (Locale.GetText ("Input string was not in the correct format."));
272 // -0 is legal but other negative values are not
273 if (negative && (number > 0)) {
274 throw new OverflowException (
275 Locale.GetText ("Negative number"));
281 public override string ToString ()
283 return ToString (null, null);
286 public string ToString (IFormatProvider provider)
288 return ToString (null, provider);
291 public string ToString (string format)
293 return ToString (format, null);
296 public string ToString (string format, IFormatProvider provider)
298 NumberFormatInfo nfi = NumberFormatInfo.GetInstance (provider);
300 // use "G" when format is null or String.Empty
301 if ((format == null) || (format.Length == 0))
304 return IntegerFormatter.NumberToString (format, nfi, m_value);
307 // =========== IConvertible Methods =========== //
308 public TypeCode GetTypeCode ()
310 return TypeCode.UInt64;
313 bool IConvertible.ToBoolean (IFormatProvider provider)
315 return System.Convert.ToBoolean (m_value);
318 byte IConvertible.ToByte (IFormatProvider provider)
320 return System.Convert.ToByte (m_value);
323 char IConvertible.ToChar (IFormatProvider provider)
325 return System.Convert.ToChar (m_value);
328 DateTime IConvertible.ToDateTime (IFormatProvider provider)
330 return System.Convert.ToDateTime (m_value);
333 decimal IConvertible.ToDecimal (IFormatProvider provider)
335 return System.Convert.ToDecimal (m_value);
338 double IConvertible.ToDouble (IFormatProvider provider)
340 return System.Convert.ToDouble (m_value);
343 short IConvertible.ToInt16 (IFormatProvider provider)
345 return System.Convert.ToInt16 (m_value);
348 int IConvertible.ToInt32 (IFormatProvider provider)
350 return System.Convert.ToInt32 (m_value);
353 long IConvertible.ToInt64 (IFormatProvider provider)
355 return System.Convert.ToInt64 (m_value);
358 sbyte IConvertible.ToSByte(IFormatProvider provider)
360 return System.Convert.ToSByte (m_value);
363 float IConvertible.ToSingle (IFormatProvider provider)
365 return System.Convert.ToSingle (m_value);
368 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
370 return System.Convert.ToType (m_value, conversionType, provider);
373 ushort IConvertible.ToUInt16 (IFormatProvider provider)
375 return System.Convert.ToUInt16 (m_value);
378 uint IConvertible.ToUInt32 (IFormatProvider provider)
380 return System.Convert.ToUInt32 (m_value);
383 ulong IConvertible.ToUInt64 (IFormatProvider provider)