diff options
author | NARUSE, Yui <naruse@airemix.jp> | 2023-01-31 11:08:50 +0900 |
---|---|---|
committer | NARUSE, Yui <naruse@airemix.jp> | 2023-01-31 11:08:50 +0900 |
commit | 1689d8bb4843f92c1805e4a4bdd94049569198f4 (patch) | |
tree | 87581af25771abaf71b1b8ef05b1c33fc3588f8b | |
parent | a22eca82314ae36668d8c1a591ffbbfa8f93147c (diff) | |
download | ruby-1689d8bb4843f92c1805e4a4bdd94049569198f4.tar.gz |
merge revision(s) 21dced8b01823a991829b66ffdc8ffc635965c76: [Backport #19389]
[ruby/stringio] [Bug #19389] Fix chomping with longer separator
https://github.com/ruby/stringio/commit/eb322a9716
---
ext/stringio/stringio.c | 5 +++--
test/stringio/test_stringio.rb | 2 ++
2 files changed, 5 insertions(+), 2 deletions(-)
-rw-r--r-- | ext/stringio/stringio.c | 5 | ||||
-rw-r--r-- | test/stringio/test_stringio.rb | 2 | ||||
-rw-r--r-- | version.h | 2 |
3 files changed, 6 insertions, 3 deletions
diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c index f82103a160..0054766dac 100644 --- a/ext/stringio/stringio.c +++ b/ext/stringio/stringio.c @@ -1340,8 +1340,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 + n; diff --git a/test/stringio/test_stringio.rb b/test/stringio/test_stringio.rb index 34c4748185..fd9974caeb 100644 --- a/test/stringio/test_stringio.rb +++ b/test/stringio/test_stringio.rb @@ -99,6 +99,8 @@ class TestStringIO < Test::Unit::TestCase assert_equal("def\n", 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 @@ -11,7 +11,7 @@ # define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR #define RUBY_VERSION_TEENY 0 #define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR -#define RUBY_PATCHLEVEL 21 +#define RUBY_PATCHLEVEL 22 #include "ruby/version.h" #include "ruby/internal/abi.h" |