aboutsummaryrefslogtreecommitdiffstats
path: root/enum.c
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-03-17 09:50:19 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-03-17 09:50:19 +0000
commite1617987c82547c0141eb2610ca84ae0fd1abf0e (patch)
tree3b10a09c8e6e5e03fe1db5fbbcf5692d30691cc8 /enum.c
parent3948b107cfacd39b879fabe196eb370dafbd9df8 (diff)
downloadruby-e1617987c82547c0141eb2610ca84ae0fd1abf0e.tar.gz
* 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
Diffstat (limited to 'enum.c')
-rw-r--r--enum.c48
1 files changed, 38 insertions, 10 deletions
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<RARRAY_LEN(obj); i++) {
v = rb_funcall(v, id, 1, RARRAY_AREF(obj, i));