diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-10-23 06:25:39 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-10-23 06:25:39 +0000 |
commit | aeaeb4b0686b01a4a56998a4b7d87da470a57209 (patch) | |
tree | 4100bc0e185938ce00c399a602e8e4f42f0bbdbb /ext | |
parent | 5682bc6ac547af13e2d2a4100c7d82995c2749f4 (diff) | |
download | ruby-aeaeb4b0686b01a4a56998a4b7d87da470a57209.tar.gz |
stringio.c: write multiple arguments
* ext/stringio/stringio.c (strio_write_m): make StringIO#write
accept multiple arguments, as well as IO#write.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext')
-rw-r--r-- | ext/stringio/stringio.c | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c index 761ec8e4d1..d6c55450df 100644 --- a/ext/stringio/stringio.c +++ b/ext/stringio/stringio.c @@ -35,6 +35,7 @@ struct StringIO { 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, VALUE self) * 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) 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) OBJ_INFECT(ptr->string, self); RB_GC_GUARD(str); ptr->pos += len; - return LONG2NUM(len); + return len; } /* @@ -1668,7 +1680,7 @@ Init_stringio(void) 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); /* |