1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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
This commit is contained in:
nobu 2016-08-11 07:20:36 +00:00
parent c93875e6e3
commit b8ad953501
2 changed files with 21 additions and 19 deletions

View file

@ -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

View file

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