summaryrefslogtreecommitdiff
path: root/compar.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-08-11 07:20:36 (GMT)
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-08-11 07:20:36 (GMT)
commitb8ad953501420d60cb820bda157dbe095a9469d0 (patch)
tree2d644135274d56f24acef3eff006a6cc8cb8c346 /compar.c
parentc93875e6e3b0fb423f764b19015848022812c43d (diff)
compar.c: utility functions
* compar.c (rb_cmp): call comparison method by predefiend ID. * compar.c (cmpint): returns compared result. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55862 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'compar.c')
-rw-r--r--compar.c39
1 files changed, 20 insertions, 19 deletions
diff --git a/compar.c b/compar.c
index b040641..fe0872c 100644
--- a/compar.c
+++ b/compar.c
@@ -10,10 +10,15 @@
**********************************************************************/
#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
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
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)
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)
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)
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)
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)
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)
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)
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("<=>");
}