Repository navigation
Fix Regex.EnumerateSplits ranges for capture groups, RightToLeft and empty matches - #642
Merged
Merged
Conversation
This was referenced Oct 12, 2026
This was referenced Oct 14, 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.
Bug
The
ValueSplitEnumeratorpolyfill (below net9.0) was built onRegex.Splitand then tried to map the returned strings back to ranges. That goes wrong wheneverSplitdoes not correspond one-to-one with the gaps between matches:Splitincludes captured text in its result, sonew Regex("(-)").EnumerateSplits("a-b")yielded0..1 2..3 3..4(the last range is outside the 3 character input) instead of0..1 2..3.RegexOptions.RightToLeft:","over"a,b,c"yielded0..1 1..2 2..3instead of4..5 2..3 0..1.RightToLeftcame back in left-to-right order.Fix
ValueSplitEnumeratornow walks the matches lazily withMatch/NextMatchand yields the range between consecutive matches, honouringcount(the final permitted range is the remainder of the input) andstartat, and walking from the end of the input forRightToLeft. Overloads without an explicitstartatnow start at the end of the input forRightToLeftregexes, as the BCL does.Test
Added to
PolyfillTests_Regex.cs, asserting the exact ranges (expected values taken from the real BCL):EnumerateSplits_CaptureGroups_NotIncluded,EnumerateSplits_RightToLeft,EnumerateSplits_CountAndStartat_RangesandEnumerateSplits_EmptyMatches. Three of the four failed on net48 and net8.0 before the fix; all pass on net48 and net8.0 (polyfill) and net10.0 (BCL) after it.Note:
count: 1is deliberately not covered. The BCL currently throwsArgumentOutOfRangeExceptionfromMoveNextin that case, which looks like a runtime bug; the polyfill returns the whole input as a single range.