summaryrefslogtreecommitdiff
path: root/ext/stringio
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-12-24 01:29:57 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-12-24 01:29:57 +0000
commitc2b63d249f9e21d7b5d92680f614952490fbe708 (patch)
treeb317298b354659eb85d302f02f8b9fcb5ca005c0 /ext/stringio
parent8365bb013f7d7493f398ff07de7cfa3da54053bf (diff)
* 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
Diffstat (limited to 'ext/stringio')
-rw-r--r--ext/stringio/stringio.c26
1 files changed, 22 insertions, 4 deletions
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;