From d77b1756fdfdc1ec6ab7ba26754da27d1492d169 Mon Sep 17 00:00:00 2001 From: nagachika Date: Wed, 20 Dec 2017 14:53:56 +0000 Subject: 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 --- array.c | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) (limited to 'array.c') 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)) { /* -- cgit v1.2.3