Repository navigation
Conversation
`gets("")`, `readlines("")` and `each_line("")` differed from IO and
String:
* The returned paragraph kept all the following empty lines, such as
`"a\n\n\n\n"` for `"a\n\n\n\nb"`. IO and String return `"a\n\n"`
and skip the rest.
* The limit counted the leading newlines that are skipped, so
`StringIO.new("\n\nabc").gets("", 1)` returned `nil` before the end
of the stream. IO returns `"a"`.
* After a line cut by the limit, the following empty lines were not
skipped as IO does.
* When only newlines were left, `gets("")` returned `nil` without
moving `pos` to the end.
On JRuby, a paragraph at the start of the string, such as
`"\r\n\r\nabc"` with `chomp: true`, was not chomped because 0 was used
as "not found" for the paragraph end.
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Paragraph mode (
gets(""),readlines(""),each_line("")) does not match IO and String in a few ways:For the first one, core Ruby already tests this for String and IO:
test/ruby/test_string.rb:"hello\n\n\nworld".each_line('')gives"hello\n\n""a\n\n"and skips the rest of the empty lines (swallowinio.c)The second one comes from the limit: it was applied from
pos, before the leading newlines were skipped. When the limit window held only newlines,getsreturnednilas if the stream had ended, so awhile (line = io.gets("", n))loop stops early.Change
In the paragraph branch of
strio_getline(C and Java):pospast all the following empty lines (also after a line cut by the limit, like IO)posto the end before returningnilThe limit code moved to a small helper so both places use the same logic. Other separators are not changed.
On JRuby,
paragraph_endused0to mean "not found", but0is a valid index, soStringIO.new("\r\n\r\nabc").each_line("", chomp: true)gave["\r\n\r\n", "abc"]while the C extension (and String) give["", "abc"]. It now uses-1.test_eachexpected the old output ("foo\nbar\n\n\n"), so I updated it to what IO and String return.Testing
test_stringio.rb(extra empty lines, only newlines, limit after leading newlines, CRLF paragraph at the start withchomp).rake testpasses after (122 tests).rake testpasses after (119 tests, 2 omissions).File#getson random paragraph-mode op sequences (gets("")with and without limit/chomp,read,getc,readlines("")): 7648 of 30k sequences differ with stringio 3.2.0, 18 after. The 18 left all start withposin the middle of a multibyte character (an older, separate case).nil,"\n","ab","\r\n", ...) give identical results before and after.