diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-03-11 10:48:12 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-03-11 10:48:12 +0000 |
commit | 83c017d7af594df9e10a1ba5fbccb063f491bb25 (patch) | |
tree | 6d1d389e7b7dd4f68b89809ddfad7344a7eb5204 | |
parent | 12043d728d73ac79e218d4fd136371c77ca8aa1f (diff) |
* numeric.c (fix_coerce): try conversion before type check.
[ruby-core:15838]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15749 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | numeric.c | 28 |
2 files changed, 24 insertions, 9 deletions
@@ -1,3 +1,8 @@ +Tue Mar 11 19:48:09 2008 Nobuyoshi Nakada <[email protected]> + + * numeric.c (fix_coerce): try conversion before type check. + [ruby-core:15838] + Tue Mar 11 12:39:53 2008 Nobuyoshi Nakada <[email protected]> * common.mk (clean-local): WINMAINOBJ is Windows specific. @@ -1493,6 +1493,7 @@ num_step(int argc, VALUE *argv, VALUE from) SIGNED_VALUE rb_num2long(VALUE val) { + again: if (NIL_P(val)) { rb_raise(rb_eTypeError, "no implicit conversion from nil to integer"); } @@ -1519,7 +1520,7 @@ rb_num2long(VALUE val) default: val = rb_to_int(val); - return NUM2LONG(val); + goto again; } } @@ -2593,6 +2594,15 @@ fix_rev(VALUE num) return LONG2NUM(val); } +static VALUE +fix_coerce(VALUE x) +{ + while (!FIXNUM_P(x) && TYPE(x) != T_BIGNUM) { + x = rb_to_int(x); + } + return x; +} + /* * call-seq: * fix & other => integer @@ -2605,10 +2615,10 @@ fix_and(VALUE x, VALUE 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); } @@ -2624,10 +2634,10 @@ fix_or(VALUE x, VALUE 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); } @@ -2643,10 +2653,10 @@ fix_xor(VALUE x, VALUE 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); } @@ -2740,7 +2750,7 @@ fix_aref(VALUE fix, VALUE 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_SIGN(idx) || val >= 0) @@ -2748,7 +2758,7 @@ fix_aref(VALUE fix, VALUE idx) return INT2FIX(1); } } - i = NUM2LONG(idx); + i = FIX2LONG(idx); if (i < 0) return INT2FIX(0); if (SIZEOF_LONG*CHAR_BIT-1 < i) { |