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

Implement String#split with block #3702

Merged
merged 5 commits into from Dec 22, 2016
Merged

Implement String#split with block #3702

merged 5 commits into from Dec 22, 2016

Conversation

splattael
Copy link
Contributor

Hi,

this PR enhances all flavors of String#split with a block.

After this PR, you can pass a block to String#split:

"foo bar".split { |chunk| p chunk }
"foo bar".split(' ') { |chunk| p chunk }
"foo bar".split(" ") { |chunk| p chunk }
"foo bar".split(/ /) { |chunk| p chunk }

Additional tests were not added because all non-block versions of String#split exercise String#split(..., &block). (Only one test for an edge case was added, though)

Kind regards,
Peter

@kostya
Copy link
Contributor

kostya commented Dec 15, 2016

#2541

@splattael
Copy link
Contributor Author

@kostya Dammit, should have searched for it before ;)

Sorry, for the noise.

(I am actually using the block version to speed-up things...)

@splattael splattael closed this Dec 15, 2016
@kostya
Copy link
Contributor

kostya commented Dec 15, 2016

it was not merged, and i also want a version which yield slice, for speed

@splattael
Copy link
Contributor Author

@kostya Yeah, my version yields Strings which is slower than Slice but still faster than "foo bar".split(' ').each { ... }.

Anyhow, if it's not wanted, I'll keep my monkey patch^W^Wextension for now ;)

@@ -738,6 +738,7 @@ describe "String" do
assert { " foo bar".split(' ').should eq(["", "", "", "foo", "", "bar"]) }
assert { " foo bar\n\t baz ".split(' ').should eq(["", "", "", "foo", "", "", "bar\n\t", "", "baz", "", "", ""]) }
assert { " foo bar\n\t baz ".split.should eq(["foo", "bar", "baz"]) }
assert { " foo bar\n\t baz ".split(1).should eq([" foo bar\n\t baz "]) }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@asterite Even if #3702 won't be merged maybe you could 🍒 this test?

@ysbaddaden
Copy link
Contributor

I'm reopening the PR. The problem of #2541 is avoided because it yields String instead of an unexpected Slice —which would fit better as Slice#split and be just as awesome. Maybe it could implement Slice#split(Char) and use it, but that wouldn't cover other cases (String, Regex), and I think this is a great improvement already (that could be further improved).

String 👍 from me!

@ysbaddaden ysbaddaden reopened this Dec 15, 2016
@kostya
Copy link
Contributor

kostya commented Dec 15, 2016

yielding slice is ok, because it internal method which i need to use for speed, common method still return strings.

@asterite
Copy link
Member

I'm also OK with this addition.

Eventually I'd also like to be able to get an Iterator from a split. Maybe it can be named each_split.

@kostya We could add a separate method that yields slices, with a different name (maybe each_split_slice or something like that). Slices are much more low-level and needed when you want to squeeze performance in case of a bottleneck, and Crystal should allow you do do that, but the default is usually "user-friendly" (String is better than Slice, Array is better than Slice and StaticArray, etc.)

# water's sound
# "
# old_pond.split { |s| ary << s }; ary # => ["Old", "pond", "a", "frog", "leaps", "in", "water's", "sound"]
# old_pond.split(3) { |s| ary << s }; ary # => ["Old", "pond", "a frog leaps in\n water's sound\n"]
Copy link
Member

@asterite asterite Dec 20, 2016

Choose a reason for hiding this comment

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

ary should probably be cleared before each split. This applies to this example and other examples below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@asterite Good catch! Fixed.

@splattael
Copy link
Contributor Author

@asterite Ready to merge. Do you want me to squash the commits?

@asterite
Copy link
Member

@splattael No problem, I think the separate commits look good :-)

I'll merge it soon. Thank you!

@asterite asterite added this to the 0.20.2 milestone Dec 22, 2016
@asterite asterite merged commit b8f0151 into crystal-lang:master Dec 22, 2016
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

4 participants