From d28d075cc1a2907e2b9db0d37b52b1711c9097c3 Mon Sep 17 00:00:00 2001 From: nobu Date: Wed, 1 Aug 2012 22:29:26 +0000 Subject: [PATCH] RB_TYPE_P BUILTIN_TYPE * string.c, vm_insnhelper.c, vm_method.c: use RB_TYPE_P() and BUILTIN_TYPE() if possible. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@36589 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- string.c | 26 +++++++++++++++++++------- vm_insnhelper.c | 6 +----- vm_method.c | 4 +--- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/string.c b/string.c index c3a0ae2038..26463b95e2 100644 --- a/string.c +++ b/string.c @@ -2539,7 +2539,8 @@ rb_str_index_m(int argc, VALUE *argv, VALUE str) } } - switch (TYPE(sub)) { + if (SPECIAL_CONST_P(sub)) goto generic; + switch (BUILTIN_TYPE(sub)) { case T_REGEXP: if (pos > str_strlen(str, STR_ENC_GET(str))) return Qnil; @@ -2550,6 +2551,7 @@ rb_str_index_m(int argc, VALUE *argv, VALUE str) pos = rb_str_sublen(str, pos); break; + generic: default: { VALUE tmp; @@ -2653,7 +2655,8 @@ rb_str_rindex_m(int argc, VALUE *argv, VALUE str) pos = len; } - switch (TYPE(sub)) { + if (SPECIAL_CONST_P(sub)) goto generic; + switch (BUILTIN_TYPE(sub)) { case T_REGEXP: /* enc = rb_get_check(str, sub); */ pos = str_offset(RSTRING_PTR(str), RSTRING_END(str), pos, @@ -2666,6 +2669,7 @@ rb_str_rindex_m(int argc, VALUE *argv, VALUE str) if (pos >= 0) return LONG2NUM(pos); break; + generic: default: { VALUE tmp; @@ -2702,13 +2706,15 @@ rb_str_rindex_m(int argc, VALUE *argv, VALUE str) static VALUE rb_str_match(VALUE x, VALUE y) { - switch (TYPE(y)) { + if (SPECIAL_CONST_P(y)) goto generic; + switch (BUILTIN_TYPE(y)) { case T_STRING: rb_raise(rb_eTypeError, "type mismatch: String given"); case T_REGEXP: return rb_reg_match(y, x); + generic: default: return rb_funcall(y, rb_intern("=~"), 1, x); } @@ -3163,15 +3169,17 @@ rb_str_aref(VALUE str, VALUE indx) { long idx; - switch (TYPE(indx)) { - case T_FIXNUM: + if (FIXNUM_P(indx)) { idx = FIX2LONG(indx); num_index: str = rb_str_substr(str, idx, 1); if (!NIL_P(str) && RSTRING_LEN(str) == 0) return Qnil; return str; + } + if (SPECIAL_CONST_P(indx)) goto generic; + switch (BUILTIN_TYPE(indx)) { case T_REGEXP: return rb_str_subpat(str, indx, INT2FIX(0)); @@ -3180,6 +3188,7 @@ rb_str_aref(VALUE str, VALUE indx) return rb_str_dup(indx); return Qnil; + generic: default: /* check if indx is Range */ { @@ -3440,13 +3449,15 @@ rb_str_aset(VALUE str, VALUE indx, VALUE val) { long idx, beg; - switch (TYPE(indx)) { - case T_FIXNUM: + if (FIXNUM_P(indx)) { idx = FIX2LONG(indx); num_index: rb_str_splice(str, idx, 1, val); return val; + } + if (SPECIAL_CONST_P(indx)) goto generic; + switch (TYPE(indx)) { case T_REGEXP: rb_str_subpat_set(str, indx, INT2FIX(0), val); return val; @@ -3460,6 +3471,7 @@ rb_str_aset(VALUE str, VALUE indx, VALUE val) rb_str_splice(str, beg, str_strlen(indx, 0), val); return val; + generic: default: /* check if indx is Range */ { diff --git a/vm_insnhelper.c b/vm_insnhelper.c index 1b23ee6e18..074e24cd36 100644 --- a/vm_insnhelper.c +++ b/vm_insnhelper.c @@ -1175,11 +1175,7 @@ static inline void vm_check_if_namespace(VALUE klass) { VALUE str; - switch (TYPE(klass)) { - case T_CLASS: - case T_MODULE: - break; - default: + if (!RB_TYPE_P(klass, T_CLASS) && !RB_TYPE_P(klass, T_MODULE)) { str = rb_inspect(klass); rb_raise(rb_eTypeError, "%s is not a class/module", StringValuePtr(str)); diff --git a/vm_method.c b/vm_method.c index af7c99a95f..f344355f4f 100644 --- a/vm_method.c +++ b/vm_method.c @@ -653,9 +653,7 @@ rb_undef(VALUE klass, ID id) if (FL_TEST(c, FL_SINGLETON)) { VALUE obj = rb_ivar_get(klass, attached); - switch (TYPE(obj)) { - case T_MODULE: - case T_CLASS: + if (RB_TYPE_P(obj, T_MODULE) || RB_TYPE_P(obj, T_CLASS)) { c = obj; s0 = ""; }