summaryrefslogtreecommitdiff
path: root/array.c
diff options
context:
space:
mode:
authornagachika <nagachika@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-12-20 14:53:56 +0000
committernagachika <nagachika@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-12-20 14:53:56 +0000
commitd77b1756fdfdc1ec6ab7ba26754da27d1492d169 (patch)
tree50287630dfc05ceffe3fcc8a6c8378a0848a6ded /array.c
parent1a028670e131ec1698a468f2089cfdfd7fe6b1f6 (diff)
merge revision(s) 57649,57651: [Backport #13222]
array.c: finish_exact_sum * array.c (finish_exact_sum): extract duplicate code from rb_ary_sum. array.c: check if numeric * array.c (finish_exact_sum): add 0 and the initial value to check if the latter is numeric. [ruby-core:79572] [Bug #13222] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_4@61361 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'array.c')
-rw-r--r--array.c44
1 files changed, 22 insertions, 22 deletions
diff --git a/array.c b/array.c
index 05e6069ad3..724cf46b38 100644
--- a/array.c
+++ b/array.c
@@ -5691,6 +5691,26 @@ rb_ary_dig(int argc, VALUE *argv, VALUE self)
return rb_obj_dig(argc, argv, self, Qnil);
}
+static inline VALUE
+finish_exact_sum(long n, VALUE r, VALUE v, int z)
+{
+ if (n != 0)
+ v = rb_fix_plus(LONG2FIX(n), v);
+ if (r != Qundef) {
+ /* r can be an Integer when mathn is loaded */
+ if (FIXNUM_P(r))
+ v = rb_fix_plus(r, v);
+ else if (RB_TYPE_P(r, T_BIGNUM))
+ v = rb_big_plus(r, v);
+ else
+ v = rb_rational_plus(r, v);
+ }
+ else if (!n && z) {
+ v = rb_fix_plus(LONG2FIX(0), v);
+ }
+ return v;
+}
+
/*
* call-seq:
* ary.sum(init=0) -> number
@@ -5772,31 +5792,11 @@ rb_ary_sum(int argc, VALUE *argv, VALUE ary)
else
goto not_exact;
}
- if (n != 0)
- v = rb_fix_plus(LONG2FIX(n), v);
- if (r != Qundef) {
- /* r can be an Integer when mathn is loaded */
- if (FIXNUM_P(r))
- v = rb_fix_plus(r, v);
- else if (RB_TYPE_P(r, T_BIGNUM))
- v = rb_big_plus(r, v);
- else
- v = rb_rational_plus(r, v);
- }
+ v = finish_exact_sum(n, r, v, argc!=0);
return v;
not_exact:
- if (n != 0)
- v = rb_fix_plus(LONG2FIX(n), v);
- if (r != Qundef) {
- /* r can be an Integer when mathn is loaded */
- if (FIXNUM_P(r))
- v = rb_fix_plus(r, v);
- else if (RB_TYPE_P(r, T_BIGNUM))
- v = rb_big_plus(r, v);
- else
- v = rb_rational_plus(r, v);
- }
+ v = finish_exact_sum(n, r, v, i!=0);
if (RB_FLOAT_TYPE_P(e)) {
/*