Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support pattern matching #58

Merged
merged 2 commits into from
Oct 22, 2022
Merged

Support pattern matching #58

merged 2 commits into from
Oct 22, 2022

Conversation

mudge
Copy link
Owner

@mudge mudge commented Oct 22, 2022

GitHub: #57

In order to support pattern matching RE2::MatchData objects, implement:

  • RE2::MatchData#deconstruct
  • RE2::MatchData#deconstruct_keys

GitHub: #57

So that RE2::MatchData can be used with Ruby's pattern matching
(https://docs.ruby-lang.org/en/3.0/syntax/pattern_matching_rdoc.html),
implement a deconstruct method that returns only the capturing groups.

This enables the following example:

    case RE2('(\d)(\d)?').match("1")
    in x, y
      puts "Matched #{x} #{y}"
    end
GitHub: #57

In order to support pattern matching in Ruby, add a deconstruct_keys
method to RE2::MatchData that will return all named matches if given nil
or a subset of matches if given an array of capturing group names as
symbols.

To match [Ruby's implementation of
this](ruby/ruby#6216) there are a few edge
cases:

1. If the number of keys given exceeds the number of named capturing
   groups, immediately return an empty hash.
2. Each key is added to the hash in turn and the first invalid key will
   cause the hash to be returned so it is possible to get a partial set
   of named captures.
@mudge mudge merged commit cee5500 into main Oct 22, 2022
@mudge mudge deleted the pattern-matching branch October 22, 2022 07:29
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.

1 participant