New tests.
[mono.git] / mcs / class / corlib / System / Int64.cs
index 1ba78d7555ad2692829d8498716e3d0a64ac8be9..bb06bae7a6431508ec23fbb88cc3afe517a6f3fb 100644 (file)
@@ -34,10 +34,8 @@ using System.Threading;
 namespace System {
        
        [Serializable]
-       public struct Int64 : IFormattable, IConvertible, IComparable
-#if NET_2_0
-               , IComparable<Int64>, IEquatable <Int64>
-#endif
+       [System.Runtime.InteropServices.ComVisible (true)]
+       public struct Int64 : IFormattable, IConvertible, IComparable, IComparable<Int64>, IEquatable <Int64>
        {
 
                public const long MaxValue = 0x7fffffffffffffff;
@@ -45,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 ()
@@ -75,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)
@@ -86,11 +82,10 @@ 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, out Exception exc)
                {
@@ -136,24 +131,32 @@ 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)
-                                                                       exc = Int32.GetFormatException ();
-                                                               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
+                                                       val = (val * 10) + d;
+
+                                               if (Int32.ProcessTrailingWhitespace (tryParse, s, i + 1, ref exc)){
+                                                       result = val;
+                                                       return true;
                                                }
-                                               break;
-                                       } else {
-                                               if (!tryParse)
-                                                       exc = Int32.GetFormatException ();
-                                               return false;
-                                       }
-                               }
+                                               goto overflow;
+                                       } else 
+                                               val = val * 10 + d;
+                                       
+                                       
+                                       digits_seen = true;
+                               } else if (!Int32.ProcessTrailingWhitespace (tryParse, s, i, ref exc))
+                                       return false;
+                                       
                        }
                        if (!digits_seen) {
                                if (!tryParse)
@@ -161,13 +164,22 @@ namespace System {
                                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)
@@ -193,12 +205,12 @@ namespace System {
                                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;
 
                        if (!Int32.CheckStyle (style, tryParse, ref exc))
@@ -317,7 +329,15 @@ 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++;
@@ -408,8 +428,15 @@ namespace System {
                        }
 
                        
-                       if (!negative && !AllowHexSpecifier)
-                               number = checked (-number);
+                       if (!negative && !AllowHexSpecifier){
+                               try {
+                                       number = checked (-number);
+                               } catch (OverflowException e){
+                                       if (!tryParse)
+                                               exc = e;
+                                       return false;
+                               }
+                       }
 
                        result = number;
                        return true;
@@ -426,18 +453,17 @@ namespace System {
                        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;
 
-                       if (!Parse (s, style, fp, false, out res, out exc))
+                       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) 
                {
                        Exception exc;
@@ -459,16 +485,15 @@ namespace System {
 
                        return true;
                }
-#endif
 
                public override string ToString ()
                {
-                       return NumberFormatter.FormatGeneral (new NumberFormatter.NumberStore (m_value));
+                       return NumberFormatter.NumberToString (m_value, null);
                }
 
-               public string ToString (IFormatProvider fp)
+               public string ToString (IFormatProvider provider)
                {
-                       return NumberFormatter.FormatGeneral (new NumberFormatter.NumberStore (m_value), fp);
+                       return NumberFormatter.NumberToString (m_value, provider);
                }
 
                public string ToString (string format)
@@ -476,10 +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 );
-                       return NumberFormatter.NumberToString (format, m_value, nfi);
+                       return NumberFormatter.NumberToString (format, m_value, provider);
                }
 
                // =========== IConvertible Methods =========== //
@@ -538,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)