summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTomoyuki Sahara <[email protected]>2016-09-30 16:27:11 +0900
committerTomoyuki Sahara <[email protected]>2016-09-30 16:27:11 +0900
commit2229a2aa0fcd76bd5498417aca934d6ec7a211ef (patch)
treef1892774b9ce4f8d583802b748d71d94e1e7428c
parent51cf6b6048d7e4182d8bb8d58c7bcac07cb3064b (diff)
downloadmruby-2229a2aa0fcd76bd5498417aca934d6ec7a211ef.tar.gz
mruby-2229a2aa0fcd76bd5498417aca934d6ec7a211ef.zip
reimplement #eof. fixes #66.
-rw-r--r--mrblib/io.rb27
-rw-r--r--test/io.rb25
2 files changed, 26 insertions, 26 deletions
diff --git a/mrblib/io.rb b/mrblib/io.rb
index 4bcf4f800..8408aea87 100644
--- a/mrblib/io.rb
+++ b/mrblib/io.rb
@@ -133,20 +133,12 @@ class IO
end
def eof?
- return true if @buf && @buf.size > 0
-
- ret = false
- char = ''
-
begin
- char = sysread(1)
- rescue EOFError => e
- ret = true
- ensure
- _ungets(char)
+ buf = _read_buf
+ return buf.size == 0
+ rescue EOFError
+ return true
end
-
- ret
end
alias_method :eof, :eof?
@@ -176,24 +168,17 @@ class IO
@buf = sysread(BUF_SIZE)
end
- def _ungets(substr)
+ def ungetc(substr)
raise TypeError.new "expect String, got #{substr.class}" unless substr.is_a?(String)
- raise IOError if @pos == 0 || @pos.nil?
@pos -= substr.size
if @buf.empty?
- @buf = substr
+ @buf = substr.dup
else
@buf = substr + @buf
end
nil
end
- def ungetc(char)
- raise IOError if @pos == 0 || @pos.nil?
- _ungets(char)
- nil
- end
-
def read(length = nil)
unless length.nil?
unless length.is_a? Fixnum
diff --git a/test/io.rb b/test/io.rb
index 1b0a2d52e..c3f78da7e 100644
--- a/test/io.rb
+++ b/test/io.rb
@@ -70,14 +70,29 @@ end
#assert('IO#each_line', '15.2.20.5.5') do
assert('IO#eof?', '15.2.20.5.6') do
+ if false # XXX: not implemented yet
+ io = IO.new(IO.sysopen($mrbtest_io_wfname, 'w'), 'w')
+ assert_raise(IOError) do
+ io.eof?
+ end
+ end
+
+ # empty file
+ io = IO.open(IO.sysopen($mrbtest_io_wfname, 'w'), 'w')
+ io.close
+ io = IO.open(IO.sysopen($mrbtest_io_wfname, 'r'), 'r')
+ assert_true io.eof?
+ io.close
+
+ # nonempty file
io = IO.new(IO.sysopen($mrbtest_io_rfname))
- $mrbtest_io_msg.each_char { |ch|
- # XXX
- #assert_false io.eof?
- io.getc
- }
+ assert_false io.eof?
+ io.readchar
+ assert_false io.eof?
+ io.read
assert_true io.eof?
io.close
+
true
end