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

Pattern matching support for RE2::MatchData #57

Closed
mudge opened this issue Oct 21, 2022 · 1 comment
Closed

Pattern matching support for RE2::MatchData #57

mudge opened this issue Oct 21, 2022 · 1 comment
Assignees
Labels

Comments

@mudge
Copy link
Owner

mudge commented Oct 21, 2022

As per ruby/ruby#6216, add support for pattern matching RE2::MatchData objects by implementing deconstruct and deconstruct_keys.

See https://docs.ruby-lang.org/en/3.0/syntax/pattern_matching_rdoc.html#label-Matching+non-primitive+objects-3A+deconstruct+and+deconstruct_keys

@mudge mudge added the feature label Oct 21, 2022
@mudge mudge self-assigned this Oct 21, 2022
mudge added a commit that referenced this issue Oct 22, 2022
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
mudge added a commit that referenced this issue Oct 22, 2022
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 added a commit that referenced this issue Oct 22, 2022
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
mudge added a commit that referenced this issue Oct 22, 2022
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 added a commit that referenced this issue Oct 22, 2022
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 added a commit that referenced this issue Oct 22, 2022
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
mudge added a commit that referenced this issue Oct 22, 2022
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
Copy link
Owner Author

mudge commented Oct 22, 2022

Added in #58

@mudge mudge closed this as completed Oct 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant