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

Work String#starts_with? with regex #5485

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions spec/std/string_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,8 @@ describe "String" do
it { "foobar".starts_with?("").should be_true }
it { "foobar".starts_with?("foobarbaz").should be_false }
it { "foobar".starts_with?("foox").should be_false }
it { "foobar".starts_with?(/foo/).should be_true }
it { "foobar".starts_with?(/bar/).should be_false }
it { "foobar".starts_with?('f').should be_true }
it { "foobar".starts_with?('g').should be_false }
it { "よし".starts_with?('よ').should be_true }
Expand All @@ -936,6 +938,8 @@ describe "String" do
it { "foobar".ends_with?("").should be_true }
it { "foobar".ends_with?("foobarbaz").should be_false }
it { "foobar".ends_with?("xbar").should be_false }
it { "foobar".ends_with?(/bar/).should be_true }
it { "foobar".ends_with?(/foo|baz/).should be_false }
it { "foobar".ends_with?('r').should be_true }
it { "foobar".ends_with?('x').should be_false }
it { "よし".ends_with?('し').should be_true }
Expand Down
8 changes: 8 additions & 0 deletions src/string.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3969,6 +3969,10 @@ class String
false
end

def starts_with?(re : Regex)
!!($~ = re.match_at_byte_index(self, 0, Regex::Options::ANCHORED))
Copy link
Contributor

Choose a reason for hiding this comment

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

That's a fairly confusing definition, how about:

def starts_with?(re : Regex)
  match_data = $~ = re.match_at_byte_index(self, 0, Regex::Options::ANCHORED)

  match_data != nil
end

Copy link
Contributor Author

@makenowjust makenowjust Dec 30, 2017

Choose a reason for hiding this comment

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

Your code has another confusability. It seems match_data is redundant and last statement can be replaced with $~ != nil, unfortunately it cannot. It is hard to explain this reason, so I cannot accept your suggestion. Please approve.

Copy link
Contributor

Choose a reason for hiding this comment

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

I disagree.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why?

Copy link
Contributor

Choose a reason for hiding this comment

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

I just think that this way is easier to understand. Perhaps:

match_data = re.match_at_byte_index(self, 0, Regex::Options::ANCHORED)

$~ = match_data
match_data != nil

Copy link
Member

Choose a reason for hiding this comment

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

This is such a small method that I think the original code is fine.

end

def ends_with?(str : String)
return false if str.bytesize > bytesize
(to_unsafe + bytesize - str.bytesize).memcmp(str.to_unsafe, str.bytesize) == 0
Expand All @@ -3991,6 +3995,10 @@ class String
true
end

def ends_with?(re : Regex)
!!($~ = /#{re}\z/.match(self))
Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't this require /(?:#{re})\z/?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Needless, re.to_s yields enclosed string.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, cool.

end

# Interpolates *other* into the string using `Kernel#sprintf`.
#
# ```
Expand Down