diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-09-16 02:39:18 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-09-16 02:39:18 +0000 |
commit | 59383b1f9cffc2815d4a213710be253d9b6114d4 (patch) | |
tree | e34c754cdde6b80559a115615e98b8747a6f5cf5 /io.c | |
parent | 4a906510a289475bb4baf662a70e07e6b01fad3b (diff) |
io.c: io_set_read_length
* io.c (io_set_read_length): if the read length equals to the buffer
string size then nothing to do. or ensure the string modifiable
before setting the length only when the former is shorter. based on
the patch in [ruby-core:47541] by Hiroshi Shirosaki.
[ruby-core:46586] [Bug #6764]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@36980 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'io.c')
-rw-r--r-- | io.c | 15 |
1 files changed, 12 insertions, 3 deletions
@@ -2158,6 +2158,15 @@ io_setstrbuf(VALUE *str, long len) rb_str_modify_expand(*str, len); } +static void +io_set_read_length(VALUE str, long n) +{ + if (RSTRING_LEN(str) != n) { + rb_str_modify(str); + rb_str_set_len(str, n); + } +} + static VALUE read_all(rb_io_t *fptr, long siz, VALUE str) { @@ -2281,7 +2290,7 @@ io_getpartial(int argc, VALUE *argv, VALUE io, int nonblock) rb_sys_fail_path(fptr->pathv); } } - rb_str_set_len(str, n); + io_set_read_length(str, n); if (n == 0) return Qnil; @@ -2602,7 +2611,7 @@ io_read(int argc, VALUE *argv, VALUE io) previous_mode = set_binary_mode_with_seek_cur(fptr); #endif n = io_fread(str, 0, len, fptr); - rb_str_set_len(str, n); + io_set_read_length(str, n); #if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32) if (previous_mode == O_TEXT) { setmode(fptr->fd, O_TEXT); @@ -4390,7 +4399,7 @@ rb_io_sysread(int argc, VALUE *argv, VALUE io) if (n == -1) { rb_sys_fail_path(fptr->pathv); } - rb_str_set_len(str, n); + io_set_read_length(str, n); if (n == 0 && ilen > 0) { rb_eof_error(); } |