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

Add Regex::MatchData#captures, named_captures, to_a, to_h #3783

Conversation

makenowjust
Copy link
Contributor

@makenowjust makenowjust commented Dec 26, 2016

to_a and to_h are just converting methods, so they follow such semantics: self[idx] == self.to_a[idx] and self[idx] == self.to_h[idx] && self[key] == self.to_h[key].

But captures and named_captures are not. captures contains only unnamed captures, and named_captures contains only named captures. Those methods look like Ruby.

@Sija
Copy link
Contributor

Sija commented Mar 24, 2017

Ping? Any chances on getting it merged before next release? 🎉

`to_a` and `to_h` are just converting methods, so they follow such
semantics: `self[idx] == self.to_a[idx]` and
`self[idx] == self.to_h[idx] && self[key] == self.to_h[key]`.

But `captures` and `named_captures` are not. `captures` contains only
unnamed captures, and `named_captures` contains only named captures.
Those methods looks like Ruby.
@makenowjust makenowjust force-pushed the feature/regex-matchdata/captures-named-captures branch from c760efe to 635545b Compare March 25, 2017 00:37
@makenowjust
Copy link
Contributor Author

Thanks @Sija. I rebased this branch to resolve conflict for the first step to merge.

@Sija
Copy link
Contributor

Sija commented Mar 27, 2017

@crystal-lang lets get this merged, please :)

@Sija
Copy link
Contributor

Sija commented Apr 15, 2017

@asterite Is there anything blocking this PR from merging? These are useful additions (as a plus bringing more feature-parity with Ruby).

@Sija
Copy link
Contributor

Sija commented May 1, 2017

@asterite ping?

@Sija
Copy link
Contributor

Sija commented May 12, 2017

Hi, is there anything preventing this PR from being merged? Almost half-year passed since its inception with completely no response from the core team... Bad PR karma :/

@makenowjust
Copy link
Contributor Author

I didn't intend this, however it was counter PR against #3653. So I should explain pros/cons of this PR.

Pros:

  1. It has simple principles such explained in above comment: self[idx] == self.to_a[idx] and self[idx] == self.to_h[idx] && self[key] == self.to_h[key]. It is useful to pass MatchData as Array or Hash to a restricted method. I think Enhancement to Regex::MatchData methods #3653's to_a and to_h are too complex and I don't know when such a behavior is useful. (NOTE: Both behaviors are different from Ruby.)
  2. Its implementations are memory efficient enough. (Maybe, if you found my wrong, I'd be welcome your report.)

Cons:

  1. Its spec is poorer than Enhancement to Regex::MatchData methods #3653's. Should I write more specs?

@mverzilli
Copy link

Hey, sorry for the delay. What's preventing this from getting merged is that we need to compare it with #3653 and review the implementation to ensure it doesn't introduce performance issues, it's well documented etc.

@makenowjust
Copy link
Contributor Author

@mverzilli What did you find an issue?

@Sija
Copy link
Contributor

Sija commented Jun 11, 2017

Bump @mverzilli @asterite

@asterite
Copy link
Member

self[i] should be self[i]?, the other PR might be better

@Sija
Copy link
Contributor

Sija commented Jun 11, 2017

@asterite Wouldn't (0..size) guarantee existence of given i index?

@asterite
Copy link
Member

@Sija No:

# Ruby
m = "foo".match(/(f)(x)?/)
p m.captures # => ["f", nil]
p m.to_a # => ["f", "f", nil]

m = "foo".match(/(f)(?<name>x)?/)
p m.named_captures # => {"name"=>nil}
p m.to_a # => ["f", nil]

Optional groups are in the MatchData as nil, and you get them with []?. Doing it with [] raises. With this PR if I try this:

m = "foo".match(/(f)(x)?/).not_nil!
p m.captures
p m.to_a

m = "foo".match(/(f)(?<name>x)?/).not_nil!
p m.named_captures
p m.to_a

Both captures and named_captures raise an exception, when they should never raise. Using self[i]? gives exactly the same result as Ruby.

I'll probably implement this myself soon, don't worry :-)

@makenowjust
Copy link
Contributor Author

@asterite I see. I didn't consider this case. My note PC is in my house today. I'd like you to implement it for fast merge. Thank you!

@Sija
Copy link
Contributor

Sija commented Jun 12, 2017

@asterite Of course, totally forgot about optional groups... thanks for reminder! :) Regarding this PR vs the other one, personally I prefer this one due to familiar API (Ruby), just my 2¢.

@asterite
Copy link
Member

@makenowjust OK, I'll wait. Take your time, there's no rush at all. Thank you!

@makenowjust
Copy link
Contributor Author

@asterite @Sija Updated this PR 😄

@asterite asterite merged commit 61f21ae into crystal-lang:master Jun 12, 2017
@asterite
Copy link
Member

@makenowjust Excellent, thank you! 🙇

@asterite asterite added this to the Next milestone Jun 12, 2017
@makenowjust makenowjust deleted the feature/regex-matchdata/captures-named-captures branch June 12, 2017 15:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants