New tests.
[mono.git] / mcs / class / corlib / System / Int64.cs
index e2dd9be2af0b000c39845a9980ad02b9a57afd69..bb06bae7a6431508ec23fbb88cc3afe517a6f3fb 100644 (file)
@@ -34,12 +34,8 @@ using System.Threading;
 namespace System {
        
        [Serializable]
-       public struct Int64 : IFormattable, IConvertible,
-#if NET_2_0
-               IComparable, IComparable<Int64>
-#else
-               IComparable
-#endif
+       [System.Runtime.InteropServices.ComVisible (true)]
+       public struct Int64 : IFormattable, IConvertible, IComparable, IComparable<Int64>, IEquatable <Int64>
        {
 
                public const long MaxValue = 0x7fffffffffffffff;
@@ -47,29 +43,28 @@ namespace System {
                
                internal long m_value;
 
-               public int CompareTo (object v)
+               public int CompareTo (object value)
                {
-                       if (v == null)
+                       if (value == null)
                                return 1;
                        
-                       if (!(v is System.Int64))
+                       if (!(value is System.Int64))
                                throw new ArgumentException (Locale.GetText ("Value is not a System.Int64"));
 
-                       if (m_value == (long) v)
-                               return 0;
+                       long lValue = (long) value;
 
-                       if (m_value < (long) v)
-                               return -1;
+                       if (m_value == lValue)
+                               return 0;
 
-                       return 1;
+                       return (m_value < lValue) ? -1 : 1;
                }
 
-               public override bool Equals (object o)
+               public override bool Equals (object obj)
                {
-                       if (!(o is System.Int64))
+                       if (!(obj is System.Int64))
                                return false;
 
-                       return ((long) o) == m_value;
+                       return ((long) obj) == m_value;
                }
 
                public override int GetHashCode ()
@@ -77,7 +72,6 @@ namespace System {
                        return (int)(m_value & 0xffffffff) ^ (int)(m_value >> 32);
                }
 
-#if NET_2_0
                public int CompareTo (long value)
                {
                        if (m_value == value)
@@ -88,13 +82,12 @@ namespace System {
                                return -1;
                }
 
-               public bool Equals (long value)
+               public bool Equals (long obj)
                {
-                       return value == m_value;
+                       return obj == m_value;
                }
-#endif
 
-               internal static bool Parse (string s, bool tryParse, out long result)
+               internal static bool Parse (string s, bool tryParse, out long result, out Exception exc)
                {
                        long val = 0;
                        int len;
@@ -103,12 +96,13 @@ namespace System {
                        bool digits_seen = false;
 
                        result = 0;
+                       exc = null;
 
-                       if (s == null)
-                               if (tryParse)
-                                       return false;
-                               else
-                                       throw new ArgumentNullException ("s");
+                       if (s == null) {
+                               if (!tryParse) 
+                                       exc = new ArgumentNullException ("s");
+                               return false;
+                       }
 
                        len = s.Length;
 
@@ -119,11 +113,11 @@ namespace System {
                                        break;
                        }
                        
-                       if (i == len)
-                               if (tryParse)
-                                       return false;
-                               else
-                                       throw new FormatException ();
+                       if (i == len) {
+                               if (!tryParse)
+                                       exc = Int32.GetFormatException ();
+                               return false;
+                       }
 
                        c = s [i];
                        if (c == '+')
@@ -137,38 +131,55 @@ namespace System {
                                c = s [i];
 
                                if (c >= '0' && c <= '9'){
-                                       val = checked (val * 10 + (c - '0') * sign);
-                                       digits_seen = true;
-                               } else {
-                                       if (Char.IsWhiteSpace (c)){
-                                               for (i++; i < len; i++){
-                                                       if (!Char.IsWhiteSpace (s [i]))
-                                                               if (tryParse)
-                                                                       return false;
-                                                               else
-                                                                       throw new FormatException ();
-                                               }
-                                               break;
-                                       } else
-                                               if (tryParse)
-                                                       return false;
+                                       byte d = (byte) (c - '0');
+                                               
+                                       if (val > (MaxValue/10))
+                                               goto overflow;
+                                       
+                                       if (val == (MaxValue/10)){
+                                               if ((d > (MaxValue % 10)) && (sign == 1 || (d > ((MaxValue % 10) + 1))))
+                                                       goto overflow;
+                                               if (sign == -1)
+                                                       val = (val * sign * 10) - d;
                                                else
-                                                       throw new FormatException ();
-                               }
-                       }
-                       if (!digits_seen)
-                               if (tryParse)
+                                                       val = (val * 10) + d;
+
+                                               if (Int32.ProcessTrailingWhitespace (tryParse, s, i + 1, ref exc)){
+                                                       result = val;
+                                                       return true;
+                                               }
+                                               goto overflow;
+                                       } else 
+                                               val = val * 10 + d;
+                                       
+                                       
+                                       digits_seen = true;
+                               } else if (!Int32.ProcessTrailingWhitespace (tryParse, s, i, ref exc))
                                        return false;
-                               else
-                                       throw new FormatException ();
+                                       
+                       }
+                       if (!digits_seen) {
+                               if (!tryParse)
+                                       exc = Int32.GetFormatException ();
+                               return false;
+                       }
                        
-                       result = val;
+                       if (sign == -1)
+                               result = val * sign;
+                       else
+                               result = val;
+
                        return true;
+
+               overflow:
+                       if (!tryParse)
+                               exc = new OverflowException ("Value is too large");
+                       return false;
                }
 
-               public static long Parse (string s, IFormatProvider fp)
+               public static long Parse (string s, IFormatProvider provider)
                {
-                       return Parse (s, NumberStyles.Integer, fp);
+                       return Parse (s, NumberStyles.Integer, provider);
                }
 
                public static long Parse (string s, NumberStyles style)
@@ -176,32 +187,34 @@ namespace System {
                        return Parse (s, style, null);
                }
 
-               internal static bool Parse (string s, NumberStyles style, IFormatProvider fp, bool tryParse, out long result)
+               internal static bool Parse (string s, NumberStyles style, IFormatProvider fp, bool tryParse, out long result, out Exception exc)
                {
                        result = 0;
+                       exc = null;
 
-                       if (s == null)
-                               if (tryParse)
-                                       return false;
-                               else
-                                       throw new ArgumentNullException ();
+                       if (s == null) {
+                               if (!tryParse)
+                                       exc = new ArgumentNullException ("s");
+                               return false;
+                       }
 
-                       if (s.Length == 0)
-                               if (tryParse)
-                                       return false;
-                               else
-                                       throw new FormatException ("Input string was not " + 
-                                                                                          "in the correct format: s.Length==0.");
+                       if (s.Length == 0) {
+                               if (!tryParse)
+                                       exc = new FormatException ("Input string was not " + 
+                                                       "in the correct format: s.Length==0.");
+                               return false;
+                       }
 
-                       NumberFormatInfo nfi;
+                       NumberFormatInfo nfi = null;
                        if (fp != null) {
                                Type typeNFI = typeof (System.Globalization.NumberFormatInfo);
                                nfi = (NumberFormatInfo) fp.GetFormat (typeNFI);
-                       }
-                       else
+                       } 
+                       if (nfi == null)
                                nfi = Thread.CurrentThread.CurrentCulture.NumberFormat;
 
-                       Int32.CheckStyle (style);
+                       if (!Int32.CheckStyle (style, tryParse, ref exc))
+                               return false;
 
                        bool AllowCurrencySymbol = (style & NumberStyles.AllowCurrencySymbol) != 0;
                        bool AllowHexSpecifier = (style & NumberStyles.AllowHexSpecifier) != 0;
@@ -215,8 +228,8 @@ namespace System {
 
                        int pos = 0;
 
-                       if (AllowLeadingWhite)
-                               pos = Int32.JumpOverWhite (pos, s, true);
+                       if (AllowLeadingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc))
+                               return false;
 
                        bool foundOpenParentheses = false;
                        bool negative = false;
@@ -230,34 +243,35 @@ namespace System {
                                negative = true; // MS always make the number negative when there parentheses
                                                 // even when NumberFormatInfo.NumberNegativePattern != 0!!!
                                pos++;
-                               if (AllowLeadingWhite)
-                                       pos = Int32.JumpOverWhite (pos, s, true);
+                               if (AllowLeadingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc))
+                                       return false;
 
-                               if (s.Substring (pos, nfi.NegativeSign.Length) == nfi.NegativeSign)
-                                       if (tryParse)
-                                               return false;
-                                       else
-                                               throw new FormatException ("Input string was not in the correct " +
-                                                                                                  "format: Has Negative Sign.");
-                               if (s.Substring (pos, nfi.PositiveSign.Length) == nfi.PositiveSign)
-                                       if (tryParse)
-                                               return false;
-                                       else
-                                               throw new FormatException ("Input string was not in the correct " +
-                                                                                                  "format: Has Positive Sign.");
+                               if (s.Substring (pos, nfi.NegativeSign.Length) == nfi.NegativeSign) {
+                                       if (!tryParse)
+                                               exc = new FormatException ("Input string was not in the correct " +
+                                                               "format: Has Negative Sign.");
+                                       return false;
+                               }
+                               if (s.Substring (pos, nfi.PositiveSign.Length) == nfi.PositiveSign) {
+                                       if (!tryParse)
+                                               exc = new FormatException ("Input string was not in the correct " +
+                                                               "format: Has Positive Sign.");
+                                       return false;
+                               }
                        }
 
                        if (AllowLeadingSign && !foundSign) {
                                // Sign + Currency
                                Int32.FindSign (ref pos, s, nfi, ref foundSign, ref negative);
                                if (foundSign) {
-                                       if (AllowLeadingWhite)
-                                               pos = Int32.JumpOverWhite (pos, s, true);
+                                       if (AllowLeadingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc))
+                                               return false;
                                        if (AllowCurrencySymbol) {
                                                Int32.FindCurrency (ref pos, s, nfi,
                                                                    ref foundCurrency);
-                                               if (foundCurrency && AllowLeadingWhite)
-                                                       pos = Int32.JumpOverWhite (pos, s, true);
+                                               if (foundCurrency && AllowLeadingWhite && 
+                                                               !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc))
+                                                       return false;
                                        }
                                }
                        }
@@ -266,14 +280,15 @@ namespace System {
                                // Currency + sign
                                Int32.FindCurrency (ref pos, s, nfi, ref foundCurrency);
                                if (foundCurrency) {
-                                       if (AllowLeadingWhite)
-                                               pos = Int32.JumpOverWhite (pos, s, true);
+                                       if (AllowLeadingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc))
+                                                       return false;
                                        if (foundCurrency) {
                                                if (!foundSign && AllowLeadingSign) {
                                                        Int32.FindSign (ref pos, s, nfi, ref foundSign,
                                                                        ref negative);
-                                                       if (foundSign && AllowLeadingWhite)
-                                                               pos = Int32.JumpOverWhite (pos, s, true);
+                                                       if (foundSign && AllowLeadingWhite &&
+                                                               !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc))
+                                                               return false;
                                                }
                                        }
                                }
@@ -314,18 +329,26 @@ namespace System {
                                                digitValue = (int) (hexDigit - 'A' + 10);
 
                                        ulong unumber = (ulong)number;
-                                       number = (long)checked(unumber * 16ul + (ulong)digitValue);
+                                       
+                                       // IMPROVME: We could avoid catching OverflowException
+                                       try {
+                                               number = (long)checked(unumber * 16ul + (ulong)digitValue);
+                                       } catch (OverflowException e){
+                                               if (!tryParse)
+                                                       exc = e;
+                                               return false;
+                                       }
                                }
                                else if (decimalPointFound) {
                                        nDigits++;
                                        // Allows decimal point as long as it's only 
                                        // followed by zeroes.
-                                       if (s [pos++] != '0')
-                                               if (tryParse)
-                                                       return false;
-                                               else
-                                                       throw new OverflowException ("Value too large or too " +
-                                                                                                                "small.");
+                                       if (s [pos++] != '0') {
+                                               if (!tryParse)
+                                                       exc = new OverflowException ("Value too large or too " +
+                                                                       "small.");
+                                               return false;
+                                       }
                                }
                                else {
                                        nDigits++;
@@ -338,28 +361,27 @@ namespace System {
                                                        (long) (s [pos++] - '0')
                                                        );
                                        } catch (OverflowException) {
-                                               if (tryParse)
-                                                       return false;
-                                               else
-                                                       throw new OverflowException ("Value too large or too " +
-                                                                                                                "small.");
+                                               if (!tryParse)
+                                                       exc = new OverflowException ("Value too large or too " +
+                                                                       "small.");
+                                               return false;
                                        }
                                }
                        } while (pos < s.Length);
 
                        // Post number stuff
-                       if (nDigits == 0)
-                               if (tryParse)
-                                       return false;
-                               else
-                                       throw new FormatException ("Input string was not in the correct format: nDigits == 0.");
+                       if (nDigits == 0) {
+                               if (!tryParse)
+                                       exc = new FormatException ("Input string was not in the correct format: nDigits == 0.");
+                               return false;
+                       }
 
                        if (AllowTrailingSign && !foundSign) {
                                // Sign + Currency
                                Int32.FindSign (ref pos, s, nfi, ref foundSign, ref negative);
                                if (foundSign) {
-                                       if (AllowTrailingWhite)
-                                               pos = Int32.JumpOverWhite (pos, s, true);
+                                       if (AllowTrailingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc))
+                                               return false;
                                        if (AllowCurrencySymbol)
                                                Int32.FindCurrency (ref pos, s, nfi,
                                                                    ref foundCurrency);
@@ -376,89 +398,102 @@ namespace System {
 
                                Int32.FindCurrency (ref pos, s, nfi, ref foundCurrency);
                                if (foundCurrency && pos < s.Length) {
-                                       if (AllowTrailingWhite)
-                                               pos = Int32.JumpOverWhite (pos, s, true);
+                                       if (AllowTrailingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc))
+                                               return false;
                                        if (!foundSign && AllowTrailingSign)
                                                Int32.FindSign (ref pos, s, nfi, ref foundSign,
                                                                ref negative);
                                }
                        }
                        
-                       if (AllowTrailingWhite && pos < s.Length)
-                               pos = Int32.JumpOverWhite (pos, s, false);
+                       if (AllowTrailingWhite && pos < s.Length && !Int32.JumpOverWhite (ref pos, s, false, tryParse, ref exc))
+                               return false;
 
                        if (foundOpenParentheses) {
-                               if (pos >= s.Length || s [pos++] != ')')
-                                       if (tryParse)
-                                               return false;
-                                       else
-                                               throw new FormatException ("Input string was not in the correct " +
-                                                                                                  "format: No room for close parens.");
-                               if (AllowTrailingWhite && pos < s.Length)
-                                       pos = Int32.JumpOverWhite (pos, s, false);
+                               if (pos >= s.Length || s [pos++] != ')') {
+                                       if (!tryParse)
+                                               exc = new FormatException ("Input string was not in the correct " +
+                                                               "format: No room for close parens.");
+                                       return false;
+                               }
+                               if (AllowTrailingWhite && pos < s.Length && !Int32.JumpOverWhite (ref pos, s, false, tryParse, ref exc))
+                                       return false;
                        }
 
-                       if (pos < s.Length && s [pos] != '\u0000')
-                               if (tryParse)
-                                       return false;
-                               else
-                                       throw new FormatException ("Input string was not in the correct format: Did not parse entire string. pos = " 
-                                                                                          + pos + " s.Length = " + s.Length);
+                       if (pos < s.Length && s [pos] != '\u0000') {
+                               if (!tryParse)
+                                       exc = new FormatException ("Input string was not in the correct format: Did not parse entire string. pos = " 
+                                                       + pos + " s.Length = " + s.Length);
+                               return false;
+                       }
 
                        
-                       if (!negative && !AllowHexSpecifier)
-                               number = -number;
+                       if (!negative && !AllowHexSpecifier){
+                               try {
+                                       number = checked (-number);
+                               } catch (OverflowException e){
+                                       if (!tryParse)
+                                               exc = e;
+                                       return false;
+                               }
+                       }
 
                        result = number;
                        return true;
                }
 
-               public static long Parse (string s) {
+               public static long Parse (string s) 
+               {
+                       Exception exc;
                        long res;
 
-                       Parse (s, false, out res);
+                       if (!Parse (s, false, out res, out exc))
+                               throw exc;
 
                        return res;
                }
 
-               public static long Parse (string s, NumberStyles style, IFormatProvider fp) {
+               public static long Parse (string s, NumberStyles style, IFormatProvider provider) 
+               {
+                       Exception exc;
                        long res;
 
-                       Parse (s, style, fp, false, out res);
+                       if (!Parse (s, style, provider, false, out res, out exc))
+                               throw exc;
 
                        return res;
                }
 
-#if NET_2_0
-               public static bool TryParse (string s, out long result) {
-                       try {
-                               return Parse (s, true, out result);
-                       }
-                       catch (Exception) {
+               public static bool TryParse (string s, out long result) 
+               {
+                       Exception exc;
+                       if (!Parse (s, true, out result, out exc)) {
                                result = 0;
                                return false;
                        }
+
+                       return true;
                }
 
-               public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out long result) {
-                       try {
-                               return Parse (s, style, provider, true, out result);
-                       }
-                       catch (Exception) {
+               public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out long result) 
+               {
+                       Exception exc;
+                       if (!Parse (s, style, provider, true, out result, out exc)) {
                                result = 0;
                                return false;
                        }
+
+                       return true;
                }
-#endif
 
                public override string ToString ()
                {
-                       return ToString (null, null);
+                       return NumberFormatter.NumberToString (m_value, null);
                }
 
-               public string ToString (IFormatProvider fp)
+               public string ToString (IFormatProvider provider)
                {
-                       return ToString (null, fp);
+                       return NumberFormatter.NumberToString (m_value, provider);
                }
 
                public string ToString (string format)
@@ -466,15 +501,9 @@ namespace System {
                        return ToString (format, null);
                }
 
-               public string ToString (string format, IFormatProvider fp)
+               public string ToString (string format, IFormatProvider provider)
                {
-                       NumberFormatInfo nfi = NumberFormatInfo.GetInstance( fp );
-                       
-                       // use "G" when format is null or String.Empty
-                       if ((format == null) || (format.Length == 0))
-                               format = "G";
-                       
-                       return IntegerFormatter.NumberToString (format, nfi, m_value);
+                       return NumberFormatter.NumberToString (format, m_value, provider);
                }
 
                // =========== IConvertible Methods =========== //
@@ -533,15 +562,17 @@ namespace System {
                {
                        return System.Convert.ToSByte (m_value);
                }
-               
+
                float IConvertible.ToSingle (IFormatProvider provider)
                {
                        return System.Convert.ToSingle (m_value);
                }
 
-               object IConvertible.ToType (Type conversionType, IFormatProvider provider)
+               object IConvertible.ToType (Type targetType, IFormatProvider provider)
                {
-                       return System.Convert.ToType (m_value, conversionType, provider);
+                       if (targetType == null)
+                               throw new ArgumentNullException ("targetType");
+                       return System.Convert.ToType (m_value, targetType, provider, false);
                }
 
                ushort IConvertible.ToUInt16 (IFormatProvider provider)