-
-
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
Work String#starts_with? with regex #5485
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3969,6 +3969,10 @@ class String | |
false | ||
end | ||
|
||
def starts_with?(re : Regex) | ||
!!($~ = re.match_at_byte_index(self, 0, Regex::Options::ANCHORED)) | ||
end | ||
|
||
def ends_with?(str : String) | ||
return false if str.bytesize > bytesize | ||
(to_unsafe + bytesize - str.bytesize).memcmp(str.to_unsafe, str.bytesize) == 0 | ||
|
@@ -3991,6 +3995,10 @@ class String | |
true | ||
end | ||
|
||
def ends_with?(re : Regex) | ||
!!($~ = /#{re}\z/.match(self)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't this require There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needless, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, cool. |
||
end | ||
|
||
# Interpolates *other* into the string using `Kernel#sprintf`. | ||
# | ||
# ``` | ||
|
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.
That's a fairly confusing definition, how about:
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.
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.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 disagree.
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.
Why?
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 just think that this way is easier to understand. Perhaps:
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 is such a small method that I think the original code is fine.