Skip to content

Commit 3903f4c

Browse files
authored
Consume decimal separator when accepted (#230)
1 parent 9265bce commit 3903f4c

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

src/Parlot/Scanner.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,11 @@ public bool ReadDecimal(bool allowLeadingSign, bool allowDecimalSeparator, bool
201201

202202
if (!ReadInteger(out number))
203203
{
204-
Cursor.ResetPosition(beforeDecimalSeparator);
205-
206204
// A decimal separator must be followed by a number if there is no integral part, e.g. `[NaN].[NaN]`
207205
if (numberIsEmpty)
208206
{
207+
Cursor.ResetPosition(beforeDecimalSeparator);
208+
209209
return false;
210210
}
211211

test/Parlot.Tests/ScannerTests.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Parlot.Tests.Calc;
22
using System;
33
using System.Buffers;
4-
4+
using System.Globalization;
55
using Xunit;
66

77
namespace Parlot.Tests;
@@ -401,14 +401,34 @@ public void ShouldReadDecimalWithGroupSeparator(string input, string expected)
401401
[Theory]
402402
[InlineData("123.456", "123.456")]
403403
[InlineData("123.456a", "123.456")]
404-
[InlineData("123.a", "123")]
404+
[InlineData("123.a", "123.")]
405405
[InlineData("123.456.789", "123.456")]
406-
[InlineData("123.", "123")]
406+
[InlineData("123.", "123.")]
407407
public void ShouldReadDecimalWithDecimalSeparator(string input, string expected)
408408
{
409409
Scanner s = new(input);
410410

411411
Assert.True(s.ReadDecimal(Fluent.NumberOptions.AllowDecimalSeparator, out var result, decimalSeparator: '.'));
412+
Assert.True(decimal.TryParse(expected, NumberStyles.Float, CultureInfo.InvariantCulture, out _));
413+
Assert.Equal(expected, result);
414+
}
415+
416+
[Theory]
417+
[InlineData("123.456", "123")]
418+
[InlineData("123.456a", "123")]
419+
[InlineData("123.a", "123")]
420+
[InlineData("123.456.789", "123")]
421+
[InlineData("123.", "123")]
422+
[InlineData("123.e", "123")]
423+
[InlineData("123.e1", "123")]
424+
[InlineData("123e", "123")]
425+
[InlineData("123e1", "123e1")]
426+
public void ShouldReadIntegerWithExponent(string input, string expected)
427+
{
428+
Scanner s = new(input);
429+
430+
Assert.True(s.ReadDecimal(Fluent.NumberOptions.Integer | Fluent.NumberOptions.AllowExponent, out var result, decimalSeparator: '.'));
431+
Assert.True(decimal.TryParse(expected, NumberStyles.AllowExponent, CultureInfo.InvariantCulture, out _));
412432
Assert.Equal(expected, result);
413433
}
414434
}

0 commit comments

Comments
 (0)