[前][次][番号順一覧][スレッド一覧]

ruby-changes:72009

From: Jeremy <ko1@a...>
Date: Mon, 30 May 2022 13:01:32 +0900 (JST)
Subject: [ruby-changes:72009] adaaf12857 (master): [ruby/stringio] Ignore chomp keyword for nil separator

https://git.ruby-lang.org/ruby.git/commit/?id=adaaf12857

From adaaf12857ce41d35b282e3fb5aa330934ce45c6 Mon Sep 17 00:00:00 2001
From: Jeremy Evans <code@j...>
Date: Thu, 26 May 2022 12:17:55 -0700
Subject: [ruby/stringio] Ignore chomp keyword for nil separator

nil separator means no separator at all, so nothing should be
chomped.

Partial fix for Ruby [Bug #18770]

https://github.com/ruby/stringio/commit/feaa2ec631
---
 ext/stringio/stringio.c        | 5 ++++-
 test/stringio/test_stringio.rb | 7 +++++--
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c
index 04ca25b0b8..8cf5113ac8 100644
--- a/ext/stringio/stringio.c
+++ b/ext/stringio/stringio.c
@@ -1127,6 +1127,7 @@ prepare_getline_args(struct getline_arg *arg, int argc, VALUE *argv) https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L1127
     long limit = -1;
 
     argc = rb_scan_args(argc, argv, "02:", &str, &lim, &opts);
+    int respect_chomp = argc == 0 || !NIL_P(str);
     switch (argc) {
       case 0:
 	str = rb_rs;
@@ -1160,7 +1161,9 @@ prepare_getline_args(struct getline_arg *arg, int argc, VALUE *argv) https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L1161
 	    keywords[0] = rb_intern_const("chomp");
 	}
 	rb_get_kwargs(opts, keywords, 0, 1, &vchomp);
-	arg->chomp = (vchomp != Qundef) && RTEST(vchomp);
+        if (respect_chomp) {
+	    arg->chomp = (vchomp != Qundef) && RTEST(vchomp);
+        }
     }
     return arg;
 }
diff --git a/test/stringio/test_stringio.rb b/test/stringio/test_stringio.rb
index f14b65a8ea..f0af708c10 100644
--- a/test/stringio/test_stringio.rb
+++ b/test/stringio/test_stringio.rb
@@ -92,7 +92,7 @@ class TestStringIO < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/stringio/test_stringio.rb#L92
     assert_equal("a", StringIO.new("a").gets(chomp: true))
     assert_equal("a", StringIO.new("a\nb").gets(chomp: true))
     assert_equal("abc", StringIO.new("abc\n\ndef\n").gets(chomp: true))
-    assert_equal("abc\n\ndef", StringIO.new("abc\n\ndef\n").gets(nil, chomp: true))
+    assert_equal("abc\n\ndef\n", StringIO.new("abc\n\ndef\n").gets(nil, chomp: true))
     assert_equal("abc\n", StringIO.new("abc\n\ndef\n").gets("", chomp: true))
     stringio = StringIO.new("abc\n\ndef\n")
     assert_equal("abc\n", stringio.gets("", chomp: true))
@@ -109,7 +109,7 @@ class TestStringIO < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/stringio/test_stringio.rb#L109
     assert_equal("a", StringIO.new("a").gets(chomp: true))
     assert_equal("a", StringIO.new("a\r\nb").gets(chomp: true))
     assert_equal("abc", StringIO.new("abc\r\n\r\ndef\r\n").gets(chomp: true))
-    assert_equal("abc\r\n\r\ndef", StringIO.new("abc\r\n\r\ndef\r\n").gets(nil, chomp: true))
+    assert_equal("abc\r\n\r\ndef\r\n", StringIO.new("abc\r\n\r\ndef\r\n").gets(nil, chomp: true))
     assert_equal("abc\r\n", StringIO.new("abc\r\n\r\ndef\r\n").gets("", chomp: true))
     stringio = StringIO.new("abc\r\n\r\ndef\r\n")
     assert_equal("abc\r\n", stringio.gets("", chomp: true))
@@ -605,6 +605,9 @@ class TestStringIO < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/stringio/test_stringio.rb#L605
     assert_equal(["foo\r\nbar\r\n\r\n", "baz\r\n"], f.each("").to_a)
     f.rewind
     assert_equal(["foo\r\nbar\r\n", "baz"], f.each("", chomp: true).to_a)
+
+    f = StringIO.new("abc\n\ndef\n")
+    assert_equal(["ab", "c\n", "\nd", "ef", "\n"], f.each(nil, 2, chomp: true).to_a)
   end
 
   def test_putc
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]