ruby-changes:51958
From: eregon <ko1@a...>
Date: Fri, 3 Aug 2018 22:52:32 +0900 (JST)
Subject: [ruby-changes:51958] eregon:r64173 (trunk): encoding.c (rb_enc_get_index): return -1 for non-encoding capable objects
eregon 2018-08-03 22:52:26 +0900 (Fri, 03 Aug 2018) New Revision: 64173 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=64173 Log: encoding.c (rb_enc_get_index): return -1 for non-encoding capable objects * Clarify logic and add spec. Modified files: trunk/encoding.c trunk/spec/ruby/optional/capi/encoding_spec.rb Index: spec/ruby/optional/capi/encoding_spec.rb =================================================================== --- spec/ruby/optional/capi/encoding_spec.rb (revision 64172) +++ spec/ruby/optional/capi/encoding_spec.rb (revision 64173) @@ -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 Index: encoding.c =================================================================== --- encoding.c (revision 64172) +++ encoding.c (revision 64173) @@ -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; -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/