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

String#index and String#rindex with Regex #3417

Merged
merged 1 commit into from
Oct 14, 2016

Conversation

zatherz
Copy link
Contributor

@zatherz zatherz commented Oct 12, 2016

Includes specs.
Allows you to do things like "ab d 64 aef".index(/\d+/).


describe "with offset" do
assert { "abcDef".index(/[A-Z]/).should eq(3) }
assert { "foobarbaz".index(/ba/, -5).should eq(6) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, fix the tabulation

@zatherz zatherz force-pushed the feature/string_regex_index branch 2 times, most recently from c8af1a0 to 375cfad Compare October 12, 2016 22:22
@zatherz
Copy link
Contributor Author

zatherz commented Oct 12, 2016

Fixed, also improved the readability of the code (thanks @RX14).

@zatherz zatherz force-pushed the feature/string_regex_index branch from 375cfad to 21960f8 Compare October 12, 2016 22:50
@zatherz zatherz changed the title String#index with Regex String#index and String#rindex with Regex Oct 12, 2016
@zatherz
Copy link
Contributor Author

zatherz commented Oct 12, 2016

Added String#rindex with Regex.


describe "with offset" do
assert { "bbbb".rindex(/b/, 1).should eq(2) }
assert { "bbbb".rindex(/b/, -1235).should be_nil }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please run crystal tool format to ensure the formatting is correct.

See Use Crystal's code formatter section in the docs:

https://crystal-lang.org/docs/conventions/documenting_code.html

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@zatherz zatherz force-pushed the feature/string_regex_index branch from 21960f8 to d3381a8 Compare October 13, 2016 14:13
offset += size if offset < 0
return nil unless 0 <= offset <= size

matchlist = self[0, self.size - offset].scan(search)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be done without storing all the matches in an array. Just use the block form and save the last match.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

offset += size if offset < 0
return nil unless 0 <= offset <= size

if matchdata = self.match(search, offset)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can simply be match(search, offset).try &.begin(0)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@zatherz zatherz force-pushed the feature/string_regex_index branch from d3381a8 to 74d096f Compare October 13, 2016 14:53
offset += size if offset < 0
return nil unless 0 <= offset <= size

if index = self.match(search, offset).try &.begin
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, the if is not needed, because try already returns nil. So just self.match(search, offset).try &.begin at this point with nothing else following it is good :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed! Sorry for forgetting about that.

matchresult = matchdata
end

if index = matchresult.try &.begin(0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, matchresult.try &.begin is good

Also, we prefer names like match_result and match_data instead of matchresult and matchdata, which are slightly harder to read

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed as well!

@asterite
Copy link
Member

@zatherz Thank you for your contribution and for your patience! :-)

@asterite asterite merged commit 3b26981 into crystal-lang:master Oct 14, 2016
@asterite asterite added this to the 0.20.0 milestone Oct 15, 2016
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