summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-02-13 09:30:40 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-02-13 09:30:40 +0000
commitdf77202b372535d58fccc244113fd9944c7b01c0 (patch)
treed74b17fe865023828138a7d841428ce90864fedb
parent23ebb5ea6cdff94d840b34ab7ef78e73ee442e75 (diff)
* io.c (io_setstrbuf): defer resizing buffer string until data is
read actually. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34580 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--io.c45
-rw-r--r--test/ruby/test_io.rb30
3 files changed, 46 insertions, 34 deletions
diff --git a/ChangeLog b/ChangeLog
index 2656236953..73a199726e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Mon Feb 13 18:30:32 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * io.c (io_setstrbuf): defer resizing buffer string until data is
+ read actually.
+
Mon Feb 13 10:24:39 2012 Loren Segal <lsegal@soen.ca>
* io.c (Init_IO): use directive hack to make ARGF documentable
diff --git a/io.c b/io.c
index e947a93b72..541263980e 100644
--- a/io.c
+++ b/io.c
@@ -1871,13 +1871,12 @@ io_bufread(char *ptr, long len, rb_io_t *fptr)
}
static long
-io_fread(VALUE str, long offset, rb_io_t *fptr)
+io_fread(VALUE str, long offset, long size, rb_io_t *fptr)
{
long len;
rb_str_locktmp(str);
- len = io_bufread(RSTRING_PTR(str) + offset, RSTRING_LEN(str) - offset,
- fptr);
+ len = io_bufread(RSTRING_PTR(str) + offset, size, fptr);
rb_str_unlocktmp(str);
if (len < 0) rb_sys_fail_path(fptr->pathv);
return len;
@@ -2076,26 +2075,16 @@ static void
io_setstrbuf(VALUE *str,long len)
{
#ifdef _WIN32
+ len = (len + 1) & ~1L; /* round up for wide char */
+#endif
if (NIL_P(*str)) {
- *str = rb_str_new(0, len+1);
- rb_str_set_len(*str,len);
- }
- else {
- StringValue(*str);
- rb_str_modify(*str);
- rb_str_resize(*str, len+1);
- rb_str_set_len(*str,len);
- }
-#else
- if (NIL_P(*str)) {
- *str = rb_str_new(0, len);
+ *str = rb_str_new(0, 0);
}
else {
StringValue(*str);
- rb_str_modify(*str);
- rb_str_resize(*str, len);
+ len -= RSTRING_LEN(*str);
}
-#endif
+ rb_str_modify_expand(*str, len);
}
static VALUE
@@ -2141,18 +2130,19 @@ read_all(rb_io_t *fptr, long siz, VALUE str)
io_setstrbuf(&str,siz);
for (;;) {
READ_CHECK(fptr);
- n = io_fread(str, bytes, fptr);
+ n = io_fread(str, bytes, siz - bytes, fptr);
if (n == 0 && bytes == 0) {
+ rb_str_set_len(str, 0);
break;
}
bytes += n;
+ rb_str_set_len(str, bytes);
if (cr != ENC_CODERANGE_BROKEN)
pos += rb_str_coderange_scan_restartable(RSTRING_PTR(str) + pos, RSTRING_PTR(str) + bytes, enc, &cr);
if (bytes < siz) break;
siz += BUFSIZ;
- rb_str_resize(str, siz);
+ rb_str_modify_expand(str, BUFSIZ);
}
- if (bytes != siz) rb_str_resize(str, bytes);
str = io_enc_str(str, fptr);
ENC_CODERANGE_SET(str, cr);
return str;
@@ -2219,7 +2209,7 @@ io_getpartial(int argc, VALUE *argv, VALUE io, int nonblock)
rb_sys_fail_path(fptr->pathv);
}
}
- rb_str_resize(str, n);
+ rb_str_set_len(str, n);
if (n == 0)
return Qnil;
@@ -2535,18 +2525,14 @@ io_read(int argc, VALUE *argv, VALUE io)
#if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
previous_mode = set_binary_mode_with_seek_cur(fptr);
#endif
- n = io_fread(str, 0, fptr);
+ n = io_fread(str, 0, len, fptr);
+ rb_str_set_len(str, n);
#if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
if (previous_mode == O_TEXT) {
setmode(fptr->fd, O_TEXT);
}
#endif
- if (n == 0) {
- if (fptr->fd < 0) return Qnil;
- rb_str_resize(str, 0);
- return Qnil;
- }
- rb_str_resize(str, n);
+ if (n == 0) return Qnil;
OBJ_TAINT(str);
return str;
@@ -4284,7 +4270,6 @@ rb_io_sysread(int argc, VALUE *argv, VALUE io)
if (n == 0 && ilen > 0) {
rb_eof_error();
}
- rb_str_resize(str, n);
OBJ_TAINT(str);
return str;
diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb
index 81b8e4aeab..83d7fda24b 100644
--- a/test/ruby/test_io.rb
+++ b/test/ruby/test_io.rb
@@ -972,7 +972,7 @@ class TestIO < Test::Unit::TestCase
with_pipe do |r, w|
s = ""
t = Thread.new { r.readpartial(5, s) }
- Thread.pass until s.size == 5
+ Thread.pass until t.stop?
assert_raise(RuntimeError) { s.clear }
w.write "foobarbaz"
w.close
@@ -991,6 +991,17 @@ class TestIO < Test::Unit::TestCase
}
end
+ def test_readpartial_buffer_error
+ with_pipe do |r, w|
+ s = ""
+ t = Thread.new { r.readpartial(5, s) }
+ Thread.pass until t.stop?
+ t.kill
+ t.value
+ assert_equal("", s)
+ end
+ end
+
def test_read
pipe(proc do |w|
w.write "foobarbaz"
@@ -1007,7 +1018,7 @@ class TestIO < Test::Unit::TestCase
with_pipe do |r, w|
s = ""
t = Thread.new { r.read(5, s) }
- Thread.pass until s.size == 5
+ Thread.pass until t.stop?
assert_raise(RuntimeError) { s.clear }
w.write "foobarbaz"
w.close
@@ -1015,6 +1026,17 @@ class TestIO < Test::Unit::TestCase
end
end
+ def test_read_buffer_error
+ with_pipe do |r, w|
+ s = ""
+ t = Thread.new { r.read(5, s) }
+ Thread.pass until t.stop?
+ t.kill
+ t.value
+ assert_equal("", s)
+ end
+ end
+
def test_write_nonblock
skip "IO#write_nonblock is not supported on file/pipe." if /mswin|bccwin|mingw/ =~ RUBY_PLATFORM
pipe(proc do |w|
@@ -2117,8 +2139,8 @@ End
end
}
IO.pipe {|r,w|
- assert(r.close_on_exec?)
- assert(w.close_on_exec?)
+ assert(r.close_on_exec?)
+ assert(w.close_on_exec?)
}
end