Skip to content

Commit

Permalink
merge revision(s) 21dced8b01823a991829b66ffdc8ffc635965c76: [Backport…
Browse files Browse the repository at this point in the history
… #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(-)
  • Loading branch information
nagachika authored and hsbt committed Mar 19, 2024
1 parent d25ef05 commit 0e59652
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
5 changes: 3 additions & 2 deletions ext/stringio/stringio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions test/stringio/test_stringio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0e59652

Please # to comment.