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.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64178 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
eregon 2018-08-03 15:11:49 +00:00
parent e7da0fc34e
commit a2c7d0cea9
2 changed files with 15 additions and 8 deletions

View File

@ -793,24 +793,26 @@ rb_enc_get_index(VALUE obj)
obj = rb_sym2str(obj); obj = rb_sym2str(obj);
} }
switch (BUILTIN_TYPE(obj)) { switch (BUILTIN_TYPE(obj)) {
as_default:
default:
case T_STRING: case T_STRING:
case T_SYMBOL:
case T_REGEXP: case T_REGEXP:
i = enc_get_index_str(obj); i = enc_get_index_str(obj);
break; break;
case T_FILE: case T_FILE:
tmp = rb_funcallv(obj, rb_intern("internal_encoding"), 0, 0); tmp = rb_funcallv(obj, rb_intern("internal_encoding"), 0, 0);
if (NIL_P(tmp)) obj = rb_funcallv(obj, rb_intern("external_encoding"), 0, 0); if (NIL_P(tmp)) {
else obj = tmp; tmp = rb_funcallv(obj, rb_intern("external_encoding"), 0, 0);
if (NIL_P(obj)) break; }
if (is_data_encoding(tmp)) {
i = enc_check_encoding(tmp);
}
break;
case T_DATA: case T_DATA:
if (is_data_encoding(obj)) { if (is_data_encoding(obj)) {
i = enc_check_encoding(obj); i = enc_check_encoding(obj);
} }
else { break;
goto as_default; default:
}
break; break;
} }
return i; return i;

View File

@ -147,6 +147,11 @@ describe "C-API Encoding function" do
it "returns -1 as the index for immediates" do it "returns -1 as the index for immediates" do
@s.send(@method, 1).should == -1 @s.send(@method, 1).should == -1
end end
it "returns -1 for an object without an encoding" do
obj = Object.new
@s.send(@method, obj).should == -1
end
end end
describe "rb_enc_set_index" do describe "rb_enc_set_index" do