diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c index 54d36c9..cab9d92 100644 --- a/ext/stringio/stringio.c +++ b/ext/stringio/stringio.c @@ -1209,7 +1209,7 @@ strio_putc(VALUE self, VALUE ch) /* * call-seq: - * strio.read([length [, buffer]]) -> string, buffer, or nil + * strio.read([length [, outbuf]]) -> string, outbuf, or nil * * See IO#read. */ diff --git a/io.c b/io.c index f0c8515..120d8ce 100644 --- a/io.c +++ b/io.c @@ -2281,6 +2281,8 @@ io_getpartial(int argc, VALUE *argv, VALUE io, int nonblock) * It doesn't block if some data available. * If the optional outbuf argument is present, * it must reference a String, which will receive the data. + * The outbuf is to contain only the received data after the method call + * even though it is not empty at the beginning. * It raises EOFError on end of file. * * readpartial is designed for streams such as pipe, socket, tty, etc. @@ -2351,6 +2353,8 @@ io_readpartial(int argc, VALUE *argv, VALUE io) * * If the optional outbuf argument is present, * it must reference a String, which will receive the data. + * The outbuf is to contain only the received data after the method call + * even though it is not empty at the beginning. * * read_nonblock just calls the read(2) system call. * It causes all errors the read(2) system call causes: Errno::EWOULDBLOCK, Errno::EINTR, etc. @@ -2485,7 +2489,7 @@ rb_io_write_nonblock(VALUE io, VALUE str) /* * call-seq: - * ios.read([length [, buffer]]) -> string, buffer, or nil + * ios.read([length [, outbuf]]) -> string, outbuf, or nil * * Reads length bytes from the I/O stream. * @@ -2505,8 +2509,10 @@ rb_io_write_nonblock(VALUE io, VALUE str) * * If length is zero, it returns "". * - * If the optional buffer argument is present, it must reference + * If the optional outbuf argument is present, it must reference * a String, which will receive the data. + * The outbuf is to contain only the received data after the method call + * even though it is not empty at the beginning. * * At end of file, it returns nil or "" * depend on length. @@ -4284,6 +4290,8 @@ rb_io_syswrite(VALUE io, VALUE str) * that read from ios or you may get unpredictable results. * If the optional outbuf argument is present, it must reference * a String, which will receive the data. + * The outbuf is to contain only the received data after the method call + * even though it is not empty at the beginning. * Raises SystemCallError on error and * EOFError at end of file. * @@ -10335,7 +10343,7 @@ argf_eof(VALUE argf) /* * call-seq: - * ARGF.read([length [, buffer]]) -> string, buffer, or nil + * ARGF.read([length [, outbuf]]) -> string, outbuf, or nil * * Reads _length_ bytes from ARGF. The files named on the command line * are concatenated and treated as a single file by this method, so when @@ -10352,8 +10360,10 @@ argf_eof(VALUE argf) * * If _length_ is zero, it returns _""_. * - * If the optional _buffer_ argument is present, it must reference a String, + * If the optional _outbuf_ argument is present, it must reference a String, * which will receive the data. + * The _outbuf_ is to contain only the received data after the method call + * even though it is not empty at the beginning. * * For example: * @@ -10439,7 +10449,10 @@ static VALUE argf_getpartial(int argc, VALUE *argv, VALUE argf, int nonblock); * Reads at most _maxlen_ bytes from the ARGF stream. It blocks only if * +ARGF+ has no data immediately available. If the optional _outbuf_ * argument is present, it must reference a String, which will receive the - * data. It raises EOFError on end of file. + * data. + * The _outbuf_ is to contain only the received data after the method call + * even though it is not empty at the beginning. + * It raises EOFError on end of file. * * +readpartial+ is designed for streams such as pipes, sockets, and ttys. It * blocks only when no data is immediately available. This means that it diff --git a/test/ruby/test_argf.rb b/test/ruby/test_argf.rb index 2f1ddf5..284f342 100644 --- a/test/ruby/test_argf.rb +++ b/test/ruby/test_argf.rb @@ -446,6 +446,16 @@ class TestArgf < Test::Unit::TestCase end end + def test_read2_with_not_empty_buffer + ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| + s = "0123456789" + ARGF.read(8, s) + p s + SRC + assert_equal("\"1\\n2\\n3\\n4\\n\"\n", f.read) + end + end + def test_read3 ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| nil while ARGF.gets @@ -463,6 +473,8 @@ class TestArgf < Test::Unit::TestCase loop do s << ARGF.readpartial(1) t = ""; ARGF.readpartial(1, t); s << t + # not empty buffer + u = "abcdef"; ARGF.readpartial(1, u); s << u end rescue EOFError puts s diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb index 0f4d523..d984252 100644 --- a/test/ruby/test_io.rb +++ b/test/ruby/test_io.rb @@ -991,6 +991,16 @@ class TestIO < Test::Unit::TestCase } end + def test_readpartial_with_not_empty_buffer + pipe(proc do |w| + w.write "foob" + w.close + end, proc do |r| + r.readpartial(5, s = "01234567") + assert_equal("foob", s) + end) + end + def test_readpartial_buffer_error with_pipe do |r, w| s = "" @@ -1026,6 +1036,16 @@ class TestIO < Test::Unit::TestCase end end + def test_read_with_not_empty_buffer + pipe(proc do |w| + w.write "foob" + w.close + end, proc do |r| + r.read(nil, s = "01234567") + assert_equal("foob", s) + end) + end + def test_read_buffer_error with_pipe do |r, w| s = "" @@ -1047,6 +1067,17 @@ class TestIO < Test::Unit::TestCase end) end + def test_read_nonblock_with_not_empty_buffer + skip "IO#read_nonblock is not supported on file/pipe." if /mswin|bccwin|mingw/ =~ RUBY_PLATFORM + pipe(proc do |w| + w.write "foob" + w.close + end, proc do |r| + r.read_nonblock(5, s = "01234567") + assert_equal("foob", s) + end) + end + def test_read_nonblock_error return if !have_nonblock? skip "IO#read_nonblock is not supported on file/pipe." if /mswin|bccwin|mingw/ =~ RUBY_PLATFORM @@ -1417,6 +1448,16 @@ class TestIO < Test::Unit::TestCase end end + def test_sysread_with_not_empty_buffer + pipe(proc do |w| + w.write "foob" + w.close + end, proc do |r| + r.sysread( 5, s = "01234567" ) + assert_equal( "foob", s ) + end) + end + def test_flag t = make_tempfile diff --git a/test/stringio/test_stringio.rb b/test/stringio/test_stringio.rb index 26fcc62..22cba38 100644 --- a/test/stringio/test_stringio.rb +++ b/test/stringio/test_stringio.rb @@ -426,6 +426,11 @@ class TestStringIO < Test::Unit::TestCase s = "" f.read(nil, s) assert_equal("\u3042\u3044", s, bug5207) + f.rewind + # not empty buffer + s = "0123456789" + f.read(nil, s) + assert_equal("\u3042\u3044", s) end def test_readpartial @@ -435,6 +440,10 @@ class TestStringIO < Test::Unit::TestCase assert_equal("\u3042\u3044", f.readpartial) f.rewind assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.readpartial(f.size)) + f.rewind + # not empty buffer + s = '0123456789' + assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.readpartial(f.size, s)) end def test_read_nonblock @@ -444,6 +453,10 @@ class TestStringIO < Test::Unit::TestCase assert_equal("\u3042\u3044", f.read_nonblock) f.rewind assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.read_nonblock(f.size)) + f.rewind + # not empty buffer + s = '0123456789' + assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.read_nonblock(f.size, s)) end def test_size