Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Parlot/Scanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,11 @@ public bool ReadDecimal(bool allowLeadingSign, bool allowDecimalSeparator, bool

if (!ReadInteger(out number))
{
Cursor.ResetPosition(beforeDecimalSeparator);

// A decimal separator must be followed by a number if there is no integral part, e.g. `[NaN].[NaN]`
if (numberIsEmpty)
{
Cursor.ResetPosition(beforeDecimalSeparator);

return false;
}

Expand Down
26 changes: 23 additions & 3 deletions test/Parlot.Tests/ScannerTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Parlot.Tests.Calc;
using System;
using System.Buffers;

using System.Globalization;
using Xunit;

namespace Parlot.Tests;
Expand Down Expand Up @@ -401,14 +401,34 @@ public void ShouldReadDecimalWithGroupSeparator(string input, string expected)
[Theory]
[InlineData("123.456", "123.456")]
[InlineData("123.456a", "123.456")]
[InlineData("123.a", "123")]
[InlineData("123.a", "123.")]
[InlineData("123.456.789", "123.456")]
[InlineData("123.", "123")]
[InlineData("123.", "123.")]
public void ShouldReadDecimalWithDecimalSeparator(string input, string expected)
{
Scanner s = new(input);

Assert.True(s.ReadDecimal(Fluent.NumberOptions.AllowDecimalSeparator, out var result, decimalSeparator: '.'));
Assert.True(decimal.TryParse(expected, NumberStyles.Float, CultureInfo.InvariantCulture, out _));
Assert.Equal(expected, result);
}

[Theory]
[InlineData("123.456", "123")]
[InlineData("123.456a", "123")]
[InlineData("123.a", "123")]
[InlineData("123.456.789", "123")]
[InlineData("123.", "123")]
[InlineData("123.e", "123")]
[InlineData("123.e1", "123")]
[InlineData("123e", "123")]
[InlineData("123e1", "123e1")]
public void ShouldReadIntegerWithExponent(string input, string expected)
{
Scanner s = new(input);

Assert.True(s.ReadDecimal(Fluent.NumberOptions.Integer | Fluent.NumberOptions.AllowExponent, out var result, decimalSeparator: '.'));
Assert.True(decimal.TryParse(expected, NumberStyles.AllowExponent, CultureInfo.InvariantCulture, out _));
Assert.Equal(expected, result);
}
}