From fb253d2032e51f333e6577aa4fcf0c03f9c6fc02 Mon Sep 17 00:00:00 2001 From: eregon Date: Fri, 3 Aug 2018 13:52:26 +0000 Subject: encoding.c (rb_enc_get_index): return -1 for non-encoding capable objects * Clarify logic and add spec. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64173 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- encoding.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'encoding.c') diff --git a/encoding.c b/encoding.c index eac4d7c50a..d130eb2612 100644 --- a/encoding.c +++ b/encoding.c @@ -793,24 +793,26 @@ rb_enc_get_index(VALUE obj) 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; -- cgit v1.2.1