-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
String#index and String#rindex with Regex #3417
Conversation
|
||
describe "with offset" do | ||
assert { "abcDef".index(/[A-Z]/).should eq(3) } | ||
assert { "foobarbaz".index(/ba/, -5).should eq(6) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, fix the tabulation
c8af1a0
to
375cfad
Compare
Fixed, also improved the readability of the code (thanks @RX14). |
375cfad
to
21960f8
Compare
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 } |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
21960f8
to
d3381a8
Compare
offset += size if offset < 0 | ||
return nil unless 0 <= offset <= size | ||
|
||
matchlist = self[0, self.size - offset].scan(search) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
d3381a8
to
74d096f
Compare
offset += size if offset < 0 | ||
return nil unless 0 <= offset <= size | ||
|
||
if index = self.match(search, offset).try &.begin |
There was a problem hiding this comment.
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 :-)
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed as well!
Includes specs.
74d096f
to
4c8da90
Compare
@zatherz Thank you for your contribution and for your patience! :-) |
Includes specs.
Allows you to do things like
"ab d 64 aef".index(/\d+/)
.