aboutsummaryrefslogtreecommitdiffstats
path: root/enum.c
diff options
context:
space:
mode:
authormrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-05-18 00:16:06 +0000
committermrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-05-18 00:16:06 +0000
commit896e1852708daa75f1ffa61e388ed62005df11a5 (patch)
treee5f8a77347596709050eb8638d7175fb5793fe33 /enum.c
parent106679b0afaf26644192cbe5a916dac52a1928d2 (diff)
downloadruby-896e1852708daa75f1ffa61e388ed62005df11a5.tar.gz
Extract int_range_sum from enum_sum
* enum.c (enum_sum, int_range_sum): Extract int_range_sum from enum_sum. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55040 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'enum.c')
-rw-r--r--enum.c37
1 files changed, 21 insertions, 16 deletions
diff --git a/enum.c b/enum.c
index 54e1364de7..67cc301f02 100644
--- a/enum.c
+++ b/enum.c
@@ -3666,6 +3666,26 @@ enum_sum_iter_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
return Qnil;
}
+static VALUE
+int_range_sum(VALUE beg, VALUE end, int excl, VALUE init)
+{
+ if (excl) {
+ if (FIXNUM_P(end))
+ end = LONG2FIX(FIX2LONG(end) - 1);
+ else
+ end = rb_big_minus(end, LONG2FIX(1));
+ }
+
+ if (rb_int_ge(end, beg)) {
+ VALUE a;
+ a = rb_int_plus(rb_int_minus(end, beg), LONG2FIX(1));
+ a = rb_int_mul(a, rb_int_plus(end, beg));
+ a = rb_int_idiv(a, LONG2FIX(2));
+ return rb_int_plus(init, a);
+ }
+
+ return init;
+}
/*
* call-seq:
@@ -3719,22 +3739,7 @@ enum_sum(int argc, VALUE* argv, VALUE obj)
if (!memo.block_given && !memo.float_value &&
(FIXNUM_P(beg) || RB_TYPE_P(beg, T_BIGNUM)) &&
(FIXNUM_P(end) || RB_TYPE_P(end, T_BIGNUM))) {
- if (excl) {
- if (FIXNUM_P(end))
- end = LONG2FIX(FIX2LONG(end) - 1);
- else
- end = rb_big_minus(end, LONG2FIX(1));
- }
- if (rb_int_ge(end, beg)) {
- VALUE a;
- a = rb_int_plus(rb_int_minus(end, beg), LONG2FIX(1));
- a = rb_int_mul(a, rb_int_plus(end, beg));
- a = rb_int_idiv(a, LONG2FIX(2));
- return rb_int_plus(memo.v, a);
- }
- else {
- return memo.v;
- }
+ return int_range_sum(beg, end, excl, memo.v);
}
}