-
-
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
IO#gets
should have same result regardless of #peek availability
#13882
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -752,11 +752,12 @@ abstract class IO | |||||
|
||||||
private def gets_slow(delimiter : Char, limit, chomp) | ||||||
buffer = String::Builder.new | ||||||
gets_slow(delimiter, limit, chomp, buffer) | ||||||
buffer.empty? ? nil : buffer.to_s | ||||||
bytes_read = gets_slow(delimiter, limit, chomp, buffer) | ||||||
bytes_read.zero? ? nil : buffer.to_s | ||||||
end | ||||||
|
||||||
private def gets_slow(delimiter : Char, limit, chomp, buffer : String::Builder) : Nil | ||||||
private def gets_slow(delimiter : Char, limit, chomp, buffer : String::Builder) : Int32 | ||||||
bytes_read = 0 | ||||||
chomp_rn = delimiter == '\n' && chomp | ||||||
|
||||||
while true | ||||||
|
@@ -766,6 +767,7 @@ abstract class IO | |||||
end | ||||||
|
||||||
char, char_bytesize = info | ||||||
bytes_read += char_bytesize | ||||||
|
||||||
# Consider the case of \r\n when the delimiter is \n and chomp = true | ||||||
if chomp_rn && char == '\r' | ||||||
|
@@ -776,6 +778,7 @@ abstract class IO | |||||
end | ||||||
|
||||||
char2, char_bytesize2 = info2 | ||||||
bytes_read += char_bytesize2 | ||||||
if char2 == '\n' | ||||||
break | ||||||
end | ||||||
|
@@ -801,6 +804,8 @@ abstract class IO | |||||
break if char_bytesize >= limit | ||||||
limit -= char_bytesize | ||||||
end | ||||||
|
||||||
bytes_read | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changing
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Join same discussion here #13882 (comment) (I'm not sure this would be correct) |
||||||
end | ||||||
|
||||||
# Reads until *delimiter* is found or the end of the `IO` is reached. | ||||||
|
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 don't think the exact number of bytes read needs to be computed, other than that it is non-zero. So
bytes_read
can be represented by aBool
instead and this line can be dropped;bytes_read += char_bytesize
above already implies at least 1 byte is consumedThere 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.
Good point.
And actually I think it can be achieved even easier by replacing
break
in line 766 withreturn false
. That's the only place where the method can return without having read anything. The last line would betrue
.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.
@HertzDevil I've just pushed a second commit 17752d1 which tracks
bytes_read
as aBool
and drops the second accumulationbytes_read += char_bytesize2
line as you've suggested.@straight-shoota If I were to change line 766 from
break
toreturn false
, I think this might incorrectly return false in any case where we've already read some bytes in previous iterations of thewhile
loop and then hit EOF?