[corlib] Make primitive types smaller than int compare result compatible
[mono.git] / mcs / class / corlib / System / Int16.cs
index a0eb592408bfe43d5e85c641b175a8040d249204..d3c0029bb57645de0bbaa14325922b6555d251fa 100644 (file)
@@ -32,13 +32,8 @@ using System.Globalization;
 namespace System {
        
        [Serializable]
-#if NET_2_0
        [System.Runtime.InteropServices.ComVisible (true)]
-#endif
-       public struct Int16 : IFormattable, IConvertible, IComparable
-#if NET_2_0
-               , IComparable<Int16>, IEquatable <Int16>
-#endif
+       public struct Int16 : IFormattable, IConvertible, IComparable, IComparable<Int16>, IEquatable <Int16>
        {
 
                public const short MaxValue =  32767;
@@ -46,29 +41,23 @@ namespace System {
                
                internal short m_value;
 
-               public int CompareTo (object v)
+               public int CompareTo (object value)
                {
-                       if (v == null)
+                       if (value == null)
                                return 1;
 
-                       if (!(v is System.Int16))
+                       if (!(value is System.Int16))
                                throw new ArgumentException (Locale.GetText ("Value is not a System.Int16"));
 
-                       short xv = (short) v;
-                       if (m_value == xv)
-                               return 0;
-                       if (m_value > xv)
-                               return 1;
-                       else
-                               return -1;
+                       return CompareTo ((short) value);
                }
 
-               public override bool Equals (object o)
+               public override bool Equals (object obj)
                {
-                       if (!(o is System.Int16))
+                       if (!(obj is System.Int16))
                                return false;
 
-                       return ((short) o) == m_value;
+                       return ((short) obj) == m_value;
                }
 
                public override int GetHashCode ()
@@ -76,22 +65,15 @@ namespace System {
                        return m_value;
                }
 
-#if NET_2_0
                public int CompareTo (short value)
                {
-                       if (m_value == value)
-                               return 0;
-                       if (m_value > value)
-                               return 1;
-                       else
-                               return -1;
+                       return m_value - value;
                }
 
-               public bool Equals (short value)
+               public bool Equals (short obj)
                {
-                       return value == m_value;
+                       return obj == m_value;
                }
-#endif
 
                internal static bool Parse (string s, bool tryParse, out short result, out Exception exc)
                {
@@ -181,9 +163,9 @@ namespace System {
                        return false;
                }
 
-               public static short Parse (string s, IFormatProvider fp)
+               public static short Parse (string s, IFormatProvider provider)
                {
-                       return Parse (s, NumberStyles.Integer, fp);
+                       return Parse (s, NumberStyles.Integer, provider);
                }
 
                public static short Parse (string s, NumberStyles style)
@@ -191,13 +173,17 @@ namespace System {
                        return Parse (s, style, null);
                }
 
-               public static short Parse (string s, NumberStyles style, IFormatProvider fp)
+               public static short Parse (string s, NumberStyles style, IFormatProvider provider)
                {
-                       int tmpResult = Int32.Parse (s, style, fp);
-                       if (tmpResult > Int16.MaxValue || tmpResult < Int16.MinValue)
-                               throw new OverflowException ("Value too large or too small.");
+                       int tmpResult = Int32.Parse (s, style, provider);
+                       if ((style & NumberStyles.AllowHexSpecifier) != 0) {
+                               if (tmpResult >= 0 && tmpResult <= ushort.MaxValue)
+                                       return (short) tmpResult;
+                       } else if (tmpResult <= MaxValue && tmpResult >= MinValue) {
+                               return (short) tmpResult;
+                       }
 
-                       return (short) tmpResult;
+                       throw new OverflowException ("Value too large or too small.");
                }
 
                public static short Parse (string s) 
@@ -211,7 +197,6 @@ namespace System {
                        return res;
                }
 
-#if NET_2_0
                public static bool TryParse (string s, out short result) 
                {
                        Exception exc;
@@ -237,7 +222,6 @@ namespace System {
                        result = (short)tmpResult;
                        return true;
                }
-#endif
 
                public override string ToString ()
                {
@@ -254,9 +238,9 @@ namespace System {
                        return ToString (format, null);
                }
 
-               public string ToString (string format, IFormatProvider fp)
+               public string ToString (string format, IFormatProvider provider)
                {
-                       return NumberFormatter.NumberToString(format, m_value, fp);
+                       return NumberFormatter.NumberToString(format, m_value, provider);
                }
 
                // =========== IConvertible Methods =========== //
@@ -315,15 +299,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)