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

ruby-changes:43789

From: nobu <ko1@a...>
Date: Thu, 11 Aug 2016 16:20:55 +0900 (JST)
Subject: [ruby-changes:43789] nobu:r55862 (trunk): compar.c: utility functions

nobu	2016-08-11 16:20:36 +0900 (Thu, 11 Aug 2016)

  New Revision: 55862

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=55862

  Log:
    compar.c: utility functions
    
    * compar.c (rb_cmp): call comparison method by predefiend ID.
    
    * compar.c (cmpint): returns compared result.

  Modified files:
    trunk/common.mk
    trunk/compar.c
Index: common.mk
===================================================================
--- common.mk	(revision 55861)
+++ common.mk	(revision 55862)
@@ -1272,6 +1272,7 @@ compar.$(OBJEXT): $(hdrdir)/ruby/ruby.h https://github.com/ruby/ruby/blob/trunk/common.mk#L1272
 compar.$(OBJEXT): {$(VPATH)}compar.c
 compar.$(OBJEXT): {$(VPATH)}config.h
 compar.$(OBJEXT): {$(VPATH)}defines.h
+compar.$(OBJEXT): {$(VPATH)}id.h
 compar.$(OBJEXT): {$(VPATH)}intern.h
 compar.$(OBJEXT): {$(VPATH)}missing.h
 compar.$(OBJEXT): {$(VPATH)}st.h
Index: compar.c
===================================================================
--- compar.c	(revision 55861)
+++ compar.c	(revision 55862)
@@ -10,10 +10,15 @@ https://github.com/ruby/ruby/blob/trunk/compar.c#L10
 **********************************************************************/
 
 #include "ruby/ruby.h"
+#include "id.h"
 
 VALUE rb_mComparable;
 
-static ID cmp;
+static VALUE
+rb_cmp(VALUE x, VALUE y)
+{
+    return rb_funcallv(x, idCmp, 1, &y);
+}
 
 void
 rb_cmperr(VALUE x, VALUE y)
@@ -34,7 +39,7 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/compar.c#L39
 invcmp_recursive(VALUE x, VALUE y, int recursive)
 {
     if (recursive) return Qnil;
-    return rb_check_funcall(y, cmp, 1, &x);
+    return rb_cmp(y, x);
 }
 
 VALUE
@@ -54,7 +59,7 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/compar.c#L59
 cmp_eq_recursive(VALUE arg1, VALUE arg2, int recursive)
 {
     if (recursive) return Qnil;
-    return rb_funcallv(arg1, cmp, 1, &arg2);
+    return rb_cmp(arg1, arg2);
 }
 
 /*
@@ -79,6 +84,12 @@ cmp_equal(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/compar.c#L84
     return Qfalse;
 }
 
+static int
+cmpint(VALUE x, VALUE y)
+{
+    return rb_cmpint(rb_cmp(x, y), x, y);
+}
+
 /*
  *  call-seq:
  *     obj > other    -> true or false
@@ -90,9 +101,7 @@ cmp_equal(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/compar.c#L101
 static VALUE
 cmp_gt(VALUE x, VALUE y)
 {
-    VALUE c = rb_funcall(x, cmp, 1, y);
-
-    if (rb_cmpint(c, x, y) > 0) return Qtrue;
+    if (cmpint(x, y) > 0) return Qtrue;
     return Qfalse;
 }
 
@@ -107,9 +116,7 @@ cmp_gt(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/compar.c#L116
 static VALUE
 cmp_ge(VALUE x, VALUE y)
 {
-    VALUE c = rb_funcall(x, cmp, 1, y);
-
-    if (rb_cmpint(c, x, y) >= 0) return Qtrue;
+    if (cmpint(x, y) >= 0) return Qtrue;
     return Qfalse;
 }
 
@@ -124,9 +131,7 @@ cmp_ge(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/compar.c#L131
 static VALUE
 cmp_lt(VALUE x, VALUE y)
 {
-    VALUE c = rb_funcall(x, cmp, 1, y);
-
-    if (rb_cmpint(c, x, y) < 0) return Qtrue;
+    if (cmpint(x, y) < 0) return Qtrue;
     return Qfalse;
 }
 
@@ -141,9 +146,7 @@ cmp_lt(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/compar.c#L146
 static VALUE
 cmp_le(VALUE x, VALUE y)
 {
-    VALUE c = rb_funcall(x, cmp, 1, y);
-
-    if (rb_cmpint(c, x, y) <= 0) return Qtrue;
+    if (cmpint(x, y) <= 0) return Qtrue;
     return Qfalse;
 }
 
@@ -165,8 +168,8 @@ cmp_le(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/compar.c#L168
 static VALUE
 cmp_between(VALUE x, VALUE min, VALUE max)
 {
-    if (RTEST(cmp_lt(x, min))) return Qfalse;
-    if (RTEST(cmp_gt(x, max))) return Qfalse;
+    if (cmpint(x, min) < 0) return Qfalse;
+    if (cmpint(x, max) > 0) return Qfalse;
     return Qtrue;
 }
 
@@ -222,6 +225,4 @@ Init_Comparable(void) https://github.com/ruby/ruby/blob/trunk/compar.c#L225
     rb_define_method(rb_mComparable, "<", cmp_lt, 1);
     rb_define_method(rb_mComparable, "<=", cmp_le, 1);
     rb_define_method(rb_mComparable, "between?", cmp_between, 2);
-
-    cmp = rb_intern("<=>");
 }

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

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