2010-03-31 Miguel de Icaza <miguel@novell.com>
[mono.git] / mcs / class / corlib / System / UInt32.cs
index bf83021ece2b11015465f9819e01e90527b4a343..950f1e8ea69fdcd0ad299fcb89d77ba8170216c0 100644 (file)
@@ -34,10 +34,8 @@ namespace System
 {
        [Serializable]
        [CLSCompliant (false)]
-       public struct UInt32 : IFormattable, IConvertible, IComparable
-#if NET_2_0
-               , IComparable<UInt32>, IEquatable <UInt32>
-#endif
+       [System.Runtime.InteropServices.ComVisible (true)]
+       public struct UInt32 : IFormattable, IConvertible, IComparable, IComparable<UInt32>, IEquatable <UInt32>
        {
                public const uint MaxValue = 0xffffffff;
                public const uint MinValue = 0;
@@ -52,13 +50,12 @@ namespace System
                        if (!(value is System.UInt32))
                                throw new ArgumentException (Locale.GetText ("Value is not a System.UInt32."));
 
-                       if (this.m_value == (uint) value)
-                               return 0;
+                       uint val = (uint) value;
 
-                       if (this.m_value < (uint) value)
-                               return -1;
+                       if (m_value == val)
+                               return 0;
 
-                       return 1;
+                       return (m_value < val) ? -1 : 1;
                }
 
                public override bool Equals (object obj)
@@ -74,7 +71,6 @@ namespace System
                        return (int) m_value;
                }
 
-#if NET_2_0
                public int CompareTo (uint value)
                {
                        if (m_value == value)
@@ -85,11 +81,10 @@ namespace System
                                return -1;
                }
 
-               public bool Equals (uint value)
+               public bool Equals (uint obj)
                {
-                       return value == m_value;
+                       return obj == m_value;
                }
-#endif
 
                internal static bool Parse (string s, bool tryParse, out uint result, out Exception exc)
                {
@@ -137,24 +132,15 @@ namespace System
                                if (c >= '0' && c <= '9') {
                                        uint d = (uint) (c - '0');
 
-                                       val = checked (val * 10 + d);
-                                       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;
-                                                       }
-                                               }
-                                               break;
-                                       } else {
+                                       if ((val > MaxValue/10) || (val == (MaxValue / 10) && d > (MaxValue % 10))){
                                                if (!tryParse)
-                                                       exc = Int32.GetFormatException ();
+                                                       exc = new OverflowException (Locale.GetText ("Value is too large"));
                                                return false;
                                        }
+                                       val = (val * 10) + d;
+                                       digits_seen = true;
+                               } else if (!Int32.ProcessTrailingWhitespace (tryParse, s, i, ref exc)){
+                                       return false;
                                }
                        }
                        if (!digits_seen) {
@@ -192,12 +178,12 @@ namespace System
                                return false;
                        }
 
-                       NumberFormatInfo nfi;
+                       NumberFormatInfo nfi = null;
                        if (provider != null) {
                                Type typeNFI = typeof (NumberFormatInfo);
                                nfi = (NumberFormatInfo) provider.GetFormat (typeNFI);
                        }
-                       else
+                       if (nfi == null)
                                nfi = Thread.CurrentThread.CurrentCulture.NumberFormat;
 
                        if (!Int32.CheckStyle (style, tryParse, ref exc))
@@ -308,7 +294,14 @@ namespace System
                                        else
                                                digitValue = (uint) (hexDigit - 'A' + 10);
 
-                                       number = checked (number * 16 + digitValue);
+                                       if (tryParse){
+                                               ulong l = number * 16 + digitValue;
+
+                                               if (l > MaxValue)
+                                                       return false;
+                                               number = (uint) l;
+                                       } else
+                                               number = checked (number * 16 + digitValue);
                                }
                                else if (decimalPointFound) {
                                        nDigits++;
@@ -408,12 +401,12 @@ namespace System
                }
 
                [CLSCompliant (false)]
-               public static uint Parse (string s, NumberStyles style, IFormatProvider fp
+               public static uint Parse (string s, NumberStyles style, IFormatProvider provider
                {
                        Exception exc;
                        uint 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;
@@ -431,7 +424,6 @@ namespace System
                        return Parse (s, style, null);
                }
 
-#if NET_2_0
                [CLSCompliant (false)]
                public static bool TryParse (string s, out uint result) 
                {
@@ -455,16 +447,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)
@@ -472,10 +463,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 =========== //
@@ -539,9 +529,12 @@ namespace System
                        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)