Author: miguel
Date: 2007-07-18 21:50:48 -0400 (Wed, 18 Jul 2007)
New Revision: 82261

Added:
   trunk/mcs/class/corlib/Test/System/IntegerTryParse.cs
Modified:
   trunk/mcs/class/corlib/System/AppDomain.cs
   trunk/mcs/class/corlib/System/Byte.cs
   trunk/mcs/class/corlib/System/ChangeLog
   trunk/mcs/class/corlib/System/Int16.cs
   trunk/mcs/class/corlib/System/Int32.cs
   trunk/mcs/class/corlib/System/Int64.cs
   trunk/mcs/class/corlib/System/SByte.cs
   trunk/mcs/class/corlib/System/UInt16.cs
   trunk/mcs/class/corlib/System/UInt32.cs
   trunk/mcs/class/corlib/System/UInt64.cs
   trunk/mcs/class/corlib/corlib_test.dll.sources
Log:
2007-06-06  Miguel de Icaza  <[EMAIL PROTECTED]>

        * Int16.cs, UInt64.cs, SByte.cs, UInt16.cs, Byte.cs, Int32.cs,
        Int64.cs, UInt32.cs: Fix for 81775.

        I removed Parse from a Byte.cs and UInt16.cs as in various cases
        they still depended on UInt32.cs for parsing, there is no reason
        to keep all of this code duplicated (UInt64 is a different case
        though). 

        I was hoping for some feedback on whether my tests are correct,
        but so far no takers (Overflow tests).




Modified: trunk/mcs/class/corlib/System/AppDomain.cs
===================================================================
--- trunk/mcs/class/corlib/System/AppDomain.cs  2007-07-19 00:20:40 UTC (rev 
82260)
+++ trunk/mcs/class/corlib/System/AppDomain.cs  2007-07-19 01:50:48 UTC (rev 
82261)
@@ -443,6 +443,7 @@
                //
                public void DoCallBack (CrossAppDomainDelegate theDelegate)
                {
+                       Console.WriteLine ("corlib: Callback is happening on 
domain: {0}", CurrentDomain.FriendlyName);
                        if (theDelegate != null)
                                theDelegate ();
                }

Modified: trunk/mcs/class/corlib/System/Byte.cs
===================================================================
--- trunk/mcs/class/corlib/System/Byte.cs       2007-07-19 00:20:40 UTC (rev 
82260)
+++ trunk/mcs/class/corlib/System/Byte.cs       2007-07-19 01:50:48 UTC (rev 
82261)
@@ -96,96 +96,6 @@
                }
 #endif
 
-               internal static bool Parse (string s, bool tryParse, out byte 
result, out Exception exc)
-               {
-                       byte val = 0;
-                       int len;
-                       int i;
-                       bool digits_seen = false;
-                       bool negative = false;
-
-                       result = 0;
-                       exc = null;
-
-                       if (s == null) {
-                               if (!tryParse)
-                                       exc = new ArgumentNullException ("s");
-                               return false;
-                       }
-
-                       len = s.Length;
-
-                       // look for the first non-whitespace character
-                       char c;
-                       for (i = 0; i < len; i++){
-                               c = s [i];
-                               if (!Char.IsWhiteSpace (c))
-                                       break;
-                       }
-
-                       // if it's all whitespace, then throw exception
-                       if (i == len) {
-                               if (!tryParse)
-                                       exc = Int32.GetFormatException ();
-                               return false;
-                       }
-
-                       // look for the optional '+' sign
-                       if (s [i] == '+')
-                               i++;
-                       else if (s [i] == '-') {
-                               negative = true;
-                               i++;
-                       }
-
-                       // we should just have numerals followed by whitespace 
now
-                       for (; i < len; i++){
-                               c = s [i];
-
-                               if (c >= '0' && c <= '9'){
-                                       // shift left and accumulate every time 
we find a numeral
-                                       byte d = (byte) (c - '0');
-
-                                       val = checked ((byte) (val * 10 + d));
-                                       digits_seen = true;
-                               } else {
-                                       // after the last numeral, only 
whitespace is allowed
-                                       if (Char.IsWhiteSpace (c)){
-                                               for (i++; i < len; i++){
-                                                       if (!Char.IsWhiteSpace 
(s [i])) {
-                                                               if (!tryParse)
-                                                                       exc = 
Int32.GetFormatException ();
-                                                               return false;
-                                                       }
-                                               }
-                                               break;
-                                       } else {
-                                               if (!tryParse)
-                                                       exc = 
Int32.GetFormatException ();
-                                               return false;
-                                       }
-                               }
-                       }
-
-                       // -0 is legal but other negative values are not
-                       if (negative && (val > 0)) {
-                               if (!tryParse)
-                                       exc = new OverflowException (
-                                           Locale.GetText ("Negative number"));
-                               return false;
-                       }
-
-                       // if all we had was a '+' sign, then throw exception
-                       if (!digits_seen) {
-                               if (!tryParse)
-                                       exc = Int32.GetFormatException ();
-                               return false;
-                       }
-
-                       result = val;
-                       return true;
-               }
-
                public static byte Parse (string s, IFormatProvider provider)
                {
                        return Parse (s, NumberStyles.Integer, provider);
@@ -199,33 +109,21 @@
                public static byte Parse (string s, NumberStyles style, 
IFormatProvider provider)
                {
                        uint tmpResult = UInt32.Parse (s, style, provider);
-                       if (tmpResult > Byte.MaxValue || tmpResult < 
Byte.MinValue)
-                               throw new OverflowException (Locale.GetText 
("Value too large or too small."));
+                       if (tmpResult > Byte.MaxValue)
+                               throw new OverflowException (Locale.GetText 
("Value too large."));
 
                        return (byte) tmpResult;
                }
 
                public static byte Parse (string s) 
                {
-                       Exception exc;
-                       byte res;
-
-                       if (!Parse (s, false, out res, out exc))
-                               throw exc;
-
-                       return res;
+                       return Parse (s, NumberStyles.Integer, null);
                }
 
 #if NET_2_0
                public static bool TryParse (string s, out byte result) 
                {
-                       Exception exc;
-                       if (!Parse (s, true, out result, out exc)) {
-                               result = 0;
-                               return false;
-                       }
-
-                       return true;
+                       return TryParse (s, NumberStyles.Integer, null, out 
result);
                }
 
                public static bool TryParse (string s, NumberStyles style, 
IFormatProvider provider, out byte result) 
@@ -236,7 +134,7 @@
                        if (!UInt32.TryParse (s, style, provider, out 
tmpResult))
                                return false;
                                
-                       if (tmpResult > Byte.MaxValue || tmpResult < 
Byte.MinValue)
+                       if (tmpResult > Byte.MaxValue)
                                return false;
                                
                        result = (byte)tmpResult;

Modified: trunk/mcs/class/corlib/System/ChangeLog
===================================================================
--- trunk/mcs/class/corlib/System/ChangeLog     2007-07-19 00:20:40 UTC (rev 
82260)
+++ trunk/mcs/class/corlib/System/ChangeLog     2007-07-19 01:50:48 UTC (rev 
82261)
@@ -1,3 +1,16 @@
+2007-06-06  Miguel de Icaza  <[EMAIL PROTECTED]>
+
+       * Int16.cs, UInt64.cs, SByte.cs, UInt16.cs, Byte.cs, Int32.cs,
+       Int64.cs, UInt32.cs: Fix for 81775.
+
+       I removed Parse from a Byte.cs and UInt16.cs as in various cases
+       they still depended on UInt32.cs for parsing, there is no reason
+       to keep all of this code duplicated (UInt64 is a different case
+       though). 
+
+       I was hoping for some feedback on whether my tests are correct,
+       but so far no takers.
+
 2007-07-14  Zoltan Varga  <[EMAIL PROTECTED]>
 
        * ModuleHandle.cs: Implement the generic versions of the 
ResolveXXXHandle () methods.

Modified: trunk/mcs/class/corlib/System/Int16.cs
===================================================================
--- trunk/mcs/class/corlib/System/Int16.cs      2007-07-19 00:20:40 UTC (rev 
82260)
+++ trunk/mcs/class/corlib/System/Int16.cs      2007-07-19 01:50:48 UTC (rev 
82261)
@@ -136,7 +136,19 @@
                                c = s [i];
 
                                if (c >= '0' && c <= '9'){
-                                       val = checked ((short) (val * 10 + (c - 
'0') * sign));
+                                       byte d = (byte) (c - '0');
+
+                                       if (tryParse){
+                                               val = (short) (val * 10 + d * 
sign);
+
+                                               if (sign == 1){
+                                                       if (val < 0)
+                                                               return false;
+                                               } else if (val > 0)
+                                                       return false;
+                                       } else
+                                               val = checked ((short) (val * 
10 + d * sign));
+                                   
                                        digits_seen = true;
                                } else {
                                        if (Char.IsWhiteSpace (c)){

Modified: trunk/mcs/class/corlib/System/Int32.cs
===================================================================
--- trunk/mcs/class/corlib/System/Int32.cs      2007-07-19 00:20:40 UTC (rev 
82260)
+++ trunk/mcs/class/corlib/System/Int32.cs      2007-07-19 01:50:48 UTC (rev 
82261)
@@ -143,13 +143,19 @@
                                }
                                
                                if (c >= '0' && c <= '9'){
-                                       try {
+                                       byte d = (byte) (c - '0');
+                                               
+                                       if (tryParse){
+                                               val = val * 10 + d * sign;
+                                               if (sign == 1){
+                                                       if (val < 0)
+                                                               return false;
+                                               } else if (val > 0)
+                                                       return false;
+                                       } else {
                                                val = checked (val * 10 + (c - 
'0') * sign);
-                                               digits_seen = true;
-                                       } catch (OverflowException e) {
-                                               exc = e;
-                                               return false;
                                        }
+                                       digits_seen = true;
                                } else {
                                        if (Char.IsWhiteSpace (c)){
                                                for (i++; i < len; i++){
@@ -443,11 +449,13 @@
                                                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) {
@@ -534,9 +542,17 @@
                                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;

Modified: trunk/mcs/class/corlib/System/Int64.cs
===================================================================
--- trunk/mcs/class/corlib/System/Int64.cs      2007-07-19 00:20:40 UTC (rev 
82260)
+++ trunk/mcs/class/corlib/System/Int64.cs      2007-07-19 01:50:48 UTC (rev 
82261)
@@ -139,7 +139,16 @@
                                c = s [i];
 
                                if (c >= '0' && c <= '9'){
-                                       val = checked (val * 10 + (c - '0') * 
sign);
+                                       if (tryParse){
+                                               val = val * 10 + (c - '0') * 
sign;
+                                       
+                                               if (sign == 1){
+                                                       if (val < 0)
+                                                               return false;
+                                               } else if (val > 0)
+                                                       return false;
+                                       } else
+                                               val = checked (val * 10 + (c - 
'0') * sign);
                                        digits_seen = true;
                                } else {
                                        if (Char.IsWhiteSpace (c)){
@@ -320,7 +329,15 @@
                                                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++;
@@ -411,8 +428,15 @@
                        }
 
                        
-                       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;

Modified: trunk/mcs/class/corlib/System/SByte.cs
===================================================================
--- trunk/mcs/class/corlib/System/SByte.cs      2007-07-19 00:20:40 UTC (rev 
82260)
+++ trunk/mcs/class/corlib/System/SByte.cs      2007-07-19 01:50:48 UTC (rev 
82261)
@@ -137,7 +137,14 @@
                                c = s [i];
 
                                if (c >= '0' && c <= '9') {
-                                       ival = checked (ival * 10 - (int) (c - 
'0'));
+                                       if (tryParse){
+                                               int intval = ival * 10 - (int) 
(c - '0');
+
+                                               if (intval < MinValue)
+                                                       return false;
+                                               ival = (sbyte) intval;
+                                       } else
+                                               ival = checked (ival * 10 - 
(int) (c - '0'));
                                        digits_seen = true;
                                } else {
                                        if (Char.IsWhiteSpace (c)) {

Modified: trunk/mcs/class/corlib/System/UInt16.cs
===================================================================
--- trunk/mcs/class/corlib/System/UInt16.cs     2007-07-19 00:20:40 UTC (rev 
82260)
+++ trunk/mcs/class/corlib/System/UInt16.cs     2007-07-19 01:50:48 UTC (rev 
82261)
@@ -87,91 +87,7 @@
                }
 #endif
 
-               internal static bool Parse (string s, bool tryParse, out ushort 
result, out Exception exc)
-               {
-                       ushort val = 0;
-                       int len;
-                       int i;
-                       bool digits_seen = false;
-                       bool has_negative_sign = false;
 
-                       result = 0;
-                       exc = null;
-
-                       if (s == null) {
-                               if (!tryParse)
-                                       exc = new ArgumentNullException ("s");
-                               return false;
-                       }
-
-                       len = s.Length;
-
-                       char c;
-                       for (i = 0; i < len; i++) {
-                               c = s [i];
-                               if (!Char.IsWhiteSpace (c))
-                                       break;
-                       }
-
-                       if (i == len) {
-                               if (!tryParse)
-                                       exc = Int32.GetFormatException ();
-                               return false;
-                       }
-
-                       if (s [i] == '+')
-                               i++;
-                       else
-                               if (s[i] == '-') {
-                                       i++;
-                                       has_negative_sign = true;
-                               }
-
-                       for (; i < len; i++) {
-                               c = s [i];
-
-                               if (c >= '0' && c <= '9') {
-                                       ushort d = (ushort) (c - '0');
-
-                                       val = checked ((ushort) (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 (!tryParse)
-                                                       exc = 
Int32.GetFormatException ();
-                                               return false;
-                                       }
-                               }
-                       }
-                       if (!digits_seen) {
-                               if (!tryParse)
-                                       exc = Int32.GetFormatException ();
-                               return false;
-                       }
-
-                       // -0 is legal but other negative values are not
-                       if (has_negative_sign && (val > 0)) {
-                               if (!tryParse)
-                                       exc = new OverflowException (
-                                           Locale.GetText ("Negative number"));
-                               return false;
-                       }
-
-                       result = val;
-                       return true;
-               }
-
                [CLSCompliant (false)]
                public static ushort Parse (string s, IFormatProvider provider)
                {
@@ -188,8 +104,8 @@
                public static ushort Parse (string s, NumberStyles style, 
IFormatProvider provider)
                {
                        uint tmpResult = UInt32.Parse (s, style, provider);
-                       if (tmpResult > UInt16.MaxValue || tmpResult < 
UInt16.MinValue)
-                               throw new OverflowException (Locale.GetText 
("Value too large or too small."));
+                       if (tmpResult > UInt16.MaxValue)
+                               throw new OverflowException (Locale.GetText 
("Value too large."));
 
                        return (ushort) tmpResult;
                }
@@ -197,26 +113,14 @@
                [CLSCompliant(false)]
                public static ushort Parse (string s) 
                {
-                       Exception exc;
-                       ushort res;
-
-                       if (!Parse (s, false, out res, out exc))
-                               throw exc;
-
-                       return res;
+                       return Parse (s, NumberStyles.Number, null);
                }
 
 #if NET_2_0
                [CLSCompliant(false)]
                public static bool TryParse (string s, out ushort result) 
                {
-                       Exception exc;
-                       if (!Parse (s, true, out result, out exc)) {
-                               result = 0;
-                               return false;
-                       }
-
-                       return true;
+                       return TryParse (s, NumberStyles.Integer, null, out 
result);
                }
 
                [CLSCompliant(false)]
@@ -228,7 +132,7 @@
                        if (!UInt32.TryParse (s, style, provider, out 
tmpResult))
                                return false;
                                
-                       if (tmpResult > UInt16.MaxValue || tmpResult < 
UInt16.MinValue)
+                       if (tmpResult > UInt16.MaxValue)
                                return false;
                                
                        result = (ushort)tmpResult;

Modified: trunk/mcs/class/corlib/System/UInt32.cs
===================================================================
--- trunk/mcs/class/corlib/System/UInt32.cs     2007-07-19 00:20:40 UTC (rev 
82260)
+++ trunk/mcs/class/corlib/System/UInt32.cs     2007-07-19 01:50:48 UTC (rev 
82261)
@@ -140,7 +140,14 @@
                                if (c >= '0' && c <= '9') {
                                        uint d = (uint) (c - '0');
 
-                                       val = checked (val * 10 + d);
+                                       if (tryParse){
+                                               ulong v = ((ulong)val) * 10 + d;
+
+                                               if (v > MaxValue)
+                                                       return false;
+                                               val = (uint) v;
+                                       } else
+                                               val = checked (val * 10 + d);
                                        digits_seen = true;
                                }
                                else {
@@ -311,7 +318,14 @@
                                        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++;

Modified: trunk/mcs/class/corlib/System/UInt64.cs
===================================================================
--- trunk/mcs/class/corlib/System/UInt64.cs     2007-07-19 00:20:40 UTC (rev 
82260)
+++ trunk/mcs/class/corlib/System/UInt64.cs     2007-07-19 01:50:48 UTC (rev 
82261)
@@ -247,7 +247,16 @@
                                        else
                                                digitValue = (ulong) (hexDigit 
- 'A' + 10);
 
-                                       number = checked (number * 16 + 
digitValue);
+                                       if (tryParse){
+                                               // Any number above 32 will do 
+                                               bool can_overflow = number > 
0xffff;
+
+                                               number = number * 16 + 
digitValue;
+
+                                               if (can_overflow && number < 16)
+                                                       return false;
+                                       } else
+                                               number = checked (number * 16 + 
digitValue);
                                }
                                else if (decimalPointFound) {
                                        nDigits++;

Added: trunk/mcs/class/corlib/Test/System/IntegerTryParse.cs
===================================================================
--- trunk/mcs/class/corlib/Test/System/IntegerTryParse.cs       2007-07-19 
00:20:40 UTC (rev 82260)
+++ trunk/mcs/class/corlib/Test/System/IntegerTryParse.cs       2007-07-19 
01:50:48 UTC (rev 82261)
@@ -0,0 +1,64 @@
+#if NET_2_0
+using System;
+
+using NUnit.Framework;
+
+[TestFixture]
+public class IntegerTryParse {
+       [Test]
+       public void Int8() {
+               sbyte i;
+               Assert.IsFalse(sbyte.TryParse("128", out i));
+               Assert.IsFalse(sbyte.TryParse("-129", out i));
+       }
+
+       [Test]
+       public void UInt8() {
+               byte i;
+               Assert.IsFalse(byte.TryParse("256", out i));
+               Assert.IsFalse(byte.TryParse("-1", out i));
+       }
+
+       [Test]
+       public void Int16() {
+               short i;
+               Assert.IsFalse(short.TryParse("32768", out i));
+               Assert.IsFalse(short.TryParse("-32769", out i));
+       }
+
+       [Test]
+       public void UInt16() {
+               ushort i;
+               Assert.IsFalse(ushort.TryParse("65536", out i));
+               Assert.IsFalse(ushort.TryParse("-1", out i));
+       }
+
+       [Test]
+       public void Int32() {
+               int i;
+               Assert.IsFalse(int.TryParse("2147483648", out i));
+               Assert.IsFalse(int.TryParse("-2147483649", out i));
+       }
+
+       [Test]
+       public void UInt32() {
+               uint i;
+               Assert.IsFalse(uint.TryParse("4294967296", out i));
+               Assert.IsFalse(uint.TryParse("-1", out i));
+       }
+
+       [Test]
+       public void Int64() {
+               long i;
+               Assert.IsFalse(long.TryParse("9223372036854775808", out i));
+               Assert.IsFalse(long.TryParse("-9223372036854775809", out i));
+       }
+
+       [Test]
+       public void UInt64() {
+               ulong i;
+               Assert.IsFalse(ulong.TryParse("18446744073709551616", out i));
+               Assert.IsFalse(ulong.TryParse("-1", out i));
+       }
+}
+#endif


Property changes on: trunk/mcs/class/corlib/Test/System/IntegerTryParse.cs
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: trunk/mcs/class/corlib/corlib_test.dll.sources
===================================================================
--- trunk/mcs/class/corlib/corlib_test.dll.sources      2007-07-19 00:20:40 UTC 
(rev 82260)
+++ trunk/mcs/class/corlib/corlib_test.dll.sources      2007-07-19 01:50:48 UTC 
(rev 82261)
@@ -17,6 +17,7 @@
 System/CharTest.cs
 System/DelegateTest.cs
 System/ModuleHandleTest.cs
+System/IntegerTryParse.cs
 System.Collections/ArrayListTest.cs
 System.Collections/BitArrayTest.cs
 System.Collections/CaseInsensitiveComparerTest.cs

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to