merge 99422:99508
[mono.git] / mcs / class / corlib / System / Int32.cs
index 515157fab6e956d6b25a748ff94f36482022148b..5539613b9b521b268940ac3a34442ceebdd54928 100644 (file)
@@ -33,6 +33,9 @@ using System.Threading;
 namespace System {
        
        [Serializable]
+#if NET_2_0
+       [System.Runtime.InteropServices.ComVisible (true)]
+#endif
        public struct Int32 : IFormattable, IConvertible, IComparable
 #if NET_2_0
                , IComparable<Int32>, IEquatable <Int32>
@@ -92,6 +95,22 @@ namespace System {
                }
 #endif
 
+               internal static bool ProcessTrailingWhitespace (bool tryParse, string s, int position, ref Exception exc)
+               {
+                       int len = s.Length;
+                       
+                       for (int i = position; i < len; i++){
+                               char c = s [i];
+                               
+                               if (!Char.IsWhiteSpace (c)){
+                                       if (!tryParse)
+                                               exc = GetFormatException ();
+                                       return false;
+                               }
+                       }
+                       return true;
+               }
+
                internal static bool Parse (string s, bool tryParse, out int result, out Exception exc)
                {
                        int val = 0;
@@ -140,30 +159,30 @@ namespace System {
                                }
                                
                                if (c >= '0' && c <= '9'){
-                                       try {
-                                               val = checked (val * 10 + (c - '0') * sign);
-                                               digits_seen = true;
-                                       } catch (OverflowException e) {
-                                               exc = e;
-                                               return false;
-                                       }
-                               } else {
-                                       if (Char.IsWhiteSpace (c)){
-                                               for (i++; i < len; i++){
-                                                       if (!Char.IsWhiteSpace (s [i])) {
-                                                               if (!tryParse)
-                                                                       exc = 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 (ProcessTrailingWhitespace (tryParse, s, i + 1, ref exc)){
+                                                       result = val;
+                                                       return true;
                                                }
-                                               break;
-                                       } else {
-                                               if (!tryParse)
-                                                       exc = GetFormatException ();
-                                               return false;
-                                       }
-                               }
+                                               goto overflow;
+                                       } else 
+                                               val = val * 10 + d;
+                                       
+                                       digits_seen = true;
+                               } else if (!ProcessTrailingWhitespace (tryParse, s, i, ref exc))
+                                       return false;
                        }
                        if (!digits_seen) {
                                if (!tryParse)
@@ -171,9 +190,17 @@ 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 int Parse (string s, IFormatProvider fp)
@@ -440,11 +467,13 @@ namespace System {
                                                digitValue = (int) (hexDigit - 'A' + 10);
 
                                        uint unumber = (uint)number;
-                                       try {
+                                       if (tryParse){
+                                               if ((unumber & 0xf0000000) != 0)
+                                                       return false;
+                                               
+                                               number = (int) (unumber * 16u + (uint) digitValue);
+                                       } else {
                                                number = (int)checked (unumber * 16u + (uint)digitValue);
-                                       } catch (OverflowException e) {
-                                               exc = e;
-                                               return false;
                                        }
                                }
                                else if (decimalPointFound) {
@@ -531,9 +560,17 @@ namespace System {
                                return false;
                        }
                        
-                       if (!negative && !AllowHexSpecifier)
-                               number = checked (-number);
+                       if (!negative && !AllowHexSpecifier){
+                               if (tryParse){
+                                       long lval = -number;
 
+                                       if (lval < MinValue || lval > MaxValue)
+                                               return false;
+                                       number = (int) lval;
+                               } else
+                                       number = checked (-number);
+                       }
+                       
                        result = number;
 
                        return true;
@@ -588,12 +625,12 @@ namespace System {
 
                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)
@@ -603,8 +640,7 @@ namespace System {
 
                public string ToString (string format, IFormatProvider fp )
                {
-                       NumberFormatInfo nfi = NumberFormatInfo.GetInstance( fp );
-                       return NumberFormatter.NumberToString (format, m_value, nfi);
+                       return NumberFormatter.NumberToString (format, m_value, fp);
                }
 
                // =========== IConvertible Methods =========== //