From 9aa5cfdf6b4e6faf4b29d668357d30bc485b4bb5 Mon Sep 17 00:00:00 2001 From: Atsushi Eno Date: Wed, 13 Jan 2010 09:57:22 +0000 Subject: [PATCH] backport r149383 and r149411 from trunk. svn path=/branches/mono-2-6/mcs/; revision=149442 --- mcs/class/corlib/System/ChangeLog | 9 +++++ mcs/class/corlib/System/Double.cs | 44 +++++++++++++--------- mcs/class/corlib/Test/System/ChangeLog | 4 ++ mcs/class/corlib/Test/System/DoubleTest.cs | 10 +++++ 4 files changed, 50 insertions(+), 17 deletions(-) diff --git a/mcs/class/corlib/System/ChangeLog b/mcs/class/corlib/System/ChangeLog index f16db389f74e8..424113a367ebf 100644 --- a/mcs/class/corlib/System/ChangeLog +++ b/mcs/class/corlib/System/ChangeLog @@ -1,3 +1,12 @@ +2010-01-13 Atsushi Enomoto + + * Double.cs : no need to preserve old code. + +2010-01-12 Atsushi Enomoto + + * Double.cs : AllowLeading/TrailingWhite should also check those + surrounding string constants such as "NaN". + 2010-01-11 Carlos Alberto Cortez * Int32.cs: Implement support for exponent in the Parse methods. diff --git a/mcs/class/corlib/System/Double.cs b/mcs/class/corlib/System/Double.cs index fe4104172c700..a21e90ede1805 100644 --- a/mcs/class/corlib/System/Double.cs +++ b/mcs/class/corlib/System/Double.cs @@ -243,30 +243,18 @@ internal static bool Parse (string s, NumberStyles style, IFormatProvider provid NumberFormatInfo format = NumberFormatInfo.GetInstance(provider); if (format == null) throw new Exception("How did this happen?"); - if (s == format.NaNSymbol) { - result = Double.NaN; - return true; - } - if (s == format.PositiveInfinitySymbol) { - result = Double.PositiveInfinity; - return true; - } - if (s == format.NegativeInfinitySymbol) { - result = Double.NegativeInfinity; - return true; - } - // // validate and prepare string for C // int len = s.Length; - byte [] b = new byte [len + 1]; int didx = 0; int sidx = 0; char c; + bool allow_leading_white = (style & NumberStyles.AllowLeadingWhite) != 0; + bool allow_trailing_white = ((style & NumberStyles.AllowTrailingWhite) != 0); - if ((style & NumberStyles.AllowLeadingWhite) != 0){ - while (sidx < len && Char.IsWhiteSpace (c = s [sidx])) + if (allow_leading_white) { + while (sidx < len && Char.IsWhiteSpace (s [sidx])) sidx++; if (sidx == len) { @@ -275,8 +263,25 @@ internal static bool Parse (string s, NumberStyles style, IFormatProvider provid return false; } } + int sEndPos = s.Length - 1; + if (allow_trailing_white) + while (Char.IsWhiteSpace (s [sEndPos])) + sEndPos--; - bool allow_trailing_white = ((style & NumberStyles.AllowTrailingWhite) != 0); + if (TryParseStringConstant (format.NaNSymbol, s, sidx, sEndPos)) { + result = double.NaN; + return true; + } + if (TryParseStringConstant (format.PositiveInfinitySymbol, s, sidx, sEndPos)) { + result = double.PositiveInfinity; + return true; + } + if (TryParseStringConstant (format.NegativeInfinitySymbol, s, sidx, sEndPos)) { + result = double.NegativeInfinity; + return true; + } + + byte [] b = new byte [len + 1]; // // Machine state @@ -480,6 +485,11 @@ internal static bool Parse (string s, NumberStyles style, IFormatProvider provid } } + static bool TryParseStringConstant (string format, string s, int start, int end) + { + return end - start + 1 == format.Length && String.CompareOrdinal (format, 0, s, start, format.Length) == 0; + } + [MethodImplAttribute(MethodImplOptions.InternalCall)] unsafe private static extern bool ParseImpl (byte *byte_ptr, out double value); diff --git a/mcs/class/corlib/Test/System/ChangeLog b/mcs/class/corlib/Test/System/ChangeLog index 9d281191dff43..df0aefb51d1f7 100644 --- a/mcs/class/corlib/Test/System/ChangeLog +++ b/mcs/class/corlib/Test/System/ChangeLog @@ -1,3 +1,7 @@ +2010-01-12 Atsushi Enomoto + + * DoubleTest.cs : add test for AllowLeading/TrailingSpace flags. + 2010-01-11 Carlos Alberto Cortez * Int32Test.cs: Add test case for exponent support in the Parse diff --git a/mcs/class/corlib/Test/System/DoubleTest.cs b/mcs/class/corlib/Test/System/DoubleTest.cs index 573d4e94ecbc8..b9b852e800022 100644 --- a/mcs/class/corlib/Test/System/DoubleTest.cs +++ b/mcs/class/corlib/Test/System/DoubleTest.cs @@ -221,6 +221,16 @@ public void Parse () } } + [Test] + public void ParseAllowWhitespaces () + { + NumberStyles style = NumberStyles.Float; + double.Parse (" 32 "); + double.Parse (" Infinity "); + double.Parse (" -Infinity "); + double.Parse (" NaN "); + } + [Test] // bug #81630 public void Parse_Whitespace () {