Skip to content

Commit feaa2ec

Browse files
committed
Ignore chomp keyword for nil separator
nil separator means no separator at all, so nothing should be chomped. Partial fix for Ruby [Bug #18770]
1 parent 4bf64d5 commit feaa2ec

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

ext/stringio/stringio.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,7 @@ prepare_getline_args(struct getline_arg *arg, int argc, VALUE *argv)
11271127
long limit = -1;
11281128

11291129
argc = rb_scan_args(argc, argv, "02:", &str, &lim, &opts);
1130+
int respect_chomp = argc == 0 || !NIL_P(str);
11301131
switch (argc) {
11311132
case 0:
11321133
str = rb_rs;
@@ -1160,7 +1161,9 @@ prepare_getline_args(struct getline_arg *arg, int argc, VALUE *argv)
11601161
keywords[0] = rb_intern_const("chomp");
11611162
}
11621163
rb_get_kwargs(opts, keywords, 0, 1, &vchomp);
1163-
arg->chomp = (vchomp != Qundef) && RTEST(vchomp);
1164+
if (respect_chomp) {
1165+
arg->chomp = (vchomp != Qundef) && RTEST(vchomp);
1166+
}
11641167
}
11651168
return arg;
11661169
}

test/stringio/test_stringio.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def test_gets_chomp
9292
assert_equal("a", StringIO.new("a").gets(chomp: true))
9393
assert_equal("a", StringIO.new("a\nb").gets(chomp: true))
9494
assert_equal("abc", StringIO.new("abc\n\ndef\n").gets(chomp: true))
95-
assert_equal("abc\n\ndef", StringIO.new("abc\n\ndef\n").gets(nil, chomp: true))
95+
assert_equal("abc\n\ndef\n", StringIO.new("abc\n\ndef\n").gets(nil, chomp: true))
9696
assert_equal("abc\n", StringIO.new("abc\n\ndef\n").gets("", chomp: true))
9797
stringio = StringIO.new("abc\n\ndef\n")
9898
assert_equal("abc\n", stringio.gets("", chomp: true))
@@ -109,7 +109,7 @@ def test_gets_chomp_eol
109109
assert_equal("a", StringIO.new("a").gets(chomp: true))
110110
assert_equal("a", StringIO.new("a\r\nb").gets(chomp: true))
111111
assert_equal("abc", StringIO.new("abc\r\n\r\ndef\r\n").gets(chomp: true))
112-
assert_equal("abc\r\n\r\ndef", StringIO.new("abc\r\n\r\ndef\r\n").gets(nil, chomp: true))
112+
assert_equal("abc\r\n\r\ndef\r\n", StringIO.new("abc\r\n\r\ndef\r\n").gets(nil, chomp: true))
113113
assert_equal("abc\r\n", StringIO.new("abc\r\n\r\ndef\r\n").gets("", chomp: true))
114114
stringio = StringIO.new("abc\r\n\r\ndef\r\n")
115115
assert_equal("abc\r\n", stringio.gets("", chomp: true))
@@ -605,6 +605,9 @@ def test_each
605605
assert_equal(["foo\r\nbar\r\n\r\n", "baz\r\n"], f.each("").to_a)
606606
f.rewind
607607
assert_equal(["foo\r\nbar\r\n", "baz"], f.each("", chomp: true).to_a)
608+
609+
f = StringIO.new("abc\n\ndef\n")
610+
assert_equal(["ab", "c\n", "\nd", "ef", "\n"], f.each(nil, 2, chomp: true).to_a)
608611
end
609612

610613
def test_putc

0 commit comments

Comments
 (0)