diff options
Diffstat (limited to 'numeric.c')
-rw-r--r-- | numeric.c | 66 |
1 files changed, 46 insertions, 20 deletions
@@ -3165,15 +3165,29 @@ fix_rev(VALUE num) return ~num | FIXNUM_FLAG; } -static VALUE -bit_coerce(VALUE x) -{ - while (!FIXNUM_P(x) && !RB_TYPE_P(x, T_BIGNUM)) { - rb_raise(rb_eTypeError, - "can't convert %s into Integer for bitwise arithmetic", - rb_obj_classname(x)); +static int +bit_coerce(VALUE *x, VALUE *y, int err) +{ + if (!FIXNUM_P(*y) && !RB_TYPE_P(*y, T_BIGNUM)) { + do_coerce(x, y, err); + if (!FIXNUM_P(*x) && !RB_TYPE_P(*x, T_BIGNUM) + && !FIXNUM_P(*y) && !RB_TYPE_P(*y, T_BIGNUM)) { + if (!err) return FALSE; + rb_raise(rb_eTypeError, + "%s can't be coerced into %s for bitwise arithmetic", + rb_special_const_p(*y) ? + RSTRING_PTR(rb_inspect(*y)) : rb_obj_classname(*y), + rb_obj_classname(*x)); + } } - return x; + return TRUE; +} + +VALUE +rb_num_coerce_bit(VALUE x, VALUE y, ID func) +{ + bit_coerce(&x, &y, TRUE); + return rb_funcall(x, func, 1, y); } /* @@ -3186,13 +3200,17 @@ bit_coerce(VALUE x) static VALUE fix_and(VALUE x, VALUE y) { - long val; + if (FIXNUM_P(y)) { + long val = FIX2LONG(x) & FIX2LONG(y); + return LONG2NUM(val); + } - if (!FIXNUM_P(y = bit_coerce(y))) { + if (RB_TYPE_P(y, T_BIGNUM)) { return rb_big_and(y, x); } - val = FIX2LONG(x) & FIX2LONG(y); - return LONG2NUM(val); + + bit_coerce(&x, &y, TRUE); + return rb_funcall(x, rb_intern("&"), 1, y); } /* @@ -3205,13 +3223,17 @@ fix_and(VALUE x, VALUE y) static VALUE fix_or(VALUE x, VALUE y) { - long val; + if (FIXNUM_P(y)) { + long val = FIX2LONG(x) | FIX2LONG(y); + return LONG2NUM(val); + } - if (!FIXNUM_P(y = bit_coerce(y))) { + if (RB_TYPE_P(y, T_BIGNUM)) { return rb_big_or(y, x); } - val = FIX2LONG(x) | FIX2LONG(y); - return LONG2NUM(val); + + bit_coerce(&x, &y, TRUE); + return rb_funcall(x, rb_intern("|"), 1, y); } /* @@ -3224,13 +3246,17 @@ fix_or(VALUE x, VALUE y) static VALUE fix_xor(VALUE x, VALUE y) { - long val; + if (FIXNUM_P(y)) { + long val = FIX2LONG(x) ^ FIX2LONG(y); + return LONG2NUM(val); + } - if (!FIXNUM_P(y = bit_coerce(y))) { + if (RB_TYPE_P(y, T_BIGNUM)) { return rb_big_xor(y, x); } - val = FIX2LONG(x) ^ FIX2LONG(y); - return LONG2NUM(val); + + bit_coerce(&x, &y, TRUE); + return rb_funcall(x, rb_intern("^"), 1, y); } static VALUE fix_lshift(long, unsigned long); |