summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog5
-rw-r--r--ext/stringio/stringio.c14
-rw-r--r--test/ruby/ut_eof.rb49
3 files changed, 43 insertions, 25 deletions
diff --git a/ChangeLog b/ChangeLog
index 54115c3..b8400de 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Wed Dec 10 19:28:56 2003 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * ext/stringio/stringio.c (strio_read): set EOF flag at short read.
+ [ruby-dev:22223], [ruby-dev:22224]
+
Wed Dec 10 18:07:25 2003 Minero Aoki <aamine@loveruby.net>
* lib/erb.rb: new method ERB#filename(=). [ruby-dev:22208]
diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c
index 2d0229a..88f6be4 100644
--- a/ext/stringio/stringio.c
+++ b/ext/stringio/stringio.c
@@ -857,10 +857,13 @@ strio_read(argc, argv, self)
/* fall through */
case 0:
olen = -1;
- len = RSTRING(ptr->string)->len - ptr->pos;
- if (len == 0 && ptr->pos == RSTRING(ptr->string)->len) {
+ len = RSTRING(ptr->string)->len;
+ if (len <= ptr->pos) {
if (ptr->flags & STRIO_EOF) return Qnil;
- ptr->flags |= STRIO_EOF;
+ len = 0;
+ }
+ else {
+ len -= ptr->pos;
}
break;
default:
@@ -869,11 +872,12 @@ strio_read(argc, argv, self)
str = rb_str_substr(ptr->string, ptr->pos, len);
if (NIL_P(str)) {
if (!(ptr->flags & STRIO_EOF)) str = rb_str_new(0, 0);
- if (olen) ptr->flags |= STRIO_EOF;
+ len = 0;
}
else {
- ptr->pos += RSTRING(str)->len;
+ ptr->pos += len = RSTRING(str)->len;
}
+ if (olen < 0 || olen > len) ptr->flags |= STRIO_EOF;
return str;
}
diff --git a/test/ruby/ut_eof.rb b/test/ruby/ut_eof.rb
index efb9f77..d4b0b6d 100644
--- a/test/ruby/ut_eof.rb
+++ b/test/ruby/ut_eof.rb
@@ -6,13 +6,13 @@ module TestEOF
assert_equal("", f.read(0))
assert_equal("", f.read(0))
assert_equal("", f.read)
- assert_equal(nil, f.read(0))
- assert_equal(nil, f.read(0))
+ assert_nil(f.read(0))
+ assert_nil(f.read(0))
}
open_file("") {|f|
- assert_equal(nil, f.read(1))
- assert_equal(nil, f.read)
- assert_equal(nil, f.read(1))
+ assert_nil(f.read(1))
+ assert_nil(f.read)
+ assert_nil(f.read(1))
}
end
@@ -20,9 +20,9 @@ module TestEOF
return unless respond_to? :open_file_rw
open_file_rw("") {|f|
assert_equal("", f.read)
- assert_equal(nil, f.read)
+ assert_nil(f.read)
assert_equal(0, f.syswrite(""))
- assert_equal(nil, f.read)
+ assert_nil(f.read)
}
end
@@ -33,24 +33,33 @@ module TestEOF
assert_equal("" , f.read(0))
assert_equal("" , f.read(0))
assert_equal("", f.read)
- assert_equal(nil, f.read(0))
- assert_equal(nil, f.read(0))
+ assert_nil(f.read(0))
+ assert_nil(f.read(0))
}
open_file("a") {|f|
assert_equal("a", f.read(1))
- assert_equal(nil, f.read(1))
+ assert_nil(f.read(1))
}
open_file("a") {|f|
assert_equal("a", f.read(2))
- assert_equal(nil, f.read(1))
- assert_equal(nil, f.read)
- assert_equal(nil, f.read(1))
+ assert_nil(f.read(1))
+ assert_nil(f.read)
+ assert_nil(f.read(1))
}
open_file("a") {|f|
assert_equal("a", f.read)
- assert_equal(nil, f.read(1))
- assert_equal(nil, f.read)
- assert_equal(nil, f.read(1))
+ assert_nil(f.read(1))
+ assert_nil(f.read)
+ assert_nil(f.read(1))
+ }
+ open_file("a") {|f|
+ assert_equal("a", f.read(2))
+ assert_nil(f.read)
+ assert_nil(f.read)
+ }
+ open_file("a") {|f|
+ assert_equal("a", f.read)
+ assert_nil(f.read(0))
}
end
@@ -67,19 +76,19 @@ module TestEOF
assert_equal(10, f.pos)
assert_equal("", f.read(0))
assert_equal("", f.read)
- assert_equal(nil, f.read(0))
- assert_equal(nil, f.read)
+ assert_nil(f.read(0))
+ assert_nil(f.read)
}
end
def test_eof_1_seek
open_file_seek("a", 10) {|f|
assert_equal("", f.read)
- assert_equal(nil, f.read)
+ assert_nil(f.read)
}
open_file_seek("a", 1) {|f|
assert_equal("", f.read)
- assert_equal(nil, f.read)
+ assert_nil(f.read)
}
end
end