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

ruby-changes:62021

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

https://git.ruby-lang.org/ruby.git/commit/?id=0fc569361b

From 0fc569361bfef9a48a6ad54b075047f02b51b4e2 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: Fri, 19 Jun 2020 11:58:50 +0900
Subject: num_exact: 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/time.c b/time.c
index ddd011e..b45715a 100644
--- a/time.c
+++ b/time.c
@@ -512,47 +512,37 @@ num_exact(VALUE v) https://github.com/ruby/ruby/blob/trunk/time.c#L512
 {
     VALUE tmp;
 
-    if (NIL_P(v)) {
-	rb_raise(rb_eTypeError, "can't convert nil into an exact number");
-    }
-    else if (RB_INTEGER_TYPE_P(v)) {
+    switch (TYPE(v)) {
+      case T_FIXNUM:
+      case T_BIGNUM:
         return v;
-    }
-    else if (RB_TYPE_P(v, T_RATIONAL)) {
-	goto rational;
-    }
-    else if (RB_TYPE_P(v, T_STRING)) {
-        goto typeerror;
-    }
-    else {
+
+      case T_RATIONAL:
+        return rb_rational_canonicalize(v);
+
+      default:
         if ((tmp = rb_check_funcall(v, idTo_r, 0, NULL)) != Qundef) {
             /* test to_int method availability to reject non-Numeric
              * objects such as String, Time, etc which have to_r method. */
-            if (!rb_respond_to(v, idTo_int)) goto typeerror;
+            if (!rb_respond_to(v, idTo_int)) {
+                /* FALLTHROUGH */
+            }
+            else if (RB_INTEGER_TYPE_P(tmp)) {
+                return tmp;
+            }
+            else if (RB_TYPE_P(tmp, T_RATIONAL)) {
+                return rb_rational_canonicalize(tmp);
+            }
         }
         else if (!NIL_P(tmp = rb_check_to_int(v))) {
             return tmp;
         }
-        else {
-            goto typeerror;
-        }
-    }
 
-    if (RB_INTEGER_TYPE_P(tmp)) {
-        v = tmp;
-    }
-    else if (RB_TYPE_P(tmp, T_RATIONAL)) {
-        v = tmp;
-      rational:
-        if (RRATIONAL(v)->den == INT2FIX(1))
-            v = RRATIONAL(v)->num;
-    }
-    else {
-      typeerror:
+      case T_NIL:
+      case T_STRING:
 	rb_raise(rb_eTypeError, "can't convert %"PRIsVALUE" into an exact number",
 		 rb_obj_class(v));
     }
-    return v;
 }
 
 /* time_t */
-- 
cgit v0.10.2


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

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