diff options
author | shyouhei <shyouhei@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-06-15 11:21:40 +0000 |
---|---|---|
committer | shyouhei <shyouhei@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-06-15 11:21:40 +0000 |
commit | e3f7b363e8b0ac7eaf78da19001438f40d287f88 (patch) | |
tree | 009d552699e0f8db8f32f59adc981fc74a328426 /numeric.c | |
parent | 870df461dd3008dd56c0147ddc2fd84688cdd9cb (diff) |
merge revision(s) 15749:
* numeric.c (fix_coerce): try conversion before type check.
[ruby-core:15838]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8_6@17206 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'numeric.c')
-rw-r--r-- | numeric.c | 29 |
1 files changed, 20 insertions, 9 deletions
@@ -1537,6 +1537,7 @@ long rb_num2long(val) VALUE val; { + again: if (NIL_P(val)) { rb_raise(rb_eTypeError, "no implicit conversion from nil to integer"); } @@ -1563,7 +1564,7 @@ rb_num2long(val) default: val = rb_to_int(val); - return NUM2LONG(val); + goto again; } } @@ -2427,6 +2428,16 @@ fix_rev(num) return LONG2NUM(val); } +static VALUE +fix_coerce(x) + VALUE x; +{ + while (!FIXNUM_P(x) && TYPE(x) != T_BIGNUM) { + x = rb_to_int(x); + } + return x; +} + /* * call-seq: * fix & other => integer @@ -2440,10 +2451,10 @@ fix_and(x, y) { long val; - if (TYPE(y) == T_BIGNUM) { + if (!FIXNUM_P(y = fix_coerce(y))) { return rb_big_and(y, x); } - val = FIX2LONG(x) & NUM2LONG(y); + val = FIX2LONG(x) & FIX2LONG(y); return LONG2NUM(val); } @@ -2460,10 +2471,10 @@ fix_or(x, y) { long val; - if (TYPE(y) == T_BIGNUM) { + if (!FIXNUM_P(y = fix_coerce(y))) { return rb_big_or(y, x); } - val = FIX2LONG(x) | NUM2LONG(y); + val = FIX2LONG(x) | FIX2LONG(y); return LONG2NUM(val); } @@ -2480,10 +2491,10 @@ fix_xor(x, y) { long val; - if (TYPE(y) == T_BIGNUM) { + if (!FIXNUM_P(y = fix_coerce(y))) { return rb_big_xor(y, x); } - val = FIX2LONG(x) ^ NUM2LONG(y); + val = FIX2LONG(x) ^ FIX2LONG(y); return LONG2NUM(val); } @@ -2582,7 +2593,7 @@ fix_aref(fix, idx) long val = FIX2LONG(fix); long i; - if (TYPE(idx) == T_BIGNUM) { + if (!FIXNUM_P(idx = fix_coerce(idx))) { idx = rb_big_norm(idx); if (!FIXNUM_P(idx)) { if (!RBIGNUM(idx)->sign || val >= 0) @@ -2590,7 +2601,7 @@ fix_aref(fix, idx) return INT2FIX(1); } } - i = NUM2LONG(idx); + i = FIX2LONG(idx); if (i < 0) return INT2FIX(0); if (sizeof(VALUE)*CHAR_BIT-1 < i) { |