Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions ext/stringio/stringio.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,20 @@ strio_extract_modeenc(VALUE *vmode_p, VALUE *vperm_p, VALUE opthash,
e = strchr(++n, ':');
len = e ? e - n : (long)strlen(n);
if (len > 0 && len <= ENCODING_MAXNAMELEN) {
rb_encoding *enc;
if (e) {
memcpy(encname, n, len);
encname[len] = '\0';
n = encname;
}
convconfig_p->enc = rb_enc_find(n);
enc = rb_enc_find(n);
if (e)
convconfig_p->enc2 = enc;
else
convconfig_p->enc = enc;
}
if (e && (len = strlen(++e)) > 0 && len <= ENCODING_MAXNAMELEN) {
convconfig_p->enc2 = rb_enc_find(e);
convconfig_p->enc = rb_enc_find(e);
}
}
}
Expand Down Expand Up @@ -1719,7 +1724,14 @@ strio_set_encoding(int argc, VALUE *argv, VALUE self)
enc = rb_default_external_encoding();
}
else {
enc = rb_to_encoding(ext_enc);
enc = rb_find_encoding(ext_enc);
if (!enc) {
struct rb_io_enc_t convconfig;
int oflags, fmode;
VALUE vmode = rb_str_append(rb_str_new_cstr("r:"), ext_enc);
rb_io_extract_modeenc(&vmode, 0, Qnil, &oflags, &fmode, &convconfig);
enc = convconfig.enc2;
}
}
ptr->enc = enc;
if (WRITABLE(self)) {
Expand Down
6 changes: 6 additions & 0 deletions test/stringio/test_stringio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,12 @@ def test_set_encoding
f.set_encoding(Encoding::ASCII_8BIT)
}
assert_equal("foo\x83".b, f.gets)

f = StringIO.new()
f.set_encoding("ISO-8859-16:ISO-8859-1")
assert_equal(Encoding::ISO_8859_16, f.external_encoding)
assert_equal(Encoding::ISO_8859_16, f.string.encoding)
assert_nil(f.internal_encoding)
end

def test_mode_error
Expand Down