From e1617987c82547c0141eb2610ca84ae0fd1abf0e Mon Sep 17 00:00:00 2001 From: akr Date: Thu, 17 Mar 2016 09:50:19 +0000 Subject: * enum.c (enum_inject): Implement the specialized code for sum of integers including Bignums. * internal.h (rb_fix_plus): Declared to be usable from enum_inject. * numeric.c (rb_fix_plus): Defined. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54147 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- enum.c | 48 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 10 deletions(-) (limited to 'enum.c') diff --git a/enum.c b/enum.c index 951e30ab32..e2ed920533 100644 --- a/enum.c +++ b/enum.c @@ -719,17 +719,45 @@ enum_inject(int argc, VALUE *argv, VALUE obj) i = 0; } id = SYM2ID(op); - if (id == idPLUS && FIXNUM_P(v) && - rb_method_basic_definition_p(rb_cFixnum, idPLUS)) { - long n = FIX2LONG(v); - while (i < RARRAY_LEN(obj)) { - VALUE e = RARRAY_AREF(obj, i); - if (!FIXNUM_P(e)) break; - n += FIX2LONG(e); /* should not overflow long type */ - i++; - if (!FIXABLE(n)) break; + if (id == idPLUS) { + if (FIXNUM_P(v) && + rb_method_basic_definition_p(rb_cFixnum, idPLUS)) { + long n = FIX2LONG(v); + while (i < RARRAY_LEN(obj)) { + VALUE e = RARRAY_AREF(obj, i); + if (!FIXNUM_P(e)) break; + n += FIX2LONG(e); /* should not overflow long type */ + i++; + if (!FIXABLE(n)) break; + } + v = LONG2NUM(n); + } + if (i < RARRAY_LEN(obj) && (FIXNUM_P(v) || RB_TYPE_P(v, T_BIGNUM)) && + rb_method_basic_definition_p(rb_cFixnum, idPLUS) && + rb_method_basic_definition_p(rb_cBignum, idPLUS)) { + long n = 0; + while (i < RARRAY_LEN(obj)) { + VALUE e = RARRAY_AREF(obj, i); + if (FIXNUM_P(e)) { + n += FIX2LONG(e); /* should not overflow long type */ + i++; + if (!FIXABLE(n)) { + v = rb_big_plus(LONG2NUM(n), v); + n = 0; + } + } + else if (RB_TYPE_P(e, T_BIGNUM)) { + v = rb_big_plus(e, v); + i++; + } + else { + break; + } + } + if (n != 0) { + v = rb_fix_plus(LONG2FIX(n), v); + } } - v = LONG2NUM(n); } for (; i