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
@ -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>
|
Mon Jul 25 21:11:32 2016 Kazuhiro NISHIYAMA <zn@mbf.nifty.com>
|
||||||
|
|
||||||
* doc/maintainers.rdoc: fix filenames.
|
* doc/maintainers.rdoc: fix filenames.
|
||||||
|
39
range.c
39
range.c
@ -333,22 +333,31 @@ linear_object_p(VALUE obj)
|
|||||||
return FALSE;
|
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
|
static VALUE
|
||||||
range_step_size(VALUE range, VALUE args, VALUE eobj)
|
range_step_size(VALUE range, VALUE args, VALUE eobj)
|
||||||
{
|
{
|
||||||
VALUE b = RANGE_BEG(range), e = RANGE_END(range);
|
VALUE b = RANGE_BEG(range), e = RANGE_END(range);
|
||||||
VALUE step = INT2FIX(1);
|
VALUE step = INT2FIX(1);
|
||||||
if (args) {
|
if (args) {
|
||||||
step = RARRAY_AREF(args, 0);
|
step = check_step_domain(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");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rb_obj_is_kind_of(b, rb_cNumeric) && rb_obj_is_kind_of(e, rb_cNumeric)) {
|
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 {
|
else {
|
||||||
rb_scan_args(argc, argv, "01", &step);
|
rb_scan_args(argc, argv, "01", &step);
|
||||||
if (!rb_obj_is_kind_of(step, rb_cNumeric)) {
|
step = check_step_domain(step);
|
||||||
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");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (FIXNUM_P(b) && FIXNUM_P(e) && FIXNUM_P(step)) { /* fixnums are special */
|
if (FIXNUM_P(b) && FIXNUM_P(e) && FIXNUM_P(step)) { /* fixnums are special */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user