diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c index 12930b3..9395c4d 100644 --- a/ext/stringio/stringio.c +++ b/ext/stringio/stringio.c @@ -1233,8 +1233,9 @@ strio_getline(struct getline_arg *arg, struct StringIO *ptr) str = strio_substr(ptr, ptr->pos, e - s - w, enc); } else { - if (n < e - s) { - if (e - s < 1024) { + if (n < e - s + arg->chomp) { + /* unless chomping, RS at the end does not matter */ + if (e - s < 1024 || n == e - s) { for (p = s; p + n <= e; ++p) { if (MEMCMP(p, RSTRING_PTR(str), char, n) == 0) { e = p + (arg->chomp ? 0 : n); diff --git a/test/stringio/test_stringio.rb b/test/stringio/test_stringio.rb index 144a9f4..0a4b90e 100644 --- a/test/stringio/test_stringio.rb +++ b/test/stringio/test_stringio.rb @@ -99,6 +99,8 @@ def test_gets_chomp assert_equal("def", stringio.gets("", chomp: true)) assert_string("", Encoding::UTF_8, StringIO.new("\n").gets(chomp: true)) + + assert_equal("", StringIO.new("ab").gets("ab", chomp: true)) end def test_gets_chomp_eol