range.c: r_less

* range.c (r_less): merge r_le() and r_lt() and make code shorter
  with less branches.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50499 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2015-05-15 09:06:18 +00:00
parent bad0560661
commit 04c10b8af3

50
range.c
View File

@ -171,35 +171,21 @@ range_eq(VALUE range, VALUE obj)
return rb_exec_recursive_paired(recursive_equal, range, obj, obj); return rb_exec_recursive_paired(recursive_equal, range, obj, obj);
} }
/* compares _a_ and _b_ and returns:
* < 0: a < b
* = 0: a = b
* > 0: a > b or not-comparable
*/
static int static int
r_lt(VALUE a, VALUE b) r_less(VALUE a, VALUE b)
{ {
VALUE r = rb_funcall(a, id_cmp, 1, b); VALUE r = rb_funcall(a, id_cmp, 1, b);
if (NIL_P(r)) if (NIL_P(r))
return (int)Qfalse; return INT_MAX;
if (rb_cmpint(r, a, b) < 0) return rb_cmpint(r, a, b);
return (int)Qtrue;
return (int)Qfalse;
} }
static int
r_le(VALUE a, VALUE b)
{
int c;
VALUE r = rb_funcall(a, id_cmp, 1, b);
if (NIL_P(r))
return (int)Qfalse;
c = rb_cmpint(r, a, b);
if (c == 0)
return (int)INT2FIX(0);
if (c < 0)
return (int)Qtrue;
return (int)Qfalse;
}
static VALUE static VALUE
recursive_eql(VALUE range, VALUE obj, int recur) recursive_eql(VALUE range, VALUE obj, int recur)
{ {
@ -275,16 +261,15 @@ range_each_func(VALUE range, rb_block_call_func *func, VALUE arg)
VALUE v = b; VALUE v = b;
if (EXCL(range)) { if (EXCL(range)) {
while (r_lt(v, e)) { while (r_less(v, e) < 0) {
(*func) (v, arg, 0, 0, 0); (*func) (v, arg, 0, 0, 0);
v = rb_funcallv(v, id_succ, 0, 0); v = rb_funcallv(v, id_succ, 0, 0);
} }
} }
else { else {
while ((c = r_le(v, e)) != Qfalse) { while ((c = r_less(v, e)) <= 0) {
(*func) (v, arg, 0, 0, 0); (*func) (v, arg, 0, 0, 0);
if (c == (int)INT2FIX(0)) if (!c) break;
break;
v = rb_funcallv(v, id_succ, 0, 0); v = rb_funcallv(v, id_succ, 0, 0);
} }
} }
@ -1232,15 +1217,10 @@ range_cover(VALUE range, VALUE val)
static VALUE static VALUE
r_cover_p(VALUE range, VALUE beg, VALUE end, VALUE val) r_cover_p(VALUE range, VALUE beg, VALUE end, VALUE val)
{ {
if (r_le(beg, val)) { if (r_less(beg, val) <= 0) {
if (EXCL(range)) { int excl = EXCL(range);
if (r_lt(val, end)) if (r_less(val, end) <= -excl)
return Qtrue; return Qtrue;
}
else {
if (r_le(val, end))
return Qtrue;
}
} }
return Qfalse; return Qfalse;
} }