2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / System / UInt64.cs
index 407c4a5fe1d6e766e29fb8be26fca728a9ad27e1..70f6bc97f130075c9576c0495d550434fbed4e65 100644 (file)
@@ -34,7 +34,12 @@ namespace System
 {
        [Serializable]
        [CLSCompliant (false)]
-       public struct UInt64 : IComparable, IFormattable, IConvertible
+       public struct UInt64 : IFormattable, IConvertible,
+#if NET_2_0
+               IComparable, IComparable<UInt64>
+#else
+               IComparable
+#endif
        {
                public const ulong MaxValue = 0xffffffffffffffff;
                public const ulong MinValue = 0;
@@ -71,6 +76,23 @@ namespace System
                        return (int)(m_value & 0xffffffff) ^ (int)(m_value >> 32);
                }
 
+#if NET_2_0
+               public int CompareTo (ulong value)
+               {
+                       if (m_value == value)
+                               return 0;
+                       if (m_value > value)
+                               return 1;
+                       else
+                               return -1;
+               }
+
+               public bool Equals (ulong value)
+               {
+                       return value == m_value;
+               }
+#endif
+
                [CLSCompliant (false)]
                public static ulong Parse (string s)
                {
@@ -89,14 +111,21 @@ namespace System
                        return Parse (s, style, null);
                }
 
-               [CLSCompliant (false)]
-               public static ulong Parse (string s, NumberStyles style, IFormatProvider provider)
+               internal static bool Parse (string s, NumberStyles style, IFormatProvider provider, bool tryParse, out ulong result)
                {
+                       result = 0;
+
                        if (s == null)
-                               throw new ArgumentNullException ("s");
+                               if (tryParse)
+                                       return false;
+                               else
+                                       throw new ArgumentNullException ("s");
 
                        if (s.Length == 0)
-                               throw new FormatException (Locale.GetText ("Input string was not in the correct format."));
+                               if (tryParse)
+                                       return false;
+                               else
+                                       throw new FormatException (Locale.GetText ("Input string was not in the correct format."));
 
                        NumberFormatInfo nfi;
                        if (provider != null) {
@@ -109,7 +138,6 @@ namespace System
                        Int32.CheckStyle (style);
 
                        bool AllowCurrencySymbol = (style & NumberStyles.AllowCurrencySymbol) != 0;
-                       bool AllowExponent = (style & NumberStyles.AllowExponent) != 0;
                        bool AllowHexSpecifier = (style & NumberStyles.AllowHexSpecifier) != 0;
                        bool AllowThousands = (style & NumberStyles.AllowThousands) != 0;
                        bool AllowDecimalPoint = (style & NumberStyles.AllowDecimalPoint) != 0;
@@ -140,9 +168,15 @@ namespace System
                                        pos = Int32.JumpOverWhite (pos, s, true);
 
                                if (s.Substring (pos, nfi.NegativeSign.Length) == nfi.NegativeSign)
-                                       throw new FormatException (Locale.GetText ("Input string was not in the correct format."));
+                                       if (tryParse)
+                                               return false;
+                                       else
+                                               throw new FormatException (Locale.GetText ("Input string was not in the correct format."));
                                if (s.Substring (pos, nfi.PositiveSign.Length) == nfi.PositiveSign)
-                                       throw new FormatException (Locale.GetText ("Input string was not in the correct format."));
+                                       if (tryParse)
+                                               return false;
+                                       else
+                                               throw new FormatException (Locale.GetText ("Input string was not in the correct format."));
                        }
 
                        if (AllowLeadingSign && !foundSign) {
@@ -215,7 +249,10 @@ namespace System
                                        // Allows decimal point as long as it's only 
                                        // followed by zeroes.
                                        if (s [pos++] != '0')
-                                               throw new OverflowException (Locale.GetText ("Value too large or too small."));
+                                               if (tryParse)
+                                                       return false;
+                                               else
+                                                       throw new OverflowException (Locale.GetText ("Value too large or too small."));
                                }
                                else {
                                        nDigits++;
@@ -224,14 +261,20 @@ namespace System
                                                number = checked (number * 10 + (ulong) (s [pos++] - '0'));
                                        }
                                        catch (OverflowException) {
-                                               throw new OverflowException (Locale.GetText ("Value too large or too small."));
+                                               if (tryParse)
+                                                       return false;
+                                               else
+                                                       throw new OverflowException (Locale.GetText ("Value too large or too small."));
                                        }
                                }
                        } while (pos < s.Length);
 
                        // Post number stuff
                        if (nDigits == 0)
-                               throw new FormatException (Locale.GetText ("Input string was not in the correct format."));
+                               if (tryParse)
+                                       return false;
+                               else
+                                       throw new FormatException (Locale.GetText ("Input string was not in the correct format."));
 
                        if (AllowTrailingSign && !foundSign) {
                                // Sign + Currency
@@ -260,23 +303,67 @@ namespace System
 
                        if (foundOpenParentheses) {
                                if (pos >= s.Length || s [pos++] != ')')
-                                       throw new FormatException (Locale.GetText
-                                               ("Input string was not in the correct format."));
+                                       if (tryParse)
+                                               return false;
+                                       else
+                                               throw new FormatException (Locale.GetText
+                                                   ("Input string was not in the correct format."));
                                if (AllowTrailingWhite && pos < s.Length)
                                        pos = Int32.JumpOverWhite (pos, s, false);
                        }
 
                        if (pos < s.Length && s [pos] != '\u0000')
-                               throw new FormatException (Locale.GetText ("Input string was not in the correct format."));
+                               if (tryParse)
+                                       return false;
+                               else
+                                       throw new FormatException (Locale.GetText ("Input string was not in the correct format."));
 
                        // -0 is legal but other negative values are not
                        if (negative && (number > 0)) {
-                               throw new OverflowException (
-                                       Locale.GetText ("Negative number"));
+                               if (tryParse)
+                                       return false;
+                               else
+                                       throw new OverflowException (
+                                           Locale.GetText ("Negative number"));
                        }
 
-                       return number;
+                       result = number;
+                       return true;
+               }
+
+               [CLSCompliant (false)]
+               public static ulong Parse (string s, NumberStyles style, IFormatProvider fp) {
+                       ulong res;
+
+                       Parse (s, style, fp, false, out res);
+
+                       return res;
+               }
+
+
+#if NET_2_0
+               [CLSCompliant (false)]
+               public static bool TryParse (string s, out ulong result) {
+                       try {
+                               return Parse (s, NumberStyles.Integer, null, true, out result);
+                       }
+                       catch (Exception) {
+                               result = 0;
+                               return false;
+                       }
+               }
+
+               [CLSCompliant (false)]
+               public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out ulong result) {
+                       try {
+                               return Parse (s, style, provider, true, out result);
+                       }
+                       catch (Exception) {
+                               result = 0;
+                               return false;
+                       }
                }
+#endif
 
                public override string ToString ()
                {