[前][次][番号順一覧][スレッド一覧]

ruby-changes:62024

From: =E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3 <ko1@a...>
Date: Mon, 29 Jun 2020 11:07:51 +0900 (JST)
Subject: [ruby-changes:62024] 689dd3aecb (master): parse_rat: do not goto into a branch

https://git.ruby-lang.org/ruby.git/commit/?id=689dd3aecb

From 689dd3aecbf6177caad793c18c1c6ab4a7cc2150 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3?=
 <shyouhei@r...>
Date: Tue, 16 Jun 2020 15:18:55 +0900
Subject: parse_rat: do not goto into a branch

I'm not necessarily against every goto in general, but jumping into a
branch is definitely a bad idea.  Better refactor.

diff --git a/rational.c b/rational.c
index f50f20d..36e4296 100644
--- a/rational.c
+++ b/rational.c
@@ -2440,24 +2440,27 @@ parse_rat(const char *s, const char *const e, int strict, int raise) https://github.com/ruby/ruby/blob/trunk/rational.c#L2440
     if (nexp != ZERO) {
         if (INT_NEGATIVE_P(nexp)) {
             VALUE mul;
-            if (!FIXNUM_P(nexp)) {
-              overflow:
-                return sign == '-' ? DBL2NUM(-HUGE_VAL) : DBL2NUM(HUGE_VAL);
+            if (FIXNUM_P(nexp)) {
+                mul = f_expt10(LONG2NUM(-FIX2LONG(nexp)));
+                if (! RB_FLOAT_TYPE_P(mul)) {
+                    num = rb_int_mul(num, mul);
+                    goto reduce;
+                }
             }
-            mul = f_expt10(LONG2NUM(-FIX2LONG(nexp)));
-            if (RB_FLOAT_TYPE_P(mul)) goto overflow;
-            num = rb_int_mul(num, mul);
+            return sign == '-' ? DBL2NUM(-HUGE_VAL) : DBL2NUM(HUGE_VAL);
         }
         else {
             VALUE div;
-            if (!FIXNUM_P(nexp)) {
-              underflow:
-                return sign == '-' ? DBL2NUM(-0.0) : DBL2NUM(+0.0);
+            if (FIXNUM_P(nexp)) {
+                div = f_expt10(nexp);
+                if (! RB_FLOAT_TYPE_P(div)) {
+                    den = rb_int_mul(den, div);
+                    goto reduce;
+                }
             }
-            div = f_expt10(nexp);
-            if (RB_FLOAT_TYPE_P(div)) goto underflow;
-            den = rb_int_mul(den, div);
+            return sign == '-' ? DBL2NUM(-0.0) : DBL2NUM(+0.0);
         }
+      reduce:
         nurat_reduce(&num, &den);
     }
 
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]