rb_convert_to_integer: 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:
parent
268962077a
commit
06ed9a7a04
Notes:
git
2020-06-29 11:06:56 +09:00
26
object.c
26
object.c
@ -3362,33 +3362,35 @@ rb_convert_to_integer(VALUE val, int base, int raise_exception)
|
|||||||
{
|
{
|
||||||
VALUE tmp;
|
VALUE tmp;
|
||||||
|
|
||||||
|
if (base) {
|
||||||
|
tmp = rb_check_string_type(val);
|
||||||
|
|
||||||
|
if (! NIL_P(tmp)) {
|
||||||
|
val = tmp;
|
||||||
|
}
|
||||||
|
else if (! raise_exception) {
|
||||||
|
return Qnil;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
rb_raise(rb_eArgError, "base specified for non string value");
|
||||||
|
}
|
||||||
|
}
|
||||||
if (RB_FLOAT_TYPE_P(val)) {
|
if (RB_FLOAT_TYPE_P(val)) {
|
||||||
double f;
|
double f = RFLOAT_VALUE(val);
|
||||||
if (base != 0) goto arg_error;
|
|
||||||
f = RFLOAT_VALUE(val);
|
|
||||||
if (!raise_exception && !isfinite(f)) return Qnil;
|
if (!raise_exception && !isfinite(f)) return Qnil;
|
||||||
if (FIXABLE(f)) return LONG2FIX((long)f);
|
if (FIXABLE(f)) return LONG2FIX((long)f);
|
||||||
return rb_dbl2big(f);
|
return rb_dbl2big(f);
|
||||||
}
|
}
|
||||||
else if (RB_INTEGER_TYPE_P(val)) {
|
else if (RB_INTEGER_TYPE_P(val)) {
|
||||||
if (base != 0) goto arg_error;
|
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
else if (RB_TYPE_P(val, T_STRING)) {
|
else if (RB_TYPE_P(val, T_STRING)) {
|
||||||
return rb_str_convert_to_inum(val, base, TRUE, raise_exception);
|
return rb_str_convert_to_inum(val, base, TRUE, raise_exception);
|
||||||
}
|
}
|
||||||
else if (NIL_P(val)) {
|
else if (NIL_P(val)) {
|
||||||
if (base != 0) goto arg_error;
|
|
||||||
if (!raise_exception) return Qnil;
|
if (!raise_exception) return Qnil;
|
||||||
rb_raise(rb_eTypeError, "can't convert nil into Integer");
|
rb_raise(rb_eTypeError, "can't convert nil into Integer");
|
||||||
}
|
}
|
||||||
if (base != 0) {
|
|
||||||
tmp = rb_check_string_type(val);
|
|
||||||
if (!NIL_P(tmp)) return rb_str_convert_to_inum(tmp, base, TRUE, raise_exception);
|
|
||||||
arg_error:
|
|
||||||
if (!raise_exception) return Qnil;
|
|
||||||
rb_raise(rb_eArgError, "base specified for non string value");
|
|
||||||
}
|
|
||||||
|
|
||||||
tmp = rb_protect(rb_check_to_int, val, NULL);
|
tmp = rb_protect(rb_check_to_int, val, NULL);
|
||||||
if (RB_INTEGER_TYPE_P(tmp)) return tmp;
|
if (RB_INTEGER_TYPE_P(tmp)) return tmp;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user