mruby-regexp: add Regexp#names and MatchData#names - #7027
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
🚧 Files skipped from review as they are similar to previous changes (3)
📝 WalkthroughWalkthroughAdded ChangesNamed capture names
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
mrbgems/mruby-regexp/test/regexp.rb (1)
1200-1204: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winAdd a regression test for duplicate capture names.
The PR contract requires duplicate names to appear once. These tests cover distinct names and unnamed patterns, but not duplicates.
Suggested test
assert_equal ["year", "month", "day"], /(?<year>\d+)-(?<month>\d+)-(?<day>\d+)/.names + assert_equal ["tag"], /(?<tag>\w+)-(?<tag>\w+)/.names assert_equal [], /\d+/.names🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@mrbgems/mruby-regexp/test/regexp.rb` around lines 1200 - 1204, Extend the Regexp#names assertions to cover a pattern with duplicate named captures, such as repeated year groups, and verify the returned array contains that name only once. Keep the existing distinct-name and unnamed-pattern assertions unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@mrbgems/mruby-regexp/README.md`:
- Line 57: Correct the documented results for the Regexp#names and
MatchData#names examples: either update both patterns to contain named captures
and retain the named-result examples, or change both expected results to [].
Keep each example internally consistent with its pattern.
---
Nitpick comments:
In `@mrbgems/mruby-regexp/test/regexp.rb`:
- Around line 1200-1204: Extend the Regexp#names assertions to cover a pattern
with duplicate named captures, such as repeated year groups, and verify the
returned array contains that name only once. Keep the existing distinct-name and
unnamed-pattern assertions unchanged.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 787202f7-a4b0-4009-ac5d-856a7bca93bd
📒 Files selected for processing (3)
mrbgems/mruby-regexp/README.mdmrbgems/mruby-regexp/mrblib/regexp.rbmrbgems/mruby-regexp/test/regexp.rb
|
This needs a rebase: #7026 landed first as cfc496a. I merged that one first only because it changes behaviour that already existed, where this adds methods that did not. Both are wanted, and I verified them together before merging anything. The conflict is not in the implementation. Both changes add a line to the same two places:
Keeping both sides is the whole resolution. Applied that way on top of #7022 and #7026, the suite passes (2133 OK, 0 KO, 78 bintests) with no sanitizer report, and the eight rows I compared against CRuby all agree: I checked your claim about key order rather than taking it on trust: One note for whoever picks up duplicate names later, since both of you flagged it and neither change touches it: |
Neither class answered `names`, so code asking a pattern for its capture
names raised `NoMethodError`.
```ruby
/(?<a>x)(?<b>y)/.names # CRuby: ["a", "b"], mruby: NoMethodError
/(?<x>a)/.match("a").names # CRuby: ["x"], mruby: NoMethodError
```
`Regexp#names` reads the keys of the `@named_captures` table that
`regexp_init()` fills. The compiler registers named groups in ascending
group number, so the key order already matches CRuby's.
`MatchData#names` composes that with the existing `MatchData#regexp`
binding.
Both live in mrblib because they need nothing that is not already
exposed to Ruby, so no new C binding is required.
|
Rebased onto
Thanks for checking the key order claim instead of taking it on trust. Agreed on duplicate names: |
Neither
RegexpnorMatchDataanswersnames, so code that asks a pattern for itscapture names raises
NoMethodError.Everything both methods need is already reachable from Ruby.
regexp_init()stores aname to group-number table in the
@named_capturesinstance variable when the patternhas a named group, and
MatchData#regexpis already a C binding.Changes
Regexp#namesreturns the keys of the@named_capturestable, or[]when thepattern has no named group and the instance variable was never set.
regexp_init()inserts in the order the compiler registered the groups, which is ascending group
number, so the key order already matches CRuby's.
MatchData#namesisregexp.names. On an uninitialized receiver such asMatchData.allocateit raises the sameTypeErrorthatMatchData#named_capturesalready raises, because
MatchData#regexpreadsDATA_PTRthroughDATA_GET_PTR().mrbgems/mruby-regexp/README.md.A pattern with duplicate names registers one table entry per group and the Hash keeps
only the last of them, so
namesreports the name once, which is what CRuby answersas well:
Tests
New
assert("Regexp#names")andassert("MatchData#names")inmrbgems/mruby-regexp/test/regexp.rbcover a pattern with named groups and a patternwith none.
rake testpasses.Summary by CodeRabbit
New Features
Regexp#namesto return named capture names in group order.MatchData#namesto return the named captures associated with a match.Documentation
Tests