Do not chomp trailing line separator IO#each with nil separator and chomp
nil separator means no sepator, so chomp should not remove a line separator. Partially Fixes [Bug #18770]
This commit is contained in:
parent
3a5ea7c688
commit
7223c0da15
Notes:
git
2022-07-22 04:55:45 +09:00
1
io.c
1
io.c
@ -3875,7 +3875,6 @@ rb_io_getline_0(VALUE rs, long limit, int chomp, rb_io_t *fptr)
|
|||||||
if (NIL_P(rs) && limit < 0) {
|
if (NIL_P(rs) && limit < 0) {
|
||||||
str = read_all(fptr, 0, Qnil);
|
str = read_all(fptr, 0, Qnil);
|
||||||
if (RSTRING_LEN(str) == 0) return Qnil;
|
if (RSTRING_LEN(str) == 0) return Qnil;
|
||||||
if (chomp) rb_str_chomp_string(str, rb_default_rs);
|
|
||||||
}
|
}
|
||||||
else if (limit == 0) {
|
else if (limit == 0) {
|
||||||
return rb_enc_str_new(0, 0, io_read_encoding(fptr));
|
return rb_enc_str_new(0, 0, io_read_encoding(fptr));
|
||||||
|
@ -190,12 +190,22 @@ describe :io_each, shared: true do
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe "when passed chomp and nil as a separator" do
|
describe "when passed chomp and nil as a separator" do
|
||||||
|
ruby_version_is "3.2" do
|
||||||
|
it "yields self's content" do
|
||||||
|
@io.pos = 100
|
||||||
|
@io.send(@method, nil, chomp: true) { |s| ScratchPad << s }
|
||||||
|
ScratchPad.recorded.should == ["qui a linha cinco.\nHere is line six.\n"]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
ruby_version_is ""..."3.2" do
|
||||||
it "yields self's content without trailing new line character" do
|
it "yields self's content without trailing new line character" do
|
||||||
@io.pos = 100
|
@io.pos = 100
|
||||||
@io.send(@method, nil, chomp: true) { |s| ScratchPad << s }
|
@io.send(@method, nil, chomp: true) { |s| ScratchPad << s }
|
||||||
ScratchPad.recorded.should == ["qui a linha cinco.\nHere is line six."]
|
ScratchPad.recorded.should == ["qui a linha cinco.\nHere is line six."]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "when passed chomp, nil as a separator, and a limit" do
|
describe "when passed chomp, nil as a separator, and a limit" do
|
||||||
it "yields each line of limit size without truncating trailing new line character" do
|
it "yields each line of limit size without truncating trailing new line character" do
|
||||||
|
@ -312,7 +312,7 @@ class TestIO < Test::Unit::TestCase
|
|||||||
w.print "a\n\nb\n\n"
|
w.print "a\n\nb\n\n"
|
||||||
w.close
|
w.close
|
||||||
end, proc do |r|
|
end, proc do |r|
|
||||||
assert_equal "a\n\nb\n", r.gets(nil, chomp: true)
|
assert_equal("a\n\nb\n\n", r.gets(nil, chomp: true), "[Bug #18770]")
|
||||||
assert_nil r.gets("")
|
assert_nil r.gets("")
|
||||||
r.close
|
r.close
|
||||||
end)
|
end)
|
||||||
@ -1894,6 +1894,20 @@ class TestIO < Test::Unit::TestCase
|
|||||||
assert_equal("baz\n", e.next)
|
assert_equal("baz\n", e.next)
|
||||||
assert_raise(StopIteration) { e.next }
|
assert_raise(StopIteration) { e.next }
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
pipe(proc do |w|
|
||||||
|
w.write "foo\n"
|
||||||
|
w.close
|
||||||
|
end, proc do |r|
|
||||||
|
assert_equal(["foo\n"], r.each_line(nil, chomp: true).to_a, "[Bug #18770]")
|
||||||
|
end)
|
||||||
|
|
||||||
|
pipe(proc do |w|
|
||||||
|
w.write "foo\n"
|
||||||
|
w.close
|
||||||
|
end, proc do |r|
|
||||||
|
assert_equal(["fo", "o\n"], r.each_line(nil, 2, chomp: true).to_a, "[Bug #18770]")
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_each_byte2
|
def test_each_byte2
|
||||||
|
Loading…
x
Reference in New Issue
Block a user