numeric.c: num_step_scan_args

* numeric.c (num_step_scan_args): turn a macro into a function.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45859 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2014-05-07 08:05:00 +00:00
parent 5c5180256a
commit ba90ac2529

View File

@ -1859,39 +1859,44 @@ ruby_num_interval_step_size(VALUE from, VALUE to, VALUE step, int excl)
} }
} }
#define NUM_STEP_SCAN_ARGS(argc, argv, to, step, hash, desc) do { \ static int
argc = rb_scan_args(argc, argv, "02:", &to, &step, &hash); \ num_step_scan_args(int argc, const VALUE *argv, VALUE *to, VALUE *step)
if (!NIL_P(hash)) { \ {
step = rb_hash_aref(hash, sym_by); \ VALUE hash;
to = rb_hash_aref(hash, sym_to); \ int desc;
} \
else { \ argc = rb_scan_args(argc, argv, "02:", to, step, &hash);
/* compatibility */ \ if (!NIL_P(hash)) {
if (argc > 1 && NIL_P(step)) { \ step = rb_hash_aref(hash, sym_by);
rb_raise(rb_eTypeError, "step must be numeric"); \ to = rb_hash_aref(hash, sym_to);
} \ }
if (rb_equal(step, INT2FIX(0))) { \ else {
rb_raise(rb_eArgError, "step can't be 0"); \ /* compatibility */
} \ if (argc > 1 && NIL_P(*step)) {
} \ rb_raise(rb_eTypeError, "step must be numeric");
if (NIL_P(step)) { \ }
step = INT2FIX(1); \ if (rb_equal(*step, INT2FIX(0))) {
} \ rb_raise(rb_eArgError, "step can't be 0");
desc = !positive_int_p(step); \ }
if (NIL_P(to)) { \ }
to = desc ? DBL2NUM(-INFINITY) : DBL2NUM(INFINITY); \ if (NIL_P(*step)) {
} \ *step = INT2FIX(1);
} while (0) }
desc = !positive_int_p(*step);
if (NIL_P(*to)) {
*to = desc ? DBL2NUM(-INFINITY) : DBL2NUM(INFINITY);
}
return desc;
}
static VALUE static VALUE
num_step_size(VALUE from, VALUE args, VALUE eobj) num_step_size(VALUE from, VALUE args, VALUE eobj)
{ {
VALUE to, step, hash; VALUE to, step;
int desc;
int argc = args ? RARRAY_LENINT(args) : 0; int argc = args ? RARRAY_LENINT(args) : 0;
const VALUE *argv = args ? RARRAY_CONST_PTR(args) : 0; const VALUE *argv = args ? RARRAY_CONST_PTR(args) : 0;
NUM_STEP_SCAN_ARGS(argc, argv, to, step, hash, desc); num_step_scan_args(argc, argv, &to, &step);
return ruby_num_interval_step_size(from, to, step, FALSE); return ruby_num_interval_step_size(from, to, step, FALSE);
} }
@ -1952,12 +1957,12 @@ num_step_size(VALUE from, VALUE args, VALUE eobj)
static VALUE static VALUE
num_step(int argc, VALUE *argv, VALUE from) num_step(int argc, VALUE *argv, VALUE from)
{ {
VALUE to, step, hash; VALUE to, step;
int desc, inf; int desc, inf;
RETURN_SIZED_ENUMERATOR(from, argc, argv, num_step_size); RETURN_SIZED_ENUMERATOR(from, argc, argv, num_step_size);
NUM_STEP_SCAN_ARGS(argc, argv, to, step, hash, desc); desc = num_step_scan_args(argc, argv, &to, &step);
if (RTEST(rb_num_coerce_cmp(step, INT2FIX(0), id_eq))) { if (RTEST(rb_num_coerce_cmp(step, INT2FIX(0), id_eq))) {
inf = 1; inf = 1;
} }