mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
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
This commit is contained in:
parent
e73b32199d
commit
706534b5f5
2 changed files with 25 additions and 19 deletions
|
@ -1,3 +1,8 @@
|
|||
Mon Jul 25 21:33:13 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
|
||||
* range.c (check_step_domain): check step argument domain by <=>
|
||||
method, instead of < and >.
|
||||
|
||||
Mon Jul 25 21:11:32 2016 Kazuhiro NISHIYAMA <zn@mbf.nifty.com>
|
||||
|
||||
* doc/maintainers.rdoc: fix filenames.
|
||||
|
|
39
range.c
39
range.c
|
@ -333,22 +333,31 @@ linear_object_p(VALUE obj)
|
|||
return FALSE;
|
||||
}
|
||||
|
||||
static VALUE
|
||||
check_step_domain(VALUE step)
|
||||
{
|
||||
VALUE zero = INT2FIX(0);
|
||||
int cmp;
|
||||
if (!rb_obj_is_kind_of(step, rb_cNumeric)) {
|
||||
step = rb_to_int(step);
|
||||
}
|
||||
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 (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 = RARRAY_AREF(args, 0);
|
||||
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(RARRAY_AREF(args, 0));
|
||||
}
|
||||
|
||||
if (rb_obj_is_kind_of(b, rb_cNumeric) && rb_obj_is_kind_of(e, rb_cNumeric)) {
|
||||
|
@ -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 */
|
||||
|
|
Loading…
Reference in a new issue