From c2b63d249f9e21d7b5d92680f614952490fbe708 Mon Sep 17 00:00:00 2001 From: nobu Date: Wed, 24 Dec 2003 01:29:57 +0000 Subject: [PATCH] * ext/stringio/stringio.c (strio_read): never return nil at unlimited read. [ruby-dev:22334] * ext/stringio/stringio.c (strio_read): support second argument. [ruby-dev:22350] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@5270 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 8 ++++++++ ext/stringio/stringio.c | 26 ++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 480a459614..403c4411bd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +Wed Dec 24 10:29:53 2003 Nobuyoshi Nakada + + * ext/stringio/stringio.c (strio_read): never return nil at + unlimited read. [ruby-dev:22334] + + * ext/stringio/stringio.c (strio_read): support second + argument. [ruby-dev:22350] + Wed Dec 24 09:38:49 2003 Nobuyoshi Nakada * parse.y (arg): should return 0 after error. [ruby-dev:22360] diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c index 88f6be42d3..1bab5b4d1f 100644 --- a/ext/stringio/stringio.c +++ b/ext/stringio/stringio.c @@ -835,10 +835,14 @@ strio_read(argc, argv, self) VALUE self; { struct StringIO *ptr = readable(StringIO(self)); - VALUE str; + VALUE str = Qnil; long len, olen; switch (argc) { + case 2: + str = argv[1]; + StringValue(str); + rb_str_modify(str); case 1: if (!NIL_P(argv[0])) { len = olen = NUM2LONG(argv[0]); @@ -859,8 +863,14 @@ strio_read(argc, argv, self) olen = -1; len = RSTRING(ptr->string)->len; if (len <= ptr->pos) { - if (ptr->flags & STRIO_EOF) return Qnil; - len = 0; + ptr->flags |= STRIO_EOF; + if (NIL_P(str)) { + str = rb_str_new(0, 0); + } + else { + rb_str_resize(str, 0); + } + return str; } else { len -= ptr->pos; @@ -869,7 +879,15 @@ strio_read(argc, argv, self) default: rb_raise(rb_eArgError, "wrong number arguments (%d for 0)", argc); } - str = rb_str_substr(ptr->string, ptr->pos, len); + if (NIL_P(str)) { + str = rb_str_substr(ptr->string, ptr->pos, len); + } + else { + long rest = RSTRING(ptr->string)->len - ptr->pos; + if (len > rest) len = rest; + rb_str_resize(str, len); + MEMCPY(RSTRING(str)->ptr, RSTRING(ptr->string)->ptr, char, len); + } if (NIL_P(str)) { if (!(ptr->flags & STRIO_EOF)) str = rb_str_new(0, 0); len = 0;