Skip to content

Commit

Permalink
Ignore chomp keyword for nil separator
Browse files Browse the repository at this point in the history
nil separator means no separator at all, so nothing should be
chomped.

Partial fix for Ruby [Bug #18770]
  • Loading branch information
jeremyevans committed May 30, 2022
1 parent 4bf64d5 commit feaa2ec
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
5 changes: 4 additions & 1 deletion ext/stringio/stringio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1160,7 +1161,9 @@ 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);
arg->chomp = (vchomp != Qundef) && RTEST(vchomp);
if (respect_chomp) {
arg->chomp = (vchomp != Qundef) && RTEST(vchomp);
}
}
return arg;
}
Expand Down
7 changes: 5 additions & 2 deletions test/stringio/test_stringio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def test_gets_chomp
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))
Expand All @@ -109,7 +109,7 @@ def test_gets_chomp_eol
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))
Expand Down Expand Up @@ -605,6 +605,9 @@ def test_each
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
Expand Down

0 comments on commit feaa2ec

Please sign in to comment.