-
-
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#partition #3435
String#partition #3435
Conversation
@@ -2200,6 +2200,45 @@ class String | |||
match_result.try &.begin(0) | |||
end | |||
|
|||
# Searches sep or pattern (regexp) in the string, | |||
# and returns the part before it, the match, and the part after it. | |||
# If it is not found, returns two empty strings and str. |
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 should be returns str followed by two empty strings
. Also, the examples below are incorrectly using rpartition
instead of partition
(and the second example has the result of rpatition
)
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.
crap, thats what happens when you try to rush a feature through at midnight
# "hello".rpartition("x") # => {"hello", "", ""} | ||
# ``` | ||
def partition(search : (Char | String)) : Tuple(String, String, String) | ||
pre, mid, post = {"", "", ""} |
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 think this is much simpler:
pre = mid = post = ""
But in fact maybe you could assign these variables in each case, or maybe just return a different tuple in every case branch.
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.
ooooh i like that .... cant believe i missed it ;-)
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.
It's ok, in any case the performance should be the same. You can also do:
pre, mid, post = "", "", ""
That is, you don't need the tuple on the right. But the shortest one, maybe the clearest one, is the chained assignment.
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.
yeah i like the chained version, its very clear
Thank you! ❤️ |
No description provided.