Repository navigation
Reset the number of registers after string pattern match - #221
Merged
Merged
Conversation
`set_registers()` used `onig_region_set()` that doesn't change
`num_regs` when the region is already allocated. So
`StringScanner#size` and `#captures` after a string pattern match
reported the number of registers of the previous `Regexp` match:
```ruby
s = StringScanner.new("abc")
s.scan(/(a)(b)/)
s.scan("c")
s.size # => 3 (should be 1)
s.captures # => [nil, nil] (should be [])
```
Use `onig_region_resize()` like `adjust_registers_to_matched()` does.
This is consistent with the JRuby and TruffleRuby implementations.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The focused implementation correctly fixes stale registers and includes appropriate regression coverage.
Review effort: Balanced
Findings: None
What changed in this PR
Fixes stale capture counts after non-Regexp matches by resizing the match region.
Changes:
- Resets match registers to one entry for string matches.
- Adds regression coverage for string,
getch, andget_bytematches.
| File | Description |
|---|---|
ext/strscan/strscan.c |
Resizes the match region before recording a string match. |
test/strscan/test_stringscanner.rb |
Verifies size and captures reset correctly. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
matzbot
pushed a commit
to ruby/ruby
that referenced
this pull request
Oct 6, 2026
match (ruby/strscan#221) `set_registers()` used `onig_region_set()` that doesn't change `num_regs` when the region is already allocated. So `StringScanner#size` and `#captures` after a string pattern match reported the number of registers of the previous `Regexp` match: ```ruby s = StringScanner.new("abc") s.scan(/(a)(b)/) s.scan("c") s.size # => 3 (should be 1) s.captures # => [nil, nil] (should be []) ``` Use `onig_region_resize()` like `adjust_registers_to_matched()` does. This is consistent with the JRuby and TruffleRuby implementations. ruby/strscan@f829eade26 Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.
set_registers()usedonig_region_set()that doesn't changenum_regswhen the region is already allocated. SoStringScanner#sizeand#capturesafter a string pattern match reported the number of registers of the previousRegexpmatch:Use
onig_region_resize()likeadjust_registers_to_matched()does. This is consistent with the JRuby and TruffleRuby implementations.