Repository navigation
Read white space around a sign or currency symbol as the BCL does [patch] - #191
Merged
Merged
Conversation
…tch]
Parse trimmed no white space between the digits and a trailing sign, or
after a leading sign, so a value written with NumberNegativePattern 2
("- 1,234.50") or 4 ("1,234.50 -") couldn't be read back with the same
format, and "5 -", "$\t5" and "$- 5" failed where decimal reads them.
An ordinary space next to a currency symbol was taken under any style,
so "$ 5" parsed though no white-space flag allowed it.
White space is now taken where the BCL takes it: among the trailing
affixes under AllowTrailingWhite, and under AllowLeadingWhite after a
leading currency symbol, or after a sign or opening parenthesis once a
currency symbol has been read or when the negative pattern is 2. The
rendered no-break spaces next to a currency symbol stay accepted under
any style.
Fixes #189
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW2U6qwK2oTLt2cfE7k
SonarCloud put StripLeadingAffixes' cognitive complexity at 17 against an allowance of 15 once it trimmed white space after a sign. The trim after a sign and the choice of what to trim beside a currency symbol are now their own helpers; behaviour is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vfJW2U6qwK2oTLt2cfE7k
…sign # Conflicts: # PreciseNumber.Test/PreciseNumberParseAffixTests.cs
|
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 #189
What was wrong
StripLeadingAffixesandStripTrailingAffixestrimmed no white space after a leading sign or before a trailing sign. A value formatted withNumberNegativePattern2 (- 1,234.50) or 4 (1,234.50 -) therefore could not be parsed back with the same format provider.decimalreads"5 -","$\t5"and"$- 5", but these were rejected here. In the other direction,CurrencySymbolSpacesincluded an ordinary space. That made"$ 5"parse underAllowCurrencySymbol | AllowLeadingSign, where the BCL rejects it.Change
The rules follow
Number.TryParseNumber's affix loops, and I checked each case againstdecimalbefore encoding it.AllowTrailingWhite, white space is trimmed after removing a trailing sign or currency symbol.AllowLeadingWhite, white space is trimmed after a leading currency symbol. After a sign or opening parenthesis, it is trimmed only if a currency symbol has already been read orNumberNegativePattern == 2. This is why- 5is rejected under the invariant culture and accepted under pattern 2, as in the BCL.TrimAllowedWhitealready uses.CurrencySymbolSpacesnow holds only U+00A0 and U+202F. These are still accepted next to a currency symbol under any style, so the existingTestParseAcceptsANoBreakSpaceBeforeACurrencySymbolleniency is kept.Tests
New rows in
PreciseNumberParseAffixTests. Each case first asserts whatdecimaldoes with the same text, style and format.decimal:"5 -","5\t-"," 5 - ","5 +"and"1,234.50 -"underNumber"- 5"and"- 1,234.50"under pattern 2"( 5)"and"(5 )"with parentheses"$\t5","5\t$","$- 5","$ - 5","-$ 5","5 $ -"and"5 - $"underCurrencydecimal:"- 5"and"+ 5"under the invariant culture withNumber"- $5"underCurrency"$ 5","$- 5"and"-$ 5"underAllowCurrencySymbol | AllowLeadingSign"5 $"underAllowCurrencySymbol | AllowTrailingSignTestParseReadsBackEveryNumberNegativePatternreadsToString("N2", nf)back for patterns 0–4. Pattern 0 writes(n), which onlyAllowParenthesesreads, fordecimalas well, so that case adds the flag.With the library change stashed, 21 of the 57 affix tests fail. With it, the full suite passes: 618/618.
Generated by Claude Code