Repository navigation
Honour AllowParentheses, AllowTrailingSign and AllowCurrencySymbol in Parse [patch] - #184
Open
matt-edmondson wants to merge 2 commits into
Open
matt-edmondson wants to merge 2 commits into
matt-edmondson wants to merge 2 commits into
Conversation
… Parse [patch] Parse took these styles, and Any and Currency include them, but never read them, so "(1.5)", "1.5-" and "$1,234.50" threw. Strip the parentheses, a trailing sign and a leading or trailing currency symbol before the digits, and read the currency separators once a symbol is present. Fixes #152 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vfJW4sAxuwubJA58cavro
SonarCloud S3776 counted its cognitive complexity at 27 against a limit of 15. Behaviour is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vfJW4sAxuwubJA58cavro
|
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.



Summary
Parse(span, style, provider)accepted these three styles, andAnyandCurrencyinclude them, but it never read them. So"(1.5)","1.5-","¤1.5"and"$1,234.50"all threwFormatException. Generic code that callsINumberBase<T>.Parse(s, NumberStyles.Currency, provider)failed only forPreciseNumber.A new
StripAffixesstep runs before the existing digit loop, which is otherwise unchanged:AllowParentheses:(…)around the number means negative. A sign inside the parentheses is rejected.AllowTrailingSign: a negative or positive sign after the digits, including after an exponent (1.5E2-). The ASCII hyphen is accepted, as it already is for a leading sign.AllowCurrencySymbol:CurrencySymbolbefore or after the number. The sign and the symbol can come in either order, as in-$1.5,$-1.5and1,5 €-. Once a symbol has been read,CurrencyDecimalSeparatorandCurrencyGroupSeparatorreplace the number separators.At most one sign and one symbol are read. Under a style that doesn't allow a construct, it is still rejected as a non-digit.
One deliberate difference from
decimal: a space before or after the symbol may be U+0020, U+00A0 or U+202F. Windows' de-DE and fr-FR write1.234,50 €with a no-break space.decimaltakes that only from a culture whose own separators use U+00A0. This repo already lets a typed space stand in for a no-break-space group separator, so this follows the same rule.TestParseAcceptsANoBreakSpaceBeforeACurrencySymbolpins it.Tests
PreciseNumberParseAffixTestsadds:$-…,-… €and… €-) parses to whatdecimal.Parsereturns for the same text, style and format, through bothParseandTryParse. The formats are hand-built (Dollars,Euros), likePreciseNumberParseCultureTests, so the cases don't depend on ICU data.decimalfirst, so the expectation is the BCL's. They are:(-1.5)-1.5-$1.5$()$-16 of the accept cases fail on
main. The reject cases pass both before and after; they guard against accepting too much. Full suite: 573 passed, 0 failed.Note
Open PR #182 also edits
Parse, in the white-space trimming just above this change. The two touch neighbouring lines, so whichever lands second may need a small rebase.Fixes #152
Generated by Claude Code