diff options
| -rw-r--r-- | mrbgems/mruby-io/README.md | 6 | ||||
| -rw-r--r-- | mrbgems/mruby-io/mrblib/io.rb | 18 | ||||
| -rw-r--r-- | mrbgems/mruby-io/src/io.c | 3 | ||||
| -rw-r--r-- | mrbgems/mruby-io/test/io.rb | 9 |
4 files changed, 31 insertions, 5 deletions
diff --git a/mrbgems/mruby-io/README.md b/mrbgems/mruby-io/README.md index db52c4a74..2c5d7a9ac 100644 --- a/mrbgems/mruby-io/README.md +++ b/mrbgems/mruby-io/README.md @@ -59,7 +59,7 @@ Add the line below to your build configuration. | IO#fileno, IO#to_i | o | | | IO#flush | o | | | IO#fsync | | | -| IO#getbyte | | | +| IO#getbyte | o | | | IO#getc | o | | | IO#gets | o | | | IO#internal_encoding | | | @@ -77,7 +77,7 @@ Add the line below to your build configuration. | IO#puts | o | | | IO#read | o | | | IO#read_nonblock | | | -| IO#readbyte | | | +| IO#readbyte | o | | | IO#readchar | o | | | IO#readline | o | | | IO#readlines | o | | @@ -93,7 +93,7 @@ Add the line below to your build configuration. | IO#sysseek | o | | | IO#syswrite | o | | | IO#to_io | | | -| IO#ungetbyte | | | +| IO#ungetbyte | o | | | IO#ungetc | o | | | IO#write | o | | | IO#write_nonblock | | | diff --git a/mrbgems/mruby-io/mrblib/io.rb b/mrbgems/mruby-io/mrblib/io.rb index 034f88529..a60562846 100644 --- a/mrbgems/mruby-io/mrblib/io.rb +++ b/mrbgems/mruby-io/mrblib/io.rb @@ -184,6 +184,11 @@ class IO nil end + def ungetbyte(substr) + substr = substr.chr if substr.is_a? Integer + ungetc substr + end + def read(length = nil, outbuf = "") unless length.nil? unless length.is_a? Integer @@ -290,12 +295,21 @@ class IO begin readchar rescue EOFError - c = @buf[0] - @buf[0,1]="" if c nil end end + def readbyte + _read_buf + IO._bufread(@buf, 1).getbyte(0) + end + + def getbyte + readbyte + rescue EOFError + nil + end + # 15.2.20.5.3 def each(&block) return to_enum unless block diff --git a/mrbgems/mruby-io/src/io.c b/mrbgems/mruby-io/src/io.c index d29f839f9..234d7b1fc 100644 --- a/mrbgems/mruby-io/src/io.c +++ b/mrbgems/mruby-io/src/io.c @@ -1476,6 +1476,9 @@ mrb_io_bufread(mrb_state *mrb, mrb_value self) mrb_int len; mrb_get_args(mrb, "Si", &str, &len); + mrb_assert(RSTRING_LEN(str) > 0); + mrb_assert(RSTRING_PTR(str) != NULL); + mrb_str_modify(mrb, RSTRING(str)); return io_bufread(mrb, str, len); } diff --git a/mrbgems/mruby-io/test/io.rb b/mrbgems/mruby-io/test/io.rb index 328b5292f..88a89fee4 100644 --- a/mrbgems/mruby-io/test/io.rb +++ b/mrbgems/mruby-io/test/io.rb @@ -105,6 +105,15 @@ assert('IO#getc', '15.2.20.5.8') do io.close end +assert('IO#getbyte') do + io = IO.new(IO.sysopen($mrbtest_io_rfname)) + $mrbtest_io_msg.split("").each do |ch| + assert_equal ch.getbyte(0), io.getbyte + end + assert_equal nil, io.getbyte + io.close +end + #assert('IO#gets', '15.2.20.5.9') do #assert('IO#initialize_copy', '15.2.20.5.10') do #assert('IO#print', '15.2.20.5.11') do |
