Skip to content

Commit

Permalink
compar.c: utility functions
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
nobu committed Aug 11, 2016
1 parent c93875e commit b8ad953
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
1 change: 1 addition & 0 deletions common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,7 @@ compar.$(OBJEXT): $(hdrdir)/ruby/ruby.h
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
Expand Down
39 changes: 20 additions & 19 deletions compar.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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);
}

/*
Expand All @@ -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
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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("<=>");
}

0 comments on commit b8ad953

Please sign in to comment.