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
21 changes: 21 additions & 0 deletions PreciseNumber.Test/PreciseNumberParseTrailingNulTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class PreciseNumberParseTrailingNulTests
[DataRow("1e3 \0", NumberStyles.Float)]
[DataRow("12345\0\0\0\0\0\0\0\0", NumberStyles.Integer)]
[DataRow("-0.25\0", NumberStyles.Number)]
[DataRow("3e1\0", NumberStyles.Float)]
[DataRow("3e1\0\0", NumberStyles.Float)]
public void ParseIgnoresTrailingNulsAsDecimalDoes(string text, NumberStyles style)
{
CultureInfo culture = CultureInfo.InvariantCulture;
Expand Down Expand Up @@ -51,4 +53,23 @@ public void ParseStillRejectsNulsThatAreNotTheLastCharacters(string text, Number
Assert.IsFalse(PreciseNumber.TryParse(text.AsSpan(), style, culture, out _));
Assert.ThrowsExactly<FormatException>(() => PreciseNumber.Parse(text, style, culture));
}

[TestMethod]
[DataRow("3e1\0 ", NumberStyles.Float)]
[DataRow("3E0\0 ", NumberStyles.Any)]
[DataRow("3e1\0-", NumberStyles.Any)]
[DataRow("3e1\0\0-", NumberStyles.Any)]
[DataRow("3e1\0$", NumberStyles.Any)]
public void ParseRejectsANulInsideTheExponent(string text, NumberStyles style)
{
// Invariant, but with en-US's currency symbol, so "$" is read as one.
NumberFormatInfo culture = (NumberFormatInfo)NumberFormatInfo.InvariantInfo.Clone();
culture.CurrencySymbol = "$";
Assert.IsFalse(decimal.TryParse(text, style, culture, out _), "The case must be one decimal rejects.");
Assert.IsFalse(double.TryParse(text, style, culture, out _), "The case must be one double rejects.");

Assert.IsFalse(PreciseNumber.TryParse(text, style, culture, out _));
Assert.IsFalse(PreciseNumber.TryParse(text.AsSpan(), style, culture, out _));
Assert.ThrowsExactly<FormatException>(() => PreciseNumber.Parse(text, style, culture));
}
}
7 changes: 7 additions & 0 deletions PreciseNumber/PreciseNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,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 759 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 759 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 @@ -1217,7 +1217,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 1220 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 30 to the 15 allowed.

Check warning on line 1220 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 1220 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 30 to the 15 allowed.

Check warning on line 1220 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 @@ -1277,7 +1277,7 @@
}

hasDecimal = true;
i += decimalLength - 1;

Check warning on line 1280 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 1280 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 All @@ -1285,7 +1285,7 @@
int groupLength = allowThousands && !hasDecimal && digitCount > 0 ? MatchSeparator(s[i..], groupSeparator, numberGroupSeparator) : 0;
if (groupLength > 0)
{
i += groupLength - 1;

Check warning on line 1288 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 1288 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 @@ -1409,6 +1409,13 @@
long exponent = 0;
if (hasExponent)
{
// long.Parse ignores trailing NULs by itself, so a NUL the parse kept because something follows
// it, as in "3e1\0 " once the white space is trimmed, would otherwise slip through here.
if (exponentText.Contains('\0'))
{
throw new FormatException(InvalidFormatMessage);
}

// Only a sign may precede its digits: NumberStyles.Integer would also let white space through,
// accepting "1e 5" where every BCL numeric type rejects it. A zero is zero whatever its written
// exponent, as for decimal and double, so only a nonzero value can overflow here.
Expand Down
Loading