num_exact: do not goto into a branch

I'm not necessarily against every goto in general, but jumping into a
branch is definitely a bad idea.  Better refactor.
This commit is contained in:
卜部昌平 2020-06-19 11:58:50 +09:00
parent f402dc3557
commit 0fc569361b
Notes: git 2020-06-29 11:06:42 +09:00

48
time.c
View File

@ -512,47 +512,37 @@ num_exact(VALUE v)
{ {
VALUE tmp; VALUE tmp;
if (NIL_P(v)) { switch (TYPE(v)) {
rb_raise(rb_eTypeError, "can't convert nil into an exact number"); case T_FIXNUM:
} case T_BIGNUM:
else if (RB_INTEGER_TYPE_P(v)) {
return v; return v;
}
else if (RB_TYPE_P(v, T_RATIONAL)) { case T_RATIONAL:
goto rational; return rb_rational_canonicalize(v);
}
else if (RB_TYPE_P(v, T_STRING)) { default:
goto typeerror;
}
else {
if ((tmp = rb_check_funcall(v, idTo_r, 0, NULL)) != Qundef) { if ((tmp = rb_check_funcall(v, idTo_r, 0, NULL)) != Qundef) {
/* test to_int method availability to reject non-Numeric /* test to_int method availability to reject non-Numeric
* objects such as String, Time, etc which have to_r method. */ * objects such as String, Time, etc which have to_r method. */
if (!rb_respond_to(v, idTo_int)) goto typeerror; if (!rb_respond_to(v, idTo_int)) {
/* FALLTHROUGH */
}
else if (RB_INTEGER_TYPE_P(tmp)) {
return tmp;
}
else if (RB_TYPE_P(tmp, T_RATIONAL)) {
return rb_rational_canonicalize(tmp);
}
} }
else if (!NIL_P(tmp = rb_check_to_int(v))) { else if (!NIL_P(tmp = rb_check_to_int(v))) {
return tmp; return tmp;
} }
else {
goto typeerror;
}
}
if (RB_INTEGER_TYPE_P(tmp)) { case T_NIL:
v = tmp; case T_STRING:
}
else if (RB_TYPE_P(tmp, T_RATIONAL)) {
v = tmp;
rational:
if (RRATIONAL(v)->den == INT2FIX(1))
v = RRATIONAL(v)->num;
}
else {
typeerror:
rb_raise(rb_eTypeError, "can't convert %"PRIsVALUE" into an exact number", rb_raise(rb_eTypeError, "can't convert %"PRIsVALUE" into an exact number",
rb_obj_class(v)); rb_obj_class(v));
} }
return v;
} }
/* time_t */ /* time_t */