summaryrefslogtreecommitdiff
path: root/ext/stringio
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-08-20 01:43:26 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-08-20 01:43:26 +0000
commitdcb7cc1a8b76b1691b59192474cb7f193acd75cd (patch)
tree641b343f1c9b7ee05d53d6d7e0a6bd2c2519b850 /ext/stringio
parente9930a4b56c874a73d9400a5568e3fa52a139138 (diff)
* ext/stringio/stringio.c (strio_read): return new string if nil
is explicitly given as a buffer ([Bug #5207]), otherwise set the encoding. also removed dead code. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@33010 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/stringio')
-rw-r--r--ext/stringio/stringio.c22
1 files changed, 12 insertions, 10 deletions
diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c
index 68baf35..5aa2d27 100644
--- a/ext/stringio/stringio.c
+++ b/ext/stringio/stringio.c
@@ -1219,12 +1219,15 @@ strio_read(int argc, VALUE *argv, VALUE self)
struct StringIO *ptr = readable(StringIO(self));
VALUE str = Qnil;
long len;
+ int binary = 0;
switch (argc) {
case 2:
str = argv[1];
- StringValue(str);
- rb_str_modify(str);
+ if (!NIL_P(str)) {
+ StringValue(str);
+ rb_str_modify(str);
+ }
case 1:
if (!NIL_P(argv[0])) {
len = NUM2LONG(argv[0]);
@@ -1235,6 +1238,7 @@ strio_read(int argc, VALUE *argv, VALUE self)
if (!NIL_P(str)) rb_str_resize(str, 0);
return Qnil;
}
+ binary = 1;
break;
}
/* fall through */
@@ -1258,21 +1262,19 @@ strio_read(int argc, VALUE *argv, VALUE self)
}
if (NIL_P(str)) {
str = strio_substr(ptr, ptr->pos, len);
- if (argc > 0) rb_enc_associate(str, rb_ascii8bit_encoding());
+ if (binary) rb_enc_associate(str, rb_ascii8bit_encoding());
}
else {
long rest = RSTRING_LEN(ptr->string) - ptr->pos;
if (len > rest) len = rest;
rb_str_resize(str, len);
MEMCPY(RSTRING_PTR(str), RSTRING_PTR(ptr->string) + ptr->pos, char, len);
+ if (binary)
+ rb_enc_associate(str, rb_ascii8bit_encoding());
+ else
+ rb_enc_copy(str, ptr->string);
}
- if (NIL_P(str)) {
- str = rb_str_new(0, 0);
- len = 0;
- }
- else {
- ptr->pos += len = RSTRING_LEN(str);
- }
+ ptr->pos += RSTRING_LEN(str);
return str;
}