ruby-changes:72013
From: Nobuyoshi <ko1@a...>
Date: Mon, 30 May 2022 14:49:02 +0900 (JST)
Subject: [ruby-changes:72013] d3e986d9ab (master): [ruby/stringio] Accept external and internal encodings pair
https://git.ruby-lang.org/ruby.git/commit/?id=d3e986d9ab From d3e986d9ab9a880b58a0b4fc68dadc2b10f4cf12 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Mon, 30 May 2022 13:46:31 +0900 Subject: [ruby/stringio] Accept external and internal encodings pair Fix https://github.com/ruby/stringio/pull/16 https://github.com/ruby/stringio/commit/c8a69e80d2 --- ext/stringio/stringio.c | 9 ++++++++- test/stringio/test_stringio.rb | 6 ++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c index 13c8af9216..13f9c28dd1 100644 --- a/ext/stringio/stringio.c +++ b/ext/stringio/stringio.c @@ -1719,7 +1719,14 @@ strio_set_encoding(int argc, VALUE *argv, VALUE self) https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L1719 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)) { diff --git a/test/stringio/test_stringio.rb b/test/stringio/test_stringio.rb index 5393162573..3fcb20e1fa 100644 --- a/test/stringio/test_stringio.rb +++ b/test/stringio/test_stringio.rb @@ -256,6 +256,12 @@ class TestStringIO < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/stringio/test_stringio.rb#L256 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 -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/