ruby-changes:51963
From: eregon <ko1@a...>
Date: Sat, 4 Aug 2018 00:18:08 +0900 (JST)
Subject: [ruby-changes:51963] eregon:r64178 (trunk): encoding.c (rb_enc_get_index): return -1 for non-encoding capable objects
eregon 2018-08-04 00:11:49 +0900 (Sat, 04 Aug 2018) New Revision: 64178 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=64178 Log: encoding.c (rb_enc_get_index): return -1 for non-encoding capable objects * Clarify logic and add spec. * Now passes test-all with the JSON fix. Modified files: trunk/encoding.c trunk/spec/ruby/optional/capi/encoding_spec.rb Index: encoding.c =================================================================== --- encoding.c (revision 64177) +++ encoding.c (revision 64178) @@ -793,24 +793,26 @@ rb_enc_get_index(VALUE obj) https://github.com/ruby/ruby/blob/trunk/encoding.c#L793 obj = rb_sym2str(obj); } switch (BUILTIN_TYPE(obj)) { - as_default: - default: case T_STRING: + case T_SYMBOL: case T_REGEXP: i = enc_get_index_str(obj); break; case T_FILE: tmp = rb_funcallv(obj, rb_intern("internal_encoding"), 0, 0); - if (NIL_P(tmp)) obj = rb_funcallv(obj, rb_intern("external_encoding"), 0, 0); - else obj = tmp; - if (NIL_P(obj)) break; + if (NIL_P(tmp)) { + tmp = rb_funcallv(obj, rb_intern("external_encoding"), 0, 0); + } + if (is_data_encoding(tmp)) { + i = enc_check_encoding(tmp); + } + break; case T_DATA: if (is_data_encoding(obj)) { i = enc_check_encoding(obj); } - else { - goto as_default; - } + break; + default: break; } return i; Index: spec/ruby/optional/capi/encoding_spec.rb =================================================================== --- spec/ruby/optional/capi/encoding_spec.rb (revision 64177) +++ spec/ruby/optional/capi/encoding_spec.rb (revision 64178) @@ -147,6 +147,11 @@ describe "C-API Encoding function" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/optional/capi/encoding_spec.rb#L147 it "returns -1 as the index for immediates" do @s.send(@method, 1).should == -1 end + + it "returns -1 for an object without an encoding" do + obj = Object.new + @s.send(@method, obj).should == -1 + end end describe "rb_enc_set_index" do -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/