Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
backport r149383 and r149411 from trunk.
svn path=/branches/mono-2-6/mcs/; revision=149442
  • Loading branch information
atsushieno committed Jan 13, 2010
1 parent 966ebb8 commit 9aa5cfd
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 17 deletions.
9 changes: 9 additions & 0 deletions mcs/class/corlib/System/ChangeLog
@@ -1,3 +1,12 @@
2010-01-13 Atsushi Enomoto <atsushi@ximian.com>

* Double.cs : no need to preserve old code.

2010-01-12 Atsushi Enomoto <atsushi@ximian.com>

* Double.cs : AllowLeading/TrailingWhite should also check those
surrounding string constants such as "NaN".

2010-01-11 Carlos Alberto Cortez <calberto.cortez@gmail.com>

* Int32.cs: Implement support for exponent in the Parse methods.
Expand Down
44 changes: 27 additions & 17 deletions mcs/class/corlib/System/Double.cs
Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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);

Expand Down
4 changes: 4 additions & 0 deletions mcs/class/corlib/Test/System/ChangeLog
@@ -1,3 +1,7 @@
2010-01-12 Atsushi Enomoto <atsushi@ximian.com>

* DoubleTest.cs : add test for AllowLeading/TrailingSpace flags.

2010-01-11 Carlos Alberto Cortez <calberto.cortez@gmail.com>

* Int32Test.cs: Add test case for exponent support in the Parse
Expand Down
10 changes: 10 additions & 0 deletions mcs/class/corlib/Test/System/DoubleTest.cs
Expand Up @@ -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 ()
{
Expand Down

0 comments on commit 9aa5cfd

Please sign in to comment.