* ext/stringio/stringio.c (strio_read): behave as IO at empty string.
[ruby-dev:21939] * ext/stringio/stringio.c (strio_getc, strio_getline): set EOF flag. * ext/stringio/stringio.c (strio_rewind, strio_seek, strio_ungetc): clear EOF flag. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4978 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
914d8f7008
commit
12eee69294
10
ChangeLog
10
ChangeLog
@ -1,3 +1,13 @@
|
|||||||
|
Tue Nov 18 18:09:37 2003 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* ext/stringio/stringio.c (strio_read): behave as IO at empty string.
|
||||||
|
[ruby-dev:21939]
|
||||||
|
|
||||||
|
* ext/stringio/stringio.c (strio_getc, strio_getline): set EOF flag.
|
||||||
|
|
||||||
|
* ext/stringio/stringio.c (strio_rewind, strio_seek, strio_ungetc):
|
||||||
|
clear EOF flag.
|
||||||
|
|
||||||
Tue Nov 18 14:06:35 2003 Minero Aoki <aamine@loveruby.net>
|
Tue Nov 18 14:06:35 2003 Minero Aoki <aamine@loveruby.net>
|
||||||
|
|
||||||
* lib/fileutils.rb (fu_each_src_dest): raise if src==dest.
|
* lib/fileutils.rb (fu_each_src_dest): raise if src==dest.
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
#include "rubyio.h"
|
#include "rubyio.h"
|
||||||
|
|
||||||
#define STRIO_APPEND 4
|
#define STRIO_APPEND 4
|
||||||
|
#define STRIO_EOF 8
|
||||||
|
|
||||||
struct StringIO {
|
struct StringIO {
|
||||||
VALUE string;
|
VALUE string;
|
||||||
@ -483,6 +484,7 @@ strio_rewind(self)
|
|||||||
struct StringIO *ptr = StringIO(self);
|
struct StringIO *ptr = StringIO(self);
|
||||||
ptr->pos = 0;
|
ptr->pos = 0;
|
||||||
ptr->lineno = 0;
|
ptr->lineno = 0;
|
||||||
|
ptr->flags &= ~STRIO_EOF;
|
||||||
return INT2FIX(0);
|
return INT2FIX(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -514,6 +516,7 @@ strio_seek(argc, argv, self)
|
|||||||
error_inval(0);
|
error_inval(0);
|
||||||
}
|
}
|
||||||
ptr->pos = offset;
|
ptr->pos = offset;
|
||||||
|
ptr->flags &= ~STRIO_EOF;
|
||||||
return INT2FIX(0);
|
return INT2FIX(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -548,6 +551,7 @@ strio_getc(self)
|
|||||||
struct StringIO *ptr = readable(StringIO(self));
|
struct StringIO *ptr = readable(StringIO(self));
|
||||||
int c;
|
int c;
|
||||||
if (ptr->pos >= RSTRING(ptr->string)->len) {
|
if (ptr->pos >= RSTRING(ptr->string)->len) {
|
||||||
|
ptr->flags |= STRIO_EOF;
|
||||||
return Qnil;
|
return Qnil;
|
||||||
}
|
}
|
||||||
c = RSTRING(ptr->string)->ptr[ptr->pos++];
|
c = RSTRING(ptr->string)->ptr[ptr->pos++];
|
||||||
@ -578,6 +582,7 @@ strio_ungetc(self, ch)
|
|||||||
OBJ_INFECT(ptr->string, self);
|
OBJ_INFECT(ptr->string, self);
|
||||||
}
|
}
|
||||||
--ptr->pos;
|
--ptr->pos;
|
||||||
|
ptr->flags &= ~STRIO_EOF;
|
||||||
}
|
}
|
||||||
return Qnil;
|
return Qnil;
|
||||||
}
|
}
|
||||||
@ -649,7 +654,10 @@ strio_getline(argc, argv, ptr)
|
|||||||
if (!NIL_P(str)) StringValue(str);
|
if (!NIL_P(str)) StringValue(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ptr->pos >= (n = RSTRING(ptr->string)->len)) return Qnil;
|
if (ptr->pos >= (n = RSTRING(ptr->string)->len)) {
|
||||||
|
ptr->flags |= STRIO_EOF;
|
||||||
|
return Qnil;
|
||||||
|
}
|
||||||
s = RSTRING(ptr->string)->ptr;
|
s = RSTRING(ptr->string)->ptr;
|
||||||
e = s + RSTRING(ptr->string)->len;
|
e = s + RSTRING(ptr->string)->len;
|
||||||
s += ptr->pos;
|
s += ptr->pos;
|
||||||
@ -659,7 +667,10 @@ strio_getline(argc, argv, ptr)
|
|||||||
else if ((n = RSTRING(str)->len) == 0) {
|
else if ((n = RSTRING(str)->len) == 0) {
|
||||||
p = s;
|
p = s;
|
||||||
while (*p == '\n') {
|
while (*p == '\n') {
|
||||||
if (++p == e) return Qnil;
|
if (++p == e) {
|
||||||
|
ptr->flags |= STRIO_EOF;
|
||||||
|
return Qnil;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
s = p;
|
s = p;
|
||||||
while (p = memchr(p, '\n', e - p)) {
|
while (p = memchr(p, '\n', e - p)) {
|
||||||
@ -827,24 +838,34 @@ strio_read(argc, argv, self)
|
|||||||
VALUE str;
|
VALUE str;
|
||||||
long len;
|
long len;
|
||||||
|
|
||||||
if (ptr->pos >= RSTRING(ptr->string)->len) {
|
|
||||||
return Qnil;
|
|
||||||
}
|
|
||||||
switch (argc) {
|
switch (argc) {
|
||||||
case 1:
|
case 1:
|
||||||
if (!NIL_P(argv[0])) {
|
if (!NIL_P(argv[0])) {
|
||||||
len = NUM2LONG(argv[0]);
|
len = NUM2LONG(argv[0]);
|
||||||
|
if (len < 0) {
|
||||||
|
rb_raise(rb_eArgError, "negative length %ld given", len);
|
||||||
|
}
|
||||||
|
if (len > 0 && ptr->pos >= RSTRING(ptr->string)->len) {
|
||||||
|
ptr->flags |= STRIO_EOF;
|
||||||
|
return Qnil;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
/* fall through */
|
/* fall through */
|
||||||
case 0:
|
case 0:
|
||||||
len = RSTRING(ptr->string)->len - ptr->pos;
|
len = RSTRING(ptr->string)->len - ptr->pos;
|
||||||
|
if (len == 0 && ptr->pos == RSTRING(ptr->string)->len) {
|
||||||
|
if (ptr->flags & STRIO_EOF) return Qnil;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
rb_raise(rb_eArgError, "wrong number arguments (%d for 0)", argc);
|
rb_raise(rb_eArgError, "wrong number arguments (%d for 0)", argc);
|
||||||
}
|
}
|
||||||
str = rb_str_substr(ptr->string, ptr->pos, len);
|
str = rb_str_substr(ptr->string, ptr->pos, len);
|
||||||
ptr->pos += len;
|
if (len > 0 &&
|
||||||
|
(NIL_P(str) || (ptr->pos += RSTRING(str)->len) >= RSTRING(ptr->string)->len)) {
|
||||||
|
ptr->flags |= STRIO_EOF;
|
||||||
|
}
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user