Repository navigation
Honour AllowDecimalPoint, AllowExponent and AllowLeadingSign in Parse [patch] - #165
Merged
Merged
Conversation
… [patch] Parse(ReadOnlySpan<char>, NumberStyles, IFormatProvider?) accepted a decimal separator, an exponent and a leading minus whatever the style said, so "1.5" and "1e5" parsed under NumberStyles.Integer and "-5" under NumberStyles.None. Code that validates input through INumberBase<T>.TryParse with a narrow style let through values it had excluded, where every BCL numeric type rejects them. Each of the three is now read only when its flag is set, and the exponent digits are parsed with AllowLeadingSign rather than Integer, so "1e 5" is rejected too. Behaviour change: callers passing NumberStyles.Integer or None now get a FormatException (or false from TryParse) for fractional, exponent or signed text, as with decimal. Fixes #132 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FtvW1bPVVfCTF7fCWpeyRE
|
This was referenced Oct 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #132
Problem
Parse(ReadOnlySpan<char>, NumberStyles, IFormatProvider?)accepted a decimal separator, an exponent and a leading minus sign whatever the style said. As a result,"1.5"and"1e5"parsed underNumberStyles.Integer,"-5"parsed underNumberStyles.None, and"1e 5"parsed underFloat. Callers that validate input throughINumberBase<T>.TryParsewith a narrow style therefore received values they had excluded, which every BCL numeric type rejects.Change (
PreciseNumber.cs)AllowDecimalPoint.e/Eis read only when the style includesAllowExponent. The exponent digits are now parsed withNumberStyles.AllowLeadingSigninstead ofInteger, so whitespace inside the exponent is rejected.ReadLeadingSignnow reads neither sign unless the style includesAllowLeadingSign. Before, it already applied this rule to the positive sign. That includes the ASCII hyphen kept for cultures that use U+2212.Behavior change (for the changelog): callers passing
NumberStyles.IntegerorNonenow get aFormatException, orfalsefromTryParse, for fractional, exponent or signed text.decimalbehaves the same way. The defaultParse(string, provider)andTryParse(string, provider, out)overloads useNumberStyles.Any, so they are unaffected.Tests (
PreciseNumberParseCultureTests)TryParse_AcceptsWhatDecimalAcceptscompares the result withdecimal.TryParsefor every input in the issue's table. It also covers inputs that must keep working:"1.5"withFloat,"-1.5e-3"withFloatandAny,"1,234.5"withAny,"-5"withInteger, and"1e+5"withFloat.Parse_ThrowsForWhatTheStyleDoesNotAllowchecks theParse(throwing) path for the same rejected inputs.Parse_WithoutAStyleStillReadsFractionsExponentsAndSignschecks the default overload.Parse_ReadsACulturesNegativeSignOnlyWhenTheStyleAllowsItchecks the U+2212 sign and the hyphen fallback underNoneandFloat.With the
PreciseNumber.cschange reverted, 8 of these tests fail. With the change, the full suite passes locally: 469 of 469.dotnet format --verify-no-changesreports one IDE0001 finding atPreciseNumber.cs:2078. It is already onmainand this PR doesn't touch that line.🤖 Generated with Claude Code
https://claude.ai/code/session_01FtvW1bPVVfCTF7fCWpeyRE
Generated by Claude Code