Skip to content
Open
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
37 changes: 37 additions & 0 deletions PreciseNumber.Test/PreciseNumberParseCultureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,41 @@ public void Parse_RejectsATypedSpaceWhenTheStyleDoesNotAllowThousands()
Assert.IsFalse(PreciseNumber.TryParse("1 234", NumberStyles.Float, NarrowSpaceGroups, out _));
Assert.IsFalse(PreciseNumber.TryParse("1 234", NumberStyles.Float, NoBreakSpaceGroups, out _));
}

[TestMethod]
[DataRow("\u00A05", NumberStyles.Integer)]
[DataRow("5\u3000", NumberStyles.AllowTrailingWhite)]
[DataRow("\u00855", NumberStyles.Float)]
[DataRow("5\u2028", NumberStyles.Float)]
public void Parse_RejectsNonAsciiWhiteSpaceAroundTheNumber(string input, NumberStyles style)
{
Assert.IsFalse(decimal.TryParse(input, style, CultureInfo.InvariantCulture, out _), "The BCL should reject this input too.");
Assert.IsFalse(PreciseNumber.TryParse(input, style, CultureInfo.InvariantCulture, out _));
Assert.ThrowsExactly<FormatException>(() => PreciseNumber.Parse(input, style, CultureInfo.InvariantCulture));
}

[TestMethod]
public void Parse_RejectsATrailingNarrowSpaceGroupSeparatorWhenTheStyleDoesNotAllowThousands()
{
// Shaped like fr-FR, whose group separator is U+202F: a stray one after the digits is not white space.
Assert.IsFalse(decimal.TryParse("1\u202F", NumberStyles.Integer, NarrowSpaceGroups, out _), "The BCL should reject this input too.");
Assert.IsFalse(PreciseNumber.TryParse("1\u202F", NumberStyles.Integer, NarrowSpaceGroups, out _));
Assert.ThrowsExactly<FormatException>(() => PreciseNumber.Parse("1\u202F", NumberStyles.Integer, NarrowSpaceGroups));
}

[TestMethod]
[DataRow(" ")]
[DataRow("\t")]
[DataRow("\r")]
[DataRow("\n")]
[DataRow("\v")]
[DataRow("\f")]
public void Parse_TrimsAsciiWhiteSpaceAroundTheNumber(string white)
{
string input = white + "5" + white;

Assert.IsTrue(PreciseNumber.TryParse(input, NumberStyles.Integer, CultureInfo.InvariantCulture, out PreciseNumber result));
Assert.AreEqual(P("5"), result);
Assert.AreEqual(P("5"), PreciseNumber.Parse(input, NumberStyles.Integer, CultureInfo.InvariantCulture));
}
}
11 changes: 9 additions & 2 deletions PreciseNumber/PreciseNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@
/// A fixed precision such as <c>E15</c> rounds values that need 17 digits, which turns
/// <see cref="double.MaxValue"/> into a number that converts back to infinity.
/// </remarks>
internal static string GetStringFormatForFloatType<TFloat>()

Check warning on line 747 in PreciseNumber/PreciseNumber.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Remove this method and declare a constant for this value.

Check warning on line 747 in PreciseNumber/PreciseNumber.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Remove this method and declare a constant for this value.
where TFloat : INumber<TFloat>
=> "R";

Expand Down Expand Up @@ -1205,7 +1205,7 @@
public static PreciseNumber MinMagnitudeNumber(PreciseNumber x, PreciseNumber y) => MinMagnitude(x, y);

/// <inheritdoc/>
public static PreciseNumber Parse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider? provider)

Check warning on line 1208 in PreciseNumber/PreciseNumber.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 28 to the 15 allowed.

Check warning on line 1208 in PreciseNumber/PreciseNumber.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

All 'Parse' method overloads should be adjacent.

Check warning on line 1208 in PreciseNumber/PreciseNumber.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 28 to the 15 allowed.

Check warning on line 1208 in PreciseNumber/PreciseNumber.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

All 'Parse' method overloads should be adjacent.
{
// Read the separator and signs from the same culture TryFormat writes them with, so that
// Parse(x.ToString(p), p) round-trips for every provider.
Expand Down Expand Up @@ -1263,14 +1263,14 @@
}

hasDecimal = true;
i += decimalSeparator.Length - 1;

Check warning on line 1266 in PreciseNumber/PreciseNumber.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Do not update the stop condition variable 'i' in the body of the for loop.

Check warning on line 1266 in PreciseNumber/PreciseNumber.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Do not update the stop condition variable 'i' in the body of the for loop.
continue;
}

// Group separators may only sit between integral digits.
if (allowThousands && !hasDecimal && digitCount > 0 && StartsWithSeparator(s[i..], groupSeparator))
{
i += groupSeparator.Length - 1;

Check warning on line 1273 in PreciseNumber/PreciseNumber.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Do not update the stop condition variable 'i' in the body of the for loop.

Check warning on line 1273 in PreciseNumber/PreciseNumber.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Do not update the stop condition variable 'i' in the body of the for loop.
continue;
}

Expand Down Expand Up @@ -1391,17 +1391,24 @@
return new((int)exponent, significand);
}

/// <summary>
/// The white space that <see cref="NumberStyles.AllowLeadingWhite"/> and <see cref="NumberStyles.AllowTrailingWhite"/>
/// permit: U+0009 to U+000D and U+0020, as for the BCL numeric types. Other Unicode white space, such as a no-break
/// space or NEL, is not trimmed.
/// </summary>
private const string AllowedWhite = " \t\n\v\f\r";

/// <summary>Trims the leading and trailing white space that <paramref name="style"/> allows.</summary>
private static ReadOnlySpan<char> TrimAllowedWhite(ReadOnlySpan<char> s, NumberStyles style)
{
if (style.HasFlag(NumberStyles.AllowLeadingWhite))
{
s = s.TrimStart();
s = s.TrimStart(AllowedWhite.AsSpan());
}

if (style.HasFlag(NumberStyles.AllowTrailingWhite))
{
s = s.TrimEnd();
s = s.TrimEnd(AllowedWhite.AsSpan());
}

return s;
Expand Down
Loading