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#partition #3435

Merged
merged 6 commits into from
Oct 20, 2016
Merged

String#partition #3435

merged 6 commits into from
Oct 20, 2016

Conversation

johnjansen
Copy link
Contributor

No description provided.

@johnjansen johnjansen changed the title String partition String#partition Oct 18, 2016
@@ -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.
Copy link
Member

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)

Copy link
Contributor Author

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 = {"", "", ""}
Copy link
Member

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.

Copy link
Contributor Author

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 ;-)

Copy link
Member

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.

Copy link
Contributor Author

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

@asterite
Copy link
Member

Thank you! ❤️

@asterite asterite merged commit 9cdc89e into crystal-lang:master Oct 20, 2016
@asterite asterite added this to the 0.20.0 milestone Oct 20, 2016
@johnjansen johnjansen deleted the string-partition branch October 21, 2016 00:09
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

2 participants