1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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
This commit is contained in:
nobu 2008-03-11 10:48:12 +00:00
parent 12043d728d
commit 83c017d7af
2 changed files with 24 additions and 9 deletions

View file

@ -1,3 +1,8 @@
Tue Mar 11 19:48:09 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
* numeric.c (fix_coerce): try conversion before type check.
[ruby-core:15838]
Tue Mar 11 12:39:53 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
* common.mk (clean-local): WINMAINOBJ is Windows specific.

View file

@ -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) {