Skip to content

Commit eb322a9

Browse files
authored
[Bug #19389] Fix chomping with longer separator
1 parent 709f8ca commit eb322a9

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

ext/stringio/stringio.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -1340,8 +1340,9 @@ strio_getline(struct getline_arg *arg, struct StringIO *ptr)
13401340
str = strio_substr(ptr, ptr->pos, e - s - w, enc);
13411341
}
13421342
else {
1343-
if (n < e - s) {
1344-
if (e - s < 1024) {
1343+
if (n < e - s + arg->chomp) {
1344+
/* unless chomping, RS at the end does not matter */
1345+
if (e - s < 1024 || n == e - s) {
13451346
for (p = s; p + n <= e; ++p) {
13461347
if (MEMCMP(p, RSTRING_PTR(str), char, n) == 0) {
13471348
e = p + n;

test/stringio/test_stringio.rb

+2
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ def test_gets_chomp
9999
assert_equal("def\n", stringio.gets("", chomp: true))
100100

101101
assert_string("", Encoding::UTF_8, StringIO.new("\n").gets(chomp: true))
102+
103+
assert_equal("", StringIO.new("ab").gets("ab", chomp: true))
102104
end
103105

104106
def test_gets_chomp_eol

0 commit comments

Comments
 (0)