Skip to content

Trim only ASCII white space under AllowLeadingWhite and AllowTrailingWhite [patch] - #182

Open
matt-edmondson wants to merge 1 commit into
mainfrom
fix/181-ascii-only-white-space
Open

matt-edmondson wants to merge 1 commit into
mainfrom
fix/181-ascii-only-white-space

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #181

What was wrong

TrimAllowedWhite trimmed with ReadOnlySpan<char>.TrimStart() and TrimEnd(), which remove anything char.IsWhiteSpace accepts: NBSP, U+202F, U+3000, NEL, LS, PS and the other Unicode space separators. NumberStyles allows only U+0009–U+000D and U+0020 as white space, and decimal, double and int reject the rest. Parse/TryParse therefore accepted input that every BCL numeric type refuses. The realistic case is a stray trailing fr-FR group separator, "1 ", under NumberStyles.Integer.

What changed

  • The trim now uses an explicit set, " \t\n\v\f\r", held in a documented AllowedWhite constant.

Tests

  • Parse_RejectsNonAsciiWhiteSpaceAroundTheNumber covers leading NBSP, trailing U+3000, leading NEL and trailing LS. Each case first asserts that decimal.TryParse rejects the input, then asserts that PreciseNumber.TryParse returns false and Parse throws FormatException.
  • Parse_RejectsATrailingNarrowSpaceGroupSeparatorWhenTheStyleDoesNotAllowThousands is the fr-FR case from the issue. It uses the test file's existing NarrowSpaceGroups format rather than fr-FR, so it also runs where the globalization-invariant mode has no culture data.
  • Parse_TrimsAsciiWhiteSpaceAroundTheNumber shows that space, tab, CR, LF, VT and FF are still trimmed on both sides.
  • With the fix reverted, five cases fail. With the fix, all 559 tests pass, and the Release build is clean.

Generated by Claude Code

…White [patch]

TrimAllowedWhite called ReadOnlySpan<char>.TrimStart()/TrimEnd(), which
strip everything char.IsWhiteSpace accepts: NBSP, U+202F, U+3000, NEL,
LS, PS and the other Unicode space separators. NumberStyles allows only
U+0009-U+000D and U+0020, and decimal, double and int reject the rest, so
PreciseNumber accepted input every BCL numeric type refuses (for example a
stray trailing fr-FR group separator, "1 ", under Integer).

The trim now uses that explicit set.

Fixes #181

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW4JrXevVTQZvqzLBX1
@sonarqubecloud

sonarqubecloud Bot commented Nov 1, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Parse/TryParse accept any Unicode whitespace (NBSP, U+202F, U+3000, NEL) as leading/trailing white space, where decimal/double/int reject it

2 participants