summaryrefslogtreecommitdiff
path: root/range.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-07-25 12:33:15 (GMT)
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-07-25 12:33:15 (GMT)
commit706534b5f5760c2a36f641918cad94bcb3f8cb3d (patch)
treefb11ba94918889596c8afdc63685cf64167cd4e6 /range.c
parente73b32199da800ca1c7c722536ed4ac5c0c59918 (diff)
range.c: check_step_domain
* range.c (check_step_domain): check step argument domain by <=> method, instead of < and >. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55745 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'range.c')
-rw-r--r--range.c39
1 files changed, 20 insertions, 19 deletions
diff --git a/range.c b/range.c
index ab3f1af..1d0cc1b 100644
--- a/range.c
+++ b/range.c
@@ -334,22 +334,31 @@ linear_object_p(VALUE obj)
}
static VALUE
-range_step_size(VALUE range, VALUE args, VALUE eobj)
+check_step_domain(VALUE step)
{
- VALUE b = RANGE_BEG(range), e = RANGE_END(range);
- VALUE step = INT2FIX(1);
- if (args) {
- step = RARRAY_AREF(args, 0);
- if (!rb_obj_is_kind_of(step, rb_cNumeric)) {
- step = rb_to_int(step);
- }
+ VALUE zero = INT2FIX(0);
+ int cmp;
+ if (!rb_obj_is_kind_of(step, rb_cNumeric)) {
+ step = rb_to_int(step);
}
- if (rb_funcall(step, '<', 1, INT2FIX(0))) {
+ cmp = rb_cmpint(rb_funcallv(step, idCmp, 1, &zero), step, zero);
+ if (cmp < 0) {
rb_raise(rb_eArgError, "step can't be negative");
}
- else if (!rb_funcall(step, '>', 1, INT2FIX(0))) {
+ else if (cmp == 0) {
rb_raise(rb_eArgError, "step can't be 0");
}
+ return step;
+}
+
+static VALUE
+range_step_size(VALUE range, VALUE args, VALUE eobj)
+{
+ VALUE b = RANGE_BEG(range), e = RANGE_END(range);
+ VALUE step = INT2FIX(1);
+ if (args) {
+ step = check_step_domain(RARRAY_AREF(args, 0));
+ }
if (rb_obj_is_kind_of(b, rb_cNumeric) && rb_obj_is_kind_of(e, rb_cNumeric)) {
return ruby_num_interval_step_size(b, e, step, EXCL(range));
@@ -405,15 +414,7 @@ range_step(int argc, VALUE *argv, VALUE range)
}
else {
rb_scan_args(argc, argv, "01", &step);
- if (!rb_obj_is_kind_of(step, rb_cNumeric)) {
- step = rb_to_int(step);
- }
- if (rb_funcall(step, '<', 1, INT2FIX(0))) {
- rb_raise(rb_eArgError, "step can't be negative");
- }
- else if (!rb_funcall(step, '>', 1, INT2FIX(0))) {
- rb_raise(rb_eArgError, "step can't be 0");
- }
+ step = check_step_domain(step);
}
if (FIXNUM_P(b) && FIXNUM_P(e) && FIXNUM_P(step)) { /* fixnums are special */