[ruby/stringio] Ignore chomp keyword for nil separator

nil separator means no separator at all, so nothing should be
chomped.

Partial fix for Ruby [Bug #18770]

https://github.com/ruby/stringio/commit/feaa2ec631
This commit is contained in:
Jeremy Evans 2022-05-26 12:17:55 -07:00 committed by git
parent 1f82269f4e
commit adaaf12857
2 changed files with 9 additions and 3 deletions

View File

@ -1127,6 +1127,7 @@ prepare_getline_args(struct getline_arg *arg, int argc, VALUE *argv)
long limit = -1;
argc = rb_scan_args(argc, argv, "02:", &str, &lim, &opts);
int respect_chomp = argc == 0 || !NIL_P(str);
switch (argc) {
case 0:
str = rb_rs;
@ -1160,8 +1161,10 @@ prepare_getline_args(struct getline_arg *arg, int argc, VALUE *argv)
keywords[0] = rb_intern_const("chomp");
}
rb_get_kwargs(opts, keywords, 0, 1, &vchomp);
if (respect_chomp) {
arg->chomp = (vchomp != Qundef) && RTEST(vchomp);
}
}
return arg;
}

View File

@ -92,7 +92,7 @@ class TestStringIO < Test::Unit::TestCase
assert_equal("a", StringIO.new("a").gets(chomp: true))
assert_equal("a", StringIO.new("a\nb").gets(chomp: true))
assert_equal("abc", StringIO.new("abc\n\ndef\n").gets(chomp: true))
assert_equal("abc\n\ndef", StringIO.new("abc\n\ndef\n").gets(nil, chomp: true))
assert_equal("abc\n\ndef\n", StringIO.new("abc\n\ndef\n").gets(nil, chomp: true))
assert_equal("abc\n", StringIO.new("abc\n\ndef\n").gets("", chomp: true))
stringio = StringIO.new("abc\n\ndef\n")
assert_equal("abc\n", stringio.gets("", chomp: true))
@ -109,7 +109,7 @@ class TestStringIO < Test::Unit::TestCase
assert_equal("a", StringIO.new("a").gets(chomp: true))
assert_equal("a", StringIO.new("a\r\nb").gets(chomp: true))
assert_equal("abc", StringIO.new("abc\r\n\r\ndef\r\n").gets(chomp: true))
assert_equal("abc\r\n\r\ndef", StringIO.new("abc\r\n\r\ndef\r\n").gets(nil, chomp: true))
assert_equal("abc\r\n\r\ndef\r\n", StringIO.new("abc\r\n\r\ndef\r\n").gets(nil, chomp: true))
assert_equal("abc\r\n", StringIO.new("abc\r\n\r\ndef\r\n").gets("", chomp: true))
stringio = StringIO.new("abc\r\n\r\ndef\r\n")
assert_equal("abc\r\n", stringio.gets("", chomp: true))
@ -605,6 +605,9 @@ class TestStringIO < Test::Unit::TestCase
assert_equal(["foo\r\nbar\r\n\r\n", "baz\r\n"], f.each("").to_a)
f.rewind
assert_equal(["foo\r\nbar\r\n", "baz"], f.each("", chomp: true).to_a)
f = StringIO.new("abc\n\ndef\n")
assert_equal(["ab", "c\n", "\nd", "ef", "\n"], f.each(nil, 2, chomp: true).to_a)
end
def test_putc