ruby-changes:48262
From: nobu <ko1@a...>
Date: Mon, 23 Oct 2017 15:25:44 +0900 (JST)
Subject: [ruby-changes:48262] nobu:r60377 (trunk): stringio.c: write multiple arguments
nobu 2017-10-23 15:25:39 +0900 (Mon, 23 Oct 2017) New Revision: 60377 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=60377 Log: stringio.c: write multiple arguments * ext/stringio/stringio.c (strio_write_m): make StringIO#write accept multiple arguments, as well as IO#write. Modified files: trunk/NEWS trunk/ext/stringio/stringio.c trunk/test/stringio/test_stringio.rb Index: ext/stringio/stringio.c =================================================================== --- ext/stringio/stringio.c (revision 60376) +++ ext/stringio/stringio.c (revision 60377) @@ -35,6 +35,7 @@ struct StringIO { https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L35 static VALUE strio_init(int, VALUE *, struct StringIO *, VALUE); static VALUE strio_unget_bytes(struct StringIO *, const char *, long); +static long strio_write(VALUE self, VALUE str); #define IS_STRIO(obj) (rb_typeddata_is_kind_of((obj), &strio_data_type)) #define error_inval(msg) (rb_syserr_fail(EINVAL, msg)) @@ -1256,6 +1257,17 @@ strio_readlines(int argc, VALUE *argv, V https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L1257 * Returns the number of bytes written. See IO#write. */ static VALUE +strio_write_m(int argc, VALUE *argv, VALUE self) +{ + long len = 0; + while (argc-- > 0) { + /* StringIO can't exceed long limit */ + len += strio_write(self, *argv++); + } + return LONG2NUM(len); +} + +static long strio_write(VALUE self, VALUE str) { struct StringIO *ptr = writable(self); @@ -1271,7 +1283,7 @@ strio_write(VALUE self, VALUE str) https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L1283 str = rb_str_conv_enc(str, enc2, enc); } len = RSTRING_LEN(str); - if (len == 0) return INT2FIX(0); + if (len == 0) return 0; check_modifiable(ptr); olen = RSTRING_LEN(ptr->string); if (ptr->flags & FMODE_APPEND) { @@ -1294,7 +1306,7 @@ strio_write(VALUE self, VALUE str) https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L1306 OBJ_INFECT(ptr->string, self); RB_GC_GUARD(str); ptr->pos += len; - return LONG2NUM(len); + return len; } /* @@ -1668,7 +1680,7 @@ Init_stringio(void) https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L1680 rb_define_method(StringIO, "readlines", strio_readlines, -1); rb_define_method(StringIO, "read", strio_read, -1); - rb_define_method(StringIO, "write", strio_write, 1); + rb_define_method(StringIO, "write", strio_write_m, -1); rb_define_method(StringIO, "putc", strio_putc, 1); /* Index: NEWS =================================================================== --- NEWS (revision 60376) +++ NEWS (revision 60377) @@ -204,6 +204,9 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L204 * Add Set#=== as alias to #include? [Feature #13801] * Add Set#reset [Feature #6589] +* StringIO + * StringIO#write accepts multiple arguments + * WEBrick * Add Server Name Indication (SNI) support [Feature #13729] Index: test/stringio/test_stringio.rb =================================================================== --- test/stringio/test_stringio.rb (revision 60376) +++ test/stringio/test_stringio.rb (revision 60377) @@ -206,6 +206,16 @@ class TestStringIO < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/stringio/test_stringio.rb#L206 } end + def test_write_with_multiple_arguments + s = "" + f = StringIO.new(s, "w") + f.write("foo", "bar") + f.close + assert_equal("foobar", s) + ensure + f.close unless f.closed? + end + def test_set_encoding bug10285 = '[ruby-core:65240] [Bug #10285]' f = StringIO.new() -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/