-
-
Notifications
You must be signed in to change notification settings - Fork 945
add zlib each_char #8608
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
Merged
Merged
add zlib each_char #8608
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| require_relative '../../../spec_helper' | ||
| require 'stringio' | ||
| require 'zlib' | ||
|
|
||
| describe "Zlib::GzipReader#each_char" do | ||
|
|
||
| before :each do | ||
| @data = '12345abcde' | ||
| @zip = [31, 139, 8, 0, 44, 220, 209, 71, 0, 3, 51, 52, 50, 54, 49, 77, | ||
| 76, 74, 78, 73, 5, 0, 157, 5, 0, 36, 10, 0, 0, 0].pack('C*') | ||
|
|
||
| @io = StringIO.new @zip | ||
| ScratchPad.clear | ||
| end | ||
|
|
||
| it "calls the given block for each char in the stream, passing the char as an argument" do | ||
| gz = Zlib::GzipReader.new @io | ||
|
|
||
| ScratchPad.record [] | ||
| gz.each_char { |b| ScratchPad << b } | ||
|
|
||
| ScratchPad.recorded.should == ["1", "2", "3", "4", "5", "a", "b", "c", "d", "e"] | ||
| end | ||
|
|
||
| it "returns an enumerator, which yields each char in the stream, when no block is passed" do | ||
| gz = Zlib::GzipReader.new @io | ||
| enum = gz.each_char | ||
|
|
||
| ScratchPad.record [] | ||
| while true | ||
| begin | ||
| ScratchPad << enum.next | ||
| rescue StopIteration | ||
| break | ||
| end | ||
| end | ||
|
|
||
| ScratchPad.recorded.should == ["1", "2", "3", "4", "5", "a", "b", "c", "d", "e"] | ||
| end | ||
|
|
||
| it "increments position before calling the block" do | ||
| gz = Zlib::GzipReader.new @io | ||
|
|
||
| i = 1 | ||
| gz.each_char do |ignore| | ||
| gz.pos.should == i | ||
| i += 1 | ||
| end | ||
| end | ||
|
|
||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 totally understand how this code got here looking at getc but this has to be wrong as it uses a byte-oriented API and returns it as a char. Perhaps no one does multiple byte chars using this API as I would think #getc would have had a report by now since this has that same logic in it.
Another amusing thing to notice is MRI unit tests have no tests for this method. No wonder we are missing it. You will be the first person to actually add a test for this!
I am ok landing this if you open an issue that both each_char and getc is not recreating chars but is using bytes. It appears initialize will process opts and set encoding so I think we just need to use that and do something like RubyString#enumerateChars where we use StringSupport to look for length and that many bytes as a new char.
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 should add since there are no tests perhaps each_char really does only return bytes. I would be a pretty big misnaming if so.
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.
In MRI it does return multi-byte chars, and should work the same as calling
each_charon the inflated stringThere 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.
@danini-the-panini so then perhaps you want to take a look at fixing that for both each_char and getc in zlib? It will largely just be looking at our String-related code and doing the same thing.
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.
(as a new issue)
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.
Issue created: #8621