Skip to content

Commit 0e59652

Browse files
nagachikahsbt
authored andcommitted
merge revision(s) 21dced8b01823a991829b66ffdc8ffc635965c76: [Backport #19389]
[ruby/stringio] [Bug #19389] Fix chomping with longer separator eb322a9716 --- ext/stringio/stringio.c | 5 +++-- test/stringio/test_stringio.rb | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-)
1 parent d25ef05 commit 0e59652

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
@@ -1233,8 +1233,9 @@ strio_getline(struct getline_arg *arg, struct StringIO *ptr)
12331233
str = strio_substr(ptr, ptr->pos, e - s - w, enc);
12341234
}
12351235
else {
1236-
if (n < e - s) {
1237-
if (e - s < 1024) {
1236+
if (n < e - s + arg->chomp) {
1237+
/* unless chomping, RS at the end does not matter */
1238+
if (e - s < 1024 || n == e - s) {
12381239
for (p = s; p + n <= e; ++p) {
12391240
if (MEMCMP(p, RSTRING_PTR(str), char, n) == 0) {
12401241
e = p + (arg->chomp ? 0 : 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", 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)