Fix TryParsePartial under-reporting charsConsumed for leading whitespace - #132270
Merged
Conversation
|
Azure Pipelines: 16 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @agocke |
…pace Co-authored-by: EgorBo <523221+EgorBo@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.
Suppressed comments (1)
src/libraries/System.Private.CoreLib/src/System/Number.Parsing.cs:385
- Comment says "Slice a copy" but
ReadOnlySpan<T>.Slicedoesn't copy data; it creates another span over the same memory. Consider rewording to avoid implying an allocation/copy while preserving the intent of keepingindexrelative to the original input.
// Slice a copy rather than reassigning value, so that index (and thus the number
// of elements reported as consumed) stays relative to the original input.
ReadOnlySpan<TChar> remaining = value.Slice(index);
EgorBo
marked this pull request as ready for review
August 13, 2026 11:35
EgorBo
approved these changes
Aug 13, 2026
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Copilot
AI
changed the title
[WIP] Fix charsConsumed reporting in TryParsePartial for cultures with whitespace
Fix TryParsePartial under-reporting charsConsumed for leading whitespace
Aug 13, 2026
Member
|
PTAL @tannergooding, .NET 11.0 only issue, not-OOB related, just incorrect value returned. |
tannergooding
approved these changes
Aug 13, 2026
tannergooding
enabled auto-merge (squash)
August 13, 2026 13:19
This was referenced Aug 13, 2026
Member
|
/ba-g #132030 and an "artifact already exists" failure |
Member
Member
Member
|
The contract, particularly for mainstream APIs, is that it should return We do have a few outliers that are allowed to return non-zero values, but they tend to be rare/specialized. We typically expect it to be some |
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.
INumberBase<T>.TryParsePartialomitted leading whitespace fromcharsConsumedwhenever the format provider's signs are not exactly"+"/"-"(sv-SE,fi-FI, Arabic/Hebrew locales, ~96 of 786 cultures) and the style allows both leading whitespace and a sign, asNumberStyles.Integerdoes. Callers advancing by the count rewind into the number they already read:Cause
In
Number.TryParseBinaryIntegerStyle, the non-invariant sign branch reassignedvalue = value.Slice(index)and resetindex = 0so the culture's sign could be matched withStartsWith. That discarded the whitespace offset, leaving the finalelementsConsumed = indexrelative to the sliced span. The branch is entered even when no sign is present, so any input with leading whitespace under such a culture is affected — for every binary integer type, since they share this path.Changes
Number.Parsing.cs: slice into a localremainingspan used only for the signStartsWithchecks, keepingvalueandindexin the original coordinate space. The remaining uses ofvalue(value[index],value.Length,ConsumeTrailingNulls, the trailing-whitespace loop,InvalidExit'sindex == value.Length) are shift-equivalent, so behavior is unchanged apart from the count. The non-partial callers (Enum,Version) discardelementsConsumed.Int32Tests/UInt32Tests: addedTryParsePartialtheory rows using aNumberFormatInfowithNegativeSign = "\u2212"(thesv-SEcase) — leading whitespace alone, with+, with the multi-byte negative sign, and combined with trailing whitespace. These fail without the product change.Adjacent, not addressed here
Number.TryParseBigIntegerNumberleaveselementsConsumednon-zero whenNumberToBigIntegerfails after a successfulTryStringToNumber, soBigInteger.TryParsePartial("1.5", NumberStyles.Float, ...)returnsfalsewithcharsConsumed = 3rather than 0. Pre-existing and a separate defect; left for its own change.