From cff28a1dabb313705819aeb4b616383ff45e266e Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Fri, 10 Jul 2026 20:00:19 +0900 Subject: [PATCH] Skip chompped record separator Fix ruby/stringio#218. Co-Authored-By: Pranjali Thakur --- ext/java/org/jruby/ext/stringio/StringIO.java | 3 ++- ext/stringio/stringio.c | 3 ++- test/stringio/test_stringio.rb | 4 ++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/ext/java/org/jruby/ext/stringio/StringIO.java b/ext/java/org/jruby/ext/stringio/StringIO.java index e84fa17..14ea18e 100644 --- a/ext/java/org/jruby/ext/stringio/StringIO.java +++ b/ext/java/org/jruby/ext/stringio/StringIO.java @@ -959,7 +959,8 @@ private IRubyObject getline(ThreadContext context, final IRubyObject rs, int lim p = rsByteList.getBegin(); bm_init_skip(skip, rsBytes, p, n); if ((pos2 = bm_search(rsBytes, p, n, stringBytes, s, e - s, skip)) >= 0) { - e = s + pos2 + (chomp ? 0 : n); + e = s + pos2 + n; + if (chomp) w = n; } } } diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c index b97eaa6..926c867 100644 --- a/ext/stringio/stringio.c +++ b/ext/stringio/stringio.c @@ -1447,7 +1447,8 @@ strio_getline(struct getline_arg *arg, struct StringIO *ptr) p = RSTRING_PTR(str); bm_init_skip(skip, p, n); if ((pos = bm_search(p, n, s, e - s, skip)) >= 0) { - e = s + pos + (arg->chomp ? 0 : n); + e = s + pos + n; + if (arg->chomp) w = n; } } } diff --git a/test/stringio/test_stringio.rb b/test/stringio/test_stringio.rb index 0f61245..5afa23b 100644 --- a/test/stringio/test_stringio.rb +++ b/test/stringio/test_stringio.rb @@ -168,6 +168,10 @@ def test_gets_chomp assert_string("", Encoding::UTF_8, StringIO.new("\n").gets(chomp: true)) assert_equal("", StringIO.new("ab").gets("ab", chomp: true)) + + sio = StringIO.new("abcde\r\n" "fghij\r\n" + "z"*1024) + assert_equal("abcde", sio.gets("\r\n", chomp: true)) + assert_equal("fghij", sio.gets("\r\n", chomp: true)) end def test_gets_chomp_eol